com.griefcraft.model.Protection Java Examples

The following examples show how to use com.griefcraft.model.Protection. 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: CreateModule.java    From Modern-LWC with MIT License 6 votes vote down vote up
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() != Result.DEFAULT) {
        return;
    }

    if (!event.hasAction("create")) {
        return;
    }

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

    BlockCache blockCache = BlockCache.getInstance();
    if (protection.isOwner(player)) {
        lwc.sendLocale(player, "protection.interact.error.alreadyregistered", "block",
                LWC.materialToString(blockCache.getBlockType(protection.getBlockId())));
    } else {
        lwc.sendLocale(player, "protection.interact.error.notowner", "block",
                LWC.materialToString(blockCache.getBlockType(protection.getBlockId())));
    }

    lwc.removeModes(player);
    event.setResult(Result.CANCEL);
}
 
Example #2
Source File: HistoryModule.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * History tool
 */
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() != DEFAULT) {
        return;
    }

    if (!event.hasAction("history")) {
        return;
    }

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

    historyTool(lwc, player, protection.getX(), protection.getY(), protection.getZ());
    event.setResult(Result.CANCEL);
    lwc.removeModes(player);
}
 
Example #3
Source File: LWCEntityListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler
public void entityBreakDoor(EntityBreakDoorEvent event) {
    Block block = event.getBlock();

    // See if there is a protection there
    Protection protection = plugin.getLWC().findProtection(block.getLocation());

    if (protection != null) {
        // protections.allowEntityBreakDoor
        boolean allowEntityBreakDoor = Boolean
                .parseBoolean(plugin.getLWC().resolveProtectionConfiguration(block, "allowEntityBreakDoor"));

        if (!allowEntityBreakDoor) {
            event.setCancelled(true);
        }
    }
}
 
Example #4
Source File: PhysDB.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Load protections using a specific type
 *
 * @param type
 * @return the Protection object
 */
public List<Protection> loadProtectionsUsingType(Protection.Type type) {
    PreparedStatement statement;
    try {
        statement = prepare(
                "SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM "
                        + prefix + "protections WHERE type = ?");
        statement.setInt(1, type.ordinal());

        return resolveProtections(statement);
    } catch (SQLException e) {
        printException(e);
    }

    return new ArrayList<Protection>();
}
 
Example #5
Source File: PhysDB.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Resolve every protection from a result set
 *
 * @param set
 * @return
 */
private List<Protection> resolveProtections(ResultSet set) {
    List<Protection> protections = new ArrayList<Protection>();

    try {
        while (set.next()) {
            Protection protection = resolveProtection(set);

            if (protection != null) {
                protections.add(protection);
            }
        }
    } catch (SQLException e) {
        printException(e);
    }

    return protections;
}
 
Example #6
Source File: LWCPlayerListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler
public void onEntityDamage(EntityDamageEvent e) {
    if (e instanceof EntityDamageByEntityEvent
            && !(e.getCause() == DamageCause.BLOCK_EXPLOSION || e.getCause() == DamageCause.ENTITY_EXPLOSION))
        return;
    Entity entity = e.getEntity();
    if (plugin.getLWC().isProtectable(e.getEntity().getType())) {
        int A = 50000 + entity.getUniqueId().hashCode();
        LWC lwc = LWC.getInstance();
        Protection protection = lwc.getPhysicalDatabase().loadProtection(entity.getWorld().getName(), A, A, A);
        if (protection != null) {
            if (e.getCause() != DamageCause.CONTACT)
                e.setCancelled(true);
        }
    }
}
 
Example #7
Source File: ProtectionFinder.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Load the protection for the calculated protectables.
 * Returns NULL if no protection was found.
 *
 * @param noAutoCache if a match is found, don't cache it to be the protection we use
 * @return
 */
public Protection loadProtection(boolean noAutoCache) {
    // Do we have a result already cached?
    if (searched) {
        return matchedProtection;
    }

    // Calculate the protectables
    calculateProtectables();
    searched = true;

    for (BlockState block : protectables) {
        if (tryLoadProtection(block, noAutoCache) == Result.E_FOUND) {
            return matchedProtection;
        }
    }

    return null;
}
 
