Command Help

Discussion in 'Plugin Development' started by DaChosenWon, Oct 28, 2014.

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

    DaChosenWon

    I'm new to bukkit development and I need some help.
    How would I add the "onPlayerFall" method into the command "nofall".

    Code:
    package me.DaChosenWon.NoFall;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class NoFall extends JavaPlugin implements Listener{
       
        @Override
        public void onEnable() {
            System.out.println("Enabled");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public void onDisable() {
            System.out.println("Disabled");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("nofall")) {
                if(player.hasPermission("nofall.nofall")) {
                    player.sendMessage(ChatColor.GREEN + "No fall damage enabled!");
                    //How would I put the "onPlayerFall" method here. Or how would I put it down there.
                }
            }
            return false;
        }
     
       
        @EventHandler
        public void onPlayerFall(EntityDamageEvent event) {
            Player player = (Player) event.getEntity();
            if (player.hasPermission("nofall.nofall")) {
                if (event.getCause().equals(DamageCause.FALL)) {
                    event.setCancelled(true);
                }
            }
        }
     
    }
     
  2. Offline

    ResultStatic

    DaChosenWon make a HashSet<String> playerswithnofall = new HashSet<String>();
    add them to the list when they run the command. check if they are in the list when they fall. if they are cancel the damage. you can also check if they are already in the list and remove them if they are to disable no fall on the command
     
    es359 likes this.
  3. Offline

    DaChosenWon

    ResultStatic
    Do you think you could show me this in code? I don't have much experience with HashSet.
     
  4. Offline

    ResultStatic

    DaChosenWon HashSet is just like ArrayList only you cannot enter duplicate values. that way you dont run the risk of people being added to the list more than once. a simple google search will explain how they work
     
  5. Offline

    mine-care

    im a bit off topic but you will expirience a lot of errors because you do this:
    Player player = (Player) sender;
    and this:
    Player player = (Player) event.getEntity();

    Please learn about Casting in Java

    Dont use System.out.println(); instead use getLogger().info("info");
    You dont have to print messages like "enabled" or disabled since bukkit does it automaticly for you :)
    What will help you in your project:
    http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
    http://wiki.bukkit.org/Plugin_Tutorial#Commands
    Good luck
     
  6. Offline

    DaChosenWon

    mine-care
    ResultStatic
    I get an issue, here is the code and the error.
    Code:
    Code:
    package me.DaChosenWon.NoFall;
     
    import java.util.HashSet;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class NoFall extends JavaPlugin implements Listener{
       
        HashSet<String> playerswithnofall = new HashSet<String>();
       
        @Override
        public void onEnable() {
            System.out.println("Enabled");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
        public void onDisable() {
            System.out.println("Disabled");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("nofalle")) {
                if(player.hasPermission("nofall.e")) {
                    player.sendMessage(ChatColor.GREEN + "No fall damage enabled!");
                    playerswithnofall.add(player.getName());
                }
            }
            return false;
        }
     
       
        @EventHandler
        public void onPlayerFall(EntityDamageEvent event) {
            Player player = (Player) event.getEntity();
            if (player.hasPermission("nofall.e")) {
                if (playerswithnofall.contains(player.getName())) {
                    if (event.getCause().equals(DamageCause.FALL)) {
                        event.setCancelled(true);
                    }
                }
            }
        }
    }
    
     
  7. Offline

    mythbusterma

    DaChosenWon

    Yes, your issue is EXACTLY what mine-care just told you not to do. If you had actually paid attention to what he said, you wouldn't be having issues. Look at his post and try again.
     
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page