Solved Getting the clicked-block name

Discussion in 'Plugin Development' started by Shzylo, Jun 14, 2013.

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

    Shzylo

    My plugin is used to allow Sulphur to act as if it was mini-TNT, and if you don't have enough Sulphur for certain blocks ( such as stone, requires 4 sulphur ) it will tell you that you need 4 sulphur to explode it. It works in all, but it doesn't tell me the blocks' name, just data about it:
    Code:
    CraftBlock{chunk=CraftChunk{x=-1z=-9},x=-14,6=55,z=-130,type=STONE,data=0}
    Sulphur.java:
    Code:java
    1. public class Sulphur implements Listener {
    2.  
    3. MobMeat plugin;
    4.  
    5. public Sulphur(MobMeat plugin) {
    6. this.plugin = plugin;
    7. }
    8.  
    9. @EventHandler
    10. public void Event(PlayerInteractEvent event) {
    11. Action act = event.getAction();
    12. Player p = event.getPlayer();
    13. Block b = event.getClickedBlock();
    14.  
    15. if (b != null) {
    16. Material hand = p.getItemInHand().getType();
    17. Material clicked = b.getType();
    18. World world = p.getWorld();
    19.  
    20. boolean enabled = false;
    21. if (plugin.getConfig().getBoolean("sulphur") == true) enabled = true;
    22.  
    23. if (enabled) {
    24. if (p.hasPermission("mobmeat.sulphur")) {
    25. if (hand.equals(Material.SULPHUR)) {
    26. if (act == Action.RIGHT_CLICK_BLOCK) {
    27. if (clicked.equals(Material.STONE)) {
    28. if (p.getInventory().contains(new ItemStack(Material.SULPHUR, 4) {
    29. p.getInventory().removeItem(new ItemStack(Material.SULPHUR, 4);
    30. world.createExplosion(b.getLocation(), 4f);
    31. } else {
    32. p.sendMessage(ChatColor.RED + "Requires 4 Sulphur to blow up " +
    33. ChatColor.AQUA + b);
    34. }
    35. }
    36. }
    37. }
    38. }
    39. }
    40. }
    41. }
     
  2. Offline

    MrTwiggy

    'b' is the Block.java object. So, by default, when it is converted to a string using the .toString() method (which you are using by default), it converts it into the form:

    CraftBlock{chunk=CraftChunk{x=-1z=-9},x=-14,6=55,z=-130,type=STONE,data=0}

    In order to get the 'name' of the block (IE: the material), which in this case, is 'Stone', you would change 'b' to 'clicked.toString()'.
     
    Shzylo likes this.
Thread Status:
Not open for further replies.

Share This Page