Skip to content

Commit

Permalink
Added Waila info for ME output hatches/busses (GTNewHorizons#3467)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <[email protected]>
  • Loading branch information
RecursivePineapple and Dream-Master authored Nov 19, 2024
1 parent 5916e9e commit 4ecb000
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_ME_HATCH_ACTIVE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;

import javax.annotation.Nullable;

import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;

import com.gtnewhorizons.modularui.api.screen.ModularWindow;
Expand All @@ -35,9 +41,11 @@
import appeng.me.GridAccessException;
import appeng.me.helpers.AENetworkProxy;
import appeng.me.helpers.IGridProxyable;
import appeng.util.IWideReadableNumberConverter;
import appeng.util.Platform;
import appeng.util.ReadableNumberConverter;
import appeng.util.item.AEItemStack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.GTMod;
import gregtech.api.enums.ItemList;
import gregtech.api.gui.modularui.GTUIInfos;
Expand All @@ -47,6 +55,8 @@
import gregtech.api.metatileentity.implementations.MTEHatchOutputBus;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTUtility;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;

public class MTEHatchOutputBusME extends MTEHatchOutputBus implements IPowerChannelState {

Expand Down Expand Up @@ -338,14 +348,97 @@ public boolean isGivingInformation() {
return true;
}

@Override
public void getWailaNBTData(EntityPlayerMP player, TileEntity tile, NBTTagCompound tag, World world, int x, int y,
int z) {
super.getWailaNBTData(player, tile, tag, world, x, y, z);
tag.setLong("cacheCapacity", getCacheCapacity());
tag.setInteger("stackCount", itemCache.size());

IAEItemStack[] stacks = itemCache.toArray(new IAEItemStack[0]);

Arrays.sort(
stacks,
Comparator.comparingLong(IAEItemStack::getStackSize)
.reversed());

if (stacks.length > 10) {
stacks = Arrays.copyOf(stacks, 10);
}

NBTTagList tagList = new NBTTagList();
tag.setTag("stacks", tagList);

for (IAEItemStack stack : stacks) {
NBTTagCompound stackTag = new NBTTagCompound();
stack.writeToNBT(stackTag);
tagList.appendTag(stackTag);
}
}

@Override
@SideOnly(Side.CLIENT)
public void getWailaBody(ItemStack itemStack, List<String> ss, IWailaDataAccessor accessor,
IWailaConfigHandler config) {
super.getWailaBody(itemStack, ss, accessor, config);

NBTTagCompound tag = accessor.getNBTData();

ss.add(
String.format(
"Item cache capacity: %s%s%s",
EnumChatFormatting.GOLD,
GTUtility.formatNumbers(tag.getLong("cacheCapacity")),
EnumChatFormatting.RESET));

if (!GuiScreen.isShiftKeyDown()) {
ss.add("Hold Shift for more info");
return;
}

NBTTagList stacks = tag.getTagList("stacks", 10);
int stackCount = tag.getInteger("stackCount");

if (stackCount == 0) {
ss.add("This bus has no cached stacks");
} else {
ss.add(
String.format(
"The bus contains %s%d%s cached stack%s: ",
EnumChatFormatting.GOLD,
stackCount,
EnumChatFormatting.RESET,
stackCount > 1 ? "s" : ""));

for (int i = 0; i < stacks.tagCount(); i++) {
IAEItemStack stack = AEItemStack.loadItemStackFromNBT(stacks.getCompoundTagAt(i));

ss.add(
String.format(
"%s: %s%s%s",
stack.getItemStack()
.getDisplayName(),
EnumChatFormatting.GOLD,
GTUtility.formatNumbers(stack.getStackSize()),
EnumChatFormatting.RESET));
}

if (stackCount > stacks.tagCount()) {
ss.add(EnumChatFormatting.ITALIC + "And " + (stackCount - stacks.tagCount()) + " more...");
}
}
}

@Override
public String[] getInfoData() {
List<String> ss = new ArrayList<>();
ss.add(
"The bus is " + ((getProxy() != null && getProxy().isActive()) ? EnumChatFormatting.GREEN + "online"
: EnumChatFormatting.RED + "offline" + getAEDiagnostics()) + EnumChatFormatting.RESET);
IWideReadableNumberConverter nc = ReadableNumberConverter.INSTANCE;
ss.add("Item cache capacity: " + nc.toWideReadableForm(getCacheCapacity()));
ss.add(
"Item cache capacity: " + EnumChatFormatting.GOLD
+ GTUtility.formatNumbers(getCacheCapacity())
+ EnumChatFormatting.RESET);
if (itemCache.isEmpty()) {
ss.add("The bus has no cached items");
} else {
Expand All @@ -356,7 +449,7 @@ public String[] getInfoData() {
s.getItem()
.getItemStackDisplayName(s.getItemStack()) + ": "
+ EnumChatFormatting.GOLD
+ nc.toWideReadableForm(s.getStackSize())
+ GTUtility.formatNumbers(s.getStackSize())
+ EnumChatFormatting.RESET);
if (++counter > 100) break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
import static gregtech.api.enums.Textures.BlockIcons.OVERLAY_ME_FLUID_HATCH_ACTIVE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;

import javax.annotation.Nullable;

import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;

Expand Down Expand Up @@ -43,8 +49,9 @@
import appeng.me.GridAccessException;
import appeng.me.helpers.AENetworkProxy;
import appeng.me.helpers.IGridProxyable;
import appeng.util.IWideReadableNumberConverter;
import appeng.util.ReadableNumberConverter;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.GTMod;
import gregtech.api.enums.GTValues;
import gregtech.api.enums.ItemList;
Expand All @@ -55,6 +62,8 @@
import gregtech.api.metatileentity.implementations.MTEHatchOutput;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTUtility;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;

public class MTEHatchOutputME extends MTEHatchOutput implements IPowerChannelState {

Expand Down Expand Up @@ -300,6 +309,89 @@ public void addAdditionalTooltipInformation(ItemStack stack, List<String> toolti
}
}

@Override
public void getWailaNBTData(EntityPlayerMP player, TileEntity tile, NBTTagCompound tag, World world, int x, int y,
int z) {
super.getWailaNBTData(player, tile, tag, world, x, y, z);
tag.setLong("cacheCapacity", getCacheCapacity());
tag.setInteger("stackCount", fluidCache.size());

IAEFluidStack[] stacks = fluidCache.toArray(new IAEFluidStack[0]);

Arrays.sort(
stacks,
Comparator.comparingLong(IAEFluidStack::getStackSize)
.reversed());

if (stacks.length > 10) {
stacks = Arrays.copyOf(stacks, 10);
}

NBTTagList tagList = new NBTTagList();
tag.setTag("stacks", tagList);

for (IAEFluidStack stack : stacks) {
NBTTagCompound stackTag = new NBTTagCompound();
stack.getFluidStack()
.writeToNBT(stackTag);
stackTag.setLong("Amount", stack.getStackSize());
tagList.appendTag(stackTag);
}
}

@Override
@SideOnly(Side.CLIENT)
public void getWailaBody(ItemStack itemStack, List<String> ss, IWailaDataAccessor accessor,
IWailaConfigHandler config) {
super.getWailaBody(itemStack, ss, accessor, config);

NBTTagCompound tag = accessor.getNBTData();

ss.add(
String.format(
"Fluid cache capacity: %s%s L%s",
EnumChatFormatting.GOLD,
GTUtility.formatNumbers(tag.getLong("cacheCapacity")),
EnumChatFormatting.RESET));

if (!GuiScreen.isShiftKeyDown()) {
ss.add("Hold Shift for more info");
return;
}

NBTTagList stacks = tag.getTagList("stacks", 10);
int stackCount = tag.getInteger("stackCount");

if (stackCount == 0) {
ss.add("This hatch has no cached fluids");
} else {
ss.add(
String.format(
"The hatch contains %s%d%s cached fluid%s: ",
EnumChatFormatting.GOLD,
stackCount,
EnumChatFormatting.RESET,
stackCount > 1 ? "s" : ""));

for (int i = 0; i < stacks.tagCount(); i++) {
NBTTagCompound stackTag = stacks.getCompoundTagAt(i);
FluidStack stack = FluidStack.loadFluidStackFromNBT(stackTag);

ss.add(
String.format(
"%s: %s%s L%s",
stack.getLocalizedName(),
EnumChatFormatting.GOLD,
GTUtility.formatNumbers(stackTag.getLong("Amount")),
EnumChatFormatting.RESET));
}

if (stackCount > stacks.tagCount()) {
ss.add(EnumChatFormatting.ITALIC + "And " + (stackCount - stacks.tagCount()) + " more...");
}
}
}

@Override
public void setItemNBT(NBTTagCompound aNBT) {
super.setItemNBT(aNBT);
Expand Down Expand Up @@ -367,8 +459,11 @@ public String[] getInfoData() {
ss.add(
"The hatch is " + ((getProxy() != null && getProxy().isActive()) ? EnumChatFormatting.GREEN + "online"
: EnumChatFormatting.RED + "offline" + getAEDiagnostics()) + EnumChatFormatting.RESET);
IWideReadableNumberConverter nc = ReadableNumberConverter.INSTANCE;
ss.add("Fluid cache capacity: " + nc.toWideReadableForm(getCacheCapacity()) + " mB");
ss.add(
"Fluid cache capacity: " + EnumChatFormatting.GOLD
+ GTUtility.formatNumbers(getCacheCapacity())
+ " L"
+ EnumChatFormatting.RESET);
if (fluidCache.isEmpty()) {
ss.add("The bus has no cached fluids");
} else {
Expand All @@ -379,8 +474,8 @@ public String[] getInfoData() {
s.getFluidStack()
.getLocalizedName() + ": "
+ EnumChatFormatting.GOLD
+ nc.toWideReadableForm(s.getStackSize())
+ " mB"
+ GTUtility.formatNumbers(s.getStackSize())
+ " L"
+ EnumChatFormatting.RESET);
if (++counter > 100) break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

import gregtech.GTMod;
import gregtech.api.interfaces.tileentity.IGregtechWailaProvider;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;
Expand All @@ -31,7 +32,13 @@ public List<String> getWailaBody(ItemStack itemStack, List<String> currenttip, I
IWailaConfigHandler config) {
final TileEntity tile = accessor.getTileEntity();
if (tile instanceof IGregtechWailaProvider) {
((IGregtechWailaProvider) tile).getWailaBody(itemStack, currenttip, accessor, config);
try {
((IGregtechWailaProvider) tile).getWailaBody(itemStack, currenttip, accessor, config);
} catch (Throwable t) {
// waila doesn't print a useful stacktrace, so catch the error and rethrow it
GTMod.GT_FML_LOGGER.error("Could not call getWailaBody on " + tile, t);
throw t;
}
}

return currenttip;
Expand All @@ -47,7 +54,12 @@ public List<String> getWailaTail(ItemStack itemStack, List<String> currenttip, I
public NBTTagCompound getNBTData(final EntityPlayerMP player, final TileEntity tile, final NBTTagCompound tag,
final World world, int x, int y, int z) {
if (tile instanceof IGregtechWailaProvider) {
((IGregtechWailaProvider) tile).getWailaNBTData(player, tile, tag, world, x, y, z);
try {
((IGregtechWailaProvider) tile).getWailaNBTData(player, tile, tag, world, x, y, z);
} catch (Throwable t) {
GTMod.GT_FML_LOGGER.error("Could not call getWailaNBTData on " + tile, t);
throw t;
}
}

return tag;
Expand Down

0 comments on commit 4ecb000

Please sign in to comment.