Java Code Examples for com.griefcraft.model.Protection#hasFlag()

The following examples show how to use com.griefcraft.model.Protection#hasFlag() . 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: LWCBlockListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onBlockExplode(BlockExplodeEvent event) {
    if (!LWC.ENABLED || event.isCancelled()) {
        return;
    }
    LWC lwc = plugin.getLWC();
    for (Block block : event.blockList()) {
        Protection protection = plugin.getLWC().findProtection(block.getLocation());
        if (protection != null) {
            boolean ignoreExplosions = Boolean
                    .parseBoolean(lwc.resolveProtectionConfiguration(protection.getBlock(), "ignoreExplosions"));
            if (!(ignoreExplosions || protection.hasFlag(Flag.Type.ALLOWEXPLOSIONS))) {
                event.setCancelled(true);
            }
        }
    }
}
 
Example 2
Source File: LWCEntityListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGH)
public void onEntityExplode(EntityExplodeEvent event) {
    if (!LWC.ENABLED || event.isCancelled()) {
        return;
    }

    LWC lwc = LWC.getInstance();

    for (Block block : event.blockList()) {
        Protection protection = plugin.getLWC().findProtection(block.getLocation());

        if (protection != null) {
            boolean ignoreExplosions = Boolean
                    .parseBoolean(lwc.resolveProtectionConfiguration(protection.getBlock(), "ignoreExplosions"));

            if (!(ignoreExplosions || protection.hasFlag(Flag.Type.ALLOWEXPLOSIONS))) {
                event.setCancelled(true);
            }
        }
    }
}
 
Example 3
Source File: RedstoneModule.java    From Modern-LWC with MIT License 5 votes vote down vote up
@Override
public void onRedstone(LWCRedstoneEvent event) {
    if (event.isCancelled()) {
        return;
    }

    LWC lwc = event.getLWC();
    Protection protection = event.getProtection();

    // check for a player using it
    ProtectionFinder finder = protection.getProtectionFinder();

    if (finder != null) {
        for (BlockState found : finder.getBlocks()) {
            if (DoorMatcher.PRESSURE_PLATES.contains(found.getType())) {
                // find a player that is using it
                int x = found.getX();
                int y = found.getY();
                int z = found.getZ();
                Player player = lwc.findPlayer(x - 1, x + 1, y, y + 1, z - 1, z + 1);

                if (player != null) {
                    if (!lwc.canAccessProtection(player, protection)) {
                        event.setCancelled(true);
                    } else {
                        // bypass the denyRedstone/REDSTONE flag check
                        return;
                    }
                }
            }
        }
    }

    boolean hasFlag = protection.hasFlag(Flag.Type.REDSTONE);
    boolean denyRedstone = lwc.getConfiguration().getBoolean("protections.denyRedstone", false);

    if ((!hasFlag && denyRedstone) || (hasFlag && !denyRedstone)) {
        event.setCancelled(true);
    }
}
 
