Search in sources :

Example 21 with PlayerRegion

use of br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion in project RedProtect by FabioZumbi12.

the class WorldMySQLRegionManager method getRegion.

@Override
public Region getRegion(final String rname) {
    if (this.dbcon == null) {
        ConnectDB();
    }
    if (!regions.containsKey(rname)) {
        if (rname == null) {
            return null;
        }
        try {
            PreparedStatement st = this.dbcon.prepareStatement("SELECT * FROM `" + tableName + "` WHERE LOWER(name) = ? AND world=?");
            st.setString(1, rname.toLowerCase());
            st.setString(2, this.world);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                Set<PlayerRegion> leaders = new HashSet<>();
                Set<PlayerRegion> admins = new HashSet<>();
                Set<PlayerRegion> members = new HashSet<>();
                HashMap<String, Object> flags = new HashMap<>();
                int maxMbrX = rs.getInt("maxMbrX");
                int minMbrX = rs.getInt("minMbrX");
                int maxMbrZ = rs.getInt("maxMbrZ");
                int minMbrZ = rs.getInt("minMbrZ");
                int maxY = rs.getInt("maxY");
                int minY = rs.getInt("minY");
                int prior = rs.getInt("prior");
                String world = rs.getString("world");
                String date = rs.getString("date");
                String wel = rs.getString("wel");
                long value = rs.getLong("value");
                boolean candel = rs.getBoolean("candelete");
                boolean canPurge = rs.getBoolean("canpurge");
                Location<World> tppoint = null;
                if (rs.getString("tppoint") != null && !rs.getString("tppoint").equalsIgnoreCase("")) {
                    String[] tpstring = rs.getString("tppoint").split(",");
                    tppoint = new Location<>(Sponge.getServer().getWorld(world).get(), Double.parseDouble(tpstring[0]), Double.parseDouble(tpstring[1]), Double.parseDouble(tpstring[2]));
                }
                String serverName = RedProtect.get().getConfigManager().configRoot().region_settings.default_leader;
                for (String member : rs.getString("members").split(", ")) {
                    String[] pi = member.split("@");
                    String[] p = new String[] { pi[0], pi.length == 2 ? pi[1] : pi[0] };
                    if (p[1].isEmpty())
                        continue;
                    if (!p[0].equalsIgnoreCase(serverName) && !p[1].equalsIgnoreCase(serverName)) {
                        if (RedProtect.get().getUtil().isUUIDs(p[1])) {
                            String before = p[1];
                            p[1] = RedProtect.get().getUtil().UUIDtoPlayer(p[1]) == null ? p[1] : RedProtect.get().getUtil().UUIDtoPlayer(p[1]).toLowerCase();
                            RedProtect.get().logger.success("Updated region " + rname + ", player &6" + before + " &ato &6" + p[1]);
                        }
                    }
                    members.add(new PlayerRegion(p[0], p[1]));
                }
                for (String admin : rs.getString("admins").split(", ")) {
                    String[] pi = admin.split("@");
                    String[] p = new String[] { pi[0], pi.length == 2 ? pi[1] : pi[0] };
                    if (p[1].isEmpty())
                        continue;
                    if (!p[0].equalsIgnoreCase(serverName) && !p[1].equalsIgnoreCase(serverName)) {
                        if (RedProtect.get().getUtil().isUUIDs(p[1])) {
                            String before = p[1];
                            p[1] = RedProtect.get().getUtil().UUIDtoPlayer(p[1]) == null ? p[1] : RedProtect.get().getUtil().UUIDtoPlayer(p[1]).toLowerCase();
                            RedProtect.get().logger.success("Updated region " + rname + ", player &6" + before + " &ato &6" + p[1]);
                        }
                    }
                    admins.add(new PlayerRegion(p[0], p[1]));
                }
                for (String leader : rs.getString("leaders").split(", ")) {
                    String[] pi = leader.split("@");
                    String[] p = new String[] { pi[0], pi.length == 2 ? pi[1] : pi[0] };
                    if (p[1].isEmpty())
                        continue;
                    if (!p[0].equalsIgnoreCase(serverName) && !p[1].equalsIgnoreCase(serverName)) {
                        if (RedProtect.get().getUtil().isUUIDs(p[1])) {
                            String before = p[1];
                            p[1] = RedProtect.get().getUtil().UUIDtoPlayer(p[1]) == null ? p[1] : RedProtect.get().getUtil().UUIDtoPlayer(p[1]).toLowerCase();
                            RedProtect.get().logger.success("Updated region " + rname + ", player &6" + before + " &ato &6" + p[1]);
                        }
                    }
                    leaders.add(new PlayerRegion(p[0], p[1]));
                }
                for (String flag : rs.getString("flags").split(",")) {
                    String key = flag.split(":")[0];
                    flags.put(key, RedProtect.get().getUtil().parseObject(flag.substring((key + ":").length())));
                }
                Region reg = new Region(rname, admins, members, leaders, maxMbrX, minMbrX, maxMbrZ, minMbrZ, minY, maxY, flags, wel, prior, world, date, value, tppoint, candel, canPurge);
                regions.put(rname, reg);
            } else {
                return null;
            }
            st.close();
            rs.close();
            RedProtect.get().logger.debug(LogLevel.DEFAULT, "Adding region to cache: " + rname);
            Sponge.getScheduler().createSyncExecutor(RedProtect.get().container).schedule(() -> {
                if (regions.containsKey(rname)) {
                    regions.remove(rname);
                    RedProtect.get().logger.debug(LogLevel.DEFAULT, "Removed cached region: " + rname);
                }
            }, RedProtect.get().getConfigManager().configRoot().mysql.region_cache_minutes, TimeUnit.MINUTES);
        } catch (SQLException e) {
            CoreUtil.printJarVersion();
            e.printStackTrace();
        }
    }
    return regions.get(rname);
}
Also used : PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) World(org.spongepowered.api.world.World) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion)

