Solved WorldGuard API Problem

Discussion in 'Spigot Plugin Development' started by Sn0wFl4ke, Mar 22, 2020.

  1. Hello,
    i am currently working on a Plugin that kills a Player when he quits from a Fight.
    Now the Problem is, that when the player attacks in a by WorldGuard Protected zone it still triggers the event.
    I already looked into the WorldGuard API Documentation but im pretty confused how to Implement it.
    Is there a way where i can check if the players are in a Protected zone or not ?

    Code (Java):

    public final class Main extends JavaPlugin implements Listener {

        public HashMap<Player, PlayerObj> PlayerS = new HashMap<>();
        XML cfgloader = new XML();


        @Override
        public void onEnable() {
            // Plugin startup logic
            getServer().getPluginManager().registerEvents(this, this);

            this.getCommand("antibail").setExecutor(new Command());

            cfgloader.getCfg();

            getServer().getScheduler().runTaskTimer(this, () -> {
                for(PlayerObj PlayerIns : PlayerS.values()){
                    updatePlayers(PlayerIns);
                }

            }, 0, 20);

        }

        @EventHandler
        public void onJoin (PlayerJoinEvent e){
            PlayerObj JoinedPlayer = new PlayerObj(e.getPlayer());
            PlayerS.put(e.getPlayer(),JoinedPlayer);
        }

        @EventHandler
        public void onPvP (EntityDamageByEntityEvent e){
            if(e.getDamager() instanceof Player && e.getEntity() instanceof Player){
                PlayerObj attacker = PlayerS.get((Player)e.getDamager());
                PlayerObj defender = PlayerS.get((Player) e.getEntity());
                if(!attacker.getFight()){
                    attacker.FightStartMsg();
                    attacker.setFight(true);
                }
                attacker.setCounter(Integer.parseInt(XML.cfg[3]));
                if(!defender.getFight()){
                    defender.FightStartMsg();
                    defender.setFight(true);
                }
                defender.setCounter(Integer.parseInt(XML.cfg[3]));

            }
        }

        @EventHandler
        public void onQuit(PlayerQuitEvent e)
        {
            Player player = e.getPlayer();
            PlayerObj Leaver = PlayerS.get(player);
            if(Leaver.counter > 0 && !player.hasPermission("antibail.leave")){
                Bukkit.broadcastMessage(XML.cfg[4] + " " + player.getName() + " " + XML.cfg[5]);
                player.setHealth(0.0);
            }
        }


        public void updatePlayers(PlayerObj Playerins){
            Player player = Playerins.getPlayer();
            if(Playerins.counter > 0)
                Playerins.counter--;
            else if(Playerins.counter == 0 && Playerins.getFight()){
                TitleAPI.sendTitle(player,10,20,10,XML.cfg[1], XML.cfg[2]);
                Playerins.setFight(false);
            }
        }
     
  2. FrostedSnowman

    Resource Staff

    You shouldn't need to hook into WorldGuard. All you will need to do is check if the event is cancelled. You can specify this in the EventHandler annotation.

    Code (Java):
    @EventHandler(ignoreCancelled = true)
     
    • Like Like x 1
  3. i did this already but it still got triggered or is it cuz i use Priority highest ?
     
  4. FrostedSnowman

    Resource Staff

    Yes, it's likely clashing with WorldGuard's. You should be fine with setting yours to normal.
     
    • Like Like x 1
  5. It worked thx alot