Java Code Examples for org.spongepowered.api.world.Location#getExtent()

The following examples show how to use org.spongepowered.api.world.Location#getExtent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RTPExecutor.java    From EssentialCmds with MIT License 6 votes vote down vote up
private Optional<Location<World>> randomLocation(Player player, int searchDiameter){
	Location<World> playerLocation = player.getLocation();
	//Adding world border support, otherwise you could murder players by using a location within the border.
	WorldBorder border = player.getWorld().getWorldBorder();
	Vector3d center = border.getCenter();
	double diameter = Math.min(border.getDiameter(), searchDiameter);
	double radius = border.getDiameter() / 2;
	Random rand = new Random();
	int x = (int) (rand.nextInt((int) (center.getX()+diameter)) - radius);
	int y = rand.nextInt(256);
	int z = rand.nextInt((int) (rand.nextInt((int) (center.getZ()+diameter)) - radius));

	Location<World> randLocation = new Location<World>(playerLocation.getExtent(), x, y, z);
	TeleportHelper teleportHelper = Sponge.getGame().getTeleportHelper();
	return teleportHelper.getSafeLocation(randLocation);
}
 
Example 2
Source File: GPClaim.java    From GriefPrevention with MIT License 6 votes vote down vote up
public GPClaim(Location<World> lesserBoundaryCorner, Location<World> greaterBoundaryCorner, UUID claimId, ClaimType type, UUID ownerUniqueId, boolean cuboid) {
    // id
    this.id = claimId;

    // store corners
    this.lesserBoundaryCorner = lesserBoundaryCorner;
    this.greaterBoundaryCorner = greaterBoundaryCorner;
    this.world = lesserBoundaryCorner.getExtent();
    if (ownerUniqueId != null) {
        this.ownerUniqueId = ownerUniqueId;
        this.ownerPlayerData = GriefPreventionPlugin.instance.dataStore.getOrCreatePlayerData(this.world, this.ownerUniqueId);
    }
    this.type = type;
    this.cuboid = cuboid;
    this.context = new Context("gp_claim", this.id.toString());
    this.hashCode = this.id.hashCode();
    this.worldClaimManager = GriefPreventionPlugin.instance.dataStore.getClaimWorldManager(this.world.getProperties());
    if (this.type == ClaimType.WILDERNESS) {
        this.wildernessClaim = this;
    } else {
        this.wildernessClaim = this.worldClaimManager.getWildernessClaim();
    }
}
 
Example 3
Source File: WorldUtil.java    From Prism with MIT License 5 votes vote down vote up
/**
 * Remove a specific block from a given radius around a region.
 *
 * @param type BlockType to remove.
 * @param location Location center
 * @param radius Integer radius around location
 * @return integer Count of removals
 */
public static int removeAroundFromLocation(BlockType type, Location<World> location, int radius) {
    final World world = location.getExtent();

    int xMin = location.getBlockX() - radius;
    int xMax = location.getBlockX() + radius;

    int zMin = location.getBlockZ() - radius;
    int zMax = location.getBlockZ() + radius;

    int yMin = location.getBlockY() - radius;
    int yMax = location.getBlockY() + radius;

    // Clamp Y basement
    if (yMin < 0) {
        yMin = 0;
    }

    // Clamp Y ceiling
    if (yMax >= world.getDimension().getBuildHeight()) {
        yMax = world.getDimension().getBuildHeight();
    }

    int changeCount = 0;
    for (int x = xMin; x <= xMax; x++) {
        for (int z = zMin; z <= zMax; z++) {
            for (int y = yMin; y <= yMax; y++) {
                if (world.getBlock(x, y, z).getType().equals(type)) {
                    world.setBlockType(x, y, z, BlockTypes.AIR);
                    changeCount++;
                }
            }
        }
    }

    return changeCount;
}
 
Example 4
Source File: WorldUtil.java    From Prism with MIT License 5 votes vote down vote up
public static int removeIllegalBlocks(Location<World> location, int radius) {
    final World world = location.getExtent();

    int xMin = location.getBlockX() - radius;
    int xMax = location.getBlockX() + radius;

    int zMin = location.getBlockZ() - radius;
    int zMax = location.getBlockZ() + radius;

    int yMin = location.getBlockY() - radius;
    int yMax = location.getBlockY() + radius;

    // Clamp Y basement
    if (yMin < 0) {
        yMin = 0;
    }

    // Clamp Y ceiling
    if (yMax >= world.getDimension().getBuildHeight()) {
        yMax = world.getDimension().getBuildHeight();
    }

    int changeCount = 0;
    for (int x = xMin; x <= xMax; x++) {
        for (int z = zMin; z <= zMax; z++) {
            for (int y = yMin; y <= yMax; y++) {
                BlockType existing = world.getBlock(x, y, z).getType();
                if (existing.equals(BlockTypes.FIRE) || existing.equals(BlockTypes.TNT)) {
                    world.setBlockType(x, y, z, BlockTypes.AIR);
                    changeCount++;
                }
            }
        }
    }

    return changeCount;
}
 