Example 22 with PlayerRegion

use of br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion in project RedProtect by FabioZumbi12.

the class CommandHandler method handleWGRegions.

private static boolean handleWGRegions() {
    if (!RedProtect.get().hooks.checkWG()) {
        return false;
    }
    WorldGuardHelper helper = RedProtect.get().hooks.worldGuardHelper;
    List<RegionManager> loaded = helper.getLoaded();
    if (loaded.isEmpty())
        return false;
    int i = 0;
    for (RegionManager rm : loaded) {
        if (rm.getRegions().isEmpty())
            continue;
        String w = rm.getName();
        for (Map.Entry<String, ProtectedRegion> pr : rm.getRegions().entrySet()) {
            if (!pr.getValue().getType().equals(RegionType.CUBOID))
                continue;
            if (RedProtect.get().getRegionManager().getRegion(pr.getKey(), w) != null)
                continue;
            Set<PlayerRegion> leaders;
            Set<PlayerRegion> members;
            if (!pr.getValue().getOwners().getUniqueIds().isEmpty())
                leaders = pr.getValue().getOwners().getUniqueIds().stream().filter(p -> Bukkit.getPlayer(p) != null).map(o -> new PlayerRegion(o.toString(), Bukkit.getPlayer(o).getName())).collect(Collectors.toSet());
            else
                leaders = pr.getValue().getOwners().getPlayers().stream().map(o -> new PlayerRegion(o, o)).collect(Collectors.toSet());
            if (!pr.getValue().getMembers().getUniqueIds().isEmpty())
                members = pr.getValue().getMembers().getUniqueIds().stream().filter(p -> Bukkit.getPlayer(p) != null).map(o -> new PlayerRegion(o.toString(), Bukkit.getPlayer(o).getName())).collect(Collectors.toSet());
            else
                members = pr.getValue().getMembers().getPlayers().stream().map(o -> new PlayerRegion(o, o)).collect(Collectors.toSet());
            if (leaders.isEmpty()) {
                if (members.isEmpty())
                    leaders.add(new PlayerRegion(RedProtect.get().getConfigManager().configRoot().region_settings.default_leader, RedProtect.get().getConfigManager().configRoot().region_settings.default_leader));
                else
                    leaders.addAll(members);
            }
            Location min = helper.getMinimumPoint(pr.getValue(), Bukkit.getWorld(w));
            Location max = helper.getMaximumPoint(pr.getValue(), Bukkit.getWorld(w));
            Region r = new Region(pr.getKey(), new HashSet<>(), members, leaders, min, max, RedProtect.get().getConfigManager().getDefFlagsValues(), "", pr.getValue().getPriority(), w, RedProtect.get().getUtil().dateNow(), 0, null, true, true);
            for (Map.Entry<Flag<?>, Object> flag : pr.getValue().getFlags().entrySet()) {
                if (r.flagExists(flag.getKey().getName()) && RedProtect.get().getUtil().parseObject(flag.getValue().toString()) != null) {
                    r.setFlag(Bukkit.getConsoleSender(), flag.getKey().getName(), RedProtect.get().getUtil().parseObject(flag.getValue().toString()));
                }
            }
            RedProtect.get().getRegionManager().add(r, w);
            RedProtect.get().logger.warning("Region converted and named to " + r.getName());
            i++;
        }
        RedProtect.get().logger.success(i + " WorldGuard regions imported for world " + w);
    }
    return i > 0;
}
Also used : PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) java.util(java.util) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) Array(java.lang.reflect.Array) CommandHandlers(br.net.fabiozumbi12.RedProtect.Bukkit.commands.CommandHandlers) TabCompleter(org.bukkit.command.TabCompleter) WorldGuardHelper(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.WorldGuardHelper) SimpleDateFormat(java.text.SimpleDateFormat) Player(org.bukkit.entity.Player) CoreUtil(br.net.fabiozumbi12.RedProtect.Core.helpers.CoreUtil) org.bukkit(org.bukkit) EventHandler(org.bukkit.event.EventHandler) PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region) ParseException(java.text.ParseException) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) Replacer(br.net.fabiozumbi12.RedProtect.Core.helpers.Replacer) Listener(org.bukkit.event.Listener) PlayerHandlers(br.net.fabiozumbi12.RedProtect.Bukkit.commands.SubCommands.PlayerHandlers) CommandSender(org.bukkit.command.CommandSender) MojangUUIDs(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.MojangUUIDs) SpigetUpdater(br.net.fabiozumbi12.RedProtect.Bukkit.updater.SpigetUpdater) RegionType(com.sk89q.worldguard.protection.regions.RegionType) RedProtect(br.net.fabiozumbi12.RedProtect.Bukkit.RedProtect) WEHook(br.net.fabiozumbi12.RedProtect.Bukkit.hooks.WEHook) Flag(com.sk89q.worldguard.protection.flags.Flag) CommandExecutor(org.bukkit.command.CommandExecutor) Collectors(java.util.stream.Collectors) RegionHandlers(br.net.fabiozumbi12.RedProtect.Bukkit.commands.SubCommands.RegionHandlers) ItemStack(org.bukkit.inventory.ItemStack) TimeUnit(java.util.concurrent.TimeUnit) LiteChunk(me.ellbristow.mychunk.LiteChunk) MyChunkChunk(me.ellbristow.mychunk.MyChunkChunk) Command(org.bukkit.command.Command) UltimateFancy(br.net.fabiozumbi12.UltimateFancy.UltimateFancy) WorldGuardHelper(br.net.fabiozumbi12.RedProtect.Bukkit.helpers.WorldGuardHelper) Flag(com.sk89q.worldguard.protection.flags.Flag) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) Region(br.net.fabiozumbi12.RedProtect.Bukkit.Region)

