Jump to content

[1.10] [SOLVED] Accessing stone variants


swordkorn

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • What MC version? What's the IP? Are any mods needed to be able to join?
    • Thank you for your answer ! Unfortunatly i have the same problem when i use setPos() public static int movingfunction(CommandContext<CommandSourceStack> context){ CommandSourceStack source = context.getSource(); if (!(source.getEntity() instanceof ServerPlayer)) { return 0; } ServerPlayer player = (ServerPlayer ) source.getEntity(); double moveSpeed = 0.5; for (int i =0; i<10000;i++) { LOGGER.info("running for the {} time", i); double x = player.getX() + player.getViewVector(1.0f).x * moveSpeed; double y = player.getY(); double z = player.getZ() + player.getViewVector(1.0f).z * moveSpeed ; Vec3 movementVec = new Vec3(x, y, z); LOGGER.info("x ={} y ={} z ={}", x, y, z); player.setPos( movementVec); } return 1; } With the logs i can see that x and z are increasing but once again my player is not moving. is there a function to use to sync the server and the client ? I also tried to use LocalPlayer instead of ServerPlayer but my code would stop when i got the object. Also i will change a bit the main topic but is there a way to similate key press ? i found KeyBinding.setKeyBindState on others post but it look like there is no more KeyBinding in 1.20   I found this code : KeyMapping.click(Minecraft.getInstance().options.keyUp.getKey()); But it doesn't seems to work   And i found this one : Minecraft.getInstance().options.keyUp.setDown(true); wich works but doesn't exactly do what i want , it doesn't release the key so for exemple i can't make him run. Minecraft.getInstance().options.keyUp.setDown(true); Minecraft.getInstance().options.keyUp.setDown(false); Minecraft.getInstance().options.keyUp.setDown(true); doesn't make him run
    • Add crash-reports with sites like https://paste.ee/ Maybe an issue with blur, essentials or cumulus_menus
    • Add the crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here  
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.