Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/main/java/mcp/mobius/waila/addons/ic2/HUDHandlerCrops.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package mcp.mobius.waila.addons.ic2;

import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;
import mcp.mobius.waila.api.IWailaDataProvider;
import mcp.mobius.waila.cbcore.LangUtil;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Optional;

public class HUDHandlerCrops implements IWailaDataProvider {

private static final Logger LOGGER = LogManager.getLogger();
static final IWailaDataProvider INSTANCE = new HUDHandlerCrops();

private static final String DEFAULT_FORMAT = "§f%s: %d§r";
private static final String TITLE_FORMAT = "§f%s§r";
private static final String STAT_GROWTH_FORMAT = "§f%s: §2%d/31§r";
private static final String STAT_GAIN_FORMAT = "§f%s: §6%d/31§r";
private static final String STAT_RESISTANCE_FORMAT = "§f%s: §3%d/31§r";

@Nonnull
@Override
public List<String> getWailaBody(ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) {
Optional.ofNullable(accessor.getTileEntity()).ifPresent(te -> {
NBTTagCompound tag = accessor.getNBTData();
int scanLevel = tag.getInteger("scanLevel");

if (scanLevel >= 1) {
addGrowthInfo(currenttip, tag);
}
if (scanLevel >= 4) {
addStatInfo(currenttip, tag);
}
addStorageInfo(currenttip, tag);
addTerrainInfo(currenttip, tag);
});

return currenttip;
}

private void addGrowthInfo(List<String> currenttip, NBTTagCompound tag) {
currenttip.add(String.format(TITLE_FORMAT, LangUtil.translateG("hud.ic2.msg.growthtitile")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.currentsize"), tag.getInteger("currentSize")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.growthpoints"), tag.getInteger("growthPoints")));
}

private void addStatInfo(List<String> currenttip, NBTTagCompound tag) {
currenttip.add(String.format(TITLE_FORMAT, LangUtil.translateG("hud.ic2.msg.stattitle")));
currenttip.add(String.format(STAT_GROWTH_FORMAT, LangUtil.translateG("hud.ic2.msg.statgrowth"), tag.getInteger("statGrowth")));
currenttip.add(String.format(STAT_GAIN_FORMAT, LangUtil.translateG("hud.ic2.msg.statgain"), tag.getInteger("statGain")));
currenttip.add(String.format(STAT_RESISTANCE_FORMAT, LangUtil.translateG("hud.ic2.msg.statresistance"), tag.getInteger("statResistance")));
}

