swordkorn Posted October 1, 2016 Posted October 1, 2016 OK so I'm either being incredibly thick or just plain blind but I can't for the life of me figure out how to call the stone variants when checking the item in my player's hand correctly. getStateFromMeta is deprecated (and only returned the wrong one anyway so that's useless...) so I was wondering if anyone can help me out? Quote
Koward Posted October 1, 2016 Posted October 1, 2016 Hi ! You compare the blockstate with Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.STONE). Here I put EnumType.STONE, other variants work the same way. Quote
swordkorn Posted October 1, 2016 Author Posted October 1, 2016 Thanks for the detailed reply Koward but I have already tried that for several hours now. Here's a snippet of my code: Item item = heldItem.getItem(); if (item == Item.getItemFromBlock(Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE))) The error message states it needs a block and this code returns a BlockState which is incompatible. You see my issue? Quote
Koward Posted October 1, 2016 Posted October 1, 2016 I'm sorry, I read your post too fast. Use EntityPlayer : public ItemStack getHeldItemMainhand() (or Offhand if you want). You get an ItemStack, which has a metadata value. Use ItemStack : public int getMetadata(), and compare it with the metadata of one of the BlockStone.EnumType you want (getMetadata() too). This way, no deprecation at all, you're as ready as possible for the future. Quote
TheGreyGhost Posted October 2, 2016 Posted October 2, 2016 Hi @SwordKorn you can do it the way you were trying, it's just a bit more involved. ItemStack heldItem = entityPlayer.getHeldItemMainhand(); IBlockState iBlockStateAndesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE); Item itemStone = Item.getItemFromBlock(Blocks.STONE); ItemStack itemStackAndesite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateAndesite)); // see Block.getItem() boolean heldItemIsAndesite = ItemStack.areItemsEqual(heldItem, itemStackAndesite); I think Koward's suggestion is probably clearer. -TGG Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 Both excellent suggestions and I tried them both. Sad to report it doesn't work though so I'll expand on my code for reference: @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { if (heldItem == null) { return false; }else{ if (!world.isRemote) { if (world.getBlockState(pos) == BlockReg.tableBlock0.getDefaultState()) { Item item = heldItem.getItem(); if (item == Item.getItemFromBlock(Blocks.COBBLESTONE)) { world.setBlockState(pos, BlockReg.tableBlock1.getDefaultState()); } if (item == Item.getItemFromBlock(Blocks.STONE)) { world.setBlockState(pos, BlockReg.tableBlock2.getDefaultState()); System.out.println("Boolean is FALSE" + itemIsAndesite + itemIsGranite + itemIsDiorite); } if (item == Item.getItemFromBlock(Blocks.STONEBRICK)) { world.setBlockState(pos, BlockReg.tableBlock3.getDefaultState()); } /*if (itemIsAndesite) { heldItem = andesite; world.setBlockState(pos, BlockReg.tableBlock4.getDefaultState()); System.out.println(heldItem); } if (itemIsDiorite) { world.setBlockState(pos, BlockReg.tableBlock5.getDefaultState()); System.out.println("Boolean is TRUE"); } if (itemIsGranite) { world.setBlockState(pos, BlockReg.tableBlock6.getDefaultState()); System.out.println("Boolean is TRUE"); }*/ } } return true; } } Quote
Animefan8888 Posted October 2, 2016 Posted October 2, 2016 You are not comparing metadata from the stacks so what you are doing is hey is this a ston block which will be true for diorite andesite granite and smooth stone. Quote VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 Well no, I'm not anymore. I took TheGreyGhost's code advice and implemented that to no avail so I removed that block of code. Sufficed to say; when I was using that code the boolean states never switched from false defaulting to the main STONE conditional. Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 Oh sorry diesie... I will. Let me rewrite it and I'll modify this post when I've reimplemented everything EDIT @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { if (heldItem == null) { return false; }else{ if (!world.isRemote) { if (world.getBlockState(pos) == BlockReg.tableBlock0.getDefaultState()) { Item item = heldItem.getItem(); heldItem = player.getHeldItemMainhand(); IBlockState iBlockStateAndesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE); IBlockState iBlockStateGranite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE); IBlockState iBlockStateDiorite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.DIORITE); Item itemStone = Item.getItemFromBlock(Blocks.STONE); ItemStack andesite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateAndesite)); ItemStack granite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateGranite)); ItemStack diorite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateDiorite)); boolean itemIsAndesite = ItemStack.areItemsEqual(heldItem, andesite); boolean itemIsGranite = ItemStack.areItemsEqual(heldItem, granite); boolean itemIsDiorite = ItemStack.areItemsEqual(heldItem, diorite); if (item == Item.getItemFromBlock(Blocks.COBBLESTONE)) { world.setBlockState(pos, BlockReg.tableBlock1.getDefaultState()); } if (item == Item.getItemFromBlock(Blocks.STONE)) { world.setBlockState(pos, BlockReg.tableBlock2.getDefaultState()); System.out.println(itemIsAndesite + " " + itemIsGranite + " " + itemIsDiorite); } if (item == Item.getItemFromBlock(Blocks.STONEBRICK)) { world.setBlockState(pos, BlockReg.tableBlock3.getDefaultState()); } if (itemIsAndesite) { world.setBlockState(pos, BlockReg.tableBlock4.getDefaultState()); System.out.println("Boolean is TRUE"); } if (itemIsDiorite) { world.setBlockState(pos, BlockReg.tableBlock5.getDefaultState()); System.out.println("Boolean is TRUE"); } if (itemIsGranite) { world.setBlockState(pos, BlockReg.tableBlock6.getDefaultState()); System.out.println("Boolean is TRUE"); } } } return true; } } Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 So you're saying I need to just add regular Stone to my list and call that instead? Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 Sorry not a physical coded list, just my list of boolean check states is what I meant Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 And I apologise for the messy over use of conditionals. Just want to get it working before I try clean up with a case and select Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 I just tried that and it came up with an interesting behaviour! All the other block types didn't work at all and normal Stone called them all so that was fun! Trying to work around that as I speak. Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 if (!world.isRemote) { if (world.getBlockState(pos) == BlockReg.tableBlock0.getDefaultState()) { Item item = heldItem.getItem(); IBlockState iBlockStateStone = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.STONE); IBlockState iBlockStateAndesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE); IBlockState iBlockStateGranite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE); IBlockState iBlockStateDiorite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.DIORITE); Item itemStone = Item.getItemFromBlock(Blocks.STONE); ItemStack stone = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateStone)); ItemStack andesite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateAndesite)); ItemStack granite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateGranite)); ItemStack diorite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateDiorite)); boolean itemIsStone = ItemStack.areItemsEqual(heldItem, stone); boolean itemIsAndesite = ItemStack.areItemsEqual(heldItem, andesite); boolean itemIsGranite = ItemStack.areItemsEqual(heldItem, granite); boolean itemIsDiorite = ItemStack.areItemsEqual(heldItem, diorite); if (item == Item.getItemFromBlock(Blocks.COBBLESTONE)) { world.setBlockState(pos, BlockReg.tableBlock1.getDefaultState()); } if (itemIsStone) { world.setBlockState(pos, BlockReg.tableBlock2.getDefaultState()); } if (itemIsAndesite) { world.setBlockState(pos, BlockReg.tableBlock4.getDefaultState()); System.out.println("Boolean is TRUE"); } if (itemIsGranite) { world.setBlockState(pos, BlockReg.tableBlock6.getDefaultState()); System.out.println("Boolean is TRUE"); } if (itemIsDiorite) { world.setBlockState(pos, BlockReg.tableBlock5.getDefaultState()); System.out.println("Boolean is TRUE"); } if (item == Item.getItemFromBlock(Blocks.STONEBRICK)) { world.setBlockState(pos, BlockReg.tableBlock3.getDefaultState()); } } } return true; } Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 It doesn't. This way causes the bug I described above. Using this method, only regular stone will activate ALL the booleans, and no other variant accesses their relevant boolean firing them all as false. It's a weird one for sure! Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 Well actually... now that I think about it... Every ItemStack variant calls on itemStone which only checks for the default state of Stone in the player's hand. Therefore calling on itemStone may make them only actve when the default variant is located. In which case, I'm back to square one more or less. Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 Well I have deduced that: Item itemStone = Item.getItemFromBlock(Blocks.STONE); May be the reason only the default state activates all booleans. I came to this conclusion when I looked at: ItemStack stone = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateStone)); ItemStack andesite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateAndesite)); ItemStack granite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateGranite)); ItemStack diorite = new ItemStack(itemStone, Blocks.STONE.damageDropped(iBlockStateDiorite)); As you can see, every instance of the ItemStack calls on the default variant and as my IDE so kindly pointed out; "Argument 'itemStone' might be null" Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 So... OH I get you! So the metadata is returning the stack size! That makes perfect sense now I think about it! Good catch Diesie! Hmm... so now it comes down to a way to simply ignore that value or set it to an integer value that just fills in a value based on reading the stack size in the slot. I'll do some more messing around and let you know what I come up with. Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 Yes You just told me what I just said... I was passing the metadata value in as the stack size Quote
swordkorn Posted October 2, 2016 Author Posted October 2, 2016 Well that was incredibly simple once you pointed that out diesie! Here's my code for people wanting to know how I made this work and I'm marking this as solved! Thanks very much everyone for all your help! @Override public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) { if (heldItem == null) { return false; }else{ if (!world.isRemote) { if (!player.capabilities.isCreativeMode) { if (world.getBlockState(pos) == BlockReg.tableBlock0.getDefaultState()) { Item item = heldItem.getItem(); IBlockState iBlockStateStone = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.STONE); IBlockState iBlockStateAndesite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.ANDESITE); IBlockState iBlockStateGranite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.GRANITE); IBlockState iBlockStateDiorite = Blocks.STONE.getDefaultState().withProperty(BlockStone.VARIANT, BlockStone.EnumType.DIORITE); Item itemStone = Item.getItemFromBlock(Blocks.STONE); ItemStack stone = new ItemStack(itemStone, 1, Blocks.STONE.damageDropped(iBlockStateStone)); ItemStack andesite = new ItemStack(itemStone, 1, Blocks.STONE.damageDropped(iBlockStateAndesite)); ItemStack granite = new ItemStack(itemStone, 1, Blocks.STONE.damageDropped(iBlockStateGranite)); ItemStack diorite = new ItemStack(itemStone, 1, Blocks.STONE.damageDropped(iBlockStateDiorite)); boolean itemIsStone = ItemStack.areItemsEqual(heldItem, stone); boolean itemIsAndesite = ItemStack.areItemsEqual(heldItem, andesite); boolean itemIsGranite = ItemStack.areItemsEqual(heldItem, granite); boolean itemIsDiorite = ItemStack.areItemsEqual(heldItem, diorite); if (item == Item.getItemFromBlock(Blocks.COBBLESTONE)) { world.setBlockState(pos, BlockReg.tableBlock1.getDefaultState()); heldItem.stackSize -= 1; } if (itemIsStone) { world.setBlockState(pos, BlockReg.tableBlock2.getDefaultState()); heldItem.stackSize -= 1; } if (itemIsAndesite) { world.setBlockState(pos, BlockReg.tableBlock4.getDefaultState()); heldItem.stackSize -= 1; System.out.println("Boolean is TRUE"); } if (itemIsGranite) { world.setBlockState(pos, BlockReg.tableBlock6.getDefaultState()); heldItem.stackSize -= 1; System.out.println("Boolean is TRUE"); } if (itemIsDiorite) { world.setBlockState(pos, BlockReg.tableBlock5.getDefaultState()); heldItem.stackSize -= 1; System.out.println("Boolean is TRUE"); } if (item == Item.getItemFromBlock(Blocks.STONEBRICK)) { world.setBlockState(pos, BlockReg.tableBlock3.getDefaultState()); heldItem.stackSize -= 1; } } } } return true; } } Quote
jeffryfisher Posted October 3, 2016 Posted October 3, 2016 Now you can move your 4 IBlockState's to the class level, adding static and final attributes (It's really not necessary to construct them every time that method is called). Likewise the item-stacks. Quote The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.
swordkorn Posted October 3, 2016 Author Posted October 3, 2016 Yeah thanks Jerry! I intended to get it working first THEN worry about tidying up after so it's on my list of to-dos! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.