Search in sources :

Example 1 with TeleportType

use of org.spongepowered.api.event.cause.entity.teleport.TeleportType in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onPlayerTeleport.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onPlayerTeleport(MoveEntityEvent.Teleport e, @First Player p) {
    TeleportType tcause = TeleportTypes.UNKNOWN;
    if (e.getCause().containsType(TeleportType.class)) {
        tcause = e.getCause().first(TeleportType.class).get();
    }
    RedProtect.get().logger.debug("player", "RPLayerListener: Is MoveEntityEvent.Teleport event. Player: " + p.getName() + ", cause: " + tcause.getId());
    if (RedProtect.get().tpWait.contains(p.getName())) {
        RedProtect.get().tpWait.remove(p.getName());
        RPLang.sendMessage(p, "cmdmanager.region.tpcancelled");
    }
    Location<World> lfrom = e.getFromTransform().getLocation();
    Location<World> lto = e.getToTransform().getLocation();
    final Region rfrom = RedProtect.get().rm.getTopRegion(lfrom);
    final Region rto = RedProtect.get().rm.getTopRegion(lto);
    Sponge.getScheduler().createAsyncExecutor(RedProtect.get().container).scheduleWithFixedDelay(() -> {
        if (rto != null && rfrom == null) {
            RegionFlags(rto, null, p);
        }
        if (rto != null && rfrom != null) {
            RegionFlags(rto, rfrom, p);
        }
        if (rto == null && rfrom != null) {
            noRegionFlags(rfrom, p);
        }
        if (rfrom == null && rto != null) {
            noRegionFlags(rto, p);
        }
        if (rfrom == null && rto == 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);
            }
        }
    }, 2, 2, TimeUnit.SECONDS);
    if (rto != null) {
        // enter max players flag
        if (rto.maxPlayers() != -1) {
            if (!checkMaxPlayer(p, rto)) {
                RPLang.sendMessage(p, RPLang.get("playerlistener.region.maxplayers").replace("{players}", String.valueOf(rto.maxPlayers())));
                e.setCancelled(true);
            }
        }
        if (!rto.canEnter(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantregionenter");
            e.setCancelled(true);
            return;
        }
        // Allow enter with items
        if (!rto.canEnterWithItens(p)) {
            RPLang.sendMessage(p, RPLang.get("playerlistener.region.onlyenter.withitems").replace("{items}", rto.flags.get("allow-enter-items").toString()));
            e.setCancelled(true);
            return;
        }
        // Deny enter with item
        if (!rto.denyEnterWithItens(p)) {
            RPLang.sendMessage(p, RPLang.get("playerlistener.region.denyenter.withitems").replace("{items}", rto.flags.get("deny-enter-items").toString()));
            e.setCancelled(true);
            return;
        }
        if (PlayerCmd.containsKey(p)) {
            if (!rto.canBack(p) && PlayerCmd.get(p).startsWith("/back")) {
                RPLang.sendMessage(p, "playerlistener.region.cantback");
                e.setCancelled(true);
            }
            if (!rto.AllowHome(p) && PlayerCmd.get(p).startsWith("/home")) {
                RPLang.sendMessage(p, "playerlistener.region.canthome");
                e.setCancelled(true);
            }
            PlayerCmd.remove(p);
        }
    }
    // 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")) {
        RPLang.sendMessage(p, RPLang.get("playerlistener.upnethery").replace("{location}", NetherY + ""));
        e.setCancelled(true);
    }
    if (tcause.equals(TeleportTypes.ENTITY_TELEPORT)) {
        if (rfrom != null && !rfrom.canTeleport(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantuse");
            e.setCancelled(true);
        }
        if (rto != null && !rto.canTeleport(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantuse");
            e.setCancelled(true);
        }
    }
    if (tcause.equals(TeleportTypes.PORTAL)) {
        if (rto != null && !rto.canExitPortal(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantteleport");
            e.setCancelled(true);
        }
        if (rfrom != null && !rfrom.canEnterPortal(p)) {
            RPLang.sendMessage(p, "playerlistener.region.cantenterteleport");
            e.setCancelled(true);
        }
    }
}
Also used : Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) World(org.spongepowered.api.world.World) TeleportType(org.spongepowered.api.event.cause.entity.teleport.TeleportType) Listener(org.spongepowered.api.event.Listener)

Aggregations

Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)1 Listener (org.spongepowered.api.event.Listener)1 TeleportType (org.spongepowered.api.event.cause.entity.teleport.TeleportType)1 World (org.spongepowered.api.world.World)1