From 68e969e417a658f7514df2e19defb495ddc97181 Mon Sep 17 00:00:00 2001 From: tom5454 Date: Tue, 26 May 2020 14:07:47 +0200 Subject: [PATCH] 1.1.1 Update Fixed Inventory Cable Connector not showing up in the creative inventory and JEI Added JEI compatibility to crafting terminal Fixed Storage Terminal recipe not using tags Terminals now emit light --- build.gradle | 2 +- .../tom/storagemod/ItemBlockConnector.java | 2 +- .../java/com/tom/storagemod/StorageMod.java | 5 +- .../storagemod/block/StorageTerminalBase.java | 2 +- .../gui/ContainerCraftingTerminal.java | 29 +++++- .../gui/GuiStorageTerminalBase.java | 13 +++ .../jei/CraftingTerminalTransferHandler.java | 97 +++++++++++++++++++ .../storagemod/jei/IJEIAutoFillTerminal.java | 8 ++ .../com/tom/storagemod/jei/JEIHandler.java | 38 ++++++++ .../tom/storagemod/network/DataPacket.java | 20 ++++ .../tom/storagemod/network/IDataReceiver.java | 7 ++ .../storagemod/network/NetworkHandler.java | 56 +++++++++++ .../tile/TileEntityCraftingTerminal.java | 50 +++++++++- .../recipes/storage_terminal.json | 4 +- version-check.json | 5 +- 15 files changed, 325 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/tom/storagemod/jei/CraftingTerminalTransferHandler.java create mode 100644 src/main/java/com/tom/storagemod/jei/IJEIAutoFillTerminal.java create mode 100644 src/main/java/com/tom/storagemod/jei/JEIHandler.java create mode 100644 src/main/java/com/tom/storagemod/network/DataPacket.java create mode 100644 src/main/java/com/tom/storagemod/network/IDataReceiver.java create mode 100644 src/main/java/com/tom/storagemod/network/NetworkHandler.java diff --git a/build.gradle b/build.gradle index 9311ead4..836e791f 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle' apply plugin: 'eclipse' apply plugin: 'maven-publish' -version = '1.1.0' +version = '1.1.1' group = 'com.yourname.modid' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'toms_storage' diff --git a/src/main/java/com/tom/storagemod/ItemBlockConnector.java b/src/main/java/com/tom/storagemod/ItemBlockConnector.java index f94eb985..969793fc 100644 --- a/src/main/java/com/tom/storagemod/ItemBlockConnector.java +++ b/src/main/java/com/tom/storagemod/ItemBlockConnector.java @@ -8,7 +8,7 @@ public class ItemBlockConnector extends BlockItem { public ItemBlockConnector() { - super(StorageMod.invCableConnector, new Properties()); + super(StorageMod.invCableConnector, new Properties().group(StorageMod.STORAGE_MOD_TAB)); setRegistryName(StorageMod.invCableConnector.getRegistryName()); } diff --git a/src/main/java/com/tom/storagemod/StorageMod.java b/src/main/java/com/tom/storagemod/StorageMod.java index eb21b092..af177340 100644 --- a/src/main/java/com/tom/storagemod/StorageMod.java +++ b/src/main/java/com/tom/storagemod/StorageMod.java @@ -39,6 +39,7 @@ import com.tom.storagemod.item.ItemBlockPainted; import com.tom.storagemod.item.ItemPaintKit; import com.tom.storagemod.item.ItemWirelessTerminal; +import com.tom.storagemod.network.NetworkHandler; import com.tom.storagemod.proxy.ClientProxy; import com.tom.storagemod.proxy.IProxy; import com.tom.storagemod.proxy.ServerProxy; @@ -52,8 +53,7 @@ // The value here should match an entry in the META-INF/mods.toml file @Mod(StorageMod.modid) -public class StorageMod -{ +public class StorageMod { public static final String modid = "toms_storage"; public static IProxy proxy = DistExecutor.runForDist(() -> ClientProxy::new, () -> ServerProxy::new); public static InventoryConnector connector; @@ -104,6 +104,7 @@ public StorageMod() { private void setup(final FMLCommonSetupEvent event) { LOGGER.info("Tom's Storage Setup starting"); proxy.setup(); + NetworkHandler.init(); } private void doClientStuff(final FMLClientSetupEvent event) { diff --git a/src/main/java/com/tom/storagemod/block/StorageTerminalBase.java b/src/main/java/com/tom/storagemod/block/StorageTerminalBase.java index d25c3f8a..b0d442a5 100644 --- a/src/main/java/com/tom/storagemod/block/StorageTerminalBase.java +++ b/src/main/java/com/tom/storagemod/block/StorageTerminalBase.java @@ -45,7 +45,7 @@ public abstract class StorageTerminalBase extends ContainerBlock implements IWat private static final VoxelShape SHAPE_U = Block.makeCuboidShape(0.0D, 10.0D, 0.0D, 16.0D, 16.0D, 16.0D); private static final VoxelShape SHAPE_D = Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 6.0D, 16.0D); public StorageTerminalBase() { - super(Block.Properties.create(Material.WOOD).hardnessAndResistance(3).harvestTool(ToolType.AXE)); + super(Block.Properties.create(Material.WOOD).hardnessAndResistance(3).harvestTool(ToolType.AXE).lightValue(6)); setDefaultState(getDefaultState().with(TERMINAL_POS, TerminalPos.CENTER)); } diff --git a/src/main/java/com/tom/storagemod/gui/ContainerCraftingTerminal.java b/src/main/java/com/tom/storagemod/gui/ContainerCraftingTerminal.java index 3aef4c0d..4704b433 100644 --- a/src/main/java/com/tom/storagemod/gui/ContainerCraftingTerminal.java +++ b/src/main/java/com/tom/storagemod/gui/ContainerCraftingTerminal.java @@ -18,6 +18,8 @@ import net.minecraft.item.crafting.IRecipe; import net.minecraft.item.crafting.RecipeItemHelper; import net.minecraft.item.crafting.ServerRecipePlacer; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.nbt.ListNBT; import net.minecraft.network.play.server.SSetSlotPacket; import net.minecraftforge.api.distmarker.Dist; @@ -27,9 +29,12 @@ import com.tom.storagemod.StorageMod; import com.tom.storagemod.StoredItemStack; +import com.tom.storagemod.jei.IJEIAutoFillTerminal; +import com.tom.storagemod.network.IDataReceiver; +import com.tom.storagemod.network.NetworkHandler; import com.tom.storagemod.tile.TileEntityCraftingTerminal; -public class ContainerCraftingTerminal extends ContainerStorageTerminal { +public class ContainerCraftingTerminal extends ContainerStorageTerminal implements IJEIAutoFillTerminal, IDataReceiver { public static class SlotCrafting extends Slot { public SlotCrafting(IInventory inventoryIn, int index, int xPosition, int yPosition) { super(inventoryIn, index, xPosition, yPosition); @@ -299,4 +304,26 @@ protected void clear() { } }).place(p_217056_3_, p_217056_2_, p_217056_1_); } + + @Override + public void sendMessage(CompoundNBT compound) { + NetworkHandler.sendDataToServer(compound); + } + + @Override + public void receive(CompoundNBT message) { + ItemStack[][] stacks = new ItemStack[9][]; + ListNBT list = message.getList("i", 10); + for (int i = 0;i < list.size();i++) { + CompoundNBT nbttagcompound = list.getCompound(i); + byte slot = nbttagcompound.getByte("s"); + byte l = nbttagcompound.getByte("l"); + stacks[slot] = new ItemStack[l]; + for (int j = 0;j < l;j++) { + CompoundNBT tag = nbttagcompound.getCompound("i" + j); + stacks[slot][j] = ItemStack.read(tag); + } + } + ((TileEntityCraftingTerminal) te).handlerItemTransfer(player, stacks); + } } diff --git a/src/main/java/com/tom/storagemod/gui/GuiStorageTerminalBase.java b/src/main/java/com/tom/storagemod/gui/GuiStorageTerminalBase.java index 45ca0495..7fb82530 100644 --- a/src/main/java/com/tom/storagemod/gui/GuiStorageTerminalBase.java +++ b/src/main/java/com/tom/storagemod/gui/GuiStorageTerminalBase.java @@ -334,4 +334,17 @@ public boolean charTyped(char p_charTyped_1_, int p_charTyped_2_) { if(searchField.charTyped(p_charTyped_1_, p_charTyped_2_))return true; return super.charTyped(p_charTyped_1_, p_charTyped_2_); } + + @Override + public boolean mouseScrolled(double p_mouseScrolled_1_, double p_mouseScrolled_3_, double p_mouseScrolled_5_) { + if (!this.needsScrollBars()) { + return false; + } else { + int i = ((this.container).itemList.size() + 9 - 1) / 9 - 5; + this.currentScroll = (float)(this.currentScroll - p_mouseScrolled_5_ / i); + this.currentScroll = MathHelper.clamp(this.currentScroll, 0.0F, 1.0F); + this.container.scrollTo(this.currentScroll); + return true; + } + } } diff --git a/src/main/java/com/tom/storagemod/jei/CraftingTerminalTransferHandler.java b/src/main/java/com/tom/storagemod/jei/CraftingTerminalTransferHandler.java new file mode 100644 index 00000000..6bf0cdd8 --- /dev/null +++ b/src/main/java/com/tom/storagemod/jei/CraftingTerminalTransferHandler.java @@ -0,0 +1,97 @@ +package com.tom.storagemod.jei; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.container.Container; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.nbt.ListNBT; + +import com.google.common.base.Function; + +import com.tom.storagemod.gui.ContainerCraftingTerminal; + +import mezz.jei.api.constants.VanillaRecipeCategoryUid; +import mezz.jei.api.gui.IRecipeLayout; +import mezz.jei.api.gui.ingredient.IGuiIngredient; +import mezz.jei.api.gui.ingredient.IGuiItemStackGroup; +import mezz.jei.api.recipe.transfer.IRecipeTransferError; +import mezz.jei.api.recipe.transfer.IRecipeTransferHandler; +import mezz.jei.api.registration.IRecipeTransferRegistration; + +@SuppressWarnings("rawtypes") +public class CraftingTerminalTransferHandler implements IRecipeTransferHandler { + private final Class containerClass; + private static final List> containerClasses = new ArrayList<>(); + private static final Function transferFunc = new Function() { + @Override + public ItemStack[][] apply(IRecipeLayout t) { + List inputs = new ArrayList<>(); + IGuiItemStackGroup itemStackGroup = t.getItemStacks(); + for (IGuiIngredient ingredient : itemStackGroup.getGuiIngredients().values()) { + if (ingredient.isInput()) { + if (!ingredient.getAllIngredients().isEmpty() && ingredient.getAllIngredients().get(0) != null) { + inputs.add(ingredient.getAllIngredients().toArray(new ItemStack[]{})); + } else { + inputs.add(null); + } + } + } + return inputs.toArray(new ItemStack[][]{}); + } + }; + private static final IRecipeTransferError ERROR_INSTANCE = new IRecipeTransferError() { + @Override public IRecipeTransferError.Type getType() { return IRecipeTransferError.Type.INTERNAL; } + @Override public void showError(int mouseX, int mouseY, IRecipeLayout recipeLayout, int recipeX, int recipeY) {} + }; + static { + containerClasses.add(ContainerCraftingTerminal.class); + } + + public CraftingTerminalTransferHandler(Class containerClass) { + this.containerClass = containerClass; + } + + @Override + public Class getContainerClass() { + return containerClass; + } + + @Override + public IRecipeTransferError transferRecipe(Container container, IRecipeLayout recipeLayout, PlayerEntity player, boolean maxTransfer, boolean doTransfer) { + if (container instanceof IJEIAutoFillTerminal) { + if (doTransfer) { + ItemStack[][] stacks = transferFunc.apply(recipeLayout); + CompoundNBT compound = new CompoundNBT(); + ListNBT list = new ListNBT(); + for (int i = 0;i < stacks.length;++i) { + if (stacks[i] != null) { + CompoundNBT CompoundNBT = new CompoundNBT(); + CompoundNBT.putByte("s", (byte) i); + for (int j = 0;j < stacks[i].length && j < 3;j++) { + if (stacks[i][j] != null && !stacks[i][j].isEmpty()) { + CompoundNBT tag = new CompoundNBT(); + stacks[i][j].write(tag); + CompoundNBT.put("i" + j, tag); + } + } + CompoundNBT.putByte("l", (byte) Math.min(3, stacks[i].length)); + list.add(CompoundNBT); + } + } + compound.put("i", list); + ((IJEIAutoFillTerminal) container).sendMessage(compound); + } + } else { + return ERROR_INSTANCE; + } + return null; + } + + public static void registerTransferHandlers(IRecipeTransferRegistration recipeTransferRegistry) { + for (int i = 0;i < containerClasses.size();i++) + recipeTransferRegistry.addRecipeTransferHandler(new CraftingTerminalTransferHandler(containerClasses.get(i)), VanillaRecipeCategoryUid.CRAFTING); + } +} diff --git a/src/main/java/com/tom/storagemod/jei/IJEIAutoFillTerminal.java b/src/main/java/com/tom/storagemod/jei/IJEIAutoFillTerminal.java new file mode 100644 index 00000000..8f6fd9cb --- /dev/null +++ b/src/main/java/com/tom/storagemod/jei/IJEIAutoFillTerminal.java @@ -0,0 +1,8 @@ +package com.tom.storagemod.jei; + +import net.minecraft.nbt.CompoundNBT; + +public interface IJEIAutoFillTerminal { + void sendMessage(CompoundNBT compound); + +} diff --git a/src/main/java/com/tom/storagemod/jei/JEIHandler.java b/src/main/java/com/tom/storagemod/jei/JEIHandler.java new file mode 100644 index 00000000..6357620b --- /dev/null +++ b/src/main/java/com/tom/storagemod/jei/JEIHandler.java @@ -0,0 +1,38 @@ +package com.tom.storagemod.jei; + +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; + +import com.tom.storagemod.StorageMod; +import com.tom.storagemod.gui.GuiCraftingTerminal; + +import mezz.jei.api.IModPlugin; +import mezz.jei.api.JeiPlugin; +import mezz.jei.api.constants.VanillaRecipeCategoryUid; +import mezz.jei.api.registration.IGuiHandlerRegistration; +import mezz.jei.api.registration.IRecipeCatalystRegistration; +import mezz.jei.api.registration.IRecipeTransferRegistration; + +@JeiPlugin +public class JEIHandler implements IModPlugin { + + @Override + public ResourceLocation getPluginUid() { + return new ResourceLocation(StorageMod.modid, "jei"); + } + + @Override + public void registerGuiHandlers(IGuiHandlerRegistration registration) { + registration.addRecipeClickArea(GuiCraftingTerminal.class, 83, 125, 28, 23, new ResourceLocation[] { VanillaRecipeCategoryUid.CRAFTING }); + } + + @Override + public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) { + CraftingTerminalTransferHandler.registerTransferHandlers(registration); + } + + @Override + public void registerRecipeCatalysts(IRecipeCatalystRegistration registration) { + registration.addRecipeCatalyst(new ItemStack(StorageMod.craftingTerminal), new ResourceLocation[] { VanillaRecipeCategoryUid.CRAFTING }); + } +} diff --git a/src/main/java/com/tom/storagemod/network/DataPacket.java b/src/main/java/com/tom/storagemod/network/DataPacket.java new file mode 100644 index 00000000..055a306f --- /dev/null +++ b/src/main/java/com/tom/storagemod/network/DataPacket.java @@ -0,0 +1,20 @@ +package com.tom.storagemod.network; + +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.network.PacketBuffer; + +public class DataPacket { + public CompoundNBT tag; + + public DataPacket(CompoundNBT tag) { + this.tag = tag; + } + + public DataPacket(PacketBuffer pb) { + tag = pb.readCompoundTag(); + } + + public void toBytes(PacketBuffer pb) { + pb.writeCompoundTag(tag); + } +} diff --git a/src/main/java/com/tom/storagemod/network/IDataReceiver.java b/src/main/java/com/tom/storagemod/network/IDataReceiver.java new file mode 100644 index 00000000..bd1a5ae9 --- /dev/null +++ b/src/main/java/com/tom/storagemod/network/IDataReceiver.java @@ -0,0 +1,7 @@ +package com.tom.storagemod.network; + +import net.minecraft.nbt.CompoundNBT; + +public interface IDataReceiver { + void receive(CompoundNBT tag); +} diff --git a/src/main/java/com/tom/storagemod/network/NetworkHandler.java b/src/main/java/com/tom/storagemod/network/NetworkHandler.java new file mode 100644 index 00000000..f5050887 --- /dev/null +++ b/src/main/java/com/tom/storagemod/network/NetworkHandler.java @@ -0,0 +1,56 @@ +package com.tom.storagemod.network; + +import java.util.function.Supplier; + +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.ServerPlayerEntity; +import net.minecraft.nbt.CompoundNBT; +import net.minecraft.util.ResourceLocation; + +import net.minecraftforge.fml.network.NetworkDirection; +import net.minecraftforge.fml.network.NetworkEvent; +import net.minecraftforge.fml.network.NetworkRegistry; +import net.minecraftforge.fml.network.PacketDistributor; +import net.minecraftforge.fml.network.simple.SimpleChannel; + +import com.tom.storagemod.StorageMod; + +public class NetworkHandler { + private static final String PROTOCOL_VERSION = "1"; + public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel( + new ResourceLocation(StorageMod.modid, "main"), + () -> PROTOCOL_VERSION, + PROTOCOL_VERSION::equals, + PROTOCOL_VERSION::equals + ); + public static void init() { + INSTANCE.registerMessage(0, DataPacket.class, DataPacket::toBytes, DataPacket::new, NetworkHandler::handleData); + StorageMod.LOGGER.info("Initilaized Network Handler"); + } + + public static void handleData(DataPacket packet, Supplier ctx) { + if(ctx.get().getDirection() == NetworkDirection.PLAY_TO_SERVER) { + ctx.get().enqueueWork(() -> { + ServerPlayerEntity sender = ctx.get().getSender(); + if(sender.openContainer instanceof IDataReceiver) { + ((IDataReceiver)sender.openContainer).receive(packet.tag); + } + }); + } else if(ctx.get().getDirection() == NetworkDirection.PLAY_TO_CLIENT) { + ctx.get().enqueueWork(() -> { + if(Minecraft.getInstance().currentScreen instanceof IDataReceiver) { + ((IDataReceiver)Minecraft.getInstance().currentScreen).receive(packet.tag); + } + }); + } + ctx.get().setPacketHandled(true); + } + + public static void sendDataToServer(CompoundNBT tag) { + INSTANCE.sendToServer(new DataPacket(tag)); + } + + public static void sendTo(ServerPlayerEntity pl, CompoundNBT tag) { + INSTANCE.send(PacketDistributor.PLAYER.with(() -> pl), new DataPacket(tag)); + } +} diff --git a/src/main/java/com/tom/storagemod/tile/TileEntityCraftingTerminal.java b/src/main/java/com/tom/storagemod/tile/TileEntityCraftingTerminal.java index 92c6b8da..7e734a00 100644 --- a/src/main/java/com/tom/storagemod/tile/TileEntityCraftingTerminal.java +++ b/src/main/java/com/tom/storagemod/tile/TileEntityCraftingTerminal.java @@ -192,12 +192,56 @@ public void clear() { for (int i = 0; i < craftMatrix.getSizeInventory(); i++) { ItemStack st = craftMatrix.removeStackFromSlot(i); if(!st.isEmpty()) { - st = pushStack(st); - if(!st.isEmpty()) { - InventoryHelper.spawnItemStack(world, pos.getX() + .5f, pos.getY() + .5f, pos.getZ() + .5f, st); + pushOrDrop(st); + } + } + onCraftingMatrixChanged(); + } + + private void pushOrDrop(ItemStack st) { + st = pushStack(st); + if(!st.isEmpty()) { + InventoryHelper.spawnItemStack(world, pos.getX() + .5f, pos.getY() + .5f, pos.getZ() + .5f, st); + } + } + + public void handlerItemTransfer(PlayerEntity player, ItemStack[][] items) { + clear(); + for (int i = 0;i < 9;i++) { + if (items[i] != null) { + ItemStack stack = ItemStack.EMPTY; + for (int j = 0;j < items[i].length;j++) { + ItemStack pulled = pullStack(items[i][j]); + if (!pulled.isEmpty()) { + stack = pulled; + break; + } + } + if (stack.isEmpty()) { + for (int j = 0;j < items[i].length;j++) { + boolean br = false; + for (int k = 0;k < player.inventory.getSizeInventory();k++) { + if(ItemStack.areItemsEqual(player.inventory.getStackInSlot(k), items[i][j])) { + stack = player.inventory.decrStackSize(k, 1); + br = true; + break; + } + } + if (br) + break; + } + } + if (!stack.isEmpty()) { + craftMatrix.setInventorySlotContents(i, stack); } } } onCraftingMatrixChanged(); } + + private ItemStack pullStack(ItemStack itemStack) { + StoredItemStack is = pullStack(new StoredItemStack(itemStack), 1); + if(is == null)return ItemStack.EMPTY; + else return is.getActualStack(); + } } diff --git a/src/main/resources/data/toms_storage/recipes/storage_terminal.json b/src/main/resources/data/toms_storage/recipes/storage_terminal.json index 1126c016..d4fb2525 100644 --- a/src/main/resources/data/toms_storage/recipes/storage_terminal.json +++ b/src/main/resources/data/toms_storage/recipes/storage_terminal.json @@ -10,7 +10,7 @@ "tag": "minecraft:planks" }, "c": { - "item": "minecraft:chest" + "tag": "forge:chests/wooden" }, "C": { "item": "minecraft:comparator" @@ -19,7 +19,7 @@ "item": "minecraft:glowstone" }, "g": { - "item": "minecraft:glass" + "tag": "forge:glass" } }, "result": { diff --git a/version-check.json b/version-check.json index 6c0f2ffd..61739ced 100644 --- a/version-check.json +++ b/version-check.json @@ -1,10 +1,11 @@ { "homepage": "https://www.curseforge.com/minecraft/mc-mods/toms-storage", "promos": { - "1.15.2-latest": "1.1.0", - "1.15.2-recommended": "1.1.0" + "1.15.2-latest": "1.1.1", + "1.15.2-recommended": "1.1.1" }, "1.15.2": { + "1.1.1": "Fixed Inventory Cable Connector not showing up in the creative inventory and JEI\nAdded JEI compatibility to crafting terminal\nFixed Storage Terminal recipe not using tags\nTerminals now emit light", "1.1.0": "Added Crafting Terminal\nAdded the ability to paint some of the blocks\nAdded Inevntory Cable & Connector\nAdded Inventory Proxy\nAdded Wireless Terminal\nAdded config to change Inventory Connector range.", "1.0.2": "Fixed crash", "1.0.1": "Fixed loot tables",