|
| 1 | +package net.neganote.gtutilities.integration.jade.provider; |
| 2 | + |
| 3 | +import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity; |
| 4 | +import com.gregtechceu.gtceu.client.util.TooltipHelper; |
| 5 | +import com.gregtechceu.gtceu.integration.jade.GTElementHelper; |
| 6 | +import com.gregtechceu.gtceu.utils.FormattingUtil; |
| 7 | + |
| 8 | +import net.minecraft.ChatFormatting; |
| 9 | +import net.minecraft.nbt.CompoundTag; |
| 10 | +import net.minecraft.nbt.ListTag; |
| 11 | +import net.minecraft.nbt.Tag; |
| 12 | +import net.minecraft.network.chat.Component; |
| 13 | +import net.minecraft.resources.ResourceLocation; |
| 14 | +import net.minecraft.world.item.ItemStack; |
| 15 | +import net.minecraftforge.fluids.FluidStack; |
| 16 | +import net.neganote.gtutilities.GregTechModernUtilities; |
| 17 | +import net.neganote.gtutilities.integration.ae2.machine.ExpandedPatternBufferPartMachine; |
| 18 | + |
| 19 | +import snownee.jade.api.BlockAccessor; |
| 20 | +import snownee.jade.api.IBlockComponentProvider; |
| 21 | +import snownee.jade.api.IServerDataProvider; |
| 22 | +import snownee.jade.api.ITooltip; |
| 23 | +import snownee.jade.api.config.IPluginConfig; |
| 24 | +import snownee.jade.api.fluid.JadeFluidObject; |
| 25 | +import snownee.jade.api.ui.IElementHelper; |
| 26 | + |
| 27 | +public class ExpandedMEPatternBufferProvider implements IBlockComponentProvider, IServerDataProvider<BlockAccessor> { |
| 28 | + |
| 29 | + @Override |
| 30 | + public void appendTooltip(ITooltip iTooltip, BlockAccessor blockAccessor, IPluginConfig iPluginConfig) { |
| 31 | + if (blockAccessor.getBlockEntity() instanceof IMachineBlockEntity blockEntity) { |
| 32 | + if (blockEntity.getMetaMachine() instanceof ExpandedPatternBufferPartMachine) { |
| 33 | + CompoundTag serverData = blockAccessor.getServerData(); |
| 34 | + if (!serverData.getBoolean("formed")) return; |
| 35 | + |
| 36 | + iTooltip.add(Component.translatable("gtceu.top.proxies_bound", serverData.getInt("proxies")) |
| 37 | + .withStyle(TooltipHelper.RAINBOW_HSL_SLOW)); |
| 38 | + readBufferTag(iTooltip, serverData); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void appendServerData(CompoundTag compoundTag, BlockAccessor blockAccessor) { |
| 45 | + if (blockAccessor.getBlockEntity() instanceof IMachineBlockEntity blockEntity) { |
| 46 | + if (blockEntity.getMetaMachine() instanceof ExpandedPatternBufferPartMachine buffer) { |
| 47 | + if (!buffer.isFormed()) { |
| 48 | + compoundTag.putBoolean("formed", false); |
| 49 | + return; |
| 50 | + } |
| 51 | + compoundTag.putBoolean("formed", true); |
| 52 | + compoundTag.putInt("proxies", buffer.getProxies().size()); |
| 53 | + writeBufferTag(compoundTag, buffer); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public ResourceLocation getUid() { |
| 60 | + return GregTechModernUtilities.id("me_expanded_pattern_buffer"); |
| 61 | + } |
| 62 | + |
| 63 | + public static void writeBufferTag(CompoundTag compoundTag, ExpandedPatternBufferPartMachine buffer) { |
| 64 | + var merged = buffer.mergeInternalSlots(); |
| 65 | + var items = merged.items(); |
| 66 | + var fluids = merged.fluids(); |
| 67 | + |
| 68 | + ListTag itemsTag = new ListTag(); |
| 69 | + for (var entry : items.object2LongEntrySet()) { |
| 70 | + var ct = entry.getKey().serializeNBT(); |
| 71 | + ct.putLong("real", entry.getLongValue()); |
| 72 | + itemsTag.add(ct); |
| 73 | + } |
| 74 | + if (!itemsTag.isEmpty()) compoundTag.put("items", itemsTag); |
| 75 | + |
| 76 | + ListTag fluidsTag = new ListTag(); |
| 77 | + for (var entry : fluids.object2LongEntrySet()) { |
| 78 | + var ct = entry.getKey().writeToNBT(new CompoundTag()); |
| 79 | + ct.putLong("real", entry.getLongValue()); |
| 80 | + fluidsTag.add(ct); |
| 81 | + } |
| 82 | + if (!fluidsTag.isEmpty()) compoundTag.put("fluids", fluidsTag); |
| 83 | + } |
| 84 | + |
| 85 | + public static void readBufferTag(ITooltip iTooltip, CompoundTag serverData) { |
| 86 | + IElementHelper helper = iTooltip.getElementHelper(); |
| 87 | + |
| 88 | + ListTag itemsTag = serverData.getList("items", Tag.TAG_COMPOUND); |
| 89 | + for (Tag t : itemsTag) { |
| 90 | + if (!(t instanceof CompoundTag ct)) continue; |
| 91 | + var stack = ItemStack.of(ct); |
| 92 | + var count = ct.getLong("real"); |
| 93 | + if (!stack.isEmpty() && count > 0) { |
| 94 | + iTooltip.add(helper.smallItem(stack)); |
| 95 | + Component text = Component.literal(" ") |
| 96 | + .append(Component.literal(FormattingUtil.formatNumbers(count)) |
| 97 | + .withStyle(ChatFormatting.DARK_PURPLE)) |
| 98 | + .append(Component.literal("× ").withStyle(ChatFormatting.WHITE)) |
| 99 | + .append(stack.getHoverName().copy().withStyle(ChatFormatting.GOLD)); |
| 100 | + iTooltip.append(text); |
| 101 | + } |
| 102 | + } |
| 103 | + ListTag fluidsTag = serverData.getList("fluids", Tag.TAG_COMPOUND); |
| 104 | + for (Tag t : fluidsTag) { |
| 105 | + if (!(t instanceof CompoundTag ct)) continue; |
| 106 | + var stack = FluidStack.loadFluidStackFromNBT(ct); |
| 107 | + var amount = ct.getLong("real"); |
| 108 | + if (!stack.isEmpty() && amount > 0) { |
| 109 | + iTooltip.add(GTElementHelper.smallFluid(JadeFluidObject.of(stack.getFluid()))); |
| 110 | + Component text = Component.literal(" ") |
| 111 | + .append(Component.literal(FormattingUtil.formatBuckets(amount))) |
| 112 | + .withStyle(ChatFormatting.DARK_PURPLE) |
| 113 | + .append(Component.literal(" ").withStyle(ChatFormatting.WHITE)) |
| 114 | + .append(stack.getDisplayName().copy().withStyle(ChatFormatting.DARK_AQUA)); |
| 115 | + iTooltip.append(text); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments