Add climate data

This commit is contained in:
Nedelosk committed 2017-07-29 22:40:56 +02:00
1 parent 212f857692
commit 6c869cab98
18 files changed
+125 -215

No files matched your search

@@ -7,20 +7,16 @@ package forestry.api.climate;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlankClimateModifier implements IClimateModifier {
@Override
public IClimateState modifyTarget(IClimateContainer container, IClimateState newState, ImmutableClimateState oldState, NBTTagCompound data) {
return newState;
}
@Override
@SideOnly(Side.CLIENT)
public void addTableEntries(IClimateContainer container, IClimateState climateState, NBTTagCompound data, ClimateType tableType, IClimateTable table) {
public void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data) {
}
@Override
public int getPriority() {
return 0;
@@ -56,9 +56,9 @@ public interface IClimateContainer extends INbtReadable, INbtWritable, IClimateS
ImmutableClimateState getTargetedState();
void setTargetedState(ImmutableClimateState state);
@Nullable
@SideOnly(Side.CLIENT)
IClimateTable getTable(IClimateTableHelper helper, ClimateType type, boolean withTitle);
IClimateData getData();
}
@@ -5,7 +5,11 @@
******************************************************************************/
package forestry.api.climate;
import java.util.Map;
public interface IClimateData {
void addData(ClimateType type, float value);
IClimateData addData(ClimateType type, String displayName, float value);
Map<String, Float> getData(ClimateType type);
}
@@ -21,13 +21,13 @@ public interface IClimateModifier {
* @param container the climate container
* @param newState the new climate state
* @param oldState the climate static of the previous modification.
* @param data A {@link NBTTagCompound} that can be used to save custom data or to send it to the client to show it in the {@link IClimateTable}.
* @param data A {@link NBTTagCompound} that can be used to save custom data or to send it to the client to add it to the {@link IClimateData}.
* @return a modify newState or the newState.
*/
IClimateState modifyTarget(IClimateContainer container, IClimateState newState, ImmutableClimateState oldState, NBTTagCompound data);
@SideOnly(Side.CLIENT)
void addTableEntries(IClimateContainer container, IClimateState climateState, NBTTagCompound data, ClimateType tableType, IClimateTable table);
void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data);
/**
* @return The priority of this modifier. The modifier with the highest priority is called first and the modifier with the lowest at last.
@@ -1,30 +0,0 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
package forestry.api.climate;
import java.util.Collection;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public interface IClimateTable {
IClimateTable addValueEntry(String textKey, String value);
IClimateTable addCenteredEntry(String textKey);
IClimateTable addEmptyEntry();
IClimateTable addEntry(IClimateTableEntry entry);
int getHeight();
int getLineWidth();
Collection<IClimateTableEntry> getEntrys();
}
@@ -1,22 +0,0 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
package forestry.api.climate;
import net.minecraft.client.gui.FontRenderer;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public interface IClimateTableEntry {
void draw(FontRenderer fontRenderer, int x, int y, int lineWidth, int lineStart, int lineEnd, int rowTopY, int rowBottomY, int fontColor, boolean drawBackground);
int getHeight(FontRenderer fontRenderer);
int getLineWidth(FontRenderer fontRenderer);
}
@@ -1,17 +0,0 @@
/*******************************************************************************
* Copyright 2011-2014 SirSengir
*
* This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details.
******************************************************************************/
package forestry.api.climate;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public interface IClimateTableHelper {
IClimateTable createTable(String titleKey);
void drawTable(IClimateTable table, int x, int y, int fontColor, boolean drawBackground);
}
@@ -29,20 +29,17 @@ import forestry.api.climate.ClimateState;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateContainer;
import forestry.api.climate.IClimateContainerListener;
import forestry.api.climate.IClimateData;
import forestry.api.climate.IClimateHousing;
import forestry.api.climate.IClimateModifier;
import forestry.api.climate.IClimateSource;
import forestry.api.climate.IClimateState;
import forestry.api.climate.IClimateTable;
import forestry.api.climate.IClimateTableHelper;
import forestry.api.climate.ImmutableClimateState;
import forestry.api.greenhouse.GreenhouseManager;
import forestry.core.gui.tables.Table;
import forestry.core.network.IStreamable;
import forestry.core.network.PacketBufferForestry;
import forestry.core.network.packets.PacketUpdateClimate;
import forestry.core.utils.NetworkUtil;
import forestry.core.utils.Translator;
public class ClimateContainer implements IClimateContainer, IStreamable {
public static final float CLIMATE_CHANGE = 0.01F;
@@ -289,21 +286,16 @@ public class ClimateContainer implements IClimateContainer, IStreamable {
public int hashCode() {
return parent.getCoordinates().hashCode();
}
@Nullable
@Override
@SideOnly(Side.CLIENT)
public IClimateTable getTable(IClimateTableHelper helper, ClimateType type, boolean withTitle) {
IClimateTable table;
if(withTitle){
table = new Table(Translator.translateToLocal("for.gui." + type.getName()));
}else{
table = new Table();
}
public IClimateData getData() {
IClimateData data = new ClimateData();
for(IClimateModifier modifier : GreenhouseManager.greenhouseHelper.getModifiers()){
modifier.addTableEntries(this, state, modifierData, type, table);
modifier.addData(this, state, modifierData, data);
}
return table;
return data;
}
private static final class ClimateContainerListeners implements IClimateContainerListener{
@@ -0,0 +1,29 @@
package forestry.core.climate;
import java.util.LinkedHashMap;
import java.util.Map;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateData;
public class ClimateData implements IClimateData {
private final Map<String, Float> temperatureData = new LinkedHashMap<>();
private final Map<String, Float> humidityData = new LinkedHashMap<>();
@Override
public ClimateData addData(ClimateType type, String displayName, float value) {
if(type == ClimateType.HUMIDITY){
humidityData.put(displayName, value);
}else{
temperatureData.put(displayName, value);
}
return this;
}
public Map<String, Float> getData(ClimateType type){
if(type == ClimateType.HUMIDITY){
return humidityData;
}
return temperatureData;
}
}
@@ -17,11 +17,8 @@ import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import forestry.api.climate.IClimateTable;
import forestry.api.climate.IClimateTableEntry;
public class Table implements IClimateTable {
private final List<IClimateTableEntry> lines = new ArrayList<>();
public class Table {
private final List<TableEntry> lines = new ArrayList<>();
public Table() {
}
@@ -29,52 +26,59 @@ public class Table implements IClimateTable {
public Table(String title) {
lines.add(new TableTitle(title));
}
@Override
public IClimateTable addValueEntry(String textKey, String value) {
public Table addValueEntry(String textKey, String value) {
lines.add(new TableValueText(textKey + ": ", value));
return this;
}
@Override
public IClimateTable addCenteredEntry(String textKey) {
public Table addCenteredEntry(String textKey) {
lines.add(new TableTextCentered(textKey));
return this;
}
@Override
public IClimateTable addEmptyEntry() {
public Table addEmptyEntry() {
lines.add(new TableEmptyLine());
return this;
}
@Override
public IClimateTable addEntry(IClimateTableEntry entry) {
public Table addEntry(TableEntry entry) {
lines.add(entry);
return this;
}
@Override
public Collection<IClimateTableEntry> getEntrys() {
public void draw(int x, int y, int fontColor, boolean drawBackground){
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
int lineWidth = getLineWidth();
int lineStart = x + 2;
int lineEnd = lineStart + lineWidth;
int rowTopY = y + 1;
for (TableEntry entry : lines) {
int rowBottomY = rowTopY + entry.getHeight(fontRenderer) - 1;
entry.draw(fontRenderer, x, y, lineWidth, lineStart, lineEnd, rowTopY, rowBottomY, fontColor, drawBackground);
rowTopY += + entry.getHeight(fontRenderer) - 1;
}
}
public Collection<TableEntry> getEntrys() {
return lines;
}
@Override
public int getHeight() {
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
int height = 0;
for (IClimateTableEntry entry : lines) {
for (TableEntry entry : lines) {
height += entry.getHeight(fontRenderer);
}
return height;
}
@Override
public int getLineWidth() {
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
int lineWidth = 0;
for (IClimateTableEntry entry : lines) {
for (TableEntry entry : lines) {
lineWidth = Math.max(entry.getLineWidth(fontRenderer), lineWidth);
}
@@ -12,15 +12,16 @@ package forestry.core.gui.tables;
import net.minecraft.client.gui.FontRenderer;
import forestry.api.climate.IClimateTableEntry;
public abstract class TableEntry implements IClimateTableEntry {
public abstract class TableEntry {
public static final int TITLE_BACKGROUND = 1610612736;
public static final int DEFAULT_BACKGROUND = 1342177280;
public static final int DEFAULT_FONT = 553648127;
@Override
public int getHeight(FontRenderer fontRenderer) {
return fontRenderer.FONT_HEIGHT + 1;
}
public abstract int getLineWidth(FontRenderer fontRenderer);
public abstract void draw(FontRenderer fontRenderer, int x, int y, int lineWidth, int lineStart, int lineEnd, int rowTopY, int rowBottomY, int fontColor, boolean drawBackground);
}
@@ -1,43 +0,0 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.core.gui.tables;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import forestry.api.climate.IClimateTable;
import forestry.api.climate.IClimateTableEntry;
import forestry.api.climate.IClimateTableHelper;
public class TableHelper implements IClimateTableHelper {
public static final TableHelper INSTANCE = new TableHelper();
@Override
public IClimateTable createTable(String titleKey) {
return new Table(titleKey);
}
@Override
public void drawTable(IClimateTable table, int x, int y, int fontColor, boolean drawBackground) {
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRendererObj;
int lineWidth = table.getLineWidth();
int lineStart = x + 2;
int lineEnd = lineStart + lineWidth;
int rowTopY = y + 1;
for (IClimateTableEntry entry : table.getEntrys()) {
int rowBottomY = rowTopY + entry.getHeight(fontRenderer) - 1;
entry.draw(fontRenderer, x, y, lineWidth, lineStart, lineEnd, rowTopY, rowBottomY, fontColor, drawBackground);
rowTopY += + entry.getHeight(fontRenderer) - 1;
}
}
}
@@ -20,11 +20,10 @@ import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.BlankClimateModifier;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateContainer;
import forestry.api.climate.IClimateData;
import forestry.api.climate.IClimateHousing;
import forestry.api.climate.IClimateState;
import forestry.api.climate.IClimateTable;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.utils.StringUtil;
import forestry.core.utils.Translator;
public class AltitudeModifier extends BlankClimateModifier {
@@ -47,10 +46,8 @@ public class AltitudeModifier extends BlankClimateModifier {
@Override
@SideOnly(Side.CLIENT)
public void addTableEntries(IClimateContainer container, IClimateState climateState, NBTTagCompound data, ClimateType tableType, IClimateTable table) {
if (tableType == ClimateType.TEMPERATURE) {
table.addValueEntry(Translator.translateToLocal("for.gui.modifier.altitude"), StringUtil.floatAsPercent(data.getFloat("altitudeChange")));
}
public void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data) {
data.addData(ClimateType.TEMPERATURE, Translator.translateToLocal("for.gui.modifier.altitude"), nbtData.getFloat("altitudeChange"));
}
}
@@ -22,13 +22,12 @@ import forestry.api.climate.ClimateChange;
import forestry.api.climate.ClimateState;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateContainer;
import forestry.api.climate.IClimateData;
import forestry.api.climate.IClimateHousing;
import forestry.api.climate.IClimateSource;
import forestry.api.climate.IClimateState;
import forestry.api.climate.IClimateTable;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.config.Config;
import forestry.core.utils.StringUtil;
import forestry.core.utils.Translator;
public class ClimateSourceModifier extends BlankClimateModifier {
@@ -89,19 +88,18 @@ public class ClimateSourceModifier extends BlankClimateModifier {
@Override
@SideOnly(Side.CLIENT)
public void addTableEntries(IClimateContainer container, IClimateState climateState, NBTTagCompound data, ClimateType tableType, IClimateTable table) {
IClimateState rangeDown = new ClimateState(data.getCompoundTag("rangeDown"));
IClimateState rangeUp = new ClimateState(data.getCompoundTag("rangeUp"));
IClimateState change = new ClimateState(data.getCompoundTag("change"));
if (tableType == ClimateType.HUMIDITY) {
table.addValueEntry(Translator.translateToLocal("for.gui.modifier.sources.range.up"), StringUtil.floatAsPercent(rangeUp.getHumidity()))
.addValueEntry(Translator.translateToLocal("for.gui.modifier.sources.range.down"), StringUtil.floatAsPercent(rangeDown.getHumidity()))
.addValueEntry(Translator.translateToLocal("for.gui.modifier.sources.change"), StringUtil.floatAsPercent(change.getHumidity()));
} else {
table.addValueEntry(Translator.translateToLocal("for.gui.modifier.sources.range.up"), StringUtil.floatAsPercent(rangeUp.getTemperature()))
.addValueEntry(Translator.translateToLocal("for.gui.modifier.sources.range.down"), StringUtil.floatAsPercent(rangeDown.getTemperature()))
.addValueEntry(Translator.translateToLocal("for.gui.modifier.sources.change"), StringUtil.floatAsPercent(change.getTemperature()));
}
public void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data) {
IClimateState rangeDown = new ClimateState(nbtData.getCompoundTag("rangeDown"));
IClimateState rangeUp = new ClimateState(nbtData.getCompoundTag("rangeUp"));
IClimateState change = new ClimateState(nbtData.getCompoundTag("change"));
data.addData(ClimateType.HUMIDITY, Translator.translateToLocal("for.gui.modifier.sources.range.up"), rangeUp.getHumidity())
.addData(ClimateType.HUMIDITY, Translator.translateToLocal("for.gui.modifier.sources.range.down"), rangeDown.getHumidity())
.addData(ClimateType.HUMIDITY, Translator.translateToLocal("for.gui.modifier.sources.change"), change.getHumidity());
data.addData(ClimateType.TEMPERATURE, Translator.translateToLocal("for.gui.modifier.sources.range.up"), rangeUp.getTemperature())
.addData(ClimateType.TEMPERATURE, Translator.translateToLocal("for.gui.modifier.sources.range.down"), rangeDown.getTemperature())
.addData(ClimateType.TEMPERATURE, Translator.translateToLocal("for.gui.modifier.sources.change"), change.getTemperature());
}
@Override
@@ -21,15 +21,14 @@ import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.BlankClimateModifier;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateContainer;
import forestry.api.climate.IClimateData;
import forestry.api.climate.IClimateState;
import forestry.api.climate.IClimateTable;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.utils.StringUtil;
import forestry.core.utils.Translator;
public class TimeModifier extends BlankClimateModifier {
private static final float TEMPERATURE_CHANGE = 0.10F;
private static final float TEMPERATURE_CHANGE = 0.01F;
public static float calculateLightRatio(World world) {
int lightValue = EnumSkyBlock.SKY.defaultLightValue - world.getSkylightSubtracted();
@@ -58,10 +57,8 @@ public class TimeModifier extends BlankClimateModifier {
@Override
@SideOnly(Side.CLIENT)
public void addTableEntries(IClimateContainer container, IClimateState climateState, NBTTagCompound data, ClimateType tableType, IClimateTable table) {
if (tableType == ClimateType.TEMPERATURE) {
table.addValueEntry(Translator.translateToLocal("for.gui.modifier.time"), StringUtil.floatAsPercent(data.getFloat("timeTemperatureChange")));
}
public void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data) {
data.addData(ClimateType.TEMPERATURE, Translator.translateToLocal("for.gui.modifier.time"), nbtData.getFloat("timeTemperatureChange"));
}
}
@@ -19,10 +19,9 @@ import net.minecraftforge.fml.relauncher.SideOnly;
import forestry.api.climate.BlankClimateModifier;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateContainer;
import forestry.api.climate.IClimateData;
import forestry.api.climate.IClimateState;
import forestry.api.climate.IClimateTable;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.utils.StringUtil;
import forestry.core.utils.Translator;
public class WeatherModifier extends BlankClimateModifier {
@@ -64,12 +63,9 @@ public class WeatherModifier extends BlankClimateModifier {
@Override
@SideOnly(Side.CLIENT)
public void addTableEntries(IClimateContainer container, IClimateState climateState, NBTTagCompound data, ClimateType tableType, IClimateTable table) {
if (tableType == ClimateType.HUMIDITY) {
table.addValueEntry(Translator.translateToLocal("for.gui.modifier.rain"), StringUtil.floatAsPercent(data.getFloat("rainHumidityChange")));
} else {
table.addValueEntry(Translator.translateToLocal("for.gui.modifier.rain"), StringUtil.floatAsPercent(data.getFloat("rainTemperatureChange")));
}
public void addData(IClimateContainer container, IClimateState climateState, NBTTagCompound nbtData, IClimateData data) {
data.addData(ClimateType.HUMIDITY, Translator.translateToLocal("for.gui.modifier.rain"), nbtData.getFloat("rainHumidityChange"))
.addData(ClimateType.TEMPERATURE, Translator.translateToLocal("for.gui.modifier.rain"), nbtData.getFloat("rainTemperatureChange"));
}
}
@@ -17,6 +17,7 @@ import net.minecraft.util.math.BlockPos;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateContainer;
import forestry.api.climate.IClimateData;
import forestry.api.climate.ImmutableClimateState;
import forestry.core.config.Constants;
import forestry.core.gui.GuiForestryTitled;
@@ -48,11 +49,13 @@ public class GuiGreenhouse extends GuiForestryTitled<ContainerGreenhouse> {
this.xSize = 196;
this.ySize = 202;
IClimateData data = container.getData();
//Add the camouflage tab
widgetManager.add(new WidgetCamouflageTab(widgetManager, xSize / 2 - WidgetCamouflageTab.WIDTH / 2, -WidgetCamouflageTab.HEIGHT, controller, tile));
widgetManager.add(new WidgetClimatePillar(widgetManager, -WidgetClimatePillar.WIDTH, GUI_HEIGHT / 2 - WidgetClimatePillar.HEIGHT / 2));
widgetManager.add(temperaturePanel = new WidgetClimatePanel(widgetManager, this, 9, 18, ClimateType.TEMPERATURE, container));
widgetManager.add(humidityPanel = new WidgetClimatePanel(widgetManager, this, 102, 18, ClimateType.HUMIDITY, container));
widgetManager.add(temperaturePanel = new WidgetClimatePanel(widgetManager, this, 9, 18, ClimateType.TEMPERATURE, data));
widgetManager.add(humidityPanel = new WidgetClimatePanel(widgetManager, this, 102, 18, ClimateType.HUMIDITY, data));
}
public void sendNetworkUpdate() {
@@ -13,6 +13,8 @@ package forestry.greenhouse.gui.widgets;
import com.google.common.base.Predicate;
import com.google.common.primitives.Floats;
import java.util.Map;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiTextField;
@@ -21,12 +23,12 @@ import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.input.Keyboard;
import forestry.api.climate.ClimateType;
import forestry.api.climate.IClimateContainer;
import forestry.api.climate.IClimateTable;
import forestry.core.gui.tables.TableHelper;
import forestry.api.climate.IClimateData;
import forestry.core.gui.tables.Table;
import forestry.core.gui.widgets.Widget;
import forestry.core.gui.widgets.WidgetManager;
import forestry.core.render.ColourProperties;
import forestry.core.utils.StringUtil;
import forestry.core.utils.Translator;
import forestry.greenhouse.gui.GuiGreenhouse;
@@ -52,10 +54,10 @@ public class WidgetClimatePanel extends Widget {
};
private final GuiGreenhouse gui;
private final ClimateType type;
private final IClimateTable table;
private final Table table;
private final GuiTextField textField;
public WidgetClimatePanel(WidgetManager manager, GuiGreenhouse gui, int xPos, int yPos, ClimateType type, IClimateContainer container) {
public WidgetClimatePanel(WidgetManager manager, GuiGreenhouse gui, int xPos, int yPos, ClimateType type, IClimateData data) {
super(manager, xPos, yPos);
this.width = 85;
this.height = 98;
@@ -66,7 +68,10 @@ public class WidgetClimatePanel extends Widget {
textField.setValidator(NUMBER_FILTER);
textField.setEnableBackgroundDrawing(false);
textField.setText(Float.toString(gui.container.getTargetedState().get(type)));
table = container.getTable(TableHelper.INSTANCE, type, false);
table = new Table(Translator.translateToLocal("for.gui." + type.getName()));
for(Map.Entry<String, Float> entry : data.getData(type).entrySet()){
table.addValueEntry(entry.getKey(), StringUtil.floatAsPercent(entry.getValue()));
}
}
@Override
@@ -83,7 +88,7 @@ public class WidgetClimatePanel extends Widget {
GlStateManager.color(1.0f, 1.0f, 1.0f);
textField.drawTextBox();
TableHelper.INSTANCE.drawTable(table, xPos + startX + 0, yPos + startY + 26, 14737632, false);
table.draw(xPos + startX + 0, yPos + startY + 26, 14737632, false);
}
@Override