From bf258580d41db6af914e0ed00834aee353a3b78e Mon Sep 17 00:00:00 2001 From: Christian Knaapen Date: Mon, 17 Sep 2018 01:17:51 +0200 Subject: [PATCH] Randomizer bag finished (including recipe, placing blocks). Changed default keybinds to ADD and SUBTRACT. QuickReplace now places under replaceable blocks (tall grass). Mirror now happens before array. --- .../nl/requios/effortlessbuilding/Array.java | 15 --- .../EffortlessBuilding.java | 4 +- .../effortlessbuilding/EventHandler.java | 5 +- .../nl/requios/effortlessbuilding/Mirror.java | 50 +++++--- .../effortlessbuilding/QuickReplace.java | 7 +- .../ItemHandlerCapabilityProvider.java} | 11 +- .../RandomizerBagContainer.java | 72 ++++++------ .../RandomizerBagGuiContainer.java | 41 +++---- .../RandomizerBagGuiHandler.java} | 18 ++- .../item/ItemRandomizerBag.java | 111 ++++++++++++++---- .../effortlessbuilding/proxy/ClientProxy.java | 4 +- .../models/item/randomizer_bag.json | 2 +- .../recipes/randomizer_bag.json | 20 ++++ .../textures/gui/container/randomizerbag.png | Bin 1432 -> 1164 bytes .../textures/items/randomizerbag.png | Bin 0 -> 358 bytes .../textures/items/randomizerbag2.png | Bin 0 -> 358 bytes .../textures/items/randomizerbag_orig.png | Bin 0 -> 1090 bytes 17 files changed, 233 insertions(+), 127 deletions(-) rename src/main/java/nl/requios/effortlessbuilding/{inventory/RandomizerBagCapabilityProvider.java => capability/ItemHandlerCapabilityProvider.java} (72%) rename src/main/java/nl/requios/effortlessbuilding/{inventory => gui}/RandomizerBagContainer.java (54%) rename src/main/java/nl/requios/effortlessbuilding/{inventory => gui}/RandomizerBagGuiContainer.java (52%) rename src/main/java/nl/requios/effortlessbuilding/{inventory/RandomizerGuiHandler.java => gui/RandomizerBagGuiHandler.java} (52%) create mode 100644 src/main/resources/assets/effortlessbuilding/recipes/randomizer_bag.json create mode 100644 src/main/resources/assets/effortlessbuilding/textures/items/randomizerbag.png create mode 100644 src/main/resources/assets/effortlessbuilding/textures/items/randomizerbag2.png create mode 100644 src/main/resources/assets/effortlessbuilding/textures/items/randomizerbag_orig.png diff --git a/src/main/java/nl/requios/effortlessbuilding/Array.java b/src/main/java/nl/requios/effortlessbuilding/Array.java index d5dc1a4..9f208b9 100644 --- a/src/main/java/nl/requios/effortlessbuilding/Array.java +++ b/src/main/java/nl/requios/effortlessbuilding/Array.java @@ -41,11 +41,6 @@ public class Array { pos = pos.add(offset); if (event.getWorld().isBlockLoaded(pos, true)) { event.getWorld().setBlockState(pos, event.getPlacedBlock()); - - //Mirror synergy - BlockSnapshot blockSnapshot = new BlockSnapshot(event.getWorld(), pos, event.getPlacedBlock()); - BlockEvent.PlaceEvent placeEvent = new BlockEvent.PlaceEvent(blockSnapshot, event.getPlacedBlock(), event.getPlayer(), EnumHand.MAIN_HAND); - Mirror.onBlockPlaced(placeEvent); } } } @@ -66,17 +61,7 @@ public class Array { pos = pos.add(offset); if (event.getWorld().isBlockLoaded(pos, false)) { event.getWorld().setBlockToAir(pos); - - //Mirror synergy - BlockEvent.BreakEvent breakEvent = new BlockEvent.BreakEvent(event.getWorld(), pos, event.getState(), event.getPlayer()); - Mirror.onBlockBroken(breakEvent); } } } - -// @SubscribeEvent -// @SideOnly(Side.CLIENT) -// public static void onRender(RenderWorldLastEvent event) { -// -// } } diff --git a/src/main/java/nl/requios/effortlessbuilding/EffortlessBuilding.java b/src/main/java/nl/requios/effortlessbuilding/EffortlessBuilding.java index baa2732..29ce149 100644 --- a/src/main/java/nl/requios/effortlessbuilding/EffortlessBuilding.java +++ b/src/main/java/nl/requios/effortlessbuilding/EffortlessBuilding.java @@ -16,7 +16,7 @@ import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; import net.minecraftforge.fml.relauncher.Side; import nl.requios.effortlessbuilding.capability.*; -import nl.requios.effortlessbuilding.inventory.RandomizerGuiHandler; +import nl.requios.effortlessbuilding.gui.RandomizerBagGuiHandler; import nl.requios.effortlessbuilding.item.ItemRandomizerBag; import nl.requios.effortlessbuilding.network.BuildSettingsMessage; import nl.requios.effortlessbuilding.network.QuickReplaceMessage; @@ -75,7 +75,7 @@ public class EffortlessBuilding // Register network handlers public void init(FMLInitializationEvent event) { - NetworkRegistry.INSTANCE.registerGuiHandler(EffortlessBuilding.instance, new RandomizerGuiHandler()); + NetworkRegistry.INSTANCE.registerGuiHandler(EffortlessBuilding.instance, new RandomizerBagGuiHandler()); proxy.init(event); } diff --git a/src/main/java/nl/requios/effortlessbuilding/EventHandler.java b/src/main/java/nl/requios/effortlessbuilding/EventHandler.java index aaadd48..02573f5 100644 --- a/src/main/java/nl/requios/effortlessbuilding/EventHandler.java +++ b/src/main/java/nl/requios/effortlessbuilding/EventHandler.java @@ -5,6 +5,7 @@ import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; +import net.minecraft.item.crafting.IRecipe; import net.minecraft.util.ResourceLocation; import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.RegistryEvent; @@ -45,15 +46,15 @@ public class EventHandler public static void onBlockPlaced(BlockEvent.PlaceEvent event) { QuickReplace.onBlockPlaced(event); if (event.isCanceled()) return; - Array.onBlockPlaced(event); Mirror.onBlockPlaced(event); + Array.onBlockPlaced(event); } @SubscribeEvent public static void onBlockBroken(BlockEvent.BreakEvent event) { - Array.onBlockBroken(event); Mirror.onBlockBroken(event); + Array.onBlockBroken(event); } } diff --git a/src/main/java/nl/requios/effortlessbuilding/Mirror.java b/src/main/java/nl/requios/effortlessbuilding/Mirror.java index 5a4b939..07166d5 100644 --- a/src/main/java/nl/requios/effortlessbuilding/Mirror.java +++ b/src/main/java/nl/requios/effortlessbuilding/Mirror.java @@ -9,11 +9,14 @@ import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import net.minecraftforge.client.event.RenderWorldLastEvent; +import net.minecraftforge.common.util.BlockSnapshot; import net.minecraftforge.event.world.BlockEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @@ -77,19 +80,19 @@ public class Mirror { return; if (m.mirrorX) { - placeMirrorX(event.getWorld(), m, oldBlockPos, event.getPlacedBlock()); + placeMirrorX(event.getWorld(), event.getPlayer(), m, oldBlockPos, event.getPlacedBlock()); } if (m.mirrorY) { - placeMirrorY(event.getWorld(), m, oldBlockPos, event.getPlacedBlock()); + placeMirrorY(event.getWorld(), event.getPlayer(), m, oldBlockPos, event.getPlacedBlock()); } if (m.mirrorZ) { - placeMirrorZ(event.getWorld(), m, oldBlockPos, event.getPlacedBlock()); + placeMirrorZ(event.getWorld(), event.getPlayer(), m, oldBlockPos, event.getPlacedBlock()); } } - private static void placeMirrorX(World world, MirrorSettings m, BlockPos oldBlockPos, IBlockState oldBlockState) { + private static void placeMirrorX(World world, EntityPlayer player, MirrorSettings m, BlockPos oldBlockPos, IBlockState oldBlockState) { //find mirror position double x = m.position.x + (m.position.x - oldBlockPos.getX() - 0.5); BlockPos newBlockPos = new BlockPos(x, oldBlockPos.getY(), oldBlockPos.getZ()); @@ -98,13 +101,13 @@ public class Mirror { if (world.isBlockLoaded(newBlockPos, true)) { newBlockState = oldBlockState.withMirror(net.minecraft.util.Mirror.FRONT_BACK); - world.setBlockState(newBlockPos, newBlockState); + placeBlock(world, player, newBlockPos, newBlockState); } - if (m.mirrorY) placeMirrorY(world, m, newBlockPos, newBlockState); - if (m.mirrorZ) placeMirrorZ(world, m, newBlockPos, newBlockState); + if (m.mirrorY) placeMirrorY(world, player, m, newBlockPos, newBlockState); + if (m.mirrorZ) placeMirrorZ(world, player, m, newBlockPos, newBlockState); } - private static void placeMirrorY(World world, MirrorSettings m, BlockPos oldBlockPos, IBlockState oldBlockState) { + private static void placeMirrorY(World world, EntityPlayer player, MirrorSettings m, BlockPos oldBlockPos, IBlockState oldBlockState) { //find mirror position double y = m.position.y + (m.position.y - oldBlockPos.getY() - 0.5); BlockPos newBlockPos = new BlockPos(oldBlockPos.getX(), y, oldBlockPos.getZ()); @@ -113,12 +116,12 @@ public class Mirror { if (world.isBlockLoaded(newBlockPos, true)) { newBlockState = getVerticalMirror(oldBlockState); - world.setBlockState(newBlockPos, newBlockState); + placeBlock(world, player, newBlockPos, newBlockState); } - if (m.mirrorZ) placeMirrorZ(world, m, newBlockPos, newBlockState); + if (m.mirrorZ) placeMirrorZ(world, player, m, newBlockPos, newBlockState); } - private static void placeMirrorZ(World world, MirrorSettings m, BlockPos oldBlockPos, IBlockState oldBlockState) { + private static void placeMirrorZ(World world, EntityPlayer player, MirrorSettings m, BlockPos oldBlockPos, IBlockState oldBlockState) { //find mirror position double z = m.position.z + (m.position.z - oldBlockPos.getZ() - 0.5); BlockPos newBlockPos = new BlockPos(oldBlockPos.getX(), oldBlockPos.getY(), z); @@ -127,10 +130,19 @@ public class Mirror { if (world.isBlockLoaded(newBlockPos, true)) { newBlockState = oldBlockState.withMirror(net.minecraft.util.Mirror.LEFT_RIGHT); - world.setBlockState(newBlockPos, newBlockState); + placeBlock(world, player, newBlockPos, newBlockState); } } + private static void placeBlock(World world, EntityPlayer player, BlockPos newBlockPos, IBlockState newBlockState){ + world.setBlockState(newBlockPos, newBlockState); + + //Array synergy + BlockSnapshot blockSnapshot = new BlockSnapshot(world, newBlockPos, newBlockState); + BlockEvent.PlaceEvent placeEvent = new BlockEvent.PlaceEvent(blockSnapshot, newBlockState, player, EnumHand.MAIN_HAND); + Array.onBlockPlaced(placeEvent); + } + private static IBlockState getVerticalMirror(IBlockState blockState) { //Stairs if (blockState.getBlock() instanceof BlockStairs) { @@ -199,7 +211,7 @@ public class Mirror { BlockPos newBlockPos = new BlockPos(x, oldBlockPos.getY(), oldBlockPos.getZ()); //break block if (event.getWorld().isBlockLoaded(newBlockPos, true)) { - event.getWorld().setBlockToAir(newBlockPos); + breakBlock(event, newBlockPos); } if (m.mirrorY) breakMirrorY(event, m, newBlockPos); if (m.mirrorZ) breakMirrorZ(event, m, newBlockPos); @@ -211,7 +223,7 @@ public class Mirror { BlockPos newBlockPos = new BlockPos(oldBlockPos.getX(), y, oldBlockPos.getZ()); //place block if (event.getWorld().isBlockLoaded(newBlockPos, true)) { - event.getWorld().setBlockToAir(newBlockPos); + breakBlock(event, newBlockPos); } if (m.mirrorZ) breakMirrorZ(event, m, newBlockPos); } @@ -222,10 +234,18 @@ public class Mirror { BlockPos newBlockPos = new BlockPos(oldBlockPos.getX(), oldBlockPos.getY(), z); //place block if (event.getWorld().isBlockLoaded(newBlockPos, true)) { - event.getWorld().setBlockToAir(newBlockPos); + breakBlock(event, newBlockPos); } } + private static void breakBlock(BlockEvent.BreakEvent event, BlockPos newBlockPos){ + event.getWorld().setBlockToAir(newBlockPos); + + //Array synergy + BlockEvent.BreakEvent breakEvent = new BlockEvent.BreakEvent(event.getWorld(), newBlockPos, event.getState(), event.getPlayer()); + Array.onBlockBroken(breakEvent); + } + @SubscribeEvent @SideOnly(Side.CLIENT) public static void onRender(RenderWorldLastEvent event) { diff --git a/src/main/java/nl/requios/effortlessbuilding/QuickReplace.java b/src/main/java/nl/requios/effortlessbuilding/QuickReplace.java index 023b1d6..27a06f7 100644 --- a/src/main/java/nl/requios/effortlessbuilding/QuickReplace.java +++ b/src/main/java/nl/requios/effortlessbuilding/QuickReplace.java @@ -48,7 +48,12 @@ public class QuickReplace { if (!message.isBlockHit() || message.getBlockPos() == null) return; BlockPos placedAgainstBlockPos = message.getBlockPos(); - //placedAgainstBlockPos = placedAgainstBlockPos.down(); + + //Get under tall grass and other replaceable blocks + if (player.world.getBlockState(placedAgainstBlockPos).getBlock().isReplaceable(player.world, placedAgainstBlockPos)) { + placedAgainstBlockPos = placedAgainstBlockPos.down(); + } + IBlockState blockState = blockStates.get(player.getUniqueID()); player.world.setBlockState(placedAgainstBlockPos, blockState); diff --git a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagCapabilityProvider.java b/src/main/java/nl/requios/effortlessbuilding/capability/ItemHandlerCapabilityProvider.java similarity index 72% rename from src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagCapabilityProvider.java rename to src/main/java/nl/requios/effortlessbuilding/capability/ItemHandlerCapabilityProvider.java index 2529825..238eb3b 100644 --- a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagCapabilityProvider.java +++ b/src/main/java/nl/requios/effortlessbuilding/capability/ItemHandlerCapabilityProvider.java @@ -1,4 +1,4 @@ -package nl.requios.effortlessbuilding.inventory; +package nl.requios.effortlessbuilding.capability; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; @@ -7,12 +7,13 @@ import net.minecraftforge.common.capabilities.ICapabilitySerializable; import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.ItemStackHandler; +import nl.requios.effortlessbuilding.item.ItemRandomizerBag; import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class RandomizerBagCapabilityProvider implements ICapabilitySerializable { - IItemHandler itemHandler = new ItemStackHandler(5); +public class ItemHandlerCapabilityProvider implements ICapabilitySerializable { + IItemHandler itemHandler = new ItemStackHandler(ItemRandomizerBag.INV_SIZE); @Override public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { @@ -31,11 +32,11 @@ public class RandomizerBagCapabilityProvider implements ICapabilitySerializable< @Override public NBTTagCompound serializeNBT() { - return ((ItemStackHandler)itemHandler).serializeNBT(); + return ((ItemStackHandler) itemHandler).serializeNBT(); } @Override public void deserializeNBT(NBTTagCompound nbt) { - ((ItemStackHandler)itemHandler).deserializeNBT(nbt); + ((ItemStackHandler) itemHandler).deserializeNBT(nbt); } } diff --git a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagContainer.java b/src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagContainer.java similarity index 54% rename from src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagContainer.java rename to src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagContainer.java index ba24af2..3b0923b 100644 --- a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagContainer.java +++ b/src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagContainer.java @@ -1,4 +1,4 @@ -package nl.requios.effortlessbuilding.inventory; +package nl.requios.effortlessbuilding.gui; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; @@ -10,30 +10,34 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.EnumHand; import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.SlotItemHandler; +import nl.requios.effortlessbuilding.item.ItemRandomizerBag; public class RandomizerBagContainer extends Container { private final IItemHandler bagInventory; private final int sizeInventory; + private static final int INV_START = ItemRandomizerBag.INV_SIZE, INV_END = INV_START + 26, + HOTBAR_START = INV_END + 1, HOTBAR_END = HOTBAR_START + 8; + public RandomizerBagContainer(InventoryPlayer parInventoryPlayer, IItemHandler parIInventory) { bagInventory = parIInventory; sizeInventory = bagInventory.getSlots(); for (int i = 0; i < sizeInventory; ++i) { - this.addSlotToContainer(new SlotItemHandler(bagInventory, i, 80 + (18 * (i / 4)), 8 + (18 * (i % 4)))); + this.addSlotToContainer(new SlotItemHandler(bagInventory, i, 44 + (18 * i), 20)); } // add player inventory slots int i; for (i = 0; i < 3; ++i) { for (int j = 0; j < 9; ++j) { - addSlotToContainer(new Slot(parInventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18)); + addSlotToContainer(new Slot(parInventoryPlayer, j + i * 9 + 9, 8 + j * 18, 51 + i * 18)); } } // add hotbar slots for (i = 0; i < 9; ++i) { - addSlotToContainer(new Slot(parInventoryPlayer, i, 8 + i * 18, 142)); + addSlotToContainer(new Slot(parInventoryPlayer, i, 8 + i * 18, 109)); } } @@ -43,58 +47,58 @@ public class RandomizerBagContainer extends Container { } @Override - public ItemStack transferStackInSlot(EntityPlayer playerIn, - int slotIndex) { - ItemStack itemStack1 = null; - Slot slot = inventorySlots.get(slotIndex); + public ItemStack transferStackInSlot(EntityPlayer playerIn, int slotIndex) { + ItemStack itemstack = null; + Slot slot = this.inventorySlots.get(slotIndex); if (slot != null && slot.getHasStack()) { - ItemStack itemStack2 = slot.getStack(); - itemStack1 = itemStack2.copy(); + ItemStack itemstack1 = slot.getStack(); + itemstack = itemstack1.copy(); - if (slotIndex == 0) { - if (!mergeItemStack(itemStack2, sizeInventory, - sizeInventory + 36, true)) { + // If item is in our custom inventory + if (slotIndex < INV_START) { + // try to place in player inventory / action bar + if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true)) { return null; } - slot.onSlotChange(itemStack2, itemStack1); - } else if (slotIndex != 1) { - if (slotIndex >= sizeInventory - && slotIndex < sizeInventory + 27) // player inventory slots - { - if (!mergeItemStack(itemStack2, sizeInventory + 27, - sizeInventory + 36, false)) { + slot.onSlotChange(itemstack1, itemstack); + } + // Item is in inventory / hotbar, try to place in custom inventory or armor slots + else { + /** + * Implementation number 1: Shift-click into your custom inventory + */ + if (slotIndex >= INV_START) { + // place in custom inventory + if (!this.mergeItemStack(itemstack1, 0, INV_START, false)) { return null; } - } else if (slotIndex >= sizeInventory + 27 - && slotIndex < sizeInventory + 36 - && !mergeItemStack(itemStack2, sizeInventory + 1, - sizeInventory + 27, false)) // hotbar slots - { - return null; } - } else if (!mergeItemStack(itemStack2, sizeInventory, - sizeInventory + 36, false)) { - return null; + } - if (itemStack2.getCount() == 0) { - slot.putStack(null); + if (itemstack1.getCount() == 0) { + slot.putStack(ItemStack.EMPTY); } else { slot.onSlotChanged(); } - if (itemStack2.getCount() == itemStack1.getCount()) { + if (itemstack1.getCount() == itemstack.getCount()) { return null; } - slot.onTake(playerIn, itemStack2); + slot.onTake(playerIn, itemstack1); } - return itemStack1; + return itemstack; } + /** + * You should override this method to prevent the player from moving the stack that + * opened the inventory, otherwise if the player moves it, the inventory will not + * be able to save properly + */ @Override public ItemStack slotClick(int slot, int dragType, ClickType clickTypeIn, EntityPlayer player) { // this will prevent the player from interacting with the item that opened the inventory: diff --git a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagGuiContainer.java b/src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagGuiContainer.java similarity index 52% rename from src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagGuiContainer.java rename to src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagGuiContainer.java index 62f48d9..327ea02 100644 --- a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerBagGuiContainer.java +++ b/src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagGuiContainer.java @@ -1,9 +1,8 @@ -package nl.requios.effortlessbuilding.inventory; +package nl.requios.effortlessbuilding.gui; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.entity.player.InventoryPlayer; -import net.minecraft.inventory.IInventory; import net.minecraft.util.ResourceLocation; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -12,35 +11,37 @@ import nl.requios.effortlessbuilding.EffortlessBuilding; @SideOnly(Side.CLIENT) public class RandomizerBagGuiContainer extends GuiContainer { - private static final ResourceLocation grinderGuiTextures = - new ResourceLocation(EffortlessBuilding.MODID - + ":textures/gui/container/randomizerbag.png"); + private static final ResourceLocation guiTextures = + new ResourceLocation(EffortlessBuilding.MODID + ":textures/gui/container/randomizerbag.png"); private final InventoryPlayer inventoryPlayer; private final IItemHandler inventoryBag; - public RandomizerBagGuiContainer(InventoryPlayer parInventoryPlayer, - IItemHandler parInventoryBag) { - super(new RandomizerBagContainer(parInventoryPlayer, - parInventoryBag)); - inventoryPlayer = parInventoryPlayer; - inventoryBag = parInventoryBag; + public RandomizerBagGuiContainer(InventoryPlayer inventoryPlayer, IItemHandler inventoryBag) { + super(new RandomizerBagContainer(inventoryPlayer, inventoryBag)); + this.inventoryPlayer = inventoryPlayer; + this.inventoryBag = inventoryBag; + + ySize = 134; + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + drawDefaultBackground(); + super.drawScreen(mouseX, mouseY, partialTicks); + this.renderHoveredToolTip(mouseX, mouseY); } @Override protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) { - String s = "Randomizer bag"; - fontRenderer.drawString(s, xSize / 2 - fontRenderer.getStringWidth(s) / 2, 6, 4210752); - fontRenderer.drawString(inventoryPlayer.getDisplayName().getUnformattedText(), 8, ySize - 96 + 2, 4210752); + String s = "Randomizer Bag"; + fontRenderer.drawString(s, 8, 6, 0x404040); + fontRenderer.drawString(inventoryPlayer.getDisplayName().getUnformattedText(), 8, ySize - 96 + 2, 0x404040); } - /** - * Args : renderPartialTicks, mouseX, mouseY - */ @Override - protected void drawGuiContainerBackgroundLayer(float partialTicks, - int mouseX, int mouseY) { + protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); - mc.getTextureManager().bindTexture(grinderGuiTextures); + mc.getTextureManager().bindTexture(guiTextures); int marginHorizontal = (width - xSize) / 2; int marginVertical = (height - ySize) / 2; drawTexturedModalRect(marginHorizontal, marginVertical, 0, 0, xSize, ySize); diff --git a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerGuiHandler.java b/src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagGuiHandler.java similarity index 52% rename from src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerGuiHandler.java rename to src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagGuiHandler.java index 0caea6a..cdb3d3e 100644 --- a/src/main/java/nl/requios/effortlessbuilding/inventory/RandomizerGuiHandler.java +++ b/src/main/java/nl/requios/effortlessbuilding/gui/RandomizerBagGuiHandler.java @@ -1,24 +1,22 @@ -package nl.requios.effortlessbuilding.inventory; +package nl.requios.effortlessbuilding.gui; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.EnumHand; import net.minecraft.world.World; import net.minecraftforge.fml.common.network.IGuiHandler; import net.minecraftforge.items.CapabilityItemHandler; -import net.minecraftforge.items.IItemHandler; -import net.minecraftforge.items.ItemStackHandler; import nl.requios.effortlessbuilding.EffortlessBuilding; -import nl.requios.effortlessbuilding.inventory.RandomizerBagContainer; import javax.annotation.Nullable; -public class RandomizerGuiHandler implements IGuiHandler { +public class RandomizerBagGuiHandler implements IGuiHandler { @Nullable @Override public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) { - // Use the player's held item to create the inventory - return new RandomizerBagContainer(player.inventory, player.getHeldItem(EnumHand.MAIN_HAND).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)); + // Use the player's held item to create the container + return new RandomizerBagContainer(player.inventory, + player.getHeldItem(EnumHand.MAIN_HAND).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)); } return null; } @@ -27,9 +25,9 @@ public class RandomizerGuiHandler implements IGuiHandler { @Override public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) { if (ID == EffortlessBuilding.RANDOMIZER_BAG_GUI) { - // We have to cast the new container as our custom class - // and pass in currently held item for the inventory - return new RandomizerBagGuiContainer(player.inventory, player.getHeldItem(EnumHand.MAIN_HAND).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)); + // Use the player's held item to create the client-side gui container + return new RandomizerBagGuiContainer(player.inventory, + player.getHeldItem(EnumHand.MAIN_HAND).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)); } return null; } diff --git a/src/main/java/nl/requios/effortlessbuilding/item/ItemRandomizerBag.java b/src/main/java/nl/requios/effortlessbuilding/item/ItemRandomizerBag.java index ba254cd..e4eb188 100644 --- a/src/main/java/nl/requios/effortlessbuilding/item/ItemRandomizerBag.java +++ b/src/main/java/nl/requios/effortlessbuilding/item/ItemRandomizerBag.java @@ -1,57 +1,122 @@ package nl.requios.effortlessbuilding.item; +import net.minecraft.block.Block; +import net.minecraft.block.SoundType; +import net.minecraft.block.state.IBlockState; +import net.minecraft.client.util.ITooltipFlag; import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.util.ActionResult; -import net.minecraft.util.EnumActionResult; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; +import net.minecraft.util.*; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; import nl.requios.effortlessbuilding.EffortlessBuilding; -import nl.requios.effortlessbuilding.inventory.RandomizerBagCapabilityProvider; +import nl.requios.effortlessbuilding.capability.ItemHandlerCapabilityProvider; import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; public class ItemRandomizerBag extends Item { + public static final int INV_SIZE = 5; - public ItemRandomizerBag(){ + private Random rand; + + public ItemRandomizerBag() { this.setRegistryName(EffortlessBuilding.MODID, "randomizer_bag"); this.setUnlocalizedName(this.getRegistryName().toString()); this.maxStackSize = 1; this.setCreativeTab(CreativeTabs.DECORATIONS); + rand = new Random(1337); } @Override - public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { + public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { - if (worldIn.isRemote) return EnumActionResult.PASS; - EffortlessBuilding.log(player, "onItemUse"); - - if (player.isSneaking()){ + if (player.isSneaking()) { + if (world.isRemote) return EnumActionResult.SUCCESS; //Open inventory - player.openGui(EffortlessBuilding.instance, EffortlessBuilding.RANDOMIZER_BAG_GUI, worldIn, 0, 0, 0); + player.openGui(EffortlessBuilding.instance, EffortlessBuilding.RANDOMIZER_BAG_GUI, world, 0, 0, 0); } else { //Place block - + return placeRandomBlockFromBag(player, world, pos, hand, facing, hitX, hitY, hitZ); } - return EnumActionResult.PASS; + return EnumActionResult.SUCCESS; + } + + private EnumActionResult placeRandomBlockFromBag(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ){ + //Get bag inventory + ItemStack bag = player.getHeldItem(hand); + if (!bag.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) return EnumActionResult.FAIL; + IItemHandler bagInventory = bag.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null); + + //Find how many stacks are non-empty, and save them in a list + int nonempty = 0; + List nonEmptyStacks = new ArrayList<>(5); + List originalSlots = new ArrayList<>(5); + for (int i = 0; i < bagInventory.getSlots(); i++) { + ItemStack stack = bagInventory.getStackInSlot(i); + if (!stack.isEmpty()) { + nonempty++; + nonEmptyStacks.add(stack); + originalSlots.add(i); + } + } + + if (nonEmptyStacks.size() != originalSlots.size()) throw new Error("NonEmptyStacks and OriginalSlots not same size"); + + if (nonempty == 0) return EnumActionResult.FAIL; + + //Pick random slot + int randomSlot = rand.nextInt(nonempty); + + ItemStack toPlace = nonEmptyStacks.get(randomSlot); + if (toPlace.isEmpty()) return EnumActionResult.FAIL; + + if (toPlace.getItem() instanceof ItemBlock) { + IBlockState existingBlockState = world.getBlockState(pos); + Block existingBlock = existingBlockState.getBlock(); + + if (!existingBlock.isReplaceable(world, pos)) + { + pos = pos.offset(facing); + } + + Block block = Block.getBlockFromItem(toPlace.getItem()); + if (!toPlace.isEmpty() && player.canPlayerEdit(pos, facing, toPlace) && world.mayPlace(block, pos, false, facing, (Entity)null)) { + IBlockState blockState = block.getStateForPlacement(world, pos, facing, hitX, hitY, hitZ, toPlace.getMetadata(), player, hand); + if (world.setBlockState(pos, blockState)){ + SoundType soundType = block.getSoundType(blockState, world, pos, player); + world.playSound(player, pos, soundType.getPlaceSound(), SoundCategory.BLOCKS, (soundType.getVolume() + 1.0F) / 2.0F, soundType.getPitch() * 0.8F); + + if (!player.isCreative()) + bagInventory.extractItem(originalSlots.get(randomSlot), 1, false); + } + + //((ItemBlock) toPlace.getItem()).placeBlockAt(toPlace, player, world, pos, facing, hitX, hitY, hitZ, blockState); + //return ((ItemBlock) toPlace.getItem()).onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ); + } + } + return EnumActionResult.SUCCESS; } @Override - public ActionResult onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) { - if (worldIn.isRemote) return new ActionResult<>(EnumActionResult.PASS, playerIn.getHeldItem(handIn)); - EffortlessBuilding.log(playerIn, "onItemRightClick"); + public ActionResult onItemRightClick(World world, EntityPlayer player, EnumHand hand) { + if (world.isRemote) return new ActionResult<>(EnumActionResult.PASS, player.getHeldItem(hand)); //Open inventory - playerIn.openGui(EffortlessBuilding.instance, EffortlessBuilding.RANDOMIZER_BAG_GUI, worldIn, 0, 0, 0); + player.openGui(EffortlessBuilding.instance, EffortlessBuilding.RANDOMIZER_BAG_GUI, world, 0, 0, 0); - return new ActionResult<>(EnumActionResult.PASS, playerIn.getHeldItem(handIn)); + return new ActionResult<>(EnumActionResult.PASS, player.getHeldItem(hand)); } @Override @@ -62,6 +127,12 @@ public class ItemRandomizerBag extends Item { @Nullable @Override public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) { - return new RandomizerBagCapabilityProvider(); + return new ItemHandlerCapabilityProvider(); + } + + @Override + public void addInformation(ItemStack stack, @Nullable World world, List tooltip, ITooltipFlag flag) { + tooltip.add("Sneak + rightclick to open inventory"); + tooltip.add("Rightclick to place a random block"); } } diff --git a/src/main/java/nl/requios/effortlessbuilding/proxy/ClientProxy.java b/src/main/java/nl/requios/effortlessbuilding/proxy/ClientProxy.java index 64620f6..cacc252 100644 --- a/src/main/java/nl/requios/effortlessbuilding/proxy/ClientProxy.java +++ b/src/main/java/nl/requios/effortlessbuilding/proxy/ClientProxy.java @@ -42,8 +42,8 @@ public class ClientProxy implements IProxy { keyBindings = new KeyBinding[2]; // instantiate the key bindings - keyBindings[0] = new KeyBinding("key.hud.desc", Keyboard.KEY_NUMPAD0, "key.effortlessbuilding.category"); - keyBindings[1] = new KeyBinding("key.replace.desc", Keyboard.KEY_NUMPAD1, "key.effortlessbuilding.category"); + keyBindings[0] = new KeyBinding("key.hud.desc", Keyboard.KEY_ADD, "key.effortlessbuilding.category"); + keyBindings[1] = new KeyBinding("key.replace.desc", Keyboard.KEY_SUBTRACT, "key.effortlessbuilding.category"); // register all the key bindings for (int i = 0; i < keyBindings.length; ++i) { diff --git a/src/main/resources/assets/effortlessbuilding/models/item/randomizer_bag.json b/src/main/resources/assets/effortlessbuilding/models/item/randomizer_bag.json index 5f74c2e..b4593bb 100644 --- a/src/main/resources/assets/effortlessbuilding/models/item/randomizer_bag.json +++ b/src/main/resources/assets/effortlessbuilding/models/item/randomizer_bag.json @@ -1,6 +1,6 @@ { "parent": "item/generated", "textures": { - "layer0": "items/fireball" + "layer0": "effortlessbuilding:items/randomizerbag" } } diff --git a/src/main/resources/assets/effortlessbuilding/recipes/randomizer_bag.json b/src/main/resources/assets/effortlessbuilding/recipes/randomizer_bag.json new file mode 100644 index 0000000..e27597b --- /dev/null +++ b/src/main/resources/assets/effortlessbuilding/recipes/randomizer_bag.json @@ -0,0 +1,20 @@ +{ + "type": "minecraft:crafting_shaped", + "pattern": [ + " # ", + "#O#", + " # " + ], + "key": { + "#": { + "item": "minecraft:leather" + }, + "O": { + "item": "minecraft:planks", + "data": 32767 + } + }, + "result": { + "item": "effortlessbuilding:randomizer_bag" + } +} \ No newline at end of file diff --git a/src/main/resources/assets/effortlessbuilding/textures/gui/container/randomizerbag.png b/src/main/resources/assets/effortlessbuilding/textures/gui/container/randomizerbag.png index 1cf27c55034c3f98296134f13b41676ab2764366..56f4b8940ae501f6c4eacd9f03cca23ca592940b 100644 GIT binary patch literal 1164 zcmeAS@N?(olHy`uVBq!ia0y~yU<5K58911MRQ8&P5C#Tjh5(-sS0EP%%+1Y1Lqof} zyN?|^_W%EXh~OwW8Un*U1d^tF&INjsu_VYZn8D%MjWi(J%G1R$q~g}w+lGA220RXd zyFGXOpLlcSu8?hBr>c{;)o2P?2ng=%e=*^>-KU4Y?ZdvYMC_JOwr0{{WD#&+Kp`GX zzJ7I0)J@d`dn{IkVaPf(>}H#hADV>Mr$vSl#Wi5Z9dLPZn&F?chyz1?-z=_bK1{8_ ziNT4+j*KijuK$x}a#^==VjIIm{`>VV@09T;hX?Ux` z7I;J!Gca%qgD@k*tT_@uLG}_)Usv`=oC3VUsxOk19f3l!nIRD+&iT2ysd*(pE(3#e zQEFmIYKlU6W=V#EyQgnJie4%^0|Uz+PZ!6Kid%258VWKna2zo>uz!hoJ2#u�yL3 zE@5bhR%T?7FaV*110dAE$P7dbY&;+Ykr>HxkU+s6E+az@k|5C21jO$nMa~iXUo&e+=GHyWK?S6ztDnm{r-UW|>|gr~ diff --git a/src/main/resources/assets/effortlessbuilding/textures/items/randomizerbag.png b/src/main/resources/assets/effortlessbuilding/textures/items/randomizerbag.png new file mode 100644 index 0000000000000000000000000000000000000000..5da78c07b863d36bcc09562fdc99aaccfc670004 GIT binary patch literal 358 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Y)RhkE)4%caKYZ?lYt_f1s;*b z3=DinK$vl=HlH+5kiEpy*OmPdrvSew%g3t@-+@A(JzX3_EKV;Cc3gGHKxEEE7mn8R z&u{x~ciOI3#jyS{YZZ_G6U99Zx0|FVDtR2#IPdED-+%HC(e$6QX8NgpZs2!h5D?6d z2%jR)e8!lS;f8$v;q7PrEGBra*q9U>IIYz!y*hAlXX-*m2H$LE_Q@X;HVd1rZM}H7 z|3akW<)}HkK5%K3t^c=xH?97yx~z?JY~a7rfbc0-4=s56>%pd*BGX^Xe)d_kXtLqX zGocP;iN|6pm{Qm6`5<%iV8)^|p?4aC|Czm7s}sj(sQTxc_KqUe)o+}1_H8MDQSk5W zk$)A^zZ{J2u&PfwRF%Cq)* z7FKVCo9lhn{;QdIH*Q6ixYEHDs~X?@D#@BNN8qtX()MowtIU2T7`lJ`H6!5j@7er? zXL9_N`&2HuOknO%zUe8~V3>V#0-uAhb9~aOE$L=g4Or&$T;Mk4+F>L0ZQF(E&vFWS ys<;?7L{~pJ5cN&trmaM9JwMxjru1k34)K?6oLt!1eNqzWeFjfgKbLh*2~7YP9gK7U literal 0 HcmV?d00001 diff --git a/src/main/resources/assets/effortlessbuilding/textures/items/randomizerbag_orig.png b/src/main/resources/assets/effortlessbuilding/textures/items/randomizerbag_orig.png new file mode 100644 index 0000000000000000000000000000000000000000..7e27e6bc3cde648ae02a5fce67715730e3d41483 GIT binary patch literal 1090 zcmV-I1ikx-P)kdg0008`X+uL$P-t&- zZ*ypGa3D!TLm+T+Z)Rz1WdHz3OJiVQp5W->C8-r4y$sIz1(ija=@}&ohL)C=3XUoH zNvR6KmBl5gxy1^edCB=j1^GpZC8;SuwNN`ifY&9zM8Q2VGf%vsL&#-{uD8m~@aYh%$QpOdG_n5?(f|w>SU0@bw zj$~fQ{D#GnrGw=bs~&4B>peD0wrOnN*u&X(b0~3iaeU!SK)R!hF;CW%w5h=m_i(bQQcJlq&Q?xK~6)WUpwr=ohg`;zr_EBq}83B#%kuOG`_i zkSUW@m%Sy|C+{NvM`5dCp^~1`TjjMX1*#^hKh+MZcWH!bDrtVuI-osCCso% z*+bn^*UQA)#>d?^*e}69KcGHvV$jmyT_G1kUxqV9$VFO3MMPJ`%#7U=_aK2SQ8y_p zxglju>dkbH42#Unthw12bJ_Fk^D7EA6}~IhF3BogRraPrzp|uiR}EvWSKZY5hmCqo zHO*&R<=P6{k9JCT6?LEPRqbo+zdO-p((K89r^Zh^JVRq<|E%wG66T(sZ@ysZB9Xxuwwi3)zQbzg{@t#7&h87_|KMQGp`Sj{004R=004l4008;_ z004mL004C`008P>0026e000+nl3&F}00009a7bBm000ie000ie0hKEb8vpG5`v_DN5zQVJECrfD$6jW)`~Uy|07*qo IM6N<$f>HnVyZ`_I literal 0 HcmV?d00001