private void addStorageInfo(List<String> currenttip, NBTTagCompound tag) {
currenttip.add(String.format(TITLE_FORMAT, LangUtil.translateG("hud.ic2.msg.storagetitle")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.storagenutrients"), tag.getInteger("storageNutrients")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.storagewater"), tag.getInteger("storageWater")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.storageweedex"), tag.getInteger("storageWeedEX")));
}

private void addTerrainInfo(List<String> currenttip, NBTTagCompound tag) {
currenttip.add(String.format(TITLE_FORMAT, LangUtil.translateG("hud.ic2.msg.terrraintitle")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.terrainnutrients"), tag.getInteger("terrainNutrients")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.terrainhumidity"), tag.getInteger("terrainHumidity")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.terrainairquality"), tag.getInteger("terrainAirQuality")));
currenttip.add(String.format(DEFAULT_FORMAT, LangUtil.translateG("hud.ic2.msg.lightlevel"), tag.getInteger("lightLevel")));
}

@Nonnull
@Override
public NBTTagCompound getNBTData(EntityPlayerMP player, TileEntity te, NBTTagCompound tag, World world, BlockPos pos) {
try {
if (IC2Module.crops.isInstance(te)) {
tag.setInteger("scanLevel", (Integer) IC2Module.cropsScanLevel.invoke(te));
tag.setInteger("storageNutrients", (Integer) IC2Module.cropsStorageNutrients.invoke(te));
tag.setInteger("storageWater", (Integer) IC2Module.cropsStorageWater.invoke(te));
tag.setInteger("storageWeedEX", (Integer) IC2Module.cropsStorageWeedEX.invoke(te));
tag.setInteger("terrainNutrients", (Integer) IC2Module.cropsTerrainNutrients.invoke(te));
tag.setInteger("terrainHumidity", (Integer) IC2Module.cropsTerrainHumidity.invoke(te));
tag.setInteger("terrainAirQuality", (Integer) IC2Module.cropsTerrainAirQuality.invoke(te));
tag.setInteger("lightLevel", (Integer) IC2Module.cropsLightLevel.invoke(te));
tag.setInteger("currentSize", (Integer) IC2Module.cropsCurrentSize.invoke(te));
tag.setInteger("growthPoints", (Integer) IC2Module.cropsGrowthPoints.invoke(te));
tag.setInteger("statGrowth", (Integer) IC2Module.cropsStatGrowth.invoke(te));
tag.setInteger("statGain", (Integer) IC2Module.cropsStatGain.invoke(te));
tag.setInteger("statResistance", (Integer) IC2Module.cropsStatResistance.invoke(te));
}
} catch (Exception e) {
LOGGER.error("Failed to get crop data", e);
}
return tag;
}
}
106 changes: 73 additions & 33 deletions src/main/java/mcp/mobius/waila/addons/ic2/HUDHandlerTEGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,108 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nonnull;
import java.util.List;

public class HUDHandlerTEGenerator implements IWailaDataProvider {

private static final Logger LOGGER = LogManager.getLogger();
static final IWailaDataProvider INSTANCE = new HUDHandlerTEGenerator();

private static final String ENERGY_STORAGE_FORMAT = "%s: §f%d§r / §f%d§r EU";
private static final String PERCENTAGE_FORMAT = " (§f%d%%§r)";
private static final String INPUT_OUTPUT_FORMAT = "%s: §f%d §r EU/t";
private static final String TIER_FORMAT = "%s: §f%d §r";

@Nonnull
@Override
public List<String> getWailaBody(ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) {
double storage = accessor.getNBTData().getDouble("storage");
int production = accessor.getNBTData().getInteger("production");
long maxStorage = accessor.getNBTData().getLong("maxStorage");

String storedStr = LangUtil.translateG("hud.ic2.msg.stored");
String outputStr = LangUtil.translateG("hud.ic2.msg.output");

if (accessor.getTileEntity() == null) {
return currenttip;
}

/* EU Storage */
if (config.getConfig("ic2.storage"))
if (maxStorage > 0) {
((ITaggedList<String, String>) currenttip)
.add(String.format(
"%s §f%d§r / §f%d§r EU",
storedStr,
Math.round(Math.min(storage, maxStorage)),
maxStorage
), "IEnergyStorage");
NBTTagCompound nbtData = accessor.getNBTData();
double stored = nbtData.getDouble("stored");
int input = nbtData.getInteger("input");
int output = nbtData.getInteger("output");
long capacity = nbtData.getLong("capacity");
int tier = nbtData.getInteger("tier");

addEnergyStorageInfo(currenttip, config, stored, capacity);
addInputOutputInfo(currenttip, config, input, output);
addTierInfo(currenttip, config, tier);

return currenttip;
}

private void addEnergyStorageInfo(List<String> currenttip, IWailaConfigHandler config, double stored, long capacity) {
if (capacity > 0) {
StringBuilder energyLine = new StringBuilder();
String storedStr = LangUtil.translateG("hud.ic2.msg.stored");

if (config.getConfig("ic2.storage")) {
energyLine.append(String.format(ENERGY_STORAGE_FORMAT, storedStr, Math.round(Math.min(stored, capacity)), capacity));
}

if (config.getConfig("ic2.percentage")) {
energyLine.append(String.format(PERCENTAGE_FORMAT, Math.round((stored / capacity) * 100)));
}

if (config.getConfig("ic2.outputeu")) {
currenttip.add(String.format("%s §f%d §r EU/t", outputStr, production));
((ITaggedList<String, String>) currenttip).add(energyLine.toString(), "IEnergyStorage");
}
}

return currenttip;
private void addInputOutputInfo(List<String> currenttip, IWailaConfigHandler config, int input, int output) {
String inputStr = LangUtil.translateG("hud.ic2.msg.input");
String outputStr = LangUtil.translateG("hud.ic2.msg.output");

if (config.getConfig("ic2.inouteu")) {
if (input > 0) {
currenttip.add(String.format(INPUT_OUTPUT_FORMAT, inputStr, input));
}
if (output > 0) {
currenttip.add(String.format(INPUT_OUTPUT_FORMAT, outputStr, output));
}
}
}

private void addTierInfo(List<String> currenttip, IWailaConfigHandler config, int tier) {
String tierStr = LangUtil.translateG("hud.ic2.msg.tier");
if (config.getConfig("ic2.tier") && tier > 0) {
currenttip.add(String.format(TIER_FORMAT, tierStr, tier));
}
}

@Nonnull
@Override
public NBTTagCompound getNBTData(EntityPlayerMP player, TileEntity te, NBTTagCompound tag, World world, BlockPos pos) {
double storage = -1;
int production = -1;
long maxStorage = -1;

try {
if (IC2Module.generator.isInstance(te)) {
storage = IC2Module.generatorStorage.getDouble(te);
production = IC2Module.generatorProduction.getInt(te);
maxStorage = IC2Module.generatorMaxStorage.getLong(te);

tag.setDouble("stored", IC2Module.generatorStored.getDouble(te));
tag.setLong("capacity", IC2Module.generatorCapacity.getLong(te));
tag.setInteger("input", 0);
tag.setInteger("output", IC2Module.generatorOutput.getInt(te));
tag.setInteger("tier", IC2Module.generatorTier.getInt(te));
} else if (IC2Module.eBlock.isInstance(te)) {
tag.setDouble("stored", IC2Module.eBlockStored.getDouble(te));
tag.setLong("capacity", IC2Module.eBlockCapacity.getLong(te));
tag.setInteger("input", 0);
tag.setInteger("output", IC2Module.eBlockOutput.getInt(te));
tag.setInteger("tier", IC2Module.eBlockTier.getInt(te));
} else if (IC2Module.eMachine.isInstance(te)) {
tag.setDouble("stored", 0);
tag.setLong("capacity", 0);
tag.setInteger("input", IC2Module.eMachineInput.getInt(te));
tag.setInteger("output", 0);
tag.setInteger("tier", IC2Module.eMachineTier.getInt(te));
}
} catch (java.lang.Exception e) {
throw new RuntimeException(e);
} catch (Exception e) {
LOGGER.error("Failed to get generator data", e);
}
tag.setDouble("storage", storage);
tag.setInteger("production", production);
tag.setLong("maxStorage", maxStorage);

return tag;
}
}
91 changes: 83 additions & 8 deletions src/main/java/mcp/mobius/waila/addons/ic2/IC2Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,106 @@
import net.minecraftforge.fml.common.Loader;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@WailaPlugin
public class IC2Module implements IWailaPlugin {

public static Class generator = null;
public static Field generatorStorage = null;
public static Field generatorMaxStorage = null;
public static Field generatorProduction = null;
public static Field generatorStored = null;
public static Field generatorCapacity = null;
public static Field generatorOutput = null;
public static Field generatorTier = null;

public static Class eBlock = null;
public static Field eBlockStored = null;
public static Field eBlockCapacity = null;
public static Field eBlockOutput = null;
public static Field eBlockTier = null;

public static Class eMachine = null;
public static Field eMachineStored = null;
public static Field eMachineCapacity = null;
public static Field eMachineInput = null;
public static Field eMachineTier = null;

public static Class crops = null;
public static Method cropsStorageNutrients = null;
public static Method cropsStorageWater = null;
public static Method cropsStorageWeedEX = null;
public static Method cropsTerrainNutrients = null;
public static Method cropsTerrainHumidity = null;
public static Method cropsTerrainAirQuality = null;
public static Method cropsLightLevel = null;
public static Method cropsScanLevel = null;
public static Method cropsCurrentSize = null;
public static Method cropsGrowthPoints = null;
public static Method cropsStatGrowth = null;
public static Method cropsStatGain = null;
public static Method cropsStatResistance = null;

@Override
public void register(IWailaRegistrar registrar) {
if (!Loader.isModLoaded("ic2")) return;
try {
/* Generators */
generator = Class.forName("ic2.core.block.base.tile.TileEntityGeneratorBase");
generatorStorage = generator.getDeclaredField("storage");
generatorMaxStorage = generator.getDeclaredField("maxStorage");
generatorProduction = generator.getDeclaredField("production");

generatorStored = generator.getDeclaredField("storage");
generatorCapacity = generator.getDeclaredField("maxStorage");
generatorOutput = generator.getDeclaredField("production");
generatorTier = generator.getDeclaredField("tier");

registrar.registerBodyProvider(HUDHandlerTEGenerator.INSTANCE, generator);
registrar.registerNBTProvider(HUDHandlerTEGenerator.INSTANCE, generator);

/* EU Storages */
eBlock = Class.forName("ic2.core.block.base.tile.TileEntityElectricBlock");
eBlockStored = eBlock.getDeclaredField("energy");
eBlockCapacity = eBlock.getDeclaredField("maxEnergy");
eBlockOutput = eBlock.getDeclaredField("output");
eBlockTier = eBlock.getDeclaredField("tier");

registrar.registerBodyProvider(HUDHandlerTEGenerator.INSTANCE, eBlock);
registrar.registerNBTProvider(HUDHandlerTEGenerator.INSTANCE, eBlock);

/* Machines */
eMachine = Class.forName("ic2.core.block.base.tile.TileEntityElecMachine");
// TODO: Вырезать или оставить
// stored и capacity понадобятся в будущем... Или нет
eMachineStored = eMachine.getDeclaredField("energy");
eMachineCapacity = eMachine.getDeclaredField("maxEnergy");
eMachineInput = eMachine.getDeclaredField("maxInput");
eMachineTier = eMachine.getDeclaredField("tier");

registrar.registerBodyProvider(HUDHandlerTEGenerator.INSTANCE, eMachine);
registrar.registerNBTProvider(HUDHandlerTEGenerator.INSTANCE, eMachine);

/* Crops */
crops = Class.forName("ic2.core.block.crop.TileEntityCrop");

cropsStorageNutrients = crops.getDeclaredMethod("getStorageNutrients");
cropsStorageWater = crops.getDeclaredMethod("getStorageWater");
cropsStorageWeedEX = crops.getDeclaredMethod("getStorageWeedEX");
cropsTerrainNutrients = crops.getDeclaredMethod("getTerrainNutrients");
cropsTerrainHumidity = crops.getDeclaredMethod("getTerrainHumidity");
cropsTerrainAirQuality = crops.getDeclaredMethod("getTerrainAirQuality");
cropsLightLevel = crops.getDeclaredMethod("getLightLevel");
cropsScanLevel = crops.getDeclaredMethod("getScanLevel");
cropsCurrentSize = crops.getDeclaredMethod("getCurrentSize");
cropsGrowthPoints = crops.getDeclaredMethod("getGrowthPoints");
cropsStatGrowth = crops.getDeclaredMethod("getStatGrowth");
cropsStatGain = crops.getDeclaredMethod("getStatGain");
cropsStatResistance = crops.getDeclaredMethod("getStatResistance");

registrar.registerBodyProvider(HUDHandlerCrops.INSTANCE, crops);
registrar.registerNBTProvider(HUDHandlerCrops.INSTANCE, crops);

/* Config */
registrar.addConfig("Industrial Craft 2", "ic2.storage", true);
registrar.addConfig("Industrial Craft 2", "ic2.outputeu", true);
registrar.addConfig("Industrial Craft 2", "ic2.percentage", true);
registrar.addConfig("Industrial Craft 2", "ic2.inouteu", true);
registrar.addConfig("Industrial Craft 2", "ic2.tier", true);
registrar.addConfig("Industrial Craft 2", "ic2.crops", true);

} catch (Exception e) {
Waila.LOGGER.warn("[Industrial Craft 2] Error while loading generator hooks.", e);
Expand Down
26 changes: 26 additions & 0 deletions src/main/resources/assets/waila/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,28 @@ hud.msg.demands=Demands
hud.msg.career=Career: %s

hud.ic2.msg.stored=Stored
hud.ic2.msg.input=Input
hud.ic2.msg.output=Output
hud.ic2.msg.tier=Tier

hud.ic2.msg.growthtitile=Growth
hud.ic2.msg.currentsize=Stage
hud.ic2.msg.growthpoints=Points

hud.ic2.msg.stattitle=Stats
hud.ic2.msg.statgrowth=Growth
hud.ic2.msg.statgain=Gain
hud.ic2.msg.statresistance=Resistance

hud.ic2.msg.storagetitle=Storage
hud.ic2.msg.storagenutrients=Nutrients
hud.ic2.msg.storagewater=Water
hud.ic2.msg.storageweedex=WeedEX
hud.ic2.msg.terrraintitle=Terrain
hud.ic2.msg.terrainnutrients=Nutrients
hud.ic2.msg.terrainhumidity=Humidity
hud.ic2.msg.terrainairquality=Air Quality
hud.ic2.msg.lightlevel=Light Level

hud.te.msg.stored=Stored
hud.te.msg.capacity=Capacity
Expand All @@ -93,4 +114,9 @@ option.general.registry=Show registry data
option.capability.tankinfo=Show tank data
option.capability.energyinfo=Show energy data

option.ic2.storage=Show EU storage
option.ic2.percentage=EU percentage
option.ic2.inouteu=Input/Output EU
option.ic2.tier=Energy tier

nei.options.keys.showenchant=Show enchanting values
Loading