[Solved] Getting Wool Color

Discussion in 'Plugin Development' started by Benedikt Wüller, Mar 16, 2012.

Thread Status:
Not open for further replies.
  1. Hey there,
    I have a problem on getting specified woolcolors.

    This is my code:
    Code:
    final Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    block.getLocation().setY(block.getY() - 1);
     
    if(block.getType() == Material.WOOL){
        try{
            Wool wool = (Wool) block;
            if(wool.getColor() == DyeColor.WHITE){
                //code here
            } else if(wool.getColor() == DyeColor.YELLOW){
                //code here
            }
        } catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    The errormessage is:
    Code:
    org.bukkit.craftbukkit.block.CraftBlock cannot be cast to org.bukkit.material.Wool
    but how can i fix this. need help :/
    Thanks!
     
  2. Offline

    Sir Savary

    Well what line is it on?
     
  3. Is't here:
    Code:
    Wool wool = (Wool) block;
    //Edit: This is block:
    Code:
    final Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    block.getLocation().setY(block.getY() - 1);
     
  4. Offline

    Sir Savary

    Well, if I am correct, you've imported the wrong Block class. Please post the entire file. And, please wrap it in Syntax=Java tags.
     
  5. Well, thats it:
    Code:java
    1. package me.bw2801.plugins.myplugin;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.DyeColor;
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.block.BlockFace;
    9. import org.bukkit.block.BlockState;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerMoveEvent;
    14. import org.bukkit.material.Wool;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.bukkit.potion.PotionEffect;
    17. import org.bukkit.potion.PotionEffectType;
    18.  
    19. public class MyPlugin extends JavaPlugin implements Listener {
    20.  
    21. public void onDisable() {
    22. System.out.println(this + " disabled!");
    23. }
    24.  
    25. public void onEnable() {
    26. System.out.println(this + " enabled!");
    27. getServer().getPluginManager().registerEvents(this, this);
    28. }
    29.  
    30. @EventHandler
    31. public void onPlayerMove(PlayerMoveEvent event){
    32. final Player player = event.getPlayer();
    33. final Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    34. block.getLocation().setY(block.getY() - 1);
    35.  
    36. this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    37. public void run() {
    38. if(block.getType() == Material.WOOL){
    39. try{
    40. Wool wool = (Wool) block;
    41. if(wool.getColor() == DyeColor.WHITE){
    42. //code here
    43. } else if(wool.getColor() == DyeColor.YELLOW){
    44. //code here
    45. }
    46. } catch(Exception e){
    47. System.out.println(e.getMessage());
    48. }
    49. } else {
    50. //DEBUGGING: System.out.println("NO WOOL: " + ChatColor.GOLD + player.getLocation().getBlock().getState().getBlock().toString());
    51. }
    52. }
    53. }, 5);
    54. }
    55. }
     
  6. Offline

    Sir Savary

    The following code works fine on my end, without error. If you're using Eclipse, try saving the file.

    Code:java
    1.  
    2. import org.bukkit.DyeColor;
    3. import org.bukkit.Material;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.block.BlockFace;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerMoveEvent;
    10. import org.bukkit.material.Wool;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class MyPlugin extends JavaPlugin implements Listener {
    14.  
    15. public void onDisable() {
    16. System.out.println(this + " disabled!");
    17. }
    18.  
    19. public void onEnable() {
    20. System.out.println(this + " enabled!");
    21. getServer().getPluginManager().registerEvents(this, this);
    22. }
    23.  
    24. @EventHandler
    25. public void onPlayerMove(PlayerMoveEvent event){
    26. final Player player = event.getPlayer();
    27. final Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
    28. block.getLocation().setY(block.getY() - 1);
    29.  
    30. this.getServer().getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
    31. public void run() {
    32. if(block.getType() == Material.WOOL){
    33. try{
    34. Wool wool = (Wool) block;
    35. if(wool.getColor() == DyeColor.WHITE){
    36. //code here
    37. } else if(wool.getColor() == DyeColor.YELLOW){
    38. //code here
    39. }
    40. } catch(Exception e){
    41. System.out.println(e.getMessage());
    42. }
    43. } else {
    44. //DEBUGGING: System.out.println("NO WOOL: " + ChatColor.GOLD + player.getLocation().getBlock().getState().getBlock().toString());
    45. }
    46. }
    47. }, 5);
    48. }
    49. }
    50.  
     
  7. This is the same code, isn't it?
     
  8. Offline

    Sir Savary

    I removed some imports but other than that it is the exact same. What IDE are you using?
     
  9. I'm using NetBeans 7.0.1
     
  10. Offline

    Sir Savary

    Interesting, so the error is still there then? Did you try saving the file, closing and then opening it?
     
  11. Offline

    BloodShura

    Change:

    Wool wool = (Wool)block;

    To:

    Wool wool = (Wool)block.getState();
     
  12. now the error is:
    org.bukkit.craftbukkit.block.CraftBlockState cannot be cast to org.bukkit.material.Wool
     
  13. Offline

    Senchy

    actual bukkit build in the plugin and server?
     
  14. Offline

    Karl Marx

    Try this:

    Code:java
    1.  
    2. if(block.getType() == Material.WOOL){
    3. Wool wool = new Wool(block.getType(), block.getData());
    4. if(wool.getColor() == DyeColor.WHITE){
    5. //code here
    6. } else if(wool.getColor() == DyeColor.YELLOW){
    7. //code here
    8. }
    9. }
    10.  


    And if you need to change the wool color:

    Code:java
    1.  
    2. wool.setColor(myDyeColor);
    3. block.setData(wool.getData());
    4.  
     
  15. thank you very much. Now it works.
     
    PogoStick29 likes this.
Thread Status:
Not open for further replies.

Share This Page