Solved Getting crop state from block

Discussion in 'Spigot Plugin Development' started by ThatBenderGuy, Jun 29, 2018.

  1. So right now here is my code
    Code (Java):
        private void checkPlantGrowth(PlayerInteractEvent e)
        {
            Player p = e.getPlayer();
            Block b = e.getClickedBlock();
     
            if(b.getType() == Material.CROPS) {
                // CropState state = (CropState)b.getState(); doesn't work either
                Crops crop = (Crops)b.getState();
                CropState state = crop.getState();
                p.sendMessage(GRPG.gaiaPrefix + "The crop is at the " + getColoredState(state) + " state.");
            }
        }
     
        private String getColoredState(CropState state) {
            if(state == CropState.SEEDED) return ChatColor.RED + state.name() + ChatColor.WHITE;
            if(state == CropState.GERMINATED) return ChatColor.RED + state.name() + ChatColor.WHITE;
            if(state == CropState.VERY_SMALL) return ChatColor.GOLD + state.name() + ChatColor.WHITE;
            if(state == CropState.SMALL) return ChatColor.YELLOW + state.name() + ChatColor.WHITE;
            if(state == CropState.MEDIUM) return ChatColor.YELLOW + state.name() +  ChatColor.WHITE;
            if(state == CropState.TALL) return ChatColor.YELLOW + state.name() +  ChatColor.WHITE;
            if(state == CropState.VERY_TALL) return ChatColor.YELLOW + state.name() + ChatColor.WHITE;
            if(state == CropState.RIPE) return ChatColor.GREEN + state.name() + ChatColor.WHITE;
     
            return "";
        }
    But I keep getting a
    error. I don't know what else from the Block class I can use to cast to Crops. I know I'm trying to cast a blockState to a material#Crops but I saw this code elsewhere and thought it would work. Any insight how to resolve this issue?

    I have already tried casting a Block#getState() to a CropState and that didn't work either.

    ---------- EDIT ----------
    I'm such an idiot, I fixed it by casting b#getState()#getData() to Crops and it worked.
     
    #1 ThatBenderGuy, Jun 29, 2018
    Last edited: Jun 29, 2018
    • Like Like x 1
  2. Can you post the code you used to solve this? It's frustrating to find so many posts marked "solved" but never actually give the solution. Any info about how you solved this materialData issue would be greatly appreciated.
     
  3. He posted the solution...

    Code (Text):
    block#getState()#getData()
    This returns a MaterialData, which Crops extends.

    Use your fundamental knowledge of Java basics to fill in the rest. ;)
     
    • Like Like x 1
  4. @BillyGalbreath Thanks for the follow-up. I was able to figure it out after reading more about examples and seeing the combination of chaining getState().getData() vs just getData() or getRawData().

    Cheers.