Example 5
Source File: DoorManager.java    From RedProtect with GNU General Public License v3.0 5 votes vote down vote up
public static void ChangeDoor(BlockSnapshot b, Region r) {
    if ((!r.flagExists("smart-door") && !RedProtect.get().config.configRoot().flags.get("smart-door")) || !r.getFlagBool("smart-door")) {
        return;
    }

    Location<World> loc = b.getLocation().get();
    World w = loc.getExtent();

    if (isDoor(b)) {
        boolean iron = b.getState().getType() == BlockTypes.IRON_DOOR;
        if (iron) {
            changeDoorState(b);
            if (getDoorState(b)) {
                w.playSound(SoundTypes.BLOCK_IRON_DOOR_OPEN, loc.getPosition(), 1);
            } else {
                w.playSound(SoundTypes.BLOCK_IRON_DOOR_CLOSE, loc.getPosition(), 1);
            }
        }

        if (loc.getRelative(Direction.DOWN).getBlock().getType() == b.getState().getType() && loc.get(Keys.PORTION_TYPE).get() == PortionTypes.TOP) {
            loc = loc.getRelative(Direction.DOWN);
        }

        //check side block if is door
        BlockSnapshot[] block = new BlockSnapshot[4];
        block[0] = loc.getRelative(Direction.EAST).createSnapshot();
        block[1] = loc.getRelative(Direction.WEST).createSnapshot();
        block[2] = loc.getRelative(Direction.NORTH).createSnapshot();
        block[3] = loc.getRelative(Direction.SOUTH).createSnapshot();

        for (BlockSnapshot b2 : block) {
            if (b.getState().getType() == b2.getState().getType()) {
                changeDoorState(b2);
                break;
            }
        }
    }
}
 
Example 6
Source File: FireballExecutor.java    From EssentialCmds with MIT License 5 votes vote down vote up
public void spawnEntity(Location<World> location, Vector3d velocity, CommandSource src)
{
	Extent extent = location.getExtent();
	Entity fireball = extent.createEntity(EntityTypes.FIREBALL, location.getPosition());
	fireball.offer(Keys.VELOCITY, velocity);
	extent.spawnEntity(fireball, Cause.of(NamedCause.source(SpawnCause.builder().type(SpawnTypes.CUSTOM).build())));
}
 
Example 7
Source File: KittyCannonExecutor.java    From EssentialCmds with MIT License 5 votes vote down vote up
public void spawnEntity(Location<World> location, Vector3d velocity, CommandSource src)
{
	velocity = velocity.mul(5);
	Extent extent = location.getExtent();
	Entity kitten = extent.createEntity(EntityTypes.OCELOT, location.getPosition());
	kitten.offer(Keys.VELOCITY, velocity);
	extent.spawnEntity(kitten, Cause.of(NamedCause.source(SpawnCause.builder().type(SpawnTypes.CUSTOM).build())));
}
 