Example 4
Source File: DoorsModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() == Result.CANCEL || !isEnabled()) {
        return;
    }

    // The more important check
    if (!event.canAccess()) {
        return;
    }

    Protection protection = event.getProtection();
    Block block = event.getEvent().getClickedBlock(); // The block they actually clicked :)
    Player player = event.getPlayer();

    // Check if the block is even something that should be opened
    if (!isValid(block.getType())) {
        return;
    }

    // Should we look for double doors?
    boolean doubleDoors = usingDoubleDoors();

    // The BOTTOM half of the other side of the double door
    Block doubleDoorBlock = null;

    // Only waste CPU if we need the double door block
    if (doubleDoors) {
        doubleDoorBlock = getDoubleDoor(block);

        if (doubleDoorBlock != null) {
            Protection other = lwc.findProtection(doubleDoorBlock.getLocation());
            if (!lwc.canAccessProtection(player, other)) {
                doubleDoorBlock = null; // don't open the other door :-)
            }
        }
    }

    // toggle the other side of the door open
    boolean opensWhenClicked = (DoorMatcher.WOODEN_DOORS.contains(block.getType())
            || DoorMatcher.WOODEN_FENCE_GATES.contains(block.getType())
            || DoorMatcher.WOODEN_TRAP_DOORS.contains(block.getType()));
    changeDoorStates(true, (opensWhenClicked ? null : block), doubleDoorBlock);

    if (action == Action.OPEN_AND_CLOSE || protection.hasFlag(Flag.Type.AUTOCLOSE)) {
        // Abuse the fact that we still use final variables inside the task
        // The double door block object is initially only assigned if we
        // need
        // it, so we just create a second variable ^^
        final Block finalBlock = block;
        final Block finalDoubleDoorBlock = doubleDoorBlock;

        // Calculate the wait time
        // This is basically Interval * TICKS_PER_SECOND
        int wait = getAutoCloseInterval() * TICKS_PER_SECOND;

        // Create the task
        // If we are set to close the door after a set period, let's create
        // a sync task for it
        lwc.getPlugin().getServer().getScheduler().scheduleSyncDelayedTask(lwc.getPlugin(), new Runnable() {
            public void run() {

                // Essentially all we need to do is reset the door
                // states
                // But DO NOT open the door if it's closed !
                changeDoorStates(false, finalBlock, finalDoubleDoorBlock);

            }
        }, wait);
    }

}
 
Example 5
Source File: LWC.java    From Modern-LWC with MIT License 4 votes vote down vote up
/**
 * Remove protections very quickly with raw SQL calls
 *
 * @param sender
 * @param where
 * @param shouldRemoveBlocks
 * @return
 */
public int fastRemoveProtections(CommandSender sender, String where, boolean shouldRemoveBlocks) {
    List<Integer> exemptedBlocks = configuration.getIntList("optional.exemptBlocks", new ArrayList<Integer>());
    List<Integer> toRemove = new LinkedList<>();
    List<Block> removeBlocks = null;
    int totalProtections = physicalDatabase.getProtectionCount();
    int completed = 0;
    int count = 0;

    // flush all changes to the database before working on the live database
    databaseThread.flush();

    if (shouldRemoveBlocks) {
        removeBlocks = new LinkedList<Block>();
    }

    if (where != null && !where.trim().isEmpty()) {
        where = " WHERE " + where.trim();
    }

    sender.sendMessage("Loading protections via STREAM mode");

    try {
        Statement resultStatement = physicalDatabase.getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,
                ResultSet.CONCUR_READ_ONLY);

        if (physicalDatabase.getType() == Database.Type.MySQL) {
            resultStatement.setFetchSize(Integer.MIN_VALUE);
        }

        String prefix = physicalDatabase.getPrefix();
        ResultSet result = resultStatement.executeQuery(
                "SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM "
                        + prefix + "protections" + where);

        while (result.next()) {
            Protection protection = physicalDatabase.resolveProtection(result);
            World world = protection.getBukkitWorld();

            // check if the protection is exempt from being removed
            if (protection.hasFlag(Flag.Type.EXEMPTION) || exemptedBlocks.contains(protection.getBlockId())) {
                continue;
            }

            count++;

            if (count % 100000 == 0 || count == totalProtections || count == 1) {
                sender.sendMessage(Colors.Dark_Red + count + " / " + totalProtections);
            }

            if (world == null) {
                continue;
            }

            // remove the protection
            toRemove.add(protection.getId());

            // remove the block ?
            if (shouldRemoveBlocks) {
                removeBlocks.add(protection.getBlock());
            }

            // Remove it from the cache if it's in there
            Protection cached = protectionCache.getProtection(protection.getCacheKey());
            if (cached != null) {
                cached.removeCache();
            }

            completed++;
        }

        // Close the streaming statement
        result.close();
        resultStatement.close();

        // flush all of the queries
        fullRemoveProtections(sender, toRemove);

        if (shouldRemoveBlocks) {
            removeBlocks(sender, removeBlocks);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return completed;
}