Search in sources :

Example 1 with LocalPlayer

use of com.sk89q.worldguard.LocalPlayer in project Karma by Rosstail.

the class WGPreps method checkMultipleKarmaFlags.

public double checkMultipleKarmaFlags(Player player) {
    LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
    com.sk89q.worldedit.util.Location location = localPlayer.getLocation();
    RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
    RegionQuery query = container.createQuery();
    if (ThreadLocalRandom.current().nextInt(0, 100) >= checkChangeKarmaFlag(player)) {
        return 0;
    }
    Double value = query.queryValue(location, localPlayer, KARMA_MULTIPLICATION);
    return value == null ? 1 : value;
}
Also used : RegionContainer(com.sk89q.worldguard.protection.regions.RegionContainer) LocalPlayer(com.sk89q.worldguard.LocalPlayer) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery)

Example 2 with LocalPlayer

use of com.sk89q.worldguard.LocalPlayer in project Karma by Rosstail.

the class WGPreps method checkRequiredKarmaFlags.

public boolean checkRequiredKarmaFlags(Player player) {
    double karma = PlayerDataManager.getPlayerDataMap().get(player).getKarma();
    boolean value = true;
    LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
    com.sk89q.worldedit.util.Location location = localPlayer.getLocation();
    RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
    RegionQuery query = container.createQuery();
    boolean hasMinKarma = hasReqFlag(location, localPlayer, query, KARMA_MINIMUM);
    boolean hasMaxKarma = hasReqFlag(location, localPlayer, query, KARMA_MAXIMUM);
    if (hasMinKarma) {
        if (karma < query.queryValue(location, localPlayer, KARMA_MINIMUM)) {
            value = false;
        }
    }
    if (hasMaxKarma) {
        if (karma > query.queryValue(location, localPlayer, KARMA_MAXIMUM)) {
            value = false;
        }
    }
    return value;
}
Also used : RegionContainer(com.sk89q.worldguard.protection.regions.RegionContainer) LocalPlayer(com.sk89q.worldguard.LocalPlayer) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery)

Example 3 with LocalPlayer

use of com.sk89q.worldguard.LocalPlayer in project Karma by Rosstail.

the class WGPreps method checkChangeKarmaFlag.

public int checkChangeKarmaFlag(Player player) {
    LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
    com.sk89q.worldedit.util.Location location = localPlayer.getLocation();
    RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
    RegionQuery query = container.createQuery();
    Integer value = query.queryValue(location, localPlayer, KARMA_CHANGE_CHANCE);
    return value == null ? 100 : value;
}
Also used : RegionContainer(com.sk89q.worldguard.protection.regions.RegionContainer) LocalPlayer(com.sk89q.worldguard.LocalPlayer) RegionQuery(com.sk89q.worldguard.protection.regions.RegionQuery)

Example 4 with LocalPlayer

use of com.sk89q.worldguard.LocalPlayer in project ProtectionStones by espidev.

the class BlockHandler method createActualRegion.