Example 8
Source File: BlockEventHandler.java    From GriefPrevention with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onSignChanged(ChangeSignEvent event) {
    final User user = CauseContextHelper.getEventUser(event);
    if (user == null) {
        return;
    }

    if (!GriefPreventionPlugin.instance.claimsEnabledForWorld(event.getTargetTile().getLocation().getExtent().getProperties())) {
        return;
    }

    GPTimings.SIGN_CHANGE_EVENT.startTimingIfSync();
    Location<World> location = event.getTargetTile().getLocation();
    // Prevent users exploiting signs
    GPClaim claim = GriefPreventionPlugin.instance.dataStore.getClaimAt(location);
    if (GPPermissionHandler.getClaimPermission(event, location, claim, GPPermissions.INTERACT_BLOCK_SECONDARY, user, location.getBlock(), user, TrustType.ACCESSOR, true) == Tristate.FALSE) {
        if (user instanceof Player) {
            event.setCancelled(true);
            final Text message = GriefPreventionPlugin.instance.messageData.permissionAccess
                    .apply(ImmutableMap.of(
                    "player", Text.of(claim.getOwnerName()))).build();
            GriefPreventionPlugin.sendClaimDenyMessage(claim, (Player) user, message);
            return;
        }
    }

    // send sign content to online administrators
    if (!GriefPreventionPlugin.getActiveConfig(location.getExtent().getProperties())
            .getConfig().general.generalAdminSignNotifications) {
        GPTimings.SIGN_CHANGE_EVENT.stopTimingIfSync();
        return;
    }

    World world = location.getExtent();
    StringBuilder lines = new StringBuilder(" placed a sign @ " + GriefPreventionPlugin.getfriendlyLocationString(event.getTargetTile().getLocation()));
    boolean notEmpty = false;
    for (int i = 0; i < event.getText().lines().size(); i++) {
        String withoutSpaces = Text.of(event.getText().lines().get(i)).toPlain().replace(" ", "");
        if (!withoutSpaces.isEmpty()) {
            notEmpty = true;
            lines.append("\n  " + event.getText().lines().get(i));
        }
    }

    String signMessage = lines.toString();
    // prevent signs with blocked IP addresses
    if (!user.hasPermission(GPPermissions.OVERRIDE_SPAM) && GriefPreventionPlugin.instance.containsBlockedIP(signMessage)) {
        event.setCancelled(true);
        GPTimings.SIGN_CHANGE_EVENT.stopTimingIfSync();
        return;
    }

    // if not empty and wasn't the same as the last sign, log it and remember it for later
    GPPlayerData playerData = this.dataStore.getOrCreatePlayerData(world, user.getUniqueId());
    if (notEmpty && playerData.lastMessage != null && !playerData.lastMessage.equals(signMessage)) {
        GriefPreventionPlugin.addLogEntry(user.getName() + lines.toString().replace("\n  ", ";"), null);
        //PlayerEventHandler.makeSocialLogEntry(player.get().getName(), signMessage);
        playerData.lastMessage = signMessage;

        if (!user.hasPermission(GPPermissions.EAVES_DROP_SIGNS)) {
            Collection<Player> players = (Collection<Player>) Sponge.getGame().getServer().getOnlinePlayers();
            for (Player otherPlayer : players) {
                if (otherPlayer.hasPermission(GPPermissions.EAVES_DROP_SIGNS)) {
                    otherPlayer.sendMessage(Text.of(TextColors.GRAY, user.getName(), signMessage));
                }
            }
        }
    }
    GPTimings.SIGN_CHANGE_EVENT.stopTimingIfSync();
}
 
Example 9
Source File: Visualization.java    From GriefPrevention with MIT License 4 votes vote down vote up
private void addClaimElements(int height, Location<World> locality, GPPlayerData playerData) {
    this.initBlockVisualTypes(type);
    Location<World> lesser = this.claim.getLesserBoundaryCorner();
    Location<World> greater = this.claim.getGreaterBoundaryCorner();
    World world = lesser.getExtent();
    boolean liquidTransparent = locality.getBlock().getType().getProperty(MatterProperty.class).isPresent() ? false : true;

    this.smallx = lesser.getBlockX();
    this.smally = this.useCuboidVisual() ? lesser.getBlockY() : 0;
    this.smallz = lesser.getBlockZ();
    this.bigx = greater.getBlockX();
    this.bigy = this.useCuboidVisual() ? greater.getBlockY() : 0;
    this.bigz = greater.getBlockZ();
    this.minx = this.claim.cuboid ? this.smallx : locality.getBlockX() - 75;
    this.minz = this.claim.cuboid ? this.smallz : locality.getBlockZ() - 75;
    this.maxx = this.claim.cuboid ? this.bigx : locality.getBlockX() + 75;
    this.maxz = this.claim.cuboid ? this.bigz : locality.getBlockZ() + 75;

    // initialize visualization elements without Y values and real data
    // that will be added later for only the visualization elements within
    // visualization range

    if (this.smallx == this.bigx && this.smally == this.bigy && this.smallz == this.bigz) {
        BlockSnapshot blockClicked =
                snapshotBuilder.from(new Location<World>(world, this.smallx, this.smally, this.smallz)).blockState(this.cornerMaterial.getDefaultState()).build();
        elements.add(new Transaction<BlockSnapshot>(blockClicked.getLocation().get().createSnapshot(), blockClicked));
        return;
    }

    // check CUI support
    if (GriefPreventionPlugin.instance.worldEditProvider != null && playerData != null
            && GriefPreventionPlugin.instance.worldEditProvider.hasCUISupport(playerData.getPlayerName())) {
        playerData.showVisualFillers = false;
        STEP = 0;
    }

    if (this.useCuboidVisual()) {
        this.addVisuals3D(claim, playerData);
    } else {
        this.addVisuals2D(claim, height, liquidTransparent);
    }
}
 
