Search in sources :

Example 86 with Listener

use of org.spongepowered.api.event.Listener in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onHunger.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onHunger(HealEntityEvent e) {
    if (!(e.getTargetEntity() instanceof Player)) {
        return;
    }
    Player p = (Player) e.getTargetEntity();
    Region r = RedProtect.get().rm.getTopRegion(p.getLocation());
    if (r != null && !r.canHunger()) {
        e.setCancelled(true);
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) Listener(org.spongepowered.api.event.Listener)

Example 87 with Listener

use of org.spongepowered.api.event.Listener in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onPlayerHarvest.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerHarvest(HarvestEntityEvent.TargetPlayer e) {
    RedProtect.get().logger.debug("player", "RPLayerListener: Is HarvestEntityEvent");
    Player p = e.getTargetEntity();
    Region r = RedProtect.get().rm.getTopRegion(p.getLocation());
    if (r != null) {
        if (r.keepInventory()) {
            e.setKeepsInventory(true);
        }
        if (r.keepLevels()) {
            e.setKeepsLevel(true);
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) Listener(org.spongepowered.api.event.Listener)

Example 88 with Listener

use of org.spongepowered.api.event.Listener in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onEntityDamageEvent.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onEntityDamageEvent(DamageEntityEvent e) {
    // victim
    Entity e1 = e.getTargetEntity();
    // damager
    Entity e2 = null;
    if (e.getCause().first(IndirectEntityDamageSource.class).isPresent()) {
        e2 = e.getCause().first(IndirectEntityDamageSource.class).get().getSource();
        RedProtect.get().logger.debug("player", "RPLayerListener: Is DamageEntityEvent event. Damager " + e2.getType().getName());
    }
    Player damager = null;
    if (e2 instanceof Projectile) {
        Projectile proj = (Projectile) e2;
        if (proj.getShooter() instanceof Player) {
            damager = (Player) proj.getShooter();
        }
    } else if (e2 instanceof Player) {
        damager = (Player) e2;
    }
    Location<World> l = e1.getLocation();
    Region r = RedProtect.get().rm.getTopRegion(l);
    if (r == null) {
        return;
    }
    RedProtect.get().logger.debug("player", "RPLayerListener: Is DamageEntityEvent event. Victim " + e1.getType().getName());
    if (damager instanceof Player) {
        if (e1 instanceof Hanging && !r.canBuild(damager)) {
            RPLang.sendMessage(damager, "entitylistener.region.cantinteract");
            e.setCancelled(true);
            return;
        }
        if (e1 instanceof Player && r.flagExists("pvp") && !r.canPVP(damager)) {
            RPLang.sendMessage(damager, "entitylistener.region.cantpvp");
            e.setCancelled(true);
            return;
        }
    }
    // return if not player
    if (!(e1 instanceof Player)) {
        return;
    }
    Player play = (Player) e.getTargetEntity();
    if (RedProtect.get().tpWait.contains(play.getName())) {
        RedProtect.get().tpWait.remove(play.getName());
        RPLang.sendMessage(play, RPLang.get("cmdmanager.region.tpcancelled"));
    }
    if (r != null && !r.canPlayerDamage()) {
        e.setCancelled(true);
    }
    // execute on health
    if (r.cmdOnHealth(play)) {
        RedProtect.get().logger.debug("player", "Cmd on healt: true");
    }
    if (!r.canDeath() && play.get(Keys.HEALTH).get() <= 1) {
        e.setCancelled(true);
    }
    // deny damagecauses
    List<String> Causes = RedProtect.get().cfgs.getStringList("server-protection.deny-playerdeath-by");
    if (e.getCause().containsType(DamageSource.class) && Causes.size() > 0) {
        DamageType damagec = e.getCause().first(DamageSource.class).get().getType();
        for (String cause : Causes) {
            if (damagec.getName().equalsIgnoreCase(cause)) {
                e.setCancelled(true);
            }
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) DamageSource(org.spongepowered.api.event.cause.entity.damage.source.DamageSource) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) Hanging(org.spongepowered.api.entity.hanging.Hanging) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) IndirectEntityDamageSource(org.spongepowered.api.event.cause.entity.damage.source.IndirectEntityDamageSource) World(org.spongepowered.api.world.World) DamageType(org.spongepowered.api.event.cause.entity.damage.DamageType) Projectile(org.spongepowered.api.entity.projectile.Projectile) Listener(org.spongepowered.api.event.Listener)

Example 89 with Listener

use of org.spongepowered.api.event.Listener 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 90 with Listener

use of org.spongepowered.api.event.Listener in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onItemPickup.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onItemPickup(CollideEntityEvent event, @Root Player p) {
    RedProtect.get().logger.debug("player", "Is CollideEntityEvent(ItemPickup) event.");
    for (Entity ent : event.getEntities()) {
        if (!(ent instanceof Item)) {
            continue;
        }
        Region r = RedProtect.get().rm.getTopRegion(ent.getLocation());
        if (r != null && !r.canPickup(p)) {
            event.setCancelled(true);
            RPLang.sendMessage(p, "playerlistener.region.cantpickup");
            return;
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Item(org.spongepowered.api.entity.Item) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) Listener(org.spongepowered.api.event.Listener)

Aggregations

Listener (org.spongepowered.api.event.Listener)360 Player (org.spongepowered.api.entity.living.player.Player)118 World (org.spongepowered.api.world.World)97 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)65 Entity (org.spongepowered.api.entity.Entity)58 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)46 Text (org.spongepowered.api.text.Text)46 ItemStack (org.spongepowered.api.item.inventory.ItemStack)45 Location (org.spongepowered.api.world.Location)45 Sponge (org.spongepowered.api.Sponge)22 BlockType (org.spongepowered.api.block.BlockType)22 Task (org.spongepowered.api.scheduler.Task)17 IOException (java.io.IOException)16 Optional (java.util.Optional)16 Living (org.spongepowered.api.entity.living.Living)15 ItemType (org.spongepowered.api.item.ItemType)15 UUID (java.util.UUID)14 Monster (org.spongepowered.api.entity.living.monster.Monster)14 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)14 Map (java.util.Map)13