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

The following examples show how to use com.griefcraft.model.Protection#getBlockId() . 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: RestorableProtection.java    From Modern-LWC with MIT License 6 votes vote down vote up
/**
 * Wrap a protection object around a RestorableProtection object
 *
 * @param protection
 * @return
 */
public static RestorableProtection wrapProtection(Protection protection) {
    if (protection == null) {
        return null;
    }

    try {
        RestorableProtection rprotection = new RestorableProtection();
        rprotection.id = protection.getId();
        rprotection.protectionType = protection.getType().ordinal();
        rprotection.blockId = protection.getBlockId();
        rprotection.owner = protection.getOwner();
        rprotection.world = protection.getWorld();
        rprotection.x = protection.getX();
        rprotection.y = protection.getY();
        rprotection.z = protection.getZ();
        rprotection.data = protection.getData().toJSONString();
        rprotection.created = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(protection.getCreation()).getTime() / 1000;
        rprotection.updated = protection.getLastAccessed();

        return rprotection;
    } catch (ParseException e) {
        System.out.println("Failed to wrap protection: " + protection + " " + e.getMessage());
        return null;
    }
}
 
Example 2
Source File: ProtectionFinder.java    From Modern-LWC with MIT License 4 votes vote down vote up
/**
 * Try and load a protection for a given block. If succeded, cache it locally
 *
 * @param block
 * @param noAutoCache if a match is found, don't cache it to be the protection we use
 * @return
 */
protected Result tryLoadProtection(BlockState block, boolean noAutoCache) {
    if (matchedProtection != null) {
        return Result.E_FOUND;
    }

    LWC lwc = LWC.getInstance();
    ProtectionCache cache = lwc.getProtectionCache();

    // Check the cache
    if ((matchedProtection = cache.getProtection(block)) != null) {
        searched = true;
        if (matchedProtection.getProtectionFinder() == null) {
            fullMatchBlocks();
            matchedProtection.setProtectionFinder(this);
            cache.addProtection(matchedProtection);
        }
        return Result.E_FOUND;
    }

    // Manual intervention is required
    if (block.getType() == Material.REDSTONE_WIRE || block.getType() == Material.REDSTONE_WALL_TORCH ||
            block.getType() == Material.REDSTONE_TORCH) {
        return Result.E_ABORT;
    }

    // don't bother trying to load it if it is not protectable
    if (!lwc.isProtectable(block)) {
        return Result.E_NOT_FOUND;
    }

    // Null-check
    if (block.getWorld() == null) {
        return Result.E_NOT_FOUND;
    }

    Protection protection = lwc.getPhysicalDatabase().loadProtection(block.getWorld().getName(), block.getX(), block.getY(), block.getZ());

    if (protection != null) {
        if (protection.getProtectionFinder() == null) {
            protection.setProtectionFinder(this);
            fullMatchBlocks();
            cache.addProtection(matchedProtection);
        }

        // ensure it's the right block
        if (protection.getBlockId() > 0) {
            if (protection.isBlockInWorld()) {
                if (noAutoCache) {
                    return Result.E_FOUND;
                }

                this.matchedProtection = protection;
                searched = true;
            } else {
                // Removing orrupted protection
                protection.remove();
            }
        }
    }

    return this.matchedProtection != null ? Result.E_FOUND : Result.E_NOT_FOUND;
}