Example 10
Source File: ProtectionManagerImpl.java    From EagleFactions with MIT License 4 votes vote down vote up
private ProtectionResult canExplode(final Location<World> location, final User user)
{
    final World world = location.getExtent();

    if(EagleFactionsPlugin.DEBUG_MODE_PLAYERS.contains(user.getUniqueId()))
    {
        if(user instanceof Player)
        {
            final Player player = (Player)user;
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Explosion:")));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Location: " + location.toString())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("User: " + user.getName())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Block at location: " + location.getBlockType().getName())));
        }
    }

    //Not claimable worlds should be always ignored by protection system.
    if(this.protectionConfig.getNotClaimableWorldNames().contains(world.getName()))
        return ProtectionResult.ok();

    boolean shouldProtectWarZoneFromPlayers = this.protectionConfig.shouldProtectWarzoneFromPlayers();
    boolean allowExplosionsByOtherPlayersInClaims = this.protectionConfig.shouldAllowExplosionsByOtherPlayersInClaims();

    //Check if admin
    if(this.playerManager.hasAdminMode(user))
        return ProtectionResult.okAdmin();

    //Check world
    if (this.protectionConfig.getSafeZoneWorldNames().contains(world.getName()))
        return ProtectionResult.forbiddenSafeZone();
    else if (this.protectionConfig.getWarZoneWorldNames().contains(world.getName()))
    {
        if (!shouldProtectWarZoneFromPlayers)
            return ProtectionResult.okWarZone();
        return ProtectionResult.forbiddenWarZone();
    }

    //If no faction
    final Optional<Faction> optionalChunkFaction = this.factionLogic.getFactionByChunk(world.getUniqueId(), location.getChunkPosition());
    if (!optionalChunkFaction.isPresent())
    {
        if (!this.protectionConfig.shouldProtectWildernessFromPlayers())
            return ProtectionResult.ok();
        else return ProtectionResult.forbidden();
    }

    //If SafeZone or WarZone
    final Faction chunkFaction = optionalChunkFaction.get();

    if(chunkFaction.isSafeZone() || chunkFaction.isWarZone())
    {
        if(chunkFaction.isSafeZone())
        {
            if (user.hasPermission(PluginPermissions.SAFE_ZONE_BUILD))
                return ProtectionResult.okSafeZone();
            else return ProtectionResult.okWarZone();
        }
        else
        {
            if (chunkFaction.isWarZone() && user.hasPermission(PluginPermissions.WAR_ZONE_BUILD))
                return ProtectionResult.okWarZone();
            else return ProtectionResult.forbiddenWarZone();
        }
    }

    //If player is in faction
    final Optional<Faction> optionalPlayerFaction = this.factionLogic.getFactionByPlayerUUID(user.getUniqueId());
    if(optionalPlayerFaction.isPresent())
    {
        final Faction playerFaction = optionalPlayerFaction.get();
        if (chunkFaction.getName().equalsIgnoreCase(playerFaction.getName()))
        {
            if (this.permsManager.canPlaceBlock(user.getUniqueId(), playerFaction, chunkFaction, chunkFaction.getClaimAt(world.getUniqueId(), location.getChunkPosition()).get()))
                return ProtectionResult.okFactionPerm();
            else return ProtectionResult.forbidden();
        }
    }

    if (allowExplosionsByOtherPlayersInClaims)
        return ProtectionResult.ok();
    else return ProtectionResult.forbidden();
}
 
