Help with PlayerMoveEvent

Discussion in 'Plugin Development' started by shadow5353, Apr 1, 2014.

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

    shadow5353

    Hello developers.

    I am working on a plugin that will explode when some one walks on a Pressure Plate with a specifyed name, but the event will not let me do that.

    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
                if (e.getTo().getBlock().getRelative(BlockFace.DOWN).getType() == Material.STONE_PLATE) {
                    ItemStack LandMine = new ItemStack (Material.STONE_PLATE);
                    ItemMeta LandMinemeta = LandMine.getItemMeta();
                    LandMinemeta.setDisplayName("LandMine");
                    if(e.getTo().getBlock().getState().equals(LandMinemeta)){
                        e.getTo().getBlock().getLocation().getWorld().createExplosion(e.getTo().getBlock().getLocation(), 4.0F);
                        }
                    }
                }
    What is wrong with this code?
     
  2. Offline

    coasterman10

    There could be an endless list of things that could have gone wrong. Any stack traces, compile errors? Or does it run fine but not yield any results?
     
  3. Offline

    shadow5353

    Thats the problem, I don't get any errors, but when I walk on the Pressure Plate nothing happens. coasterman10
     
  4. Offline

    coasterman10

    I believe that the player's position is stored as the position of their feet, so in effect you are getting the block they are standing on top of, whereas they would be technically standing in the pressure plate.

    You can also use PlayerInteractEvent when the action is Action.PHYSICAL, humorously described in the javadoc as "Ass-pressure", which is triggered whenever a pressure plate is pressed.
     
  5. The players location is the foot-position, however it is the block the player is in, as stated above. In case of a pressure plate it would be the pressure plate itself, because it is not covering the whole block, on "normal" ground it usually would just be the air above the ground.

    Do note that a players feet can be on 4 blocks at the same time! This is due to the bounding box, a player has roughly 0.3 to each side of x and z axis, and it could be that you would have air at a location and air below as well, while the player is still standing just on the edge of solid blocks.


    Using the PlayerInteractEvent should be more accurate, you might also check for cancelled ones, e.g. if you are using WorldGuard with use flag on deny for some reason.
     
  6. Offline

    shadow5353

    So how should I make the event?
     
  7. Offline

    SuppaTim

    This may be a stupid question but lots of people still forget it: Have you registered your events?
     
  8. Offline

    shadow5353

    I have registered it. Here is the full code

    Code:
    package me.shadow5353.landmine;
     
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class LandMine extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        Plugin plugin = this;
       
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            //MCStats
                try {
                    Metrics metrics = new Metrics(this);
                    metrics.start();
                    } catch (IOException e) {
                        // Failed to submit the stats :-(
                    }
           
        }
       
        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
                if (e.getTo().getBlock().getRelative(BlockFace.DOWN).getType() == Material.STONE_PLATE) {
                    ItemStack LandMine = new ItemStack (Material.STONE_PLATE);
                    ItemMeta LandMinemeta = LandMine.getItemMeta();
                    LandMinemeta.setDisplayName("LandMine");
                    if(e.getTo().getBlock().getState().equals(LandMinemeta)){
                        e.getTo().getBlock().getLocation().getWorld().createExplosion(e.getTo().getBlock().getLocation(), 4.0F);
                        }
                    }
                }
       
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            final Player p = (Player) sender;
            PlayerInventory pi = p.getInventory();
         
            if (cmd.getName().equalsIgnoreCase("landmine")) {
                    if (!sender.hasPermission("landmine.use")) {
                            sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                            return true;
                    }
                    if (args.length == 0) {
                            sender.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.GOLD + "/landmine give" + ChatColor.BLACK + " : " + ChatColor.YELLOW + "This will give you 3 landmines");
                            return true;
                    }
                    else if (args.length == 1) {
                            if (args[0].equalsIgnoreCase("give")) {
                                    if (!sender.hasPermission("landmine.use")) {
                                            sender.sendMessage(ChatColor.DARK_RED + "You do not have permission!");
                                            return true;
                                    }
                                    ItemStack LandMine = new ItemStack (Material.STONE_PLATE, 3);
                                    ItemMeta LandMinemeta = LandMine.getItemMeta();
                                    LandMinemeta.setDisplayName("LandMine");
                                    LandMine.setItemMeta(LandMinemeta);
                                    pi.addItem(LandMine);
                                    p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.GREEN + "Gave you 3 landmines");
                            }
                    }
                    return true;
            }
            return true;
    }
        }
    
     
  9. Offline

    MooshViolet

    shadow5353 don't use your playermoveevent, use a playerinteractevent and declare when they interact with a pressure plate, do stuff.
     
  10. Offline

    shadow5353

    MooshViolet So can it be like this?

    Code:
        @EventHandler
        public void playerinteract(PlayerInteractEvent  e) {
            if(e.getAction().name().equalsIgnoreCase(ChatColor.GREEN + "name")){
                //Do something
                }
            }
     
  11. This:
    You check for the PHYSICAL action, then get the (clicked, though not really clicked) block of the even, check that for being a pressure plate.
     
  12. Offline

    Maurdekye

    shadow5353 asofold means to do this;
    Code:java
    1. @EventHandler
    2. public void Interact(PlayerInteractEvent e) {
    3. if (e.getAction() == Action.PHYSICAL && e.getClickedBlock().getType() == Material.STONE_PLATE) {
    4. // Do stuff
    5. }
    6. }
     
  13. Offline

    shadow5353

    asofold Maurdekye Now I have done that, how can I check for if the material have a specify name?
     
  14. Offline

    Maurdekye

    shadow5353 What? I don't know what you mean. Check if it's a pressure plate?
     
  15. Offline

    shadow5353

    Maurdekye No check if the pressure plate have the name LandMine
     
  16. Offline

    Maurdekye

    shadow5353 You can't assign custom names to blocks. Placing a block item with a custom name destroys it, because the blocks have no way of keeping that information. You'll have to store that info another way, like in an ArrayList.
     
  17. Offline

    shadow5353

    Maurdekye Oh, I did not know that. I will try and make an ArrayList with another event
     
  18. I think you can add metadata to blocks, however it might not get saved to disk. So you could as well maintain a HashMap with blocks to keep track of landmines.
     
Thread Status:
Not open for further replies.

Share This Page