// create the actual WG region for PS region
public static boolean createActualRegion(Player p, Location l, PSProtectBlock blockOptions) {
    // create region
    double bx = l.getX(), by = l.getY(), bz = l.getZ();
    RegionManager rm = WGUtils.getRegionManagerWithPlayer(p);
    LocalPlayer lp = WorldGuardPlugin.inst().wrapPlayer(p);
    String id = WGUtils.createPSID(bx, by, bz);
    // if the region's id already exists, possibly placing block where a region is hidden
    if (rm.hasRegion(id)) {
        PSL.msg(p, PSL.REGION_ALREADY_IN_LOCATION_IS_HIDDEN.msg());
        return false;
    }
    // check for minimum distance between claims by using fake region
    if (blockOptions.distanceBetweenClaims != -1 && !p.hasPermission("protectionstones.superowner")) {
        if (!isFarEnoughFromOtherClaims(blockOptions, p.getWorld(), lp, bx, by, bz)) {
            PSL.msg(p, PSL.REGION_TOO_CLOSE.msg().replace("%num%", "" + blockOptions.distanceBetweenClaims));
            return false;
        }
    }
    // create actual region
    ProtectedRegion region = WGUtils.getDefaultProtectedRegion(blockOptions, WGUtils.parsePSRegionToLocation(id));
    region.getOwners().addPlayer(p.getUniqueId());
    region.setPriority(blockOptions.priority);
    // added to the region manager, be careful in implementing checks
    rm.addRegion(region);
    // check if new region overlaps more powerful region
    if (!blockOptions.allowOverlapUnownedRegions && !p.hasPermission("protectionstones.superowner") && WGUtils.overlapsStrongerRegion(p.getWorld(), region, lp)) {
        rm.removeRegion(id);
        PSL.msg(p, PSL.REGION_OVERLAP.msg());
        return false;
    }
    // add corresponding flags to new region by cloning blockOptions default flags
    HashMap<Flag<?>, Object> flags = new HashMap<>(blockOptions.regionFlags);
    // replace greeting and farewell messages with player name
    FlagHandler.initDefaultFlagPlaceholders(flags, p);
    // set flags
    try {
        region.setFlags(flags);
    } catch (Exception e) {
        ProtectionStones.getPluginLogger().severe(String.format("Region flags have failed to initialize for: %s (%s)", blockOptions.alias, blockOptions.type));
        throw e;
    }
    FlagHandler.initCustomFlagsForPS(region, l, blockOptions);
    // check for player's number of adjacent region groups
    if (ProtectionStones.getInstance().getConfigOptions().regionsMustBeAdjacent) {
        if (MiscUtil.getPermissionNumber(p, "protectionstones.adjacent.", 1) >= 0 && !p.hasPermission("protectionstones.admin")) {
            HashMap<String, ArrayList<String>> adjGroups = WGUtils.getPlayerAdjacentRegionGroups(p, rm);
            int permNum = MiscUtil.getPermissionNumber(p, "protectionstones.adjacent.", 1);
            if (adjGroups.size() > permNum && permNum != -1) {
                PSL.msg(p, PSL.REGION_NOT_ADJACENT.msg());
                rm.removeRegion(id);
                return false;
            }
        }
    }
    // fire event and check if cancelled
    PSCreateEvent event = new PSCreateEvent(PSRegion.fromWGRegion(p.getWorld(), region), p);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        rm.removeRegion(id);
        return false;
    }
    PSL.msg(p, PSL.PROTECTED.msg());
    // hide block if auto hide is enabled
    if (blockOptions.autoHide) {
        PSL.msg(p, PSL.REGION_HIDDEN.msg());
        // run on next tick so placing tile entities don't complain
        Bukkit.getScheduler().runTask(ProtectionStones.getInstance(), () -> l.getBlock().setType(Material.AIR));
    }
    if (blockOptions.startWithTaxAutopay) {
        // set tax auto-pay (even if taxing is not enabled)
        region.setFlag(FlagHandler.PS_TAX_AUTOPAYER, p.getUniqueId().toString());
    }
    // show merge menu
    if (ProtectionStones.getInstance().getConfigOptions().allowMergingRegions && blockOptions.allowMerging && p.hasPermission("protectionstones.merge")) {
        PSRegion r = PSRegion.fromWGRegion(p.getWorld(), region);
        if (r != null)
            playerMergeTask(p, r);
    }
    return true;
}
Also used : PSCreateEvent(dev.espi.protectionstones.event.PSCreateEvent) HashMap(java.util.HashMap) LocalPlayer(com.sk89q.worldguard.LocalPlayer) ArrayList(java.util.ArrayList) Flag(com.sk89q.worldguard.protection.flags.Flag) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager)

Example 5 with LocalPlayer

use of com.sk89q.worldguard.LocalPlayer in project ProtectionStones by espidev.

the class ArgMerge method executeArgument.