Example 11
Source File: ProtectionManagerImpl.java    From EagleFactions with MIT License 4 votes vote down vote up
private ProtectionResult canPlace(final Location<World> location, final User user)
{
    if(EagleFactionsPlugin.DEBUG_MODE_PLAYERS.contains(user.getUniqueId()))
    {
        if(user instanceof Player)
        {
            Player player = (Player)user;
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Block place:")));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Location: " + location.toString())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("User: " + user.getName())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Block at location: " + location.getBlockType().getName())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Item in hand: " + (user.getItemInHand(HandTypes.MAIN_HAND).isPresent() ? user.getItemInHand(HandTypes.MAIN_HAND).get().getType().getName() : ""))));
        }
    }

    World world = location.getExtent();
    final String itemId = user.getItemInHand(HandTypes.MAIN_HAND).isPresent() ? user.getItemInHand(HandTypes.MAIN_HAND).get().getType().getId() : "";

    //Not claimable worlds should be always ignored by protection system.
    if(this.protectionConfig.getNotClaimableWorldNames().contains(world.getName()))
        return ProtectionResult.ok();

    if(this.playerManager.hasAdminMode(user))
        return ProtectionResult.okAdmin();

    final Set<String> safeZoneWorlds = this.protectionConfig.getSafeZoneWorldNames();
    final Set<String> warZoneWorlds = this.protectionConfig.getWarZoneWorldNames();

    if (safeZoneWorlds.contains(world.getName()))
    {
        if (isBlockWhitelistedForPlaceDestroy(itemId, FactionType.SAFE_ZONE))
            return ProtectionResult.okSafeZone();
        if (user.hasPermission(PluginPermissions.SAFE_ZONE_BUILD))
            return ProtectionResult.okSafeZone();
        else return ProtectionResult.forbiddenSafeZone();
    }
    if (warZoneWorlds.contains(world.getName()))
    {
        if (isBlockWhitelistedForPlaceDestroy(itemId, FactionType.WAR_ZONE))
            return ProtectionResult.okWarZone();
        if (user.hasPermission(PluginPermissions.WAR_ZONE_BUILD))
            return ProtectionResult.okWarZone();
        else return ProtectionResult.forbiddenWarZone();
    }

    Optional<Faction> optionalPlayerFaction = this.factionLogic.getFactionByPlayerUUID(user.getUniqueId());
    Optional<Faction> optionalChunkFaction = this.factionLogic.getFactionByChunk(world.getUniqueId(), location.getChunkPosition());
    if(optionalChunkFaction.isPresent())
    {
        if(optionalChunkFaction.get().isSafeZone() || optionalChunkFaction.get().isWarZone())
        {
            if(optionalChunkFaction.get().isSafeZone())
            {
                if (isBlockWhitelistedForPlaceDestroy(itemId, FactionType.SAFE_ZONE))
                    return ProtectionResult.okSafeZone();
                if (user.hasPermission(PluginPermissions.SAFE_ZONE_BUILD))
                    return ProtectionResult.okSafeZone();
                else return ProtectionResult.forbiddenSafeZone();
            }
            else //WarZone
            {
                if (isBlockWhitelistedForPlaceDestroy(itemId, FactionType.WAR_ZONE))
                    return ProtectionResult.okWarZone();
                if (user.hasPermission(PluginPermissions.WAR_ZONE_BUILD))
                    return ProtectionResult.okWarZone();
                else return ProtectionResult.forbiddenWarZone();
            }
        }

        if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.FACTION))
            return ProtectionResult.ok();

        final Faction chunkFaction = optionalChunkFaction.get();
        if (optionalPlayerFaction.filter(faction -> this.permsManager.canPlaceBlock(user.getUniqueId(), faction, chunkFaction, chunkFaction.getClaimAt(world.getUniqueId(), location.getChunkPosition()).get())).isPresent())
            return ProtectionResult.okFactionPerm();
        else return ProtectionResult.forbidden();
    }
    else
    {
        if (!this.protectionConfig.shouldProtectWildernessFromPlayers())
            return ProtectionResult.ok();
        else return ProtectionResult.forbidden();
    }
}
 
Example 12
Source File: LightningExecutor.java    From EssentialCmds with MIT License 4 votes vote down vote up
public void spawnEntity(Location<World> location, CommandSource src)
{
	Extent extent = location.getExtent();
	Entity lightning = extent.createEntity(EntityTypes.LIGHTNING, location.getPosition());
	extent.spawnEntity(lightning, Cause.of(NamedCause.source(SpawnCause.builder().type(SpawnTypes.CUSTOM).build())));
}
 