Example 23 with PlayerRegion

use of br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion in project RedProtect by FabioZumbi12.

the class Region method addAdmin.

/**
 * Add an admin to the Region. The string need to be UUID if Online Mode, or Player Name if Offline Mode.
 *
 * @param uuid - UUID or Player Name.
 */
public void addAdmin(String uuid) {
    setToSave(true);
    String name = RedProtect.get().getUtil().UUIDtoPlayer(uuid).toLowerCase();
    PlayerRegion pInfo = new PlayerRegion(RedProtect.get().getUtil().PlayerToUUID(uuid), name);
    this.members.removeIf(m -> m.getUUID().equalsIgnoreCase(uuid) || m.getPlayerName().equalsIgnoreCase(uuid));
    this.leaders.removeIf(m -> m.getUUID().equalsIgnoreCase(uuid) || m.getPlayerName().equalsIgnoreCase(uuid));
    this.admins.add(pInfo);
    RedProtect.get().getRegionManager().updateLiveRegion(this, "leaders", serializeMembers(leaders));
    RedProtect.get().getRegionManager().updateLiveRegion(this, "admins", serializeMembers(admins));
    RedProtect.get().getRegionManager().updateLiveRegion(this, "members", serializeMembers(members));
}
Also used : PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion)

Example 24 with PlayerRegion

