Solved PvP Toggle command

Discussion in 'Plugin Development' started by TheFl4me, Feb 19, 2015.

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

    TheFl4me

    Hi, I would like to code a command for my plugin wich would disable/enable pvp on the entire server by using the commands:

    /pvp true
    /pvp false

    Only problem:
    I have absolutely no idea how to do this.

    Thank you for your help.
     
  2. Offline

    Skionz

    Use a Set and the EntityDamageByEntityEvent.
     
  3. Offline

    TheFl4me

    OK i did what you said but whenever i did it with the EntityDamageByEntityEvent, i works yes but i keep getting errors spammed in the console. so i tried the same thing with the EntityDamageEvent but still the same thing.

    Im happy that it is working but i would like to get rid of all these errors

    here are the classes:

    Code:
    package KitPvP;
    
    import Main.Main;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class PvPToggle implements CommandExecutor {
     
        public static ArrayList<Player> nopvp = new ArrayList<Player>();
     
        public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
         
            String pvpusage = "§cWrong syntax! Usage: /pvp [true : false]";
         
            Player p = (Player)cs;
            if (!(cs instanceof Player)) { 
                cs.sendMessage(ChatColor.RED + "Only Players can do this!");
                return true;
            }
            for (Player all : Bukkit.getOnlinePlayers()) {
                if (cmd.getName().equalsIgnoreCase("pvp")) {
                    if(p.hasPermission("kitpvp.pvptoggle")) {
                        if(args.length == 0) {
                            p.sendMessage(pvpusage);
                        }
                        if(args.length == 1) {
                            if(args[0].equalsIgnoreCase("false")) {
                                if (!nopvp.contains(all)) {
                                    nopvp.add(all);
                                    p.sendMessage("§7PvP enabled?: §ffalse");
                                }
                                else {
                                    p.sendMessage("§cPvP is already disabled");
                                }
                            }
                            if(args[0].equalsIgnoreCase("true")) {
                                if (nopvp.contains(all)) {
                                    nopvp.remove(all);
                                    p.sendMessage("§7PvP enabled?: §ftrue");
                                }
                                else {
                                    p.sendMessage("§cPvP is already enabled");
                                }
                            }
                            else {
                                p.sendMessage(pvpusage);
                            }
                        }
                        if(args.length > 1) {
                            p.sendMessage(pvpusage);
                        }
                    }
                    else {
                        p.sendMessage(Main.noperm);
                    }
                }
                return true;
            }
            return true;
        }
    }        

    Code:
    package Listeners;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    
    import KitPvP.PvPToggle;
    
    public class PvPToggleListener implements Listener {
     
        @EventHandler
        public void EntityDamageEvent(EntityDamageEvent e) {
            Player p = (Player)e.getEntity();
            if(PvPToggle.nopvp.contains(p)) {
                e.setCancelled(true);
            }
        }
    }
    
     

    Attached Files:

  4. the problem could possibly be that you don't check if the entity is a player...
    try adding this
    Code:
    if(e.getEntity() instanceof Player){
            Player p = (Player)e.getEntity();
            if(PvPToggle.nopvp.contains(p)) {
                e.setCancelled(true);
            }
    }
    
     
  5. Offline

    TheFl4me

    Thanks alot man this fixed it :)
     
  6. Offline

    SuperOriginal

    I'm not sure if this is intended, but you're also blocking other mobs from hitting players.
     
  7. Offline

    TheFl4me

    its not intended but im actually kinda happy it does it aswell
     
Thread Status:
Not open for further replies.

Share This Page