Example 13
Source File: ProtectionManagerImpl.java    From EagleFactions with MIT License 4 votes vote down vote up
@Override
public ProtectionResult canBreak(final Location<World> location)
{
    final World world = location.getExtent();

    //Not claimable worlds should be always ignored by protection system.
    if(this.protectionConfig.getNotClaimableWorldNames().contains(world.getName()))
        return ProtectionResult.ok();

    if(this.protectionConfig.getSafeZoneWorldNames().contains(world.getName()))
    {
        if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.SAFE_ZONE))
            return ProtectionResult.okSafeZone();
        else return ProtectionResult.forbiddenSafeZone();
    }

    if(this.protectionConfig.getWarZoneWorldNames().contains(world.getName()) && this.protectionConfig.shouldProtectWarZoneFromMobGrief())
    {
        //Not sure if we should use white-list for mobs...
        if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.WAR_ZONE))
            return ProtectionResult.okWarZone();
        else return ProtectionResult.forbiddenWarZone();
    }

    final Optional<Faction> optionalChunkFaction = this.factionLogic.getFactionByChunk(world.getUniqueId(), location.getChunkPosition());
    if(!optionalChunkFaction.isPresent())
        return ProtectionResult.ok();

    if(optionalChunkFaction.get().isSafeZone())
    {
        if(isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.SAFE_ZONE))
            return ProtectionResult.okSafeZone();
        else return ProtectionResult.forbiddenSafeZone();
    }

    if(optionalChunkFaction.get().isWarZone() && this.protectionConfig.shouldProtectWarZoneFromMobGrief())
    {
        if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.WAR_ZONE))
            return ProtectionResult.okWarZone();
        else return ProtectionResult.forbiddenWarZone();
    }

    if(this.protectionConfig.shouldProtectClaimFromMobGrief())
    {
        if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.FACTION))
            return ProtectionResult.ok();
        else return ProtectionResult.forbidden();
    }
    return ProtectionResult.ok();
}
 
Example 14
Source File: ProtectionManagerImpl.java    From EagleFactions with MIT License 4 votes vote down vote up
private ProtectionResult canBreak(final Location<World> location, final User user)
{
    if(EagleFactionsPlugin.DEBUG_MODE_PLAYERS.contains(user.getUniqueId()))
    {
        if(user instanceof Player)
        {
            Player player = (Player)user;
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of(TextColors.GOLD, "Block break event!")));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of(TextColors.GOLD, "Location: " + location.toString())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of(TextColors.GOLD, "User: " + user.getName())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of(TextColors.GOLD, "Block at location: " + location.getBlockType().getName())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of(TextColors.GOLD, "Block id: " + location.getBlockType().getId())));
        }
    }

    final World world = location.getExtent();

    //Not claimable worlds should be always ignored by protection system.
    if(this.protectionConfig.getNotClaimableWorldNames().contains(world.getName()))
        return ProtectionResult.ok();

    if(this.playerManager.hasAdminMode(user))
        return ProtectionResult.okAdmin();

    final Set<String> safeZoneWorlds = this.protectionConfig.getSafeZoneWorldNames();
    final Set<String> warZoneWorlds = this.protectionConfig.getWarZoneWorldNames();

    if (safeZoneWorlds.contains(world.getName()))
    {
        if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.SAFE_ZONE))
            return ProtectionResult.okSafeZone();
        if (user.hasPermission(PluginPermissions.SAFE_ZONE_BUILD))
            return ProtectionResult.okSafeZone();
        else return ProtectionResult.forbiddenSafeZone();
    }
    if (warZoneWorlds.contains(world.getName()))
    {
        if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.WAR_ZONE))
            return ProtectionResult.okWarZone();
        if (user.hasPermission(PluginPermissions.WAR_ZONE_BUILD))
            return ProtectionResult.okWarZone();
        else return ProtectionResult.forbiddenWarZone();
    }

    final Optional<Faction> optionalChunkFaction = this.factionLogic.getFactionByChunk(world.getUniqueId(), location.getChunkPosition());
    final Optional<Faction> optionalPlayerFaction = this.factionLogic.getFactionByPlayerUUID(user.getUniqueId());
    if(optionalChunkFaction.isPresent())
    {
        if(optionalChunkFaction.get().isSafeZone() || optionalChunkFaction.get().isWarZone())
        {
            if(optionalChunkFaction.get().isSafeZone())
            {
                if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.SAFE_ZONE))
                    return ProtectionResult.okSafeZone();
                if (user.hasPermission(PluginPermissions.SAFE_ZONE_BUILD))
                    return ProtectionResult.okSafeZone();
                else return ProtectionResult.forbiddenSafeZone();
            }
            else //WarZone
            {
                if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.WAR_ZONE))
                    return ProtectionResult.okWarZone();
                if (user.hasPermission(PluginPermissions.WAR_ZONE_BUILD))
                    return ProtectionResult.okWarZone();
                else return ProtectionResult.forbiddenWarZone();
            }
        }

        if (isBlockWhitelistedForPlaceDestroy(location.getBlockType().getId(), FactionType.FACTION))
            return ProtectionResult.ok();

        final Faction chunkFaction = optionalChunkFaction.get();
        final Optional<Claim> optionalClaim = chunkFaction.getClaimAt(world.getUniqueId(), location.getChunkPosition());

        if (optionalPlayerFaction.filter(faction -> this.permsManager.canBreakBlock(user.getUniqueId(), faction, optionalChunkFaction.get(), optionalClaim.get())).isPresent())
            return ProtectionResult.okFactionPerm();
        else return ProtectionResult.forbidden();
    }
    else
    {
        if (!this.protectionConfig.shouldProtectWildernessFromPlayers())
            return ProtectionResult.ok();
        else return ProtectionResult.forbidden();
    }
}
 