Example #8
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 #9
Source File: LWCPlayerListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler
public void minecartBreak(VehicleDestroyEvent e) {
    Entity entity = e.getVehicle();
    if (plugin.getLWC().isProtectable(e.getVehicle().getType())) {
        int A = 50000 + entity.getUniqueId().hashCode();
        LWC lwc = LWC.getInstance();
        Protection protection = lwc.getPhysicalDatabase().loadProtection(entity.getWorld().getName(), A, A, A);
        if ((((entity instanceof StorageMinecart)) || ((entity instanceof HopperMinecart)))
                && (protection != null)) {
            if (e.getAttacker() instanceof Projectile) {
                e.setCancelled(true);
            }
            Player p = (Player) e.getAttacker();
            boolean canAccess = lwc.canAccessProtection(p, protection);
            if (canAccess) {
                protection.remove();
                protection.removeAllPermissions();
                protection.removeCache();
                return;
            }
            e.setCancelled(true);
        }
    }
}
 
Example #10
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 #11
Source File: PhysDB.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Load all protections in the coordinate ranges
 *
 * @param world
 * @param x1
 * @param x2
 * @param y1
 * @param y2
 * @param z1
 * @param z2
 * @return list of Protection objects found
 */
public List<Protection> loadProtections(String world, int x1, int x2, int y1, int y2, int z1, int z2) {
    try {
        PreparedStatement statement = prepare(
                "SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM "
                        + prefix
                        + "protections WHERE world = ? AND x >= ? AND x <= ? AND y >= ? AND y <= ? AND z >= ? AND z <= ?");
        statement.setString(1, world);
        statement.setInt(2, x1);
        statement.setInt(3, x2);
        statement.setInt(4, y1);
        statement.setInt(5, y2);
        statement.setInt(6, z1);
        statement.setInt(7, z2);

        return resolveProtections(statement);
    } catch (Exception e) {
        printException(e);
    }

    return new ArrayList<Protection>();
}
 
Example #12
Source File: LWCBlockListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityChangeBlock(EntityChangeBlockEvent event) {
    if (!LWC.ENABLED) {
        return;
    }

    LWC lwc = LWC.getInstance();

    Block block = event.getBlock();
    if (!lwc.isProtectable(block)) {
        return;
    }

    Protection protection = lwc.findProtection(block);
    if (protection != null) {
        event.setCancelled(true);
    }
}
 
Example #13
Source File: PhysDB.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Load protections by a player
 *
 * @param player
 * @param start
 * @param count
 * @return
 */
public List<Protection> loadProtectionsByPlayer(String player, int start, int count) {
    List<Protection> protections = new ArrayList<Protection>();

    UUID uuid = UUIDRegistry.getUUID(player);

    try {
        PreparedStatement statement = prepare(
                "SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM "
                        + prefix + "protections WHERE owner = ? ORDER BY id DESC limit ?,?");
        statement.setString(1, uuid != null ? uuid.toString() : player);
        statement.setInt(2, start);
        statement.setInt(3, count);

        return resolveProtections(statement);
    } catch (Exception e) {
        printException(e);
    }

    return protections;
}
 
Example #14
Source File: AdminForceOwner.java    From Modern-LWC with MIT License 6 votes vote down vote up
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() != Result.DEFAULT) {
        return;
    }

    if (!event.hasAction("forceowner")) {
        return;
    }

    LWC lwc = event.getLWC();
    Protection protection = event.getProtection();
    LWCPlayer player = lwc.wrapPlayer(event.getPlayer());

    Action action = player.getAction("forceowner");
    String newOwner = action.getData();

    protection.setOwner(newOwner);
    protection.save();

    lwc.sendLocale(player, "protection.interact.forceowner.finalize", "player", protection.getFormattedOwnerPlayerName());
    lwc.removeModes(player);
    event.setResult(Result.CANCEL);

}
 
