@Override public void load(LWC lwc) { configuration = Configuration.load("doors.yml"); doors = new LinkedList<DoorAction>(); enabled = configuration.getBoolean("doors.enabled", true); String action = configuration.getString("doors.action"); if (action == null) { this.action = Action.NULL; return; } if (action.equalsIgnoreCase("openAndClose")) { this.action = Action.OPEN_AND_CLOSE; this.interval = configuration.getInt("doors.interval", 3); } else if (action.equalsIgnoreCase("toggle")) { this.action = Action.TOGGLE; } // start the task DoorTask doorTask = new DoorTask(); lwc.getPlugin() .getServer() .getScheduler() .scheduleSyncRepeatingTask(lwc.getPlugin(), doorTask, 20, 20); }
/** * Find and match all of the limits in a given list of nodes for the config * * @param node * @return */ private List<Limit> findLimits(String node) { List<Limit> limits = new LinkedList<Limit>(); List<String> keys = configuration.getKeys(node); for (String key : keys) { String value = configuration.getString(node + "." + key); int limit; if (value.equalsIgnoreCase("unlimited")) { limit = UNLIMITED; } else { limit = Integer.parseInt(value); } // Match default if (key.equalsIgnoreCase("default")) { limits.add(new DefaultLimit(limit)); } else { Material material = Material.getMaterial(key.toUpperCase()); limits.add(new BlockLimit(material, limit)); } } // Order it orderLimits(limits); return limits; }
/** Load all of the limits */ private void loadLimits() { // make sure we're working on a clean slate defaultLimits.clear(); playerLimits.clear(); groupLimits.clear(); // add the default limits defaultLimits.addAll(findLimits("defaults")); // add all of the player limits try { for (String player : configuration.getKeys("players")) { playerLimits.put(player.toLowerCase(), findLimits("players." + player)); } } catch (NullPointerException e) { } // add all of the group limits try { for (String group : configuration.getKeys("groups")) { groupLimits.put(group, findLimits("groups." + group)); } } catch (NullPointerException e) { } }
/** @return the path where the database file should be saved */ public String getDatabasePath() { Configuration lwcConfiguration = LWC.getInstance().getConfiguration(); if (currentType == Type.MySQL) { return "//" + lwcConfiguration.getString("database.host") + "/" + lwcConfiguration.getString("database.database"); } return lwcConfiguration.getString("database.path"); }
@Override public Result onProtectionInteract( LWC lwc, Player player, Protection protection, List<String> actions, boolean canAccess, boolean canAdmin) { if (!enabled || action == Action.NULL) { return DEFAULT; } if (!canAccess) { return DEFAULT; } // get the blocks for the door List<Block> blocks = lwc.getProtectionSet( protection.getBukkitWorld(), protection.getX(), protection.getY(), protection.getZ()); // only send them one message :-) boolean sentMessage = false; // search around for iron doors if enabled if (configuration.getBoolean("doors.doubleDoors", true)) { Block protectionBlock = protection.getBlock(); Block temp = null; BlockFace[] faces = new BlockFace[] {BlockFace.NORTH, BlockFace.WEST, BlockFace.EAST, BlockFace.SOUTH}; for (BlockFace face : faces) { if (isValid((temp = protectionBlock.getFace(face)).getType())) { Protection found = lwc.findProtection(temp); if (found != null) { if (lwc.canAccessProtection(player, found)) { // we can access it, add it to the blocks blocks.addAll( lwc.getProtectionSet( found.getBukkitWorld(), found.getX(), found.getY(), found.getZ())); } } } } } for (Block block : blocks) { if (!isValid(block.getType())) { continue; } // create the door instance Door door = new Door(block.getType(), block.getData()); // process the current door's data byte data = initializeDoorData(door); switch (this.action) { case TOGGLE: if ((block.getData() & 0x4) != 0x4) { data |= 0x4; } if (!sentMessage) { sentMessage = true; if (isDoorOpen(door)) { // lwc.sendLocale(player, "protection.doors.open"); } else { // lwc.sendLocale(player, "protection.doors.close"); } } break; case OPEN_AND_CLOSE: if ((block.getData() & 0x4) != 0x4) { data |= 0x4; } if (!sentMessage) { sentMessage = true; if (isDoorOpen(door)) { // lwc.sendLocale(player, "protection.doors.open"); } else { // lwc.sendLocale(player, "protection.doors.close"); } } if (!isDoorOpen(door)) { Location location = new Location(block.getWorld(), block.getX(), block.getY(), block.getZ()); DoorAction doorAction = new DoorAction(); doorAction.location = location; doorAction.triggerTime = System.currentTimeMillis() + (interval * 1000L); doors.push(doorAction); } break; } // update the door block.setData(data); } return DEFAULT; }
public class LimitsV2 extends JavaModule { /** The limit represented by unlimited */ public static final int UNLIMITED = Integer.MAX_VALUE; /** The limits configuration */ private final Configuration configuration = Configuration.load("limitsv2.yml"); /** A map of the default limits */ private final List<Limit> defaultLimits = new LinkedList<Limit>(); /** A map of all of the player limits */ private final Map<String, List<Limit>> playerLimits = new HashMap<String, List<Limit>>(); /** A map of all of the group limits - downcasted to lowercase to simplify comparisons */ private final Map<String, List<Limit>> groupLimits = new HashMap<String, List<Limit>>(); public abstract class Limit { /** The limit */ private final int limit; public Limit(int limit) { this.limit = limit; } /** * Get the player's protection count that should be used with this limit * * @param player * @param material * @return */ public abstract int getProtectionCount(Player player, Material material); /** @return */ public int getLimit() { return limit; } } public final class DefaultLimit extends Limit { public DefaultLimit(int limit) { super(limit); } @Override public int getProtectionCount(Player player, Material material) { return LWC.getInstance().getPhysicalDatabase().getProtectionCount(player.getName()); } } public final class BlockLimit extends Limit { /** The block material to limit */ private final Material material; public BlockLimit(Material material, int limit) { super(limit); this.material = material; } @Override public int getProtectionCount(Player player, Material material) { return LWC.getInstance() .getPhysicalDatabase() .getProtectionCount(player.getName(), material.getId()); } /** @return */ public Material getMaterial() { return material; } } public LimitsV2() { loadLimits(); } @Override public void onReload(LWCReloadEvent event) { reload(); } @Override public void onRegisterProtection(LWCProtectionRegisterEvent event) { if (event.isCancelled()) { return; } LWC lwc = event.getLWC(); Player player = event.getPlayer(); Block block = event.getBlock(); if (hasReachedLimit(player, block.getType())) { lwc.sendLocale(player, "protection.exceeded"); event.setCancelled(true); } } @Override public void onCommand(LWCCommandEvent event) { if (event.isCancelled()) { return; } if (!event.hasFlag("limits")) { return; } LWC lwc = event.getLWC(); CommandSender sender = event.getSender(); String[] args = event.getArgs(); event.setCancelled(true); if (args.length == 0 && !(sender instanceof Player)) { sender.sendMessage(Colors.Red + "Unsupported"); return; } String playerName; if (args.length == 0) { playerName = sender.getName(); } else { if (lwc.isAdmin(sender)) { playerName = args[0]; } else { lwc.sendLocale(sender, "protection.accessdenied"); return; } } Player player = lwc.getPlugin().getServer().getPlayer(playerName); if (player == null) { return; } // send their limits to them sendLimits(player, player, getPlayerLimits(player)); } /** * Gets the raw limits configuration * * @return */ public Configuration getConfiguration() { return configuration; } /** Reload the limits */ public void reload() { loadLimits(); } /** * Sends the list of limits to the player * * @param sender the commandsender to send the limits to * @param target the player limits are being shown for, can be null * @param limits */ public void sendLimits(CommandSender sender, Player target, List<Limit> limits) { LWC lwc = LWC.getInstance(); for (Limit limit : limits) { if (limit == null) { continue; } String stringLimit = limit.getLimit() == UNLIMITED ? "Unlimited" : Integer.toString(limit.getLimit()); String colour = Colors.Yellow; if (target != null) { boolean reachedLimit = hasReachedLimit( target, ((limit instanceof BlockLimit) ? ((BlockLimit) limit).getMaterial() : null)); colour = reachedLimit ? Colors.Red : Colors.Green; } if (limit instanceof DefaultLimit) { String currentProtected = target != null ? (Integer.toString(limit.getProtectionCount(target, null)) + "/") : ""; sender.sendMessage("Default: " + colour + currentProtected + stringLimit); } else if (limit instanceof BlockLimit) { BlockLimit blockLimit = (BlockLimit) limit; String currentProtected = target != null ? (Integer.toString(limit.getProtectionCount(target, blockLimit.getMaterial())) + "/") : ""; sender.sendMessage( lwc.materialToString(blockLimit.getMaterial()) + ": " + colour + currentProtected + stringLimit); } else { sender.sendMessage(limit.getClass().getSimpleName() + ": " + Colors.Yellow + stringLimit); } } } /** * Checks if a player has reached their protection limit * * @param player * @param material the material type the player has interacted with * @return */ public boolean hasReachedLimit(Player player, Material material) { LWC lwc = LWC.getInstance(); Limit limit = getEffectiveLimit(player, material); // if they don't have a limit it's not possible to reach it ^^ // ... but if it's null, what the hell did the server owner do? if (limit == null) { return false; } // Get the effective limit placed upon them int neverPassThisNumber = limit.getLimit(); // get the amount of protections the player has int protections = limit.getProtectionCount(player, material); return protections >= neverPassThisNumber; } /** * Gets the list of limits that may apply to the player. For group limits, it uses the highest one * found. * * @param player * @return */ public List<Limit> getPlayerLimits(Player player) { LWC lwc = LWC.getInstance(); List<Limit> limits = new LinkedList<Limit>(); // get all of their own limits String playerName = player.getName().toLowerCase(); if (playerLimits.containsKey(playerName)) { limits.addAll(playerLimits.get(playerName)); } // Look over the group limits for (String group : lwc.getPermissions().getGroups(player)) { if (groupLimits.containsKey(group)) { for (Limit limit : groupLimits.get(group)) { // try to match one already inside what we found Limit matched = findLimit(limits, limit); if (matched != null) { // Is our limit better? if (limit.getLimit() > matched.getLimit()) { limits.remove(matched); limits.add(limit); } } else { limits.add(limit); } } } } // Look at the default limits for (Limit limit : defaultLimits) { // try to match one already inside what we found Limit matched = findLimit(limits, limit); if (matched == null) { limits.add(limit); } } return limits; } /** * Get the player's effective limit that should take precedence * * @param player * @param material * @return */ public Limit getEffectiveLimit(Player player, Material material) { return getEffectiveLimit(getPlayerLimits(player), material); } /** * Gets an immutable list of the default limits * * @return */ public List<Limit> getDefaultLimits() { return Collections.unmodifiableList(defaultLimits); } /** * Gets an unmodiable map of the player limits * * @return */ public Map<String, List<Limit>> getPlayerLimits() { return Collections.unmodifiableMap(playerLimits); } /** * Gets an unmodiable map of the group limits * * @return */ public Map<String, List<Limit>> getGroupLimits() { return Collections.unmodifiableMap(groupLimits); } /** * Orders the limits, putting the default limit at the top of the list * * @param limits * @return */ private List<Limit> orderLimits(List<Limit> limits) { Limit defaultLimit = null; // Locate the default limit for (Limit limit : limits) { if (limit instanceof DefaultLimit) { defaultLimit = limit; break; } } // remove it limits.remove(defaultLimit); // readd it at the head limits.add(0, defaultLimit); return limits; } /** * Gets the material's effective limit that should take precedence * * @param limits * @param material * @return Limit object if one is found otherwise NULL */ private Limit getEffectiveLimit(List<Limit> limits, Material material) { if (limits == null) { return null; } // Temporary storage to use if the default is found so we save time if no override was found Limit defaultLimit = null; for (Limit limit : limits) { // Record the default limit if found if (limit instanceof DefaultLimit) { defaultLimit = limit; } else if (limit instanceof BlockLimit) { BlockLimit blockLimit = (BlockLimit) limit; // If it's the appropriate material we can return immediately if (blockLimit.getMaterial() == material) { return blockLimit; } } } return defaultLimit; } /** Load all of the limits */ private void loadLimits() { // make sure we're working on a clean slate defaultLimits.clear(); playerLimits.clear(); groupLimits.clear(); // add the default limits defaultLimits.addAll(findLimits("defaults")); // add all of the player limits try { for (String player : configuration.getKeys("players")) { playerLimits.put(player.toLowerCase(), findLimits("players." + player)); } } catch (NullPointerException e) { } // add all of the group limits try { for (String group : configuration.getKeys("groups")) { groupLimits.put(group, findLimits("groups." + group)); } } catch (NullPointerException e) { } } /** * Find and match all of the limits in a given list of nodes for the config * * @param node * @return */ private List<Limit> findLimits(String node) { List<Limit> limits = new LinkedList<Limit>(); List<String> keys = configuration.getKeys(node); for (String key : keys) { String value = configuration.getString(node + "." + key); int limit; if (value.equalsIgnoreCase("unlimited")) { limit = UNLIMITED; } else { limit = Integer.parseInt(value); } // Match default if (key.equalsIgnoreCase("default")) { limits.add(new DefaultLimit(limit)); } else { Material material = Material.getMaterial(key.toUpperCase()); limits.add(new BlockLimit(material, limit)); } } // Order it orderLimits(limits); return limits; } /** * Find a limit in the list of limits that equals the given limit in class; The LIMIT itself does * not need to be equal; only the class & if it's a BlockLimit, the material must equal * * @param limits * @param compare * @return */ private Limit findLimit(List<Limit> limits, Limit compare) { for (Limit limit : limits) { if (limit != null && limit.getClass().isInstance(compare)) { if (limit instanceof BlockLimit) { BlockLimit cmp1 = (BlockLimit) limit; BlockLimit cmp2 = (BlockLimit) compare; if (cmp1.getMaterial() == cmp2.getMaterial()) { return limit; } } else { return limit; } } } return null; } }