@Override
public boolean executeArgument(CommandSender s, String[] args, HashMap<String, String> flags) {
    if (!s.hasPermission("protectionstones.merge"))
        return PSL.msg(s, PSL.NO_PERMISSION_MERGE.msg());
    if (!ProtectionStones.getInstance().getConfigOptions().allowMergingRegions)
        return PSL.msg(s, PSL.MERGE_DISABLED.msg());
    Player p = (Player) s;
    if (args.length == 1) {
        // GUI
        PSRegion r = PSRegion.fromLocationGroup(p.getLocation());
        if (r == null)
            return PSL.msg(s, PSL.NOT_IN_REGION.msg());
        if (r.getTypeOptions() == null) {
            PSL.msg(p, ChatColor.RED + "This region is problematic, and the block type (" + r.getType() + ") is not configured. Please contact an administrator.");
            Bukkit.getLogger().info(ChatColor.RED + "This region is problematic, and the block type (" + r.getType() + ") is not configured.");
            return true;
        }
        if (!r.getTypeOptions().allowMerging)
            return PSL.msg(s, PSL.MERGE_NOT_ALLOWED.msg());
        List<TextComponent> components = getGUI(p, r);
        if (components.isEmpty()) {
            PSL.msg(p, PSL.MERGE_NO_REGIONS.msg());
        } else {
            // send empty line
            p.sendMessage(ChatColor.WHITE + "");
            PSL.msg(p, PSL.MERGE_HEADER.msg().replace("%region%", r.getId()));
            PSL.msg(p, PSL.MERGE_WARNING.msg());
            for (TextComponent tc : components) p.spigot().sendMessage(tc);
            // send empty line
            p.sendMessage(ChatColor.WHITE + "");
        }
    } else if (args.length == 3) {
        // /ps merge [region] [root]
        RegionManager rm = WGUtils.getRegionManagerWithPlayer(p);
        ProtectedRegion region = rm.getRegion(args[1]), root = rm.getRegion(args[2]);
        LocalPlayer lp = WorldGuardPlugin.inst().wrapPlayer(p);
        if (!ProtectionStones.isPSRegion(region) || !ProtectionStones.isPSRegion(root))
            return PSL.msg(p, PSL.MULTI_REGION_DOES_NOT_EXIST.msg());
        if (!p.hasPermission("protectionstones.admin") && (!region.isOwner(lp) || !root.isOwner(lp)))
            return PSL.msg(p, PSL.NO_ACCESS.msg());
        // check if region is actually overlapping the region
        var overlappingRegionIds = WGUtils.findOverlapOrAdjacentRegions(root, rm, p.getWorld()).stream().map(ProtectedRegion::getId).collect(Collectors.toList());
        if (!overlappingRegionIds.contains(region.getId()))
            return PSL.msg(p, PSL.REGION_NOT_OVERLAPPING.msg());
        // check if merging is allowed in config
        PSRegion aRegion = PSRegion.fromWGRegion(p.getWorld(), region), aRoot = PSRegion.fromWGRegion(p.getWorld(), root);
        if (!aRegion.getTypeOptions().allowMerging || !aRoot.getTypeOptions().allowMerging)
            return PSL.msg(p, PSL.MERGE_NOT_ALLOWED.msg());
        // check if the region types allow for it
        if (!WGUtils.canMergeRegionTypes(aRegion.getTypeOptions(), aRoot))
            return PSL.msg(p, PSL.MERGE_NOT_ALLOWED.msg());
        Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
            try {
                WGMerge.mergeRealRegions(p.getWorld(), rm, aRoot, Arrays.asList(aRegion, aRoot));
            } catch (WGMerge.RegionHoleException e) {
                PSL.msg(p, PSL.NO_REGION_HOLES.msg());
                return;
            } catch (WGMerge.RegionCannotMergeWhileRentedException e) {
                PSL.msg(p, PSL.CANNOT_MERGE_RENTED_REGION.msg().replace("%region%", e.getRentedRegion().getName() == null ? e.getRentedRegion().getId() : e.getRentedRegion().getName()));
                return;
            }
            PSL.msg(p, PSL.MERGE_MERGED.msg());
            // show menu again if the new region still has overlapping regions
            Bukkit.getScheduler().runTask(ProtectionStones.getInstance(), () -> {
                if (!getGUI(p, PSRegion.fromWGRegion(p.getWorld(), rm.getRegion(aRoot.getId()))).isEmpty()) {
                    Bukkit.dispatchCommand(p, ProtectionStones.getInstance().getConfigOptions().base_command + " merge");
                }
            });
        });
    } else {
        PSL.msg(s, PSL.MERGE_HELP.msg());
    }
    return true;
}
Also used : TextComponent(net.md_5.bungee.api.chat.TextComponent) LocalPlayer(com.sk89q.worldguard.LocalPlayer) Player(org.bukkit.entity.Player) LocalPlayer(com.sk89q.worldguard.LocalPlayer) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager)

Aggregations

LocalPlayer (com.sk89q.worldguard.LocalPlayer)172 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)87 Test (org.junit.jupiter.api.Test)82 Player (org.bukkit.entity.Player)50 EventHandler (org.bukkit.event.EventHandler)36 WorldConfiguration (com.sk89q.worldguard.config.WorldConfiguration)26 Command (com.sk89q.minecraft.util.commands.Command)23 BukkitWorldConfiguration (com.sk89q.worldguard.bukkit.BukkitWorldConfiguration)19 StringFlag (com.sk89q.worldguard.protection.flags.StringFlag)18 ApplicableRegionSet (com.sk89q.worldguard.protection.ApplicableRegionSet)15 CommandException (com.sk89q.minecraft.util.commands.CommandException)14 RegionQuery (com.sk89q.worldguard.protection.regions.RegionQuery)14 Entity (org.bukkit.entity.Entity)12 CommandPermissions (com.sk89q.minecraft.util.commands.CommandPermissions)10 World (com.sk89q.worldedit.world.World)10 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)10 CommandPermissionsException (com.sk89q.minecraft.util.commands.CommandPermissionsException)9 RegionContainer (com.sk89q.worldguard.protection.regions.RegionContainer)9 Session (com.sk89q.worldguard.session.Session)9 HumanEntity (org.bukkit.entity.HumanEntity)9