• Recently Browsing

    No registered users viewing this page.

  • Posts

    • Up to date code: Container: package com.budrunbun.lavalamp.containers; import com.budrunbun.lavalamp.containers.slots.InputSlot; import com.budrunbun.lavalamp.containers.slots.OutputSlot; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.Slot; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.SlotItemHandler; import net.minecraftforge.items.wrapper.InvWrapper; public class CheeseGeneratorContainer extends Container { private final IItemHandler generatorInventory, playerInventory; private final int[] data; public CheeseGeneratorContainer(int windowId, PlayerInventory playerInventory, IItemHandler generatorInventory, int[] data) { super(ModContainers.CHEESE_GENERATOR_CONTAINER, windowId); this.generatorInventory = generatorInventory; this.playerInventory = new InvWrapper(playerInventory); this.data = data; this.addSlot(new InputSlot(Items.MILK_BUCKET, generatorInventory, 0, 12, 58)); this.addSlot(new InputSlot(Items.ACACIA_FENCE,generatorInventory, 1, 84, 75)); this.addSlot(new InputSlot(Items.WATER_BUCKET,generatorInventory, 2, 156, 58)); this.addSlot(new OutputSlot(generatorInventory, 3, 84, 13, playerInventory.player)); int leftCol = (184 - 162) / 2 + 1; for (int playerInvRow = 0; playerInvRow < 3; playerInvRow++) { for (int playerInvCol = 0; playerInvCol < 9; playerInvCol++) { this.addSlot(new SlotItemHandler(this.playerInventory, playerInvCol + playerInvRow * 9 + 9, leftCol + playerInvCol * 18, 184 - (4 - playerInvRow) * 18 - 10)); } } for (int hotbarSlot = 0; hotbarSlot < 9; hotbarSlot++) { this.addSlot(new SlotItemHandler(this.playerInventory, hotbarSlot, leftCol + hotbarSlot * 18, 184 - 24)); } System.out.println("Container " + this.data[0] + "/8"); } @Override public boolean canInteractWith(PlayerEntity playerIn) { return true; } public int getMilkCapacity() { return this.data[0]; } @Override public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) { ItemStack itemstack = ItemStack.EMPTY; Slot slot = this.inventorySlots.get(index); if (slot != null && slot.getHasStack()) { ItemStack stack = slot.getStack(); itemstack = stack.copy(); if (index == 0) { if (!this.mergeItemStack(stack, 1, 37, true)) { return ItemStack.EMPTY; } slot.onSlotChange(stack, itemstack); } else { if (stack.getItem() == Items.MILK_BUCKET) { if (!this.mergeItemStack(stack, 0, 1, false)) { return ItemStack.EMPTY; } } else if (index < 28) { if (!this.mergeItemStack(stack, 28, 37, false)) { return ItemStack.EMPTY; } } else if (index < 37 && !this.mergeItemStack(stack, 1, 28, false)) { return ItemStack.EMPTY; } } if (stack.isEmpty()) { slot.putStack(ItemStack.EMPTY); } else { slot.onSlotChanged(); } if (stack.getCount() == itemstack.getCount()) { return ItemStack.EMPTY; } slot.onTake(playerIn, stack); } return itemstack; } public int getMilkCapacityScaled() { return this.data[0] * 49 / 8; } public int getWaterCapacityScaled() { return this.data[1] * 49 / 8; } } Tile entity: package com.budrunbun.lavalamp.tileEntities; import com.budrunbun.lavalamp.containers.CheeseGeneratorContainer; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.container.Container; import net.minecraft.inventory.container.INamedContainerProvider; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; import net.minecraft.nbt.CompoundNBT; import net.minecraft.tileentity.ITickableTileEntity; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TranslationTextComponent; import net.minecraftforge.items.ItemStackHandler; import javax.annotation.Nullable; public class CheeseGeneratorTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity { private int milkCapacity; private int waterCapacity; protected int[] data = new int[2]; private final ItemStackHandler handler = new ItemStackHandler(4) { @Override protected void onContentsChanged(int slot) { super.onContentsChanged(slot); markDirty(); } }; public CheeseGeneratorTileEntity() { super(ModTileEntities.CHEESE_GENERATOR_TE); System.out.println("Tile entity " + milkCapacity + "/8"); } @Override public void read(CompoundNBT compound) { super.read(compound); this.handler.deserializeNBT(compound.getCompound("inv")); this.milkCapacity = compound.getInt("milk"); this.waterCapacity = compound.getInt("water"); } @Override public CompoundNBT write(CompoundNBT compound) { super.write(compound); compound.put("inv", handler.serializeNBT()); compound.putInt("milk", milkCapacity); compound.putInt("water", milkCapacity); return compound; } @Override public ITextComponent getDisplayName() { return new TranslationTextComponent("cheese_generator"); } @Nullable @Override public Container createMenu(int windowId, PlayerInventory playerInventory, PlayerEntity playerEntity) { return new CheeseGeneratorContainer(windowId, playerInventory, handler, this.data); } public ItemStackHandler getHandler() { return handler; } public int getMilkCapacity() { return milkCapacity; } public int[] getData() { return data; } @Override public void tick() { this.data[0] = this.milkCapacity; this.data[1] = this.waterCapacity; if (handler.getStackInSlot(0).getItem() == (Items.MILK_BUCKET) && milkCapacity < 8) { handler.setStackInSlot(0, new ItemStack(Items.BUCKET)); this.milkCapacity++; this.markDirty(); } if (handler.getStackInSlot(2).getItem() == (Items.WATER_BUCKET) && waterCapacity < 8) { handler.setStackInSlot(2, new ItemStack(Items.BUCKET)); this.waterCapacity++; this.markDirty(); } } }  
    • Those tutorials are bad then. Unfortunately most modding tutorials are written by amateurs, who don't even come here to ask for feedback before posting it as just "the way to do it" on Youtube, etc.   Registration must happen exactly when Forge tells you. Forge does so by firing the registry event. If you use a static initializer you completely bypass this system. This prevents Forge from doing improvements in the future, such as reloading mods at runtime or even disabling/enabling them.   Yes, it's fine now. However if you do write a tutorial I would recommend you use DeferredRegister or at least RegistryObject as they are the current recommended approach. "Magic injection into fields" using @ObjectHolder is ugly. Why do you not want to use them?
    • I know it would fit and that means that problem is somewhere else. I wonder why it does not get to client? Maybe problem is in the registries: @SubscribeEvent public static void onContainerRegistry(RegistryEvent.Register<ContainerType<?>> event) { // register a new container here event.getRegistry().register(IForgeContainerType.create((id, inv, data) -> { CheeseGeneratorTileEntity tile = new CheeseGeneratorTileEntity(); return new CheeseGeneratorContainer(id, inv, tile.getHandler(), tile.getData()); }).setRegistryName("cheese_generator")); }
    • To make a key binding spawn an entity you have to send a packet to the server when the key is pressed. Make sure to include safety checks to avoid exploits (e.g. a time limit between consecutive presses) on the server.
    • Does the crash happen if you load the world without any mods installed?
  • Who's Online (See full list)