PlayerCommandPreprocessEvent

Discussion in 'Spigot Plugin Development' started by GB_6, Apr 14, 2017.

  1. Hey fellow devs,

    I have a problem. How would i go about executing the constructor of another class when this works (this already works):
    Code (Text):
    package commands;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;

    import com.gb6.homegui.Main;

    public class CommandPreprocess implements Listener {
       
        private Main plugin;

        public CommandPreprocess(Main pl) {
            plugin = pl;
        }

        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPreCommand(PlayerCommandPreprocessEvent e) {
            String[] args = e.getMessage().split(" ");
            if (args[0].equalsIgnoreCase("/home") && args.length == 1) {
                e.setCancelled(true);
                if (e instanceof Player) {
                    //I WANT TO RUN THE THE CONSTRUCTOR OF THE OTHER CLASS HERE
                }
            }
        }

    }
     
     
  2. This is how I do it in my System32 plugin:

    System32API class:

    Code (Text):
    package me.kyllian.system32.API;

    import java.util.Set;

    import org.bukkit.Bukkit;
    import org.bukkit.block.Block;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.kyllian.system32.utils.Data;

    public class System32API {

        public static System32API sys32API;

        public static System32API getInstance() {
            return sys32API;
        }

        /**
         * @param heal
         *            Heals the player to its maximum health
         * @param plr
         *            Player
         */

        @SuppressWarnings("deprecation")
        public void heal(Player plr) {
            plr.setHealth(plr.getMaxHealth());
        }

        /**
         * '
         *
         * @param feed
         *            Feeds the player to its maximum
         * @param plr
         *            Player
         */

        public void feed(Player plr) {
            plr.setFoodLevel(20);
        }

        /**
         * @param plr
         *            Player
         * @return The block where the player looks at
         */

        @SuppressWarnings({ "unchecked", "rawtypes" })
        public Block block(Player plr) {
            Block target = plr.getTargetBlock((Set) null, 20);
            return target;
        }

        public boolean hasFly(Player plr) {
            return Data.getData(plr).fly;
        }

        public boolean hasGod(Player plr) {
            return Data.getData(plr).god;
        }
       
        public void kickAll(CommandSender s, String reason) {
            for (Player o : Bukkit.getOnlinePlayers()) {
                if (s != o) {
                    o.kickPlayer(reason);
                }
            }
        }
    }
     
    Heal class:

    Code (Text):
    package me.kyllian.system32.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.kyllian.system32.API.System32API;
    import me.kyllian.system32.utils.Messages;

    public class Heal implements CommandExecutor {

        private System32API sys32API = new System32API();
        private Messages mes = new Messages();

        public boolean onCommand(CommandSender s, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("heal")) {
                if (args.length == 0) {
                    if (!(s.hasPermission("system32.heal"))) {
                        mes.noPermissions(s);
                        return true;
                    }
                    if (!(s instanceof Player)) {
                        mes.notAPlayer(s);
                        return true;
                    }
                    mes.youHaveBeen(s, "healed");
                    sys32API.heal((Player) s);
                    return true;
                }
                if (args.length == 1) {
                    if (!(s.hasPermission("system32.heal.others"))) {
                        mes.noPermissions(s);
                        return true;
                    }
                    Player t = Bukkit.getPlayer(args[0]);
                    if (t == null) {
                        mes.notAPlayer(s, args[0]);
                        return true;
                    }
                    sys32API.heal(t);
                    mes.youHaveBeen(t, "healed");
                } else {
                    mes.tooManyArguments(s);
                    return true;
                }
            }
            return true;
        }
    }
     
     
    • Like Like x 1
  3. this is very simplified:
    Code (Text):


    public class SendHelp {
     
    Player p;  
       public SendHelp(Player player) {
         this.p = player;
      }

      public void execute() {
             //RunCode
      }
    }
    And in your CommandPreProcess
    Code (Text):

    SendHelp sh = new SendHelp( e.getPlayer());
    sh.execute();
     
    • Like Like x 1
    • Useful Useful x 1
  4. That worked! Thank you so much!