Example 15
Source File: BlockListener.java    From RedProtect with GNU General Public License v3.0 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onBlockPlace(ChangeBlockEvent.Place e, @First Player p) {
    RedProtect.get().logger.debug(LogLevel.BLOCKS, "BlockListener - Is BlockPlaceEvent event!");

    BlockSnapshot b = e.getTransactions().get(0).getOriginal();
    Location<World> bloc = b.getLocation().get();
    World w = bloc.getExtent();

    ItemType m = RedProtect.get().getVersionHelper().getItemInHand(p);
    boolean antih = RedProtect.get().config.configRoot().region_settings.anti_hopper;
    Region r = RedProtect.get().rm.getTopRegion(b.getLocation().get(), this.getClass().getName());

    if (r == null && canPlaceList(w, b.getState().getType().getName())) {
        return;
    }

    if (r != null) {

        if (!r.canMinecart(p) && m.getName().contains("minecart")) {
            RedProtect.get().lang.sendMessage(p, "blocklistener.region.cantplace");
            e.setCancelled(true);
            return;
        }

        if (b.getState().getType().equals(BlockTypes.MOB_SPAWNER) && r.allowSpawner(p)) {
            return;
        }

        try {
            if (!r.canBuild(p) && !r.canPlace(b)) {
                RedProtect.get().lang.sendMessage(p, "blocklistener.region.cantbuild");
                e.setCancelled(true);
            } else {
                if (!RedProtect.get().ph.hasPerm(p, "redprotect.bypass") && antih &&
                        (m.equals(ItemTypes.HOPPER) || m.getName().contains("rail"))) {
                    int x = bloc.getBlockX();
                    int y = bloc.getBlockY();
                    int z = bloc.getBlockZ();
                    BlockSnapshot ib = w.createSnapshot(x, y + 1, z);
                    if (!cont.canBreak(p, ib) || !cont.canBreak(p, b)) {
                        RedProtect.get().lang.sendMessage(p, "blocklistener.container.chestinside");
                        e.setCancelled(true);
                    }
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
 
Example 16
Source File: ProtectionManagerImpl.java    From EagleFactions with MIT License 4 votes vote down vote up
private ProtectionResult canUseItem(final Location<World> location, final User user, final ItemStackSnapshot usedItem)
{
    if(EagleFactionsPlugin.DEBUG_MODE_PLAYERS.contains(user.getUniqueId()))
    {
        if(user instanceof Player)
        {
            Player player = (Player)user;
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Usage of item:")));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Location: " + location.toString())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("User: " + user.getName())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Block at location: " + location.getBlockType().getName())));
            player.sendMessage(PluginInfo.PLUGIN_PREFIX.concat(Text.of("Used item: " + usedItem.getType().getName())));
        }
    }

    final World world = location.getExtent();

    //Not claimable worlds should be always ignored by protection system.
    if(this.protectionConfig.getNotClaimableWorldNames().contains(world.getName()))
        return ProtectionResult.ok();

    if (this.playerManager.hasAdminMode(user))
        return ProtectionResult.okAdmin();

    final Set<String> safeZoneWorlds = this.protectionConfig.getSafeZoneWorldNames();
    final Set<String> warZoneWorlds = this.protectionConfig.getWarZoneWorldNames();

    if (safeZoneWorlds.contains(world.getName()))
    {
        if (isItemWhitelisted(usedItem.getType().getId(), FactionType.SAFE_ZONE))
            return ProtectionResult.okSafeZone();
        if (user.hasPermission(PluginPermissions.SAFE_ZONE_INTERACT))
            return ProtectionResult.okSafeZone();
        else return ProtectionResult.forbiddenSafeZone();
    }
    if (warZoneWorlds.contains(world.getName()))
    {
        if (isItemWhitelisted(usedItem.getType().getId(), FactionType.WAR_ZONE))
            return ProtectionResult.okWarZone();
        if (user.hasPermission(PluginPermissions.WAR_ZONE_INTERACT))
            return ProtectionResult.okWarZone();
        else return ProtectionResult.forbiddenWarZone();
    }

    final Optional<Faction> optionalChunkFaction = this.factionLogic.getFactionByChunk(world.getUniqueId(), location.getChunkPosition());
    final Optional<Faction> optionalPlayerFaction = this.factionLogic.getFactionByPlayerUUID(user.getUniqueId());
    if (!optionalChunkFaction.isPresent())
    {
        if (!this.protectionConfig.shouldProtectWildernessFromPlayers())
            return ProtectionResult.ok();
        else return ProtectionResult.forbidden();
    }

    final Faction chunkFaction = optionalChunkFaction.get();
    if (chunkFaction.isSafeZone())
    {
        if (isItemWhitelisted(usedItem.getType().getId(), FactionType.SAFE_ZONE))
            return ProtectionResult.okSafeZone();
        if (user.hasPermission(PluginPermissions.SAFE_ZONE_INTERACT))
            return ProtectionResult.okSafeZone();
        else return ProtectionResult.forbiddenSafeZone();
    }
    if (chunkFaction.isWarZone())
    {
        if (isItemWhitelisted(usedItem.getType().getId(), FactionType.WAR_ZONE))
            return ProtectionResult.okWarZone();
        if (user.hasPermission(PluginPermissions.WAR_ZONE_INTERACT))
            return ProtectionResult.okWarZone();
        else return ProtectionResult.forbiddenWarZone();
    }

    if (isItemWhitelisted(usedItem.getType().getId(), FactionType.FACTION))
        return ProtectionResult.ok();

    //If player is not in a faction but there is a faction at chunk
    if(!optionalPlayerFaction.isPresent())
        return ProtectionResult.forbidden();

    Faction playerFaction = optionalPlayerFaction.get();
    if(this.permsManager.canInteract(user.getUniqueId(), playerFaction, chunkFaction, chunkFaction.getClaimAt(world.getUniqueId(), location.getChunkPosition()).get()))
        return ProtectionResult.okFactionPerm();
    else return ProtectionResult.forbidden();
}
 
Example 17
Source File: BlockEventHandler.java    From GriefDefender with MIT License 4 votes vote down vote up
@Listener(order = Order.FIRST, beforeModifications = true)
public void onExplosionPre(ExplosionEvent.Pre event) {
    final World world = event.getExplosion().getWorld();
    if (!GDFlags.EXPLOSION_BLOCK || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(world.getUniqueId())) {
        return;
    }

    Object source = event.getSource();
    final Explosion explosion = event.getExplosion();
    if (explosion.getSourceExplosive().isPresent()) {
        source = explosion.getSourceExplosive().get();
    } else {
        Entity exploder = event.getCause().first(Entity.class).orElse(null);
        if (exploder != null) {
            source = exploder;
        }
    }

    if (GriefDefenderPlugin.isSourceIdBlacklisted(Flags.EXPLOSION_BLOCK.getName(), source, event.getExplosion().getWorld().getProperties())) {
        return;
    }

    GDTimings.EXPLOSION_PRE_EVENT.startTimingIfSync();
    final User user = CauseContextHelper.getEventUser(event);
    final Location<World> location = event.getExplosion().getLocation();
    final GDClaim radiusClaim = NMSUtil.getInstance().createClaimFromCenter(location, event.getExplosion().getRadius());
    final GDClaimManager claimManager = GriefDefenderPlugin.getInstance().dataStore.getClaimWorldManager(location.getExtent().getUniqueId());
    final Set<Claim> surroundingClaims = claimManager.findOverlappingClaims(radiusClaim);
    if (surroundingClaims.size() == 0) {
        return;
    }
    for (Claim claim : surroundingClaims) {
        // Use any location for permission check
        Location<World> targetLocation = new Location<>(location.getExtent(), claim.getLesserBoundaryCorner());
        Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.EXPLOSION_BLOCK, source, targetLocation, user, true);
        if (result == Tristate.FALSE) {
            event.setCancelled(true);
            break;
        }
    }

    GDTimings.EXPLOSION_PRE_EVENT.stopTimingIfSync();
}