Search in sources :

Example 1 with EnterExitRegionEvent

use of br.net.fabiozumbi12.RedProtect.Bukkit.events.EnterExitRegionEvent in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onPlayerMovement.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerMovement(MoveEntityEvent e) {
    if (RedProtect.get().cfgs.getBool("performance.disable-onPlayerMoveEvent-handler")) {
        return;
    }
    Entity ent = e.getTargetEntity();
    Player p = null;
    if (ent instanceof Player) {
        p = (Player) ent;
    } else if (ent.get(Keys.PASSENGERS).isPresent()) {
        for (Object uuidEnt : ent.get(Keys.PASSENGERS).get()) {
            if (uuidEnt instanceof UUID) {
                if (Sponge.getServer().getPlayer((UUID) uuidEnt).isPresent()) {
                    p = Sponge.getServer().getPlayer((UUID) uuidEnt).get();
                }
            } else if (uuidEnt instanceof EntitySnapshot) {
                if (Sponge.getServer().getPlayer(((EntitySnapshot) uuidEnt).getUniqueId().get()).isPresent()) {
                    p = Sponge.getServer().getPlayer(((EntitySnapshot) uuidEnt).getUniqueId().get()).get();
                }
            }
        }
    } else {
        return;
    }
    if (p == null) {
        return;
    }
    RedProtect.get().logger.debug("player", "PlayerMoveEvent - Entity name: " + ent.getType().getName());
    if (e.getFromTransform() != e.getToTransform() && RedProtect.get().tpWait.contains(p.getName())) {
        RedProtect.get().tpWait.remove(p.getName());
        RPLang.sendMessage(p, "cmdmanager.region.tpcancelled");
    }
    Transform<World> lfromForm = e.getFromTransform();
    Transform<World> ltoForm = e.getToTransform();
    Location<World> lfrom = e.getFromTransform().getLocation();
    Location<World> lto = e.getToTransform().getLocation();
    // teleport player to coord/world if playerup 128 y
    int NetherY = RedProtect.get().cfgs.getInt("netherProtection.maxYsize");
    if (lto.getExtent().getDimension().getType().equals(DimensionTypes.NETHER) && NetherY != -1 && lto.getBlockY() >= NetherY && !p.hasPermission("RedProtect.get().bypass.nether-roof")) {
        for (String cmd : RedProtect.get().cfgs.getStringList("netherProtection.execute-cmd")) {
            RedProtect.get().game.getCommandManager().process(RedProtect.get().serv.getConsole(), cmd.replace("{player}", p.getName()));
        }
        RPLang.sendMessage(p, RPLang.get("playerlistener.upnethery").replace("{location}", NetherY + ""));
    }
    Region r = RedProtect.get().rm.getTopRegion(lto);
    /*
        //deny enter if no perm doors
    	String door = lto.getBlock().getType().getName();
    	if (r != null && (door.contains("DOOR") || door.contains("_GATE")) && !r.canDoor(p)){
    		if (RPDoor.isDoorClosed(p.getWorld().getBlockAt(lto))){
    			e.setCancelled(true);
    		}
    	}*/
    World w = lfrom.getExtent();
    if (r != null) {
        // enter max players flag
        if (r.maxPlayers() != -1) {
            if (!checkMaxPlayer(p, r)) {
                e.setToTransform(DenyEnterPlayer(w, lfromForm, ltoForm, p, r));
                RPLang.sendMessage(p, RPLang.get("playerlistener.region.maxplayers").replace("{players}", String.valueOf(r.maxPlayers())));
            }
        }
        // remove pots
        if (!r.allowEffects(p) && p.get(Keys.POTION_EFFECTS).isPresent()) {
            for (PotionEffect pot : p.get(Keys.POTION_EFFECTS).get()) {
                if (pot.getDuration() < 36000) {
                    p.offer(Keys.POTION_EFFECTS, new ArrayList<>());
                }
            }
        }
        // Enter flag
        if (!r.canEnter(p)) {
            e.setToTransform(DenyEnterPlayer(w, lfromForm, ltoForm, p, r));
            RPLang.sendMessage(p, "playerlistener.region.cantregionenter");
        }
        // Allow enter with items
        if (!r.canEnterWithItens(p)) {
            e.setToTransform(DenyEnterPlayer(w, lfromForm, ltoForm, p, r));
            RPLang.sendMessage(p, RPLang.get("playerlistener.region.onlyenter.withitems").replace("{items}", r.flags.get("allow-enter-items").toString()));
        }
        // Deny enter with item
        if (!r.denyEnterWithItens(p)) {
            e.setToTransform(DenyEnterPlayer(w, lfromForm, ltoForm, p, r));
            RPLang.sendMessage(p, RPLang.get("playerlistener.region.denyenter.withitems").replace("{items}", r.flags.get("deny-enter-items").toString()));
        }
        // Deny Fly
        if (!p.get(Keys.GAME_MODE).get().getName().equalsIgnoreCase("SPECTATOR") && !r.canFly(p) && p.get(Keys.IS_FLYING).get()) {
            p.offer(Keys.IS_FLYING, false);
            // p.setAllowFlight(false);
            RPLang.sendMessage(p, "playerlistener.region.cantfly");
        }
        // update region admin or leander visit
        if (RedProtect.get().cfgs.getString("region-settings.record-player-visit-method").equalsIgnoreCase("ON-REGION-ENTER")) {
            if (r.isLeader(p) || r.isAdmin(p)) {
                if (r.getDate() == null || (!r.getDate().equals(RPUtil.DateNow()))) {
                    r.setDate(RPUtil.DateNow());
                }
            }
        }
        if (Ownerslist.get(p) != r.getName()) {
            Region er = RedProtect.get().rm.getRegion(Ownerslist.get(p), p.getWorld());
            Ownerslist.put(p, r.getName());
            // Execute listener:
            EnterExitRegionEvent event = new EnterExitRegionEvent(er, r, p);
            if (Sponge.getEventManager().post(event)) {
                return;
            }
            // --
            RegionFlags(r, er, p);
            if (!r.getWelcome().equalsIgnoreCase("hide ")) {
                EnterExitNotify(r, p);
            }
        } else {
            RegionFlags(r, null, p);
        }
    } else {
        // if (r == null) >>
        if (Ownerslist.get(p) != null) {
            Region er = RedProtect.get().rm.getRegion(Ownerslist.get(p), p.getWorld());
            if (Ownerslist.containsKey(p)) {
                Ownerslist.remove(p);
            }
            // Execute listener:
            EnterExitRegionEvent event = new EnterExitRegionEvent(er, null, p);
            if (Sponge.getEventManager().post(event)) {
                return;
            }
            // ---
            if (er == null) {
                // remove all if no regions
                List<String> toRemove = new ArrayList<>();
                for (String taskId : PlayertaskID.keySet()) {
                    if (PlayertaskID.get(taskId).equals(p.getName())) {
                        if (taskId.contains("forcefly")) {
                            p.offer(Keys.CAN_FLY, false);
                            p.offer(Keys.IS_FLYING, false);
                        } else {
                            p.remove(Keys.POTION_EFFECTS);
                        }
                        toRemove.add(taskId);
                        stopTaskPlayer(taskId);
                    }
                }
                for (String key : toRemove) {
                    PlayertaskID.remove(key);
                }
            } else {
                noRegionFlags(er, p);
                if (!er.getWelcome().equalsIgnoreCase("hide ") && RedProtect.get().cfgs.getBool("notify.region-exit")) {
                    SendNotifyMsg(p, RPLang.get("playerlistener.region.wilderness"));
                }
            }
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) World(org.spongepowered.api.world.World) EntitySnapshot(org.spongepowered.api.entity.EntitySnapshot) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) EnterExitRegionEvent(br.net.fabiozumbi12.RedProtect.Sponge.events.EnterExitRegionEvent) Listener(org.spongepowered.api.event.Listener)

Example 2 with EnterExitRegionEvent

use of br.net.fabiozumbi12.RedProtect.Bukkit.events.EnterExitRegionEvent in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onPlayerMovement.

@EventHandler
public void onPlayerMovement(PlayerMoveEvent e) {
    if (e.isCancelled() || RPConfig.getBool("performance.disable-onPlayerMoveEvent-handler")) {
        return;
    }
    Player p = e.getPlayer();
    // test player target
    if (RPConfig.getBool("server-protection.check-killaura-freekill.enable")) {
        Entity pent = getTarget(p);
        int trysp = 0;
        int mrate = 0;
        if (pent != null && pent instanceof Player && dmgp.containsKey(pent.getName())) {
            if (trys.containsKey(p.getName())) {
                trysp = trys.get(p.getName());
            }
            trysp++;
            trys.put(p.getName(), trysp);
            if (RPConfig.getBool("server-protection.check-killaura-freekill.debug-trys")) {
                RedProtect.get().logger.warning("Player: " + p.getName() + " | Try count: " + trysp);
            }
            if (trysp % RPConfig.getInt("server-protection.check-killaura-freekill.check-rate") == 0) {
                RedProtect.get().logger.warning("Player: " + p.getName() + " | Check Rate Match: (" + trysp + ")");
                if (matchRate.containsKey(p.getName())) {
                    mrate = matchRate.get(p.getName());
                }
                mrate++;
                matchRate.put(p.getName(), mrate);
                if (mrate == RPConfig.getInt("server-protection.check-killaura-freekill.rate-multiples")) {
                    RedProtect.get().logger.severe("Player: " + p.getName() + " | MATCH RATE! Possible Hack or FreeKill: (" + mrate + ")");
                    matchRate.put(p.getName(), 0);
                }
            }
        } else {
            trys.remove(p.getName());
            matchRate.remove(p.getName());
        }
    }
    Location lfrom = e.getFrom();
    Location lto = e.getTo();
    // RedProtect.get().logger.debug("RPPlayerListener - PlayerMoveEvent from "+lfrom.toString()+" to "+lto.toString());
    if (lto.getWorld().equals(lfrom.getWorld()) && lto.distance(lfrom) > 0.1 && RedProtect.get().tpWait.contains(p.getName())) {
        RedProtect.get().tpWait.remove(p.getName());
        RPLang.sendMessage(p, "cmdmanager.region.tpcancelled");
    }
    // teleport player to coord/world if playerup 128 y
    int NetherY = RPConfig.getInt("netherProtection.maxYsize");
    if (lto.getWorld().getEnvironment().equals(World.Environment.NETHER) && NetherY != -1 && lto.getBlockY() >= NetherY && !p.hasPermission("redprotect.bypass.nether-roof")) {
        for (String cmd : RPConfig.getStringList("netherProtection.execute-cmd")) {
            RedProtect.get().serv.dispatchCommand(RedProtect.get().serv.getConsoleSender(), cmd.replace("{player}", p.getName()));
        }
        RPLang.sendMessage(p, RPLang.get("playerlistener.upnethery").replace("{location}", NetherY + ""));
    }
    Region r = RedProtect.get().rm.getTopRegion(lto);
    World w = lfrom.getWorld();
    if (r != null) {
        // Enter flag
        if (!r.canEnter(p)) {
            e.setTo(RPUtil.DenyEnterPlayer(w, lfrom, e.getTo(), p, r, false));
            RPLang.sendMessage(p, "playerlistener.region.cantregionenter");
        }
        // enter max players flag
        if (r.maxPlayers() != -1) {
            if (!checkMaxPlayer(p, r)) {
                e.setTo(RPUtil.DenyEnterPlayer(w, lfrom, e.getTo(), p, r, false));
                RPLang.sendMessage(p, RPLang.get("playerlistener.region.maxplayers").replace("{players}", String.valueOf(r.maxPlayers())));
            }
        }
        // remove pots
        if (!r.allowEffects(p)) {
            for (PotionEffect pot : p.getActivePotionEffects()) {
                if (pot.getDuration() < 36000) {
                    p.removePotionEffect(pot.getType());
                }
            }
        }
        // Mypet Flag
        if (RedProtect.get().MyPet && !r.canPet(p)) {
            if (MyPetApi.getPlayerManager().isMyPetPlayer(p)) {
                MyPetPlayer mpp = MyPetApi.getPlayerManager().getMyPetPlayer(p);
                if (mpp.hasMyPet() && mpp.getMyPet().getStatus() == PetState.Here) {
                    mpp.getMyPet().removePet();
                    RPLang.sendMessage(p, "playerlistener.region.cantpet");
                }
            }
        }
        // Allow enter with items
        if (!r.canEnterWithItens(p)) {
            e.setTo(RPUtil.DenyEnterPlayer(w, lfrom, e.getTo(), p, r, false));
            RPLang.sendMessage(p, RPLang.get("playerlistener.region.onlyenter.withitems").replace("{items}", r.flags.get("allow-enter-items").toString()));
        }
        // Deny enter with item
        if (!r.denyEnterWithItens(p)) {
            e.setTo(RPUtil.DenyEnterPlayer(w, lfrom, e.getTo(), p, r, false));
            RPLang.sendMessage(p, RPLang.get("playerlistener.region.denyenter.withitems").replace("{items}", r.flags.get("deny-enter-items").toString()));
        }
        // update region admin or leander visit
        if (RPConfig.getString("region-settings.record-player-visit-method").equalsIgnoreCase("ON-REGION-ENTER")) {
            if (r.isLeader(p) || r.isAdmin(p)) {
                if (r.getDate() == null || (!r.getDate().equals(RPUtil.DateNow()))) {
                    r.setDate(RPUtil.DateNow());
                }
            }
        }
        // Deny Fly
        if (!p.getGameMode().toString().equalsIgnoreCase("SPECTATOR") && !r.canFly(p) && p.isFlying()) {
            p.setFlying(false);
            // p.setAllowFlight(false);
            RPLang.sendMessage(p, "playerlistener.region.cantfly");
        }
        if (Ownerslist.get(p.getName()) != r.getName()) {
            Region er = RedProtect.get().rm.getRegion(Ownerslist.get(p.getName()), p.getWorld());
            Ownerslist.put(p.getName(), r.getName());
            // Execute listener:
            EnterExitRegionEvent event = new EnterExitRegionEvent(er, r, p);
            Bukkit.getPluginManager().callEvent(event);
            if (event.isCancelled()) {
                return;
            }
            // --
            RegionFlags(r, er, p);
            if (!r.getWelcome().equalsIgnoreCase("hide ")) {
                EnterExitNotify(r, p);
            }
        } else {
            RegionFlags(r, null, p);
        }
    } else {
        // if (r == null) >>
        if (Ownerslist.get(p.getName()) != null) {
            Region er = RedProtect.get().rm.getRegion(Ownerslist.get(p.getName()), p.getWorld());
            if (Ownerslist.containsKey(p.getName())) {
                Ownerslist.remove(p.getName());
            }
            // Execute listener:
            EnterExitRegionEvent event = new EnterExitRegionEvent(er, null, p);
            Bukkit.getPluginManager().callEvent(event);
            if (event.isCancelled()) {
                return;
            }
            // ---
            if (er == null) {
                // remove all if no regions
                List<String> toRemove = new ArrayList<>();
                for (String taskId : PlayertaskID.keySet()) {
                    if (PlayertaskID.get(taskId).equals(p.getName())) {
                        if (taskId.contains("forcefly")) {
                            p.setFlying(false);
                            p.setAllowFlight(false);
                        } else {
                            for (PotionEffect pot : p.getActivePotionEffects()) {
                                p.removePotionEffect(pot.getType());
                            }
                        }
                        toRemove.add(taskId);
                        stopTaskPlayer(taskId);
                    }
                }
                for (String key : toRemove) {
                    PlayertaskID.remove(key);
                }
            } else {
                noRegionFlags(er, p);
                if (!er.getWelcome().equalsIgnoreCase("hide ") && RPConfig.getBool("notify.region-exit")) {
                    if (RedProtect.get().version >= 1110) {
                        SendNotifyMsg(p, RPLang.get("playerlistener.region.wilderness"), "RED");
                    } else {
                        SendNotifyMsg(p, RPLang.get("playerlistener.region.wilderness"), null);
                    }
                }
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) MyPetBukkitEntity(de.Keyle.MyPet.api.entity.MyPetBukkitEntity) Player(org.bukkit.entity.Player) PvPlayer(me.NoChance.PvPManager.PvPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) PotionEffect(org.bukkit.potion.PotionEffect) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) EnterExitRegionEvent(br.net.fabiozumbi12.RedProtect.Bukkit.events.EnterExitRegionEvent) World(org.bukkit.World) Location(org.bukkit.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)1 EnterExitRegionEvent (br.net.fabiozumbi12.RedProtect.Bukkit.events.EnterExitRegionEvent)1 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)1 EnterExitRegionEvent (br.net.fabiozumbi12.RedProtect.Sponge.events.EnterExitRegionEvent)1 MyPetBukkitEntity (de.Keyle.MyPet.api.entity.MyPetBukkitEntity)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 PvPlayer (me.NoChance.PvPManager.PvPlayer)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1 Entity (org.bukkit.entity.Entity)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 PotionEffect (org.bukkit.potion.PotionEffect)1 PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)1 Entity (org.spongepowered.api.entity.Entity)1 EntitySnapshot (org.spongepowered.api.entity.EntitySnapshot)1 Player (org.spongepowered.api.entity.living.player.Player)1 Listener (org.spongepowered.api.event.Listener)1 World (org.spongepowered.api.world.World)1