use of br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion in project RedProtect by FabioZumbi12.

the class Region method removeAdmin.

/**
 * Remove an admin to the Region. The string need to be UUID if Online Mode, or Player Name if Offline Mode.
 *
 * @param uuid - UUID or Player Name.
 */
public void removeAdmin(String uuid) {
    setToSave(true);
    String name = RedProtect.get().getUtil().UUIDtoPlayer(uuid).toLowerCase();
    PlayerRegion pInfo = new PlayerRegion(RedProtect.get().getUtil().PlayerToUUID(uuid), name);
    this.leaders.removeIf(m -> m.getUUID().equalsIgnoreCase(uuid) || m.getPlayerName().equalsIgnoreCase(uuid));
    this.admins.removeIf(m -> m.getUUID().equalsIgnoreCase(uuid) || m.getPlayerName().equalsIgnoreCase(uuid));
    this.members.add(pInfo);
    RedProtect.get().getRegionManager().updateLiveRegion(this, "leaders", serializeMembers(leaders));
    RedProtect.get().getRegionManager().updateLiveRegion(this, "admins", serializeMembers(admins));
    RedProtect.get().getRegionManager().updateLiveRegion(this, "members", serializeMembers(members));
}
Also used : PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion)

Example 25 with PlayerRegion

use of br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion in project RedProtect by FabioZumbi12.

the class Region method addMember.

/**
 * Add a member to the Region. The string need to be UUID if Online Mode, or Player Name if Offline Mode.
 *
 * @param uuid - UUID or Player Name.
 */
public void addMember(String uuid) {
    setToSave(true);
    String name = RedProtect.get().getUtil().UUIDtoPlayer(uuid).toLowerCase();
    PlayerRegion pInfo = new PlayerRegion(RedProtect.get().getUtil().PlayerToUUID(uuid), name);
    this.admins.removeIf(m -> m.getUUID().equalsIgnoreCase(uuid) || m.getPlayerName().equalsIgnoreCase(uuid));
    this.leaders.removeIf(m -> m.getUUID().equalsIgnoreCase(uuid) || m.getPlayerName().equalsIgnoreCase(uuid));
    this.members.add(pInfo);
    RedProtect.get().getRegionManager().updateLiveRegion(this, "leaders", serializeMembers(leaders));
    RedProtect.get().getRegionManager().updateLiveRegion(this, "admins", serializeMembers(admins));
    RedProtect.get().getRegionManager().updateLiveRegion(this, "members", serializeMembers(members));
}
Also used : PlayerRegion(br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion)

Aggregations

PlayerRegion (br.net.fabiozumbi12.RedProtect.Core.region.PlayerRegion)31 Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)11 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)6 World (org.spongepowered.api.world.World)6 DefineRegionBuilder (br.net.fabiozumbi12.RedProtect.Bukkit.actions.DefineRegionBuilder)4 RegionBuilder (br.net.fabiozumbi12.RedProtect.Bukkit.region.RegionBuilder)4 File (java.io.File)4 IOException (java.io.IOException)4 Player (org.bukkit.entity.Player)4 CoreUtil (br.net.fabiozumbi12.RedProtect.Core.helpers.CoreUtil)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 Location (org.bukkit.Location)3 ConsoleCommandSender (org.bukkit.command.ConsoleCommandSender)3 RedProtect (br.net.fabiozumbi12.RedProtect.Bukkit.RedProtect)2 LogLevel (br.net.fabiozumbi12.RedProtect.Core.helpers.LogLevel)2 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)2 ClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat)2 ClipboardReader (com.sk89q.worldedit.extent.clipboard.io.ClipboardReader)2 Operation (com.sk89q.worldedit.function.operation.Operation)2