From c4d5364e997f9a1c398fdd3ce5b6b14adcdfa6ee Mon Sep 17 00:00:00 2001 From: TheMCLoveMan <68924107+TheMCLoveMan@users.noreply.github.com> Date: Mon, 26 Feb 2024 21:53:56 +0100 Subject: [PATCH] Jade plugin (#16) Co-authored-by: TheMCCrazyMan --- gradle.properties | 2 +- .../assets/usefulmachinery/lang/en_us.json | 1 + .../entity/AbstractMachineBlockEntity.java | 4 +- .../entity/CoalGeneratorBlockEntity.java | 4 +- .../block/entity/CompactorBlockEntity.java | 4 +- .../block/entity/CrusherBlockEntity.java | 4 +- .../entity/ElectricSmelterBlockEntity.java | 4 +- .../entity/LavaGeneratorBlockEntity.java | 5 +- .../compat/jade/MachineProvider.java | 119 ++++++++++++++++++ .../compat/jade/MachineryJadePlugin.java | 5 + .../jade/RemoveVanillaStuffProvider.java | 41 ++++++ .../datagen/MachineryLanguageProvider.java | 3 + .../usefulmachinery/setup/CommonSetup.java | 11 +- 13 files changed, 188 insertions(+), 19 deletions(-) create mode 100644 src/main/java/net/themcbrothers/usefulmachinery/compat/jade/MachineProvider.java create mode 100644 src/main/java/net/themcbrothers/usefulmachinery/compat/jade/RemoveVanillaStuffProvider.java diff --git a/gradle.properties b/gradle.properties index 6066c47..944d09a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,7 +19,7 @@ minecraft_version_range=[1.20.4,1.21) # NeoForge neo_version=20.4.167 -neo_version_range=[20.4,) +neo_version_range=[20.4.167,) loader_version_range=[2,) # Mappings diff --git a/src/generated/resources/assets/usefulmachinery/lang/en_us.json b/src/generated/resources/assets/usefulmachinery/lang/en_us.json index d0fb88a..12d3100 100644 --- a/src/generated/resources/assets/usefulmachinery/lang/en_us.json +++ b/src/generated/resources/assets/usefulmachinery/lang/en_us.json @@ -5,6 +5,7 @@ "block.usefulmachinery.crusher": "Crusher", "block.usefulmachinery.electric_smelter": "Electric Smelter", "block.usefulmachinery.lava_generator": "Lava Generator", + "config.jade.plugin_usefulmachinery.machine": "Machine", "container.usefulmachinery.coal_generator": "Coal Generator", "container.usefulmachinery.compactor": "Compactor", "container.usefulmachinery.crusher": "Crusher", diff --git a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/AbstractMachineBlockEntity.java b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/AbstractMachineBlockEntity.java index 2d42d1f..4790be8 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/AbstractMachineBlockEntity.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/AbstractMachineBlockEntity.java @@ -157,9 +157,9 @@ public void clearContent() { this.stacks.clear(); } - protected abstract int[] getInputSlots(); + public abstract int[] getInputSlots(); - protected abstract int[] getOutputSlots(); + public abstract int[] getOutputSlots(); protected abstract boolean canRun(); diff --git a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CoalGeneratorBlockEntity.java b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CoalGeneratorBlockEntity.java index 5704aac..e40c920 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CoalGeneratorBlockEntity.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CoalGeneratorBlockEntity.java @@ -58,12 +58,12 @@ public CoalGeneratorBlockEntity(BlockPos pos, BlockState state) { } @Override - protected int[] getInputSlots() { + public int[] getInputSlots() { return new int[]{0}; } @Override - protected int[] getOutputSlots() { + public int[] getOutputSlots() { return new int[0]; } diff --git a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CompactorBlockEntity.java b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CompactorBlockEntity.java index e7964b2..b36c3ec 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CompactorBlockEntity.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CompactorBlockEntity.java @@ -63,12 +63,12 @@ public CompactorBlockEntity(BlockPos pos, BlockState state) { } @Override - protected int[] getInputSlots() { + public int[] getInputSlots() { return new int[]{0}; } @Override - protected int[] getOutputSlots() { + public int[] getOutputSlots() { return new int[]{1}; } diff --git a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CrusherBlockEntity.java b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CrusherBlockEntity.java index 41de171..aefc072 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CrusherBlockEntity.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/CrusherBlockEntity.java @@ -59,12 +59,12 @@ public CrusherBlockEntity(BlockPos pos, BlockState state) { } @Override - protected int[] getInputSlots() { + public int[] getInputSlots() { return new int[]{0}; } @Override - protected int[] getOutputSlots() { + public int[] getOutputSlots() { return new int[]{1, 2}; } diff --git a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/ElectricSmelterBlockEntity.java b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/ElectricSmelterBlockEntity.java index 414e9e3..1c5a036 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/ElectricSmelterBlockEntity.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/ElectricSmelterBlockEntity.java @@ -56,12 +56,12 @@ public ElectricSmelterBlockEntity(BlockPos pos, BlockState state) { } @Override - protected int[] getInputSlots() { + public int[] getInputSlots() { return new int[]{0}; } @Override - protected int[] getOutputSlots() { + public int[] getOutputSlots() { return new int[]{1}; } diff --git a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/LavaGeneratorBlockEntity.java b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/LavaGeneratorBlockEntity.java index 145c637..daa7efd 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/block/entity/LavaGeneratorBlockEntity.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/block/entity/LavaGeneratorBlockEntity.java @@ -18,7 +18,6 @@ import net.neoforged.neoforge.fluids.FluidUtil; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.fluids.capability.templates.FluidTank; -import net.themcbrothers.usefulmachinery.block.AbstractMachineBlock; import net.themcbrothers.usefulmachinery.core.MachineryBlockEntities; import net.themcbrothers.usefulmachinery.core.MachineryItems; import net.themcbrothers.usefulmachinery.machine.RedstoneMode; @@ -76,12 +75,12 @@ public LavaGeneratorBlockEntity(BlockPos pos, BlockState state) { } @Override - protected int[] getInputSlots() { + public int[] getInputSlots() { return new int[]{0}; } @Override - protected int[] getOutputSlots() { + public int[] getOutputSlots() { return new int[0]; } diff --git a/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/MachineProvider.java b/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/MachineProvider.java new file mode 100644 index 0000000..4be902f --- /dev/null +++ b/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/MachineProvider.java @@ -0,0 +1,119 @@ +package net.themcbrothers.usefulmachinery.compat.jade; + +import net.minecraft.core.NonNullList; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.phys.Vec2; +import net.themcbrothers.usefulmachinery.UsefulMachinery; +import net.themcbrothers.usefulmachinery.block.entity.AbstractMachineBlockEntity; +import net.themcbrothers.usefulmachinery.core.MachineryItems; +import net.themcbrothers.usefulmachinery.machine.MachineTier; +import snownee.jade.api.BlockAccessor; +import snownee.jade.api.IBlockComponentProvider; +import snownee.jade.api.IServerDataProvider; +import snownee.jade.api.ITooltip; +import snownee.jade.api.config.IPluginConfig; +import snownee.jade.api.ui.IElementHelper; + +import java.util.Arrays; +import java.util.Optional; +import java.util.stream.IntStream; + +/** + * Adds the machine's progress and tier information + */ +public enum MachineProvider implements IBlockComponentProvider, IServerDataProvider { + INSTANCE; + + private static final ResourceLocation UID = UsefulMachinery.rl("machine"); + + @Override + public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfig pluginConfig) { + CompoundTag data = accessor.getServerData(); + IElementHelper helper = IElementHelper.get(); + + if (data.contains("progress")) { + ListTag machineItems = data.getList("machine", ListTag.TAG_COMPOUND); + NonNullList inventory = NonNullList.withSize(data.getInt("size"), ItemStack.EMPTY); + + for (int i = 0; i < machineItems.size(); ++i) { + inventory.set(i, ItemStack.of(machineItems.getCompound(i))); + } + + int progress = data.getInt("progress"); + int total = data.getInt("total"); + + int count = 0; + + for (int slot : data.getIntArray("inputs")) { + if (count++ == 0) { + tooltip.add(helper.item(inventory.get(slot))); + } else { + tooltip.append(helper.item(inventory.get(slot))); + } + } + + // Progress Bar + tooltip.append(helper.spacer(4, 0)); + tooltip.append(helper.progress((float) progress / (float) total) + .translate(new Vec2(-2.0F, 0.0F))); + + Arrays.stream(data.getIntArray("outputs")) + .mapToObj(inventory::get) + .map(helper::item) + .forEach(tooltip::append); + } + + MachineTier tier = MachineTier.byOrdinal(data.getInt("tier")); + + if (accessor.getPlayer().isShiftKeyDown() && tier.ordinal() > 0) { + CompoundTag tierTag = new CompoundTag(); + tierTag.putInt("Tier", tier.ordinal()); + + // Tier + tooltip.add(helper.smallItem(new ItemStack(MachineryItems.TIER_UPGRADE.get(), 1, Optional.of(tierTag)))); + tooltip.append(helper.spacer(4, 0)); + tooltip.append(helper.text(Component.literal(tier.getSerializedName()))); + } + } + + @Override + public void appendServerData(CompoundTag data, BlockAccessor accessor) { + AbstractMachineBlockEntity machine = (AbstractMachineBlockEntity) accessor.getBlockEntity(); + + boolean showJadeProgress = Arrays.stream(machine.getSlotsForFace(null)) + .mapToObj(machine::getItem) + .anyMatch(itemStack -> !itemStack.isEmpty()); + + if (showJadeProgress) { + ListTag items = new ListTag(); + + IntStream.range(0, machine.getContainerSize()) + .mapToObj(machine::getItem) + .map(stack -> stack.save(new CompoundTag())) + .forEach(items::add); + + data.putIntArray("inputs", machine.getInputSlots()); + data.putIntArray("outputs", machine.getOutputSlots()); + + data.putInt("size", machine.getContainerSize()); + data.put("machine", items); + + CompoundTag furnaceTag = machine.saveWithoutMetadata(); + + data.putInt("progress", furnaceTag.getInt("ProcessTime")); + data.putInt("total", furnaceTag.getInt("ProcessTimeTotal")); + + } + + data.putInt("tier", machine.getMachineTier(accessor.getBlockState()).ordinal()); + } + + @Override + public ResourceLocation getUid() { + return UID; + } +} diff --git a/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/MachineryJadePlugin.java b/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/MachineryJadePlugin.java index bdb6063..acd8394 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/MachineryJadePlugin.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/MachineryJadePlugin.java @@ -1,5 +1,7 @@ package net.themcbrothers.usefulmachinery.compat.jade; +import net.themcbrothers.usefulmachinery.block.AbstractMachineBlock; +import net.themcbrothers.usefulmachinery.block.entity.AbstractMachineBlockEntity; import snownee.jade.api.IWailaClientRegistration; import snownee.jade.api.IWailaCommonRegistration; import snownee.jade.api.IWailaPlugin; @@ -12,9 +14,12 @@ public class MachineryJadePlugin implements IWailaPlugin { @Override public void register(IWailaCommonRegistration registration) { + registration.registerBlockDataProvider(MachineProvider.INSTANCE, AbstractMachineBlockEntity.class); } @Override public void registerClient(IWailaClientRegistration registration) { + registration.registerBlockComponent(MachineProvider.INSTANCE, AbstractMachineBlock.class); + registration.registerBlockComponent(RemoveVanillaStuffProvider.INSTANCE, AbstractMachineBlock.class); } } diff --git a/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/RemoveVanillaStuffProvider.java b/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/RemoveVanillaStuffProvider.java new file mode 100644 index 0000000..253a812 --- /dev/null +++ b/src/main/java/net/themcbrothers/usefulmachinery/compat/jade/RemoveVanillaStuffProvider.java @@ -0,0 +1,41 @@ +package net.themcbrothers.usefulmachinery.compat.jade; + +import net.minecraft.resources.ResourceLocation; +import net.themcbrothers.usefulmachinery.UsefulMachinery; +import net.themcbrothers.usefulmachinery.block.entity.AbstractMachineBlockEntity; +import snownee.jade.api.BlockAccessor; +import snownee.jade.api.IBlockComponentProvider; +import snownee.jade.api.ITooltip; +import snownee.jade.api.Identifiers; +import snownee.jade.api.config.IPluginConfig; + +/** + * Provider that removes tooltip components from our machines + */ +public enum RemoveVanillaStuffProvider implements IBlockComponentProvider { + INSTANCE; + + private static final ResourceLocation UID = UsefulMachinery.rl("remove_vanilla_stuff"); + + @Override + public void appendTooltip(ITooltip tooltip, BlockAccessor accessor, IPluginConfig pluginConfig) { + if (accessor.getBlockEntity() instanceof AbstractMachineBlockEntity) { + tooltip.remove(Identifiers.UNIVERSAL_ITEM_STORAGE); + } + } + + @Override + public ResourceLocation getUid() { + return UID; + } + + @Override + public int getDefaultPriority() { + return Integer.MAX_VALUE; + } + + @Override + public boolean isRequired() { + return true; + } +} diff --git a/src/main/java/net/themcbrothers/usefulmachinery/datagen/MachineryLanguageProvider.java b/src/main/java/net/themcbrothers/usefulmachinery/datagen/MachineryLanguageProvider.java index d952c8d..387f87a 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/datagen/MachineryLanguageProvider.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/datagen/MachineryLanguageProvider.java @@ -59,6 +59,9 @@ protected void addTranslations() { this.add("jei.usefulmachinery.compacting", "Compacting"); this.add("jei.usefulmachinery.fuel", "Fuel for RF"); + // Jade + this.add("config.jade.plugin_usefulmachinery.machine", "Machine"); + // Misc this.add("misc.usefulmachinery.energy", "%s FE"); this.add("misc.usefulmachinery.energyWithMax", "%s / %s FE"); diff --git a/src/main/java/net/themcbrothers/usefulmachinery/setup/CommonSetup.java b/src/main/java/net/themcbrothers/usefulmachinery/setup/CommonSetup.java index 4a4424d..9f02af4 100644 --- a/src/main/java/net/themcbrothers/usefulmachinery/setup/CommonSetup.java +++ b/src/main/java/net/themcbrothers/usefulmachinery/setup/CommonSetup.java @@ -12,6 +12,7 @@ import net.neoforged.neoforge.capabilities.Capabilities; import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; import net.neoforged.neoforge.energy.IEnergyStorage; +import net.neoforged.neoforge.items.wrapper.InvWrapper; import net.neoforged.neoforge.items.wrapper.SidedInvWrapper; import net.themcbrothers.lib.energy.EnergyContainerItem; import net.themcbrothers.lib.util.Version; @@ -52,11 +53,11 @@ private void enqueueIMC(final InterModEnqueueEvent event) { private void capabilities(final RegisterCapabilitiesEvent event) { // Items - event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, COAL_GENERATOR.get(), SidedInvWrapper::new); - event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, COMPACTOR.get(), SidedInvWrapper::new); - event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, CRUSHER.get(), SidedInvWrapper::new); - event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, ELECTRIC_SMELTER.get(), SidedInvWrapper::new); - event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, LAVA_GENERATOR.get(), SidedInvWrapper::new); + event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, COAL_GENERATOR.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side)); + event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, COMPACTOR.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side)); + event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, CRUSHER.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side)); + event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, ELECTRIC_SMELTER.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side)); + event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, LAVA_GENERATOR.get(), (sidedContainer, side) -> side == null ? new InvWrapper(sidedContainer) : new SidedInvWrapper(sidedContainer, side)); // Energy event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, COAL_GENERATOR.get(), (machine, context) -> machine.getEnergyStorage());