Example #15
Source File: LWCBlockListener.java    From Modern-LWC with MIT License 6 votes vote down vote up
@EventHandler
public void onSignChange(SignChangeEvent event) {
    if (!LWC.ENABLED || event.isCancelled()) {
        return;
    }

    LWC lwc = plugin.getLWC();
    Block block = event.getBlock();
    Player player = event.getPlayer();

    if (block == null) {
        return;
    }

    Protection protection = lwc.findProtection(block.getLocation());

    if (protection == null) {
        return;
    }

    boolean canAccess = lwc.canAccessProtection(player, protection);

    if (!canAccess) {
        event.setCancelled(true);
    }
}
 
Example #16
Source File: LWC.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Find a protection that is adjacent to another block on any of the block's 6
 * sides
 *
 * @param block
 * @param ignore
 * @return
 */
public List<Protection> findAdjacentProtectionsOnAllSides(Block block, Block... ignore) {
    BlockFace[] faces = new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST,
            BlockFace.UP, BlockFace.DOWN};
    List<Block> ignoreList = Arrays.asList(ignore);
    List<Protection> found = new ArrayList<Protection>();

    for (BlockFace face : faces) {
        Protection protection;
        Block adjacentBlock = block.getRelative(face);

        if (!ignoreList.contains(adjacentBlock.getLocation())
                && (protection = findProtection(adjacentBlock.getLocation())) != null) {
            found.add(protection);
        }
    }

    return found;
}
 
Example #17
Source File: PhysDB.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Save a protection to the database
 *
 * @param protection
 */
public void saveProtection(Protection protection) {
    try {
        PreparedStatement statement = prepare("REPLACE INTO " + prefix
                + "protections (id, type, blockId, world, data, owner, password, x, y, z, date, last_accessed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

        statement.setInt(1, protection.getId());
        statement.setInt(2, protection.getType().ordinal());
        statement.setInt(3, protection.getBlockId());
        statement.setString(4, protection.getWorld());
        statement.setString(5, protection.getData().toJSONString());
        statement.setString(6, protection.getOwner());
        statement.setString(7, protection.getPassword());
        statement.setInt(8, protection.getX());
        statement.setInt(9, protection.getY());
        statement.setInt(10, protection.getZ());
        statement.setString(11, protection.getCreation());
        statement.setLong(12, protection.getLastAccessed());

        statement.executeUpdate();
    } catch (SQLException e) {
        printException(e);
    }
}
 
Example #18
Source File: LWCEntityListener.java    From Modern-LWC with MIT License 5 votes vote down vote up
@EventHandler
public void entityInteract(EntityInteractEvent event) {
    Block block = event.getBlock();

    Protection protection = plugin.getLWC().findProtection(block.getLocation());

    if (protection != null) {
        boolean allowEntityInteract = Boolean
                .parseBoolean(plugin.getLWC().resolveProtectionConfiguration(block, "allowEntityInteract"));

        if (!allowEntityInteract) {
            event.setCancelled(true);
        }
    }
}
 
Example #19
Source File: PhysDB.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Fill the protection cache as much as possible with protections Caches the
 * most recent protections
 */
public void precache() {
    LWC lwc = LWC.getInstance();
    ProtectionCache cache = lwc.getProtectionCache();

    // clear the cache incase we're working on a dirty cache
    cache.clear();

    int precacheSize = lwc.getConfiguration().getInt("core.precache", -1);

    if (precacheSize == -1) {
        precacheSize = lwc.getConfiguration().getInt("core.cacheSize", 10000);
    }
    try {
        statement = prepare(
                "SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM "
                        + prefix + "protections ORDER BY id DESC LIMIT ?");
        statement.setInt(1, precacheSize);
        statement.setFetchSize(10);

        // scrape the protections from the result set now
        List<Protection> protections = resolveProtections(statement);

        // throw all of the protections in
        for (Protection protection : protections) {
            cache.addProtection(protection);
        }

    } catch (SQLException e) {
        printException(e);
    }
}
 
Example #20
Source File: ModifyModule.java    From Modern-LWC with MIT License 5 votes vote down vote up
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() != Result.DEFAULT) {
        return;
    }

    if (!event.hasAction("modify")) {
        return;
    }

    LWC lwc = event.getLWC();
    Protection protection = event.getProtection();
    LWCPlayer player = lwc.wrapPlayer(event.getPlayer());
    Player bPlayer = event.getPlayer();
    event.setResult(Result.CANCEL);

    if (!lwc.isAdmin(bPlayer) && Boolean.parseBoolean(lwc.resolveProtectionConfiguration(protection.getBlock(), "readonly-modify"))) {
        lwc.sendLocale(player, "protection.accessdenied");
        return;
    }

    if (lwc.canAdminProtection(player.getBukkitPlayer(), protection)) {
        Action action = player.getAction("modify");

        String data = action.getData();
        String[] rights = new String[0];

        if (data.length() > 0) {
            rights = data.split(" ");
        }

        lwc.removeModes(player);
        lwc.processRightsModifications(player, protection, rights);
    } else {
        lwc.sendLocale(player, "protection.interact.error.notowner", "block",
                LWC.materialToString(BlockCache.getInstance().getBlockType(protection.getBlockId())));
        lwc.removeModes(player);
    }

}
 
Example #21
Source File: PhysDB.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Load all protections (use sparingly !!)
 *
 * @return
 */
public List<Protection> loadProtections() {
    try {
        PreparedStatement statement = prepare(
                "SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM "
                        + prefix + "protections");
        return resolveProtections(statement);
    } catch (Exception e) {
        printException(e);
    }

    return new ArrayList<Protection>();
}
 
Example #22
Source File: PhysDB.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Load a protection at the given coordinates
 *
 * @param x
 * @param y
 * @param z
 * @param ignoreProtectionCount
 * @return the Protection object
 */
private Protection loadProtection(String worldName, int x, int y, int z, boolean ignoreProtectionCount) {
    // the unique key to use in the cache
    String cacheKey = worldName + ":" + x + ":" + y + ":" + z;

    // the protection cache
    ProtectionCache cache = LWC.getInstance().getProtectionCache();

    // check if the protection is already cached
    Protection cached = cache.getProtection(cacheKey);
    if (cached != null) {
        return cached;
    }

    // Is it possible that there are protections in the cache?
    if (!ignoreProtectionCount && hasAllProtectionsCached()) {
        return null; // nothing was in the cache, nothing assumed to be in the database
    }
    try {
        statement = prepare(
                "SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM "
                        + prefix + "protections WHERE x = ? AND y = ? AND z = ? AND world = ?");
        statement.setInt(1, x);
        statement.setInt(2, y);
        statement.setInt(3, z);
        statement.setString(4, worldName);

        Protection protection = resolveProtection(statement);

        if (protection != null) {
            // cache the protection
            cache.addProtection(protection);
        }
        return protection;
    } catch (SQLException e) {
        printException(e);
    }

    return null;
}
 
Example #23
Source File: LWCPlayerListener.java    From Modern-LWC with MIT License 5 votes vote down vote up
@EventHandler
public void onDeath(EntityDeathEvent e) {
    Entity entity = e.getEntity();
    if (plugin.getLWC().isProtectable(e.getEntity().getType())) {
        int A = 50000 + entity.getUniqueId().hashCode();
        Player player = e.getEntity().getKiller();
        LWC lwc = LWC.getInstance();
        Protection protection = lwc.getPhysicalDatabase().loadProtection(entity.getWorld().getName(), A, A, A);
        if (protection != null) {
            boolean canAccess = lwc.canAccessProtection(player, protection);
            boolean canAdmin = lwc.canAdminProtection(player, protection);
            try {
                if (player != null) {
                    LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection,
                            LWCProtectionDestroyEvent.Method.ENTITY_DESTRUCTION, canAccess, canAdmin);
                    lwc.getModuleLoader().dispatchEvent(evt);
                } else {
                    protection.remove();
                    protection.removeAllPermissions();
                    protection.removeCache();
                }
            } catch (Exception ex) {
                if (player != null) {
                    lwc.sendLocale(player, "protection.internalerror", "id", "ENTITY_DEATH");
                }
                lwc.sendLocale(Bukkit.getServer().getConsoleSender(), "protection.internalerror", "id",
                        "ENTITY_DEATH");
                ex.printStackTrace();
            }
        }
    }
}
 
Example #24
Source File: LWCBlockListener.java    From Modern-LWC with MIT License 5 votes vote down vote up
@EventHandler
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
    if ((!LWC.ENABLED) || (event.isCancelled())) {
        return;
    }
    LWC lwc = this.plugin.getLWC();
    for (Block block : event.getBlocks()) {
        Protection protection = lwc.findProtection(block);
        if (protection != null) {
            event.setCancelled(true);
            return;
        }
    }
}
 
Example #25
Source File: LWC114Listener.java    From Modern-LWC with MIT License 5 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onPlayerTakeLecternBook(PlayerTakeLecternBookEvent event) {
    LWC lwc = LWC.getInstance();
    Protection protection = lwc.findProtection(event.getLectern());
    if (protection == null || protection.isOwner(event.getPlayer()) || protection.getType() == Protection.Type.PUBLIC) {
        return;
    }
    if (protection.getAccess(event.getPlayer().getUniqueId().toString(), Permission.Type.PLAYER) == Permission.Access.NONE) {
        event.setCancelled(true);
    }
}
 
Example #26
Source File: ProtectionCache.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Remove the protection from the cache
 *
 * @param protection
 */
public void removeProtection(Protection protection) {
    counter.increment("removeProtection");

    references.remove(protection);
    byId.remove(protection.getId());

    if (protection.getProtectionFinder() != null) {
        for (BlockState state : protection.getProtectionFinder()
                .getBlocks()) {
            remove(cacheKey(state.getLocation()));
        }
    }
}
 
Example #27
Source File: ProtectionCache.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Cache a protection
 *
 * @param protection
 */
public void addProtection(Protection protection) {
    if (protection == null) {
        return;
    }

    counter.increment("addProtection");

    // Add the hard reference
    references.put(protection, null);

    // Add weak references which are used to lookup protections
    byCacheKey.put(protection.getCacheKey(), protection);
    byId.put(protection.getId(), protection);

    // get the protection's finder if it was found via that
    if (protection.getProtectionFinder() != null) {
        Block protectedBlock = protection.getBlock();

        for (BlockState state : protection.getProtectionFinder()
                .getBlocks()) {
            if (!protectedBlock.equals(state.getBlock())) {
                String cacheKey = cacheKey(state.getLocation());
                byKnownBlock.put(cacheKey, protection);
            }
        }
    }
}
 
Example #28
Source File: LWC.java    From Modern-LWC with MIT License 5 votes vote down vote up
public Protection findProtection(World world, int x, int y, int z) {
    if (world == null) {
        return null;
    }

    return findProtection(new Location(world, x, y, z));
}
 
Example #29
Source File: LWCProtectionEvent.java    From Modern-LWC with MIT License 5 votes vote down vote up
public LWCProtectionEvent(ModuleLoader.Event event, Player player, Protection protection, boolean canAccess, boolean canAdmin) {
    super(event, player);

    this.protection = protection;
    this.canAccess = canAccess;
    this.canAdmin = canAdmin;
}
 
Example #30
Source File: LWC.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Find a protection linked to the location
 *
 * @param location
 * @return
 */
public Protection findProtection(Location location) {
    String cacheKey = protectionCache.cacheKey(location);

    if (protectionCache.isKnownNull(cacheKey)) {
        return null;
    }

    Protection protection = protectionCache.getProtection(cacheKey);

    return protection != null ? protection : findProtection(location.getBlock());
}