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

The following examples show how to use com.griefcraft.model.Protection#isOwner() . 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: 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 3
Source File: DestroyModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onDestroyProtection(LWCProtectionDestroyEvent event) {

    if (event.isCancelled()) {
        return;
    }

    if (event.getMethod() != LWCProtectionDestroyEvent.Method.BLOCK_DESTRUCTION &&
            event.getMethod() != LWCProtectionDestroyEvent.Method.ENTITY_DESTRUCTION) {
        return;
    }

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

    boolean isOwner = protection.isOwner(player);

    if (isOwner) {
        if (!lwc.isAdmin(player) && Boolean.parseBoolean(lwc.resolveProtectionConfiguration(protection.getBlock(), "readonly-remove"))) {
            lwc.sendLocale(player, "protection.accessdenied");
            event.setCancelled(true);
            return;
        }

        // bind the player of destroyed the protection
        // We don't need to save the history we modify because it will be saved anyway immediately after this
        for (History history : protection.getRelatedHistory(History.Type.TRANSACTION)) {
            if (history.getStatus() != History.Status.ACTIVE) {
                continue;
            }

            history.addMetaData("destroyer=" + player.getName());
            history.addMetaData("destroyerTime=" + System.currentTimeMillis() / 1000L);
        }

        protection.remove();

        if (!Boolean.parseBoolean(lwc.resolveProtectionConfiguration(protection.getBlock(), "quiet"))) {
            BlockCache blockCache = BlockCache.getInstance();
            lwc.sendLocaleToActionBar(player, "protection.unregistered", "block",
                    LWC.materialToString(blockCache.getBlockType(protection.getBlockId())));
        }
        return;
    }

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

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

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

    /* Due to treating entities as blocks some issues are triggered
     *  such as when clicking an armor stand the block is air.
     *  I feel there is something better to be done here but this works */
    if (protection.getBlock().getType() == Material.AIR) {
        if (!protection.toString().contains("ARMOR_STAND")) {
            return;
        }
    }

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

    if (lwc.hasAdminPermission(player, "lwc.admin.remove") || protection.isOwner(player)) {
        LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection,
                LWCProtectionDestroyEvent.Method.COMMAND, true, true);
        lwc.getModuleLoader().dispatchEvent(evt);

        if (!evt.isCancelled()) {
            // bind the player of destroyed the protection
            // We don't need to save the history we modify because it will
            // be saved anyway immediately after this
            for (History history : protection.getRelatedHistory(History.Type.TRANSACTION)) {
                if (history.getStatus() != History.Status.ACTIVE) {
                    continue;
                }

                history.addMetaData("destroyer=" + player.getName());
                history.addMetaData("destroyerTime=" + System.currentTimeMillis() / 1000L);
            }

            protection.remove();
            if (protection.getBlock() instanceof EntityBlock) {
                lwc.sendLocaleToActionBar(player, "protection.interact.remove.finalize", "block",
                        EntityBlock.getEntity().getType().name());
            } else {
                lwc.sendLocaleToActionBar(player, "protection.interact.remove.finalize", "block",
                        !protection.toString().contains("ARMOR_STAND") ?
                                LWC.materialToString(protection.getBlock()) : "ARMOR_STAND");
            }
        }

        lwc.removeModes(player);
    } else {
        if (protection.getBlock() instanceof EntityBlock) {
            lwc.sendLocale(player, "protection.interact.error.notowner", "block",
                    EntityBlock.getEntity().getType().name());
        } else {
            lwc.sendLocale(player, "protection.interact.error.notowner", "block",
                    LWC.materialToString(protection.getBlock()));
        }
        lwc.removeModes(player);
    }
}
 
Example 5
Source File: FreeModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onEntityInteractProtection(LWCProtectionInteractEntityEvent event) {
    if (event.getResult() != Result.DEFAULT) {
        return;
    }

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

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

    if (protection.getBlock().getType() != Material.AIR) {
        return;
    }

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

    if (lwc.hasAdminPermission(player, "lwc.admin.remove") || protection.isOwner(player)) {
        LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection,
                LWCProtectionDestroyEvent.Method.COMMAND, true, true);
        lwc.getModuleLoader().dispatchEvent(evt);

        if (!evt.isCancelled()) {
            // bind the player of destroyed the protection
            // We don't need to save the history we modify because it will
            // be saved anyway immediately after this
            for (History history : protection.getRelatedHistory(History.Type.TRANSACTION)) {
                if (history.getStatus() != History.Status.ACTIVE) {
                    continue;
                }

                history.addMetaData("destroyer=" + player.getName());
                history.addMetaData("destroyerTime=" + System.currentTimeMillis() / 1000L);
            }

            protection.remove();
            lwc.sendLocaleToActionBar(player, "protection.interact.remove.finalize", "block",
                    event.getEvent().getRightClicked().getType().name());
        }

        lwc.removeModes(player);
    } else {
        lwc.sendLocale(player, "protection.interact.error.notowner", "block",
                event.getEvent().getRightClicked().getType().name());
        lwc.removeModes(player);
    }
}
 
Example 6
Source File: LWC.java    From Modern-LWC with MIT License 4 votes vote down vote up
/**
 * Check if a player has the ability to administrate a protection
 *
 * @param player
 * @param protection
 * @return
 */
public boolean canAdminProtection(Player player, Protection protection) {
    if (protection == null || player == null) {
        return true;
    }

    if (isAdmin(player)) {
        return true;
    }

    // Their access level
    Permission.Access access = Permission.Access.NONE;

    switch (protection.getType()) {
        case PUBLIC:
            if (protection.isOwner(player)) {
                return true;
            }

            break;

        case PASSWORD:
            if (protection.isOwner(player) && wrapPlayer(player).getAccessibleProtections().contains(protection)) {
                return true;
            }

            break;

        case PRIVATE:
        case DONATION:
            if (protection.isOwner(player)) {
                return true;
            }

            if (protection.getAccess(player.getUniqueId().toString(),
                    Permission.Type.PLAYER) == Permission.Access.ADMIN) {
                return true;
            }

            if (protection.getAccess(player.getName(), Permission.Type.PLAYER) == Permission.Access.ADMIN) {
                return true;
            }

            for (String groupName : permissions.getGroups(player)) {
                if (protection.getAccess(groupName, Permission.Type.GROUP) == Permission.Access.ADMIN) {
                    return true;
                }
            }

            break;
        default:
            break;
    }

    // call the canAccessProtection hook
    LWCAccessEvent event = new LWCAccessEvent(player, protection, access);
    moduleLoader.dispatchEvent(event);

    return event.getAccess() == Permission.Access.ADMIN;
}
 
Example 7
Source File: LWC.java    From Modern-LWC with MIT License 4 votes vote down vote up
/**
 * Check if a player has the ability to access a protection
 *
 * @param player
 * @param protection
 * @return
 */
@SuppressWarnings("deprecation")
public boolean canAccessProtection(Player player, Protection protection) {
    if (protection == null || player == null) {
        return true;
    }

    if (isAdmin(player)) {
        return true;
    }

    if (isMod(player)) {
        Player protectionOwner = protection.getBukkitOwner();

        if (protectionOwner == null) {
            return true;
        }

        if (!isAdmin(protectionOwner)) {
            return true;
        }
    }

    // Their access level
    Permission.Access access = Permission.Access.NONE;

    switch (protection.getType()) {
        case PUBLIC:
        case DONATION:
            return true;

        case PASSWORD:
            if (wrapPlayer(player).getAccessibleProtections().contains(protection)) {
                return true;
            }

            break;

        case PRIVATE:
            if (protection.isOwner(player)) {
                return true;
            }

            if (protection.getAccess(player.getUniqueId().toString(), Permission.Type.PLAYER)
                    .ordinal() >= Permission.Access.PLAYER.ordinal()) {
                return true;
            }

            if (protection.getAccess(player.getName(), Permission.Type.PLAYER).ordinal() >= Permission.Access.PLAYER
                    .ordinal()) {
                return true;
            }

            // Check for item keys
            for (Permission permission : protection.getPermissions()) {
                if (permission.getType() != Permission.Type.ITEM) {
                    continue;
                }

                // Get the item they need to have
                int item = Integer.parseInt(permission.getName());

                // Are they wielding it?
                BlockCache blockCache = BlockCache.getInstance();
                if (blockCache.getBlockId(player.getItemInHand().getType()) == item) {
                    return true;
                }
            }

            for (String groupName : permissions.getGroups(player)) {
                if (protection.getAccess(groupName, Permission.Type.GROUP).ordinal() >= Permission.Access.PLAYER
                        .ordinal()) {
                    return true;
                }
            }

            break;
        default:
            break;
    }

    // call the canAccessProtection hook
    LWCAccessEvent event = new LWCAccessEvent(player, protection, access);
    moduleLoader.dispatchEvent(event);

    return event.getAccess() == Permission.Access.PLAYER || event.getAccess() == Permission.Access.ADMIN;
}
 
Example 8
Source File: LWCBlockListener.java    From Modern-LWC with MIT License 4 votes vote down vote up
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
    if (!LWC.ENABLED || event.isCancelled()) {
        return;
    }

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

    boolean ignoreBlockDestruction = Boolean
            .parseBoolean(lwc.resolveProtectionConfiguration(block, "ignoreBlockDestruction"));

    if (ignoreBlockDestruction) {
        return;
    }

    ProtectionCache cache = lwc.getProtectionCache();
    String cacheKey = cache.cacheKey(block.getLocation());

    // In the event they place a block, remove any known nulls there
    if (cache.isKnownNull(cacheKey)) {
        cache.remove(cacheKey);
    }

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

    if (protection == null) {
        return;
    }

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

    // when destroying a chest, it's possible they are also destroying a
    // double chest
    // in the event they're trying to destroy a double chest, we should just
    // move
    // the protection to the chest that is not destroyed, if it is not that
    // one already.
    if (protection.isOwner(player) && DoubleChestMatcher.PROTECTABLES_CHESTS.contains(block.getType())) {
        Block doubleChest = lwc.findAdjacentDoubleChest(block);

        if (doubleChest != null) {
            // if they destroyed the protected block we want to move it aye?
            if (lwc.blockEquals(protection.getBlock(), block)) {
                // correct the block
                BlockCache blockCache = BlockCache.getInstance();
                protection.setBlockId(blockCache.getBlockId(doubleChest));
                protection.setX(doubleChest.getX());
                protection.setY(doubleChest.getY());
                protection.setZ(doubleChest.getZ());
                protection.saveNow();
            }

            // Repair the cache
            protection.radiusRemoveCache();

            if (protection.getProtectionFinder() != null) {
                protection.getProtectionFinder().removeBlock(block.getState());
            }

            lwc.getProtectionCache().addProtection(protection);

            return;
        }
    }

    try {
        LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection,
                LWCProtectionDestroyEvent.Method.BLOCK_DESTRUCTION, canAccess, canAdmin);
        lwc.getModuleLoader().dispatchEvent(evt);

        if (evt.isCancelled() || !canAccess) {
            event.setCancelled(true);
        }
    } catch (Exception e) {
        event.setCancelled(true);
        lwc.sendLocale(player, "protection.internalerror", "id", "BLOCK_BREAK");
        e.printStackTrace();
    }
}