Blocks mysteriously not being set

Discussion in 'Plugin Development' started by Codex Arcanum, Mar 14, 2013.

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

    Codex Arcanum

    I am trying to determine why the following code does not work properly. Does it have something to do with java passing by copy?

    Code:
    package me.sleightofmind.wards.listeners;
     
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class CorePlaceListener implements Listener{
     
        @EventHandler(priority = EventPriority.LOWEST)
        public void onCorePlace(BlockPlaceEvent e){
            ItemStack item = e.getItemInHand();
            if (item == null) return;
            String name = item.getItemMeta().getDisplayName();
            if (name == null) return;
       
            Player player = e.getPlayer();
            if(item.getType() == Material.ENCHANTMENT_TABLE && name.equals("Core")){
                item.setAmount(item.getAmount() - 1);
                player.setItemInHand(item);
                buildCore(player, e.getBlock());
                e.setCancelled(true);
            }
        }
     
        public void buildCore(Player p, Block block){
            block.setType(Material.MELON_BLOCK);
        }
     
    }
    Oh, and yes, I am registering the event properly. This code is intended to make it so that when you place a Enchanting Table with the name "Core" (which is created by another part of the plugin), then it will turn into a melon block.

    Edit: Now it works, but only if I remove the e.setCancelled(true). Can someone explain why?
     
  2. Offline

    TheUpdater

    what do you want it to do lol cant understand the code?! if enchanttable is named core?
     
  3. Offline

    Codex Arcanum

    My bad. This code is intended to make it so that when you place a Enchanting Table with the name "Core" (which is created by another part of the plugin), then it will turn into a melon block.
     
  4. Offline

    TheUpdater

    wtf why do you want an enchanttabel to be melon ?! u have good reasons for your plugin send me code whit other so i can see if any wrong
     
  5. Offline

    Tirelessly

    Lets think it through:

    Place block
    Event fires
    Block set to melon
    Event cancelled
    Bukkit realizes event is cancelled and sets the block back to air (thinking it was the block you placed)

    If I had to guess, that's why.
     
  6. Offline

    Codex Arcanum

    Yeah, I kind of thought something along those lines myself. Doesn't make a great deal of sense though.
     
  7. Actually it's more like you're changing the temporary block (e.getBlock()) and in the minecraft code the event gets cancelled and the block is not applied to the world at all.
    You can actually test if this is the case and probably fix it with: e.getBlock().getLocation().getBlock(), that will give you the actual block at that location, which should be air when you check it.
     
Thread Status:
Not open for further replies.

Share This Page