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.

This commit is contained in:
Christian Knaapen
2018-09-17 01:17:51 +02:00
parent efa47851be
commit bf258580d4
17 changed files with 233 additions and 127 deletions

View File

@@ -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) {
//
// }
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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<NBTTagCompound> {
IItemHandler itemHandler = new ItemStackHandler(5);
public class ItemHandlerCapabilityProvider implements ICapabilitySerializable<NBTTagCompound> {
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);
}
}

View File

@@ -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:

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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<ItemStack> nonEmptyStacks = new ArrayList<>(5);
List<Integer> 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<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
if (worldIn.isRemote) return new ActionResult<>(EnumActionResult.PASS, playerIn.getHeldItem(handIn));
EffortlessBuilding.log(playerIn, "onItemRightClick");
public ActionResult<ItemStack> 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<String> tooltip, ITooltipFlag flag) {
tooltip.add("Sneak + rightclick to open inventory");
tooltip.add("Rightclick to place a random block");
}
}

View File

@@ -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) {

View File

@@ -1,6 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "items/fireball"
"layer0": "effortlessbuilding:items/randomizerbag"
}
}

View File

@@ -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"
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB