Solved Set Crop Size (1.11.2)

Discussion in 'Plugin Development' started by ScrouthTV, Mar 21, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    ScrouthTV

    What I want to do for my plugin is, when the player right clicks a crop, this crop is replaced with one at size 0 and the player gets all the items the crop dropped, except the one that is needed for replanting.

    @EventHandler
    public void onInteract(PlayerInteractEvent ev) {
    if(ev.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    if(ev.getClickedBlock().getType().equals(Material.CROPS)) {
    Block bl = ev.getClickedBlock();
    bl.
    }
    }
    }​

    How do I now set the crop size? As it says in the API, Crops is not a subclass of Block, but of MetaData...

    As far as I can see, there is no way to set the MetaData of a block.
     
  2. Offline

    Drkmaster83

    Likely a blockstate that modifies the materialdata... sadly I'm pretty sure the size is determined by the damage value, which is represented by a byte, and is deprecated to use, but the only way to do so. You can also get the blockstate, cast its getData() (MaterialData) to Crops (assuming the block is crops) and then call Crops#setCropState()
     
  3. Offline

    ScrouthTV

    @Drkmaster83 Just tested this out
    So this would be:
    Code:
    @EventHandler
        public void onInteract(PlayerInteractEvent ev) {
            if(!ev.getAction().equals(Action.RIGHT_CLICK_BLOCK))
                return;
            if(!ev.getClickedBlock().getType().equals(Material.CROPS))
                return;
            Block bl = ev.getClickedBlock();
            Crops c = (Crops) bl.getState();
            if(c.getState().equals(CropState.RIPE))
                 c.setState(CropState.GERMINATED);
        }
    If I run it, there is this error:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_11_R1.block.CraftBlockState cannot be cast to org.bukkit.material.Crops
    EDIT: I left out a NOT() in the 2nd if clause
     
    Last edited: Mar 22, 2017
  4. Offline

    Zombie_Striker

    \

    Compare enums using ==.

    Main problem: You are casting BlockState to a Crops without checking if they are crops. Make sure it is an instance of Crops before casting.
     
  5. Offline

    Side8StarLite

    @ScrouthTV
    1 - I think you're missing a NOT operator in the if statement checking if they are crops
    2 - The reason why you can't cast CraftBlockState to Crops is because is because you have to get the DATA of the state of the block using getState().getData()
    3 - You have to set the block with the data of the crop data after you change the data of the crop.
    4 - What @Zombie_Striker said : )
     
    ScrouthTV likes this.
  6. Offline

    ScrouthTV

    @Zombie_Striker
    Why? Both are technically correct, I just decided for myself to use equals().
    That is not the problem. I just left the NOT out, because I changed some things up from my code, because you can't put tabs in here. In my plugin I'm checking if they are crops, and only if they are crops, I'm casting their BlockState.


    @Side8StarLite
    1. Yes, I left it out at editing the code for here. In my plugin it is there.
    I'll definetily take a look at 2. and 3.
     
    Last edited: Mar 22, 2017
  7. Offline

    Zombie_Striker

    The issue is that A) It is convention to use == for primitives and enums, and B) == compares only the memory space, while .equals compares all the values of the object. Since Enums are only single instances, == will be a lot faster and efficient compared to .equals.
     
    Side8StarLite and ScrouthTV like this.
  8. Offline

    ScrouthTV

    @Side8StarLite So I rewrote my code with your tips:
    Code:
        @EventHandler
        public void onInteract(PlayerInteractEvent ev) {
            if(ev.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                if(ev.getClickedBlock().getType().equals(Material.CROPS)) {
                    PlayerInventory inv = ev.getPlayer().getInventory();
                    Block bl = ev.getClickedBlock();
                    Crops c = (Crops) bl.getState().getData();
                    if(c.getState().equals(CropState.RIPE)) {
                        Collection<ItemStack> drps = bl.getDrops();
                        for(ItemStack itm : drps) {
                            if(itm.getType().equals(Material.SEEDS))
                                itm.setAmount(itm.getAmount() - 1);
                            inv.addItem(itm);
                        }
                    }
                    c.setState(CropState.GERMINATED);
                    bl.getState().setData(c);
                }
            }
    I don't have any more errors coming when clicking on ripe seeds. But there isn't anything happening. Do I have to save something to the block again (the only thing I did was "bl.getState().setData(c)")?

    @Zombie_Striker If Bukkit didn't mess up the equals method of their enums, they should work like the ==. ;)
     
    Last edited: Mar 22, 2017
  9. Offline

    Side8StarLite

    @ScrouthTV
    Close - for block bl you don't have to call "getState()". Just call setData() directly, and set it with Crops c's data instead of just Crops c.
    So that would make it like this:
    Code:
    bl.setData(c.getData());
    I noticed the strikethroughs in Eclipse indicating setData() and getData() are deprecated, but for now I couldn't find a better solution after combing through all the forums. If you only worry about getting it to work, I'd say this would be a bottom line. If there's a better way of doing it, let me know....
     
    ScrouthTV likes this.
  10. Offline

    Drkmaster83

    @Side8StarLite Pretty sure it was an attempt at Bukkit to let us know that IDs (which run the backend for those states for now) will be removed in the near future. For now, as far as the API is concerned, I'm pretty sure there's no other way.

    Also, @ScrouthTV if it still doesn't work, try BlockState#update() for bl.
     
  11. Offline

    Side8StarLite

Thread Status:
Not open for further replies.

Share This Page