From 4276e63e62a3e6a974bc5cd2dbe1d7640921d91d Mon Sep 17 00:00:00 2001 From: gravityfox Date: Sun, 29 Nov 2015 17:19:40 -0800 Subject: [PATCH] Yet another update that breaks everything. Objects can now be enabled and disabled without being deleted. --- build.gradle | 2 +- permissionslist.txt | 4 + .../gravityfox/foxguard/FGStorageManager.java | 78 +++++--- .../net/gravityfox/foxguard/FoxGuardMain.java | 4 +- .../net/gravityfox/foxguard/IFGObject.java | 4 + .../foxguard/commands/CommandDetail.java | 2 + .../commands/CommandEnableDisable.java | 166 ++++++++++++++++++ .../foxguard/factory/FGFactoryManager.java | 8 +- .../foxguard/factory/FGHandlerFactory.java | 11 +- .../foxguard/factory/FGRegionFactory.java | 5 +- .../foxguard/factory/IHandlerFactory.java | 2 +- .../foxguard/factory/IRegionFactory.java | 2 +- .../foxguard/handlers/GlobalHandler.java | 9 + .../foxguard/handlers/IHandler.java | 4 - .../foxguard/handlers/PassiveHandler.java | 3 +- .../foxguard/handlers/PermissionHandler.java | 2 +- .../foxguard/handlers/util/Flags.java | 26 +-- .../foxguard/regions/CuboidRegion.java | 2 +- .../foxguard/regions/ElevationRegion.java | 2 +- .../foxguard/regions/GlobalRegion.java | 9 + .../foxguard/regions/RectangularRegion.java | 2 +- .../foxguard/regions/RegionBase.java | 11 ++ .../net/gravityfox/foxguard/util/Aliases.java | 8 +- .../foxguard/util/CallbackHashMap.java | 2 +- .../foxguard/util/DeferredObject.java | 10 +- .../gravityfox/foxguard/util/FGHelper.java | 4 +- 26 files changed, 312 insertions(+), 70 deletions(-) create mode 100644 src/main/java/net/gravityfox/foxguard/commands/CommandEnableDisable.java diff --git a/build.gradle b/build.gradle index 5b86a4f..ebfcd88 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ import java.nio.file.Files import java.nio.file.StandardCopyOption group 'tk.elektrofuchse.fox.foxguard' -version '0.9.1-SNAPSHOT' +version '0.10-SNAPSHOT' apply plugin: 'java' diff --git a/permissionslist.txt b/permissionslist.txt index 91d65a7..8138c01 100644 --- a/permissionslist.txt +++ b/permissionslist.txt @@ -33,6 +33,10 @@ foxguard { handlers.own regions.own } + enabledisable{ + handlers.own + regions.own + } modify{ handlers.own regions.own diff --git a/src/main/java/net/gravityfox/foxguard/FGStorageManager.java b/src/main/java/net/gravityfox/foxguard/FGStorageManager.java index bad0981..597dcc3 100644 --- a/src/main/java/net/gravityfox/foxguard/FGStorageManager.java +++ b/src/main/java/net/gravityfox/foxguard/FGStorageManager.java @@ -67,7 +67,8 @@ public void initHandlers() { "CREATE TABLE IF NOT EXISTS HANDLERS (" + "NAME VARCHAR(256), " + "TYPE VARCHAR(256)," + - "PRIORITY INTEGER);"); + "PRIORITY INTEGER," + + "ENABLED BOOLEAN);"); } } catch (SQLException e) { e.printStackTrace(); @@ -88,7 +89,8 @@ public void initWorld(World world) { statement.execute( "CREATE TABLE IF NOT EXISTS REGIONS (" + "NAME VARCHAR(256), " + - "TYPE VARCHAR(256));" + + "TYPE VARCHAR(256)," + + "ENABLED BOOLEAN);" + "CREATE TABLE IF NOT EXISTS LINKAGES (" + "REGION VARCHAR(256)," + "HANDLER VARCHAR(256));"); @@ -122,10 +124,12 @@ public void loadHandlers() { if (metaSet.getString("CATEGORY").equalsIgnoreCase("handler") && metaSet.getString("NAME").equalsIgnoreCase(handlerDataSet.getString("NAME")) && metaSet.getString("TYPE").equalsIgnoreCase(handlerDataSet.getString("TYPE")) && - metaSet.getInt("PRIORITY") == handlerDataSet.getInt("PRIORITY")) { + metaSet.getInt("PRIORITY") == handlerDataSet.getInt("PRIORITY") && + metaSet.getBoolean("ENABLED") == handlerDataSet.getBoolean("ENABLED")) { FGManager.getInstance().addHandler( FGFactoryManager.getInstance().createHandler( - source, handlerDataSet.getString("NAME"), handlerDataSet.getString("TYPE"), handlerDataSet.getInt("PRIORITY"))); + source, handlerDataSet.getString("NAME"), handlerDataSet.getString("TYPE"), + handlerDataSet.getInt("PRIORITY"), handlerDataSet.getBoolean("ENABLED"))); } else if (FGConfigManager.getInstance().forceLoad) { FoxGuardMain.getInstance().getLogger().info("Mismatched database found. Attempting force load..."); if (metaSet.getString("CATEGORY").equalsIgnoreCase("region")) { @@ -138,6 +142,8 @@ public void loadHandlers() { deferredRegion.type = metaSet.getString("TYPE"); deferredRegion.listWorld = null; deferredRegion.metaWorld = metaSet.getString("WORLD"); + deferredRegion.listEnabled = handlerDataSet.getBoolean("ENABLED"); + deferredRegion.metaEnabled = metaSet.getBoolean("ENABLED"); this.deferedObjects.add(deferredRegion); } else if (metaSet.getString("CATEGORY").equalsIgnoreCase("handler")) { DeferredObject deferredHandler = new DeferredObject(); @@ -148,6 +154,8 @@ public void loadHandlers() { deferredHandler.metaName = metaSet.getString("NAME"); deferredHandler.type = metaSet.getString("TYPE"); deferredHandler.priority = metaSet.getInt("PRIORITY"); + deferredHandler.listEnabled = handlerDataSet.getBoolean("ENABLED"); + deferredHandler.metaEnabled = metaSet.getBoolean("ENABLED"); this.deferedObjects.add(deferredHandler); } else { FoxGuardMain.getInstance().getLogger().info("Found potentially corrupted database."); @@ -204,11 +212,13 @@ public void loadWorldRegions(World world) { if (metaSet.getString("CATEGORY").equalsIgnoreCase("region") && metaSet.getString("NAME").equalsIgnoreCase(regionDataSet.getString("NAME")) && metaSet.getString("TYPE").equalsIgnoreCase(regionDataSet.getString("TYPE")) && - metaSet.getString("WORLD").equals(world.getName())) { + metaSet.getString("WORLD").equals(world.getName()) && + metaSet.getBoolean("ENABLED") == regionDataSet.getBoolean("ENABLED")) { FGManager.getInstance().addRegion(world, FGFactoryManager.getInstance().createRegion( source, - regionDataSet.getString("NAME"), regionDataSet.getString("TYPE") + regionDataSet.getString("NAME"), regionDataSet.getString("TYPE"), + regionDataSet.getBoolean("ENABLED") ) ); } else if (FGConfigManager.getInstance().forceLoad) { @@ -223,6 +233,8 @@ public void loadWorldRegions(World world) { deferredRegion.type = metaSet.getString("TYPE"); deferredRegion.listWorld = world; deferredRegion.metaWorld = metaSet.getString("WORLD"); + deferredRegion.listEnabled = regionDataSet.getBoolean("ENABLED"); + deferredRegion.metaEnabled = metaSet.getBoolean("ENABLED"); this.deferedObjects.add(deferredRegion); } else if (metaSet.getString("CATEGORY").equalsIgnoreCase("handler")) { DeferredObject deferredHandler = new DeferredObject(); @@ -233,6 +245,8 @@ public void loadWorldRegions(World world) { deferredHandler.metaName = metaSet.getString("NAME"); deferredHandler.type = metaSet.getString("TYPE"); deferredHandler.priority = metaSet.getInt("PRIORITY"); + deferredHandler.listEnabled = regionDataSet.getBoolean("ENABLED"); + deferredHandler.metaEnabled = metaSet.getBoolean("ENABLED"); this.deferedObjects.add(deferredHandler); } else { FoxGuardMain.getInstance().getLogger().info("Found potentially corrupted database."); @@ -294,10 +308,11 @@ public void writeHandlers() { try (Statement statement = conn.createStatement()) { statement.addBatch("DELETE FROM HANDLERS;"); for (IHandler handler : FGManager.getInstance().getHandlerListCopy()) { - statement.addBatch("INSERT INTO HANDLERS(NAME, TYPE, PRIORITY) VALUES ('" + + statement.addBatch("INSERT INTO HANDLERS(NAME, TYPE, PRIORITY, ENABLED) VALUES ('" + handler.getName() + "', '" + handler.getUniqueTypeString() + "', " + - handler.getPriority() + ");"); + handler.getPriority() + ", " + + (handler.isEnabled() ? "TRUE" : "FALSE") + ");"); } statement.executeBatch(); } @@ -318,13 +333,15 @@ public void writeHandlers() { "CATEGORY VARCHAR(16), " + "NAME VARCHAR(256), " + "TYPE VARCHAR(256), " + - "PRIORITY INTEGER);"); + "PRIORITY INTEGER," + + "ENABLED BOOLEAN);"); statement.addBatch("DELETE FROM FOXGUARD_META.METADATA"); - statement.addBatch("INSERT INTO FOXGUARD_META.METADATA(CATEGORY, NAME, TYPE, PRIORITY) VALUES (" + + statement.addBatch("INSERT INTO FOXGUARD_META.METADATA(CATEGORY, NAME, TYPE, PRIORITY, ENABLED) VALUES (" + "'handler', '" + handler.getName() + "', '" + handler.getUniqueTypeString() + "', " + - handler.getPriority() + ");"); + handler.getPriority() + ", " + + (handler.isEnabled() ? "TRUE" : "FALSE") + ");"); statement.executeBatch(); } } @@ -346,9 +363,10 @@ public void writeWorld(World world) { try (Statement statement = conn.createStatement()) { statement.addBatch("DELETE FROM REGIONS; DELETE FROM LINKAGES;"); for (IRegion region : FGManager.getInstance().getRegionsListCopy(world)) { - statement.addBatch("INSERT INTO REGIONS(NAME, TYPE) VALUES ('" + + statement.addBatch("INSERT INTO REGIONS(NAME, TYPE, ENABLED) VALUES ('" + region.getName() + "', '" + - region.getUniqueTypeString() + "');"); + region.getUniqueTypeString() + "', " + + (region.isEnabled() ? "TRUE" : "FALSE") + ");"); for (IHandler handler : region.getHandlers()) { statement.addBatch("INSERT INTO LINKAGES(REGION, HANDLER) VALUES ('" + region.getName() + "', '" + @@ -371,13 +389,15 @@ public void writeWorld(World world) { "CATEGORY VARCHAR(16), " + "NAME VARCHAR(256), " + "TYPE VARCHAR(256), " + - "WORLD VARCHAR(256));"); + "WORLD VARCHAR(256)," + + "ENABLED BOOLEAN);"); statement.addBatch("DELETE FROM FOXGUARD_META.METADATA"); - statement.addBatch("INSERT INTO FOXGUARD_META.METADATA(CATEGORY, NAME, TYPE, WORLD) VALUES (" + + statement.addBatch("INSERT INTO FOXGUARD_META.METADATA(CATEGORY, NAME, TYPE, WORLD, ENABLED) VALUES (" + "'region', '" + region.getName() + "', '" + region.getUniqueTypeString() + "', '" + - region.getWorld().getName() + "');"); + region.getWorld().getName() + "', " + + (region.isEnabled() ? "TRUE" : "FALSE") + ");"); statement.executeBatch(); } } @@ -413,13 +433,15 @@ public void updateRegion(IRegion region) { "CATEGORY VARCHAR(16), " + "NAME VARCHAR(256), " + "TYPE VARCHAR(256), " + - "WORLD VARCHAR(256));"); + "WORLD VARCHAR(256)," + + "ENABLED BOOLEAN);"); statement.addBatch("DELETE FROM FOXGUARD_META.METADATA"); - statement.addBatch("INSERT INTO FOXGUARD_META.METADATA(CATEGORY, NAME, TYPE, WORLD) VALUES (" + + statement.addBatch("INSERT INTO FOXGUARD_META.METADATA(CATEGORY, NAME, TYPE, WORLD, ENABLED) VALUES (" + "'region', '" + region.getName() + "', '" + region.getUniqueTypeString() + "', '" + - region.getWorld().getName() + "');"); + region.getWorld().getName() + "', " + + (region.isEnabled() ? "TRUE" : "FALSE") + ");"); statement.executeBatch(); } } @@ -444,13 +466,15 @@ public void updateHandler(IHandler handler) { "CATEGORY VARCHAR(16), " + "NAME VARCHAR(256), " + "TYPE VARCHAR(256), " + - "PRIORITY INTEGER);"); + "PRIORITY INTEGER," + + "ENABLED BOOLEAN);"); statement.addBatch("DELETE FROM FOXGUARD_META.METADATA"); - statement.addBatch("INSERT INTO FOXGUARD_META.METADATA(CATEGORY, NAME, TYPE, PRIORITY) VALUES (" + + statement.addBatch("INSERT INTO FOXGUARD_META.METADATA(CATEGORY, NAME, TYPE, PRIORITY, ENABLED) VALUES (" + "'handler', '" + handler.getName() + "', '" + handler.getUniqueTypeString() + "', " + - handler.getPriority() + ");"); + handler.getPriority() + ", " + + (handler.isEnabled() ? "TRUE" : "FALSE") + ");"); statement.executeBatch(); } } @@ -474,9 +498,10 @@ public void updateLists() { try (Statement statement = conn.createStatement()) { statement.addBatch("DELETE FROM REGIONS; DELETE FROM LINKAGES;"); for (IRegion region : FGManager.getInstance().getRegionsListCopy(world)) { - statement.addBatch("INSERT INTO REGIONS(NAME, TYPE) VALUES ('" + + statement.addBatch("INSERT INTO REGIONS(NAME, TYPE, ENABLED) VALUES ('" + region.getName() + "', '" + - region.getUniqueTypeString() + "');"); + region.getUniqueTypeString() + "', " + + (region.isEnabled() ? "TRUE" : "FALSE") + ");"); for (IHandler handler : region.getHandlers()) { statement.addBatch("INSERT INTO LINKAGES(REGION, HANDLER) VALUES ('" + region.getName() + "', '" + @@ -493,10 +518,11 @@ public void updateLists() { try (Statement statement = conn.createStatement()) { statement.addBatch("DELETE FROM HANDLERS;"); for (IHandler handler : FGManager.getInstance().getHandlerListCopy()) { - statement.addBatch("INSERT INTO HANDLERS(NAME, TYPE, PRIORITY) VALUES ('" + + statement.addBatch("INSERT INTO HANDLERS(NAME, TYPE, PRIORITY, ENABLED) VALUES ('" + handler.getName() + "', '" + handler.getUniqueTypeString() + "', " + - handler.getPriority() + ");"); + handler.getPriority() + ", " + + (handler.isEnabled() ? "TRUE" : "FALSE") + ");"); } statement.executeBatch(); } diff --git a/src/main/java/net/gravityfox/foxguard/FoxGuardMain.java b/src/main/java/net/gravityfox/foxguard/FoxGuardMain.java index 84a2d12..50f7c60 100644 --- a/src/main/java/net/gravityfox/foxguard/FoxGuardMain.java +++ b/src/main/java/net/gravityfox/foxguard/FoxGuardMain.java @@ -64,7 +64,7 @@ @Plugin(id = "foxguard", name = "FoxGuard", version = FoxGuardMain.PLUGIN_VERSION) public class FoxGuardMain { - public static final String PLUGIN_VERSION = "0.9.1"; + public static final String PLUGIN_VERSION = "0.10"; private static FoxGuardMain instance; @@ -167,6 +167,8 @@ private void registerCommands() { fgDispatcher.register(new CommandModify(), "modify", "mod", "change", "edit", "update"); fgDispatcher.register(new CommandLink(), "link", "connect", "attach"); fgDispatcher.register(new CommandUnlink(), "unlink", "disconnect", "detach"); + fgDispatcher.register(new CommandEnableDisable(true), "enable", "activate","engage", "on"); + fgDispatcher.register(new CommandEnableDisable(false), "disable", "deactivate", "disengage", "off"); fgDispatcher.register(new CommandList(), "list", "ls"); fgDispatcher.register(new CommandDetail(), "detail", "det", "show"); fgDispatcher.register(new CommandState(), "state", "current", "cur"); diff --git a/src/main/java/net/gravityfox/foxguard/IFGObject.java b/src/main/java/net/gravityfox/foxguard/IFGObject.java index 3eb0f7f..7d3e3ed 100644 --- a/src/main/java/net/gravityfox/foxguard/IFGObject.java +++ b/src/main/java/net/gravityfox/foxguard/IFGObject.java @@ -47,6 +47,10 @@ public interface IFGObject { String getUniqueTypeString(); + boolean isEnabled(); + + void setIsEnabled(boolean state); + Text getDetails(String arguments); void writeToDatabase(DataSource dataSource) throws SQLException; diff --git a/src/main/java/net/gravityfox/foxguard/commands/CommandDetail.java b/src/main/java/net/gravityfox/foxguard/commands/CommandDetail.java index 1a0728e..eff0387 100644 --- a/src/main/java/net/gravityfox/foxguard/commands/CommandDetail.java +++ b/src/main/java/net/gravityfox/foxguard/commands/CommandDetail.java @@ -90,6 +90,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm builder.append(Texts.of(TextColors.GREEN, "---General---\n")); builder.append(Texts.of(TextColors.GOLD, "Name: "), Texts.of(TextColors.RESET, region.getName() + "\n")); builder.append(Texts.of(TextColors.GOLD, "Type: "), Texts.of(TextColors.RESET, region.getLongTypeName() + "\n")); + builder.append(Texts.of(TextColors.GOLD, "Enabled: "), Texts.of(TextColors.RESET, (region.isEnabled() ? "True" : "False") + "\n")); builder.append(Texts.of(TextColors.GOLD, "World: "), Texts.of(TextColors.RESET, region.getWorld().getName() + "\n")); builder.append(Texts.of(TextColors.GREEN, "---Details---\n")); builder.append(region.getDetails(args.length < 3 + flag ? "" : args[2 + flag])); @@ -113,6 +114,7 @@ public CommandResult process(CommandSource source, String arguments) throws Comm builder.append(Texts.of(TextColors.GREEN, "---General---\n")); builder.append(Texts.of(TextColors.GOLD, "Name: "), Texts.of(TextColors.RESET, handler.getName() + "\n")); builder.append(Texts.of(TextColors.GOLD, "Type: "), Texts.of(TextColors.RESET, handler.getLongTypeName() + "\n")); + builder.append(Texts.of(TextColors.GOLD, "Enabled: "), Texts.of(TextColors.RESET, (handler.isEnabled() ? "True" : "False") + "\n")); builder.append(Texts.of(TextColors.GOLD, "Priority: "), Texts.of(TextColors.RESET, handler.getPriority() + "\n")); builder.append(Texts.of(TextColors.GREEN, "---Details---\n")); builder.append(handler.getDetails(args.length < 3 ? "" : args[2])); diff --git a/src/main/java/net/gravityfox/foxguard/commands/CommandEnableDisable.java b/src/main/java/net/gravityfox/foxguard/commands/CommandEnableDisable.java new file mode 100644 index 0000000..a54126b --- /dev/null +++ b/src/main/java/net/gravityfox/foxguard/commands/CommandEnableDisable.java @@ -0,0 +1,166 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) 2015 - 2015. gravityfox - https://gravityfox.net/ and contributors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package net.gravityfox.foxguard.commands; + +import net.gravityfox.foxguard.FGManager; +import net.gravityfox.foxguard.FoxGuardMain; +import net.gravityfox.foxguard.handlers.GlobalHandler; +import net.gravityfox.foxguard.handlers.IHandler; +import net.gravityfox.foxguard.regions.GlobalRegion; +import net.gravityfox.foxguard.regions.IRegion; +import net.gravityfox.foxguard.util.FGHelper; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.Texts; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.util.command.CommandCallable; +import org.spongepowered.api.util.command.CommandException; +import org.spongepowered.api.util.command.CommandResult; +import org.spongepowered.api.util.command.CommandSource; +import org.spongepowered.api.util.command.args.ArgumentParseException; +import org.spongepowered.api.world.World; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import static net.gravityfox.foxguard.util.Aliases.*; + +/** + * Created by Fox on 11/29/2015. + * Project: SpongeForge + */ +public class CommandEnableDisable implements CommandCallable { + + private final boolean enableState; + + public CommandEnableDisable(boolean enableState) { + this.enableState = enableState; + } + + @Override + public CommandResult process(CommandSource source, String arguments) throws CommandException { + if (!testPermission(source)) { + source.sendMessage(Texts.of(TextColors.RED, "You don't have permission to use this command!")); + return CommandResult.empty(); + } + String[] args = {}; + if (!arguments.isEmpty()) args = arguments.split(" +"); + if (source instanceof Player) { + Player player = (Player) source; + if (args.length == 0) { + source.sendMessage(Texts.builder() + .append(Texts.of(TextColors.GREEN, "Usage: ")) + .append(getUsage(source)) + .build()); + return CommandResult.empty(); + } else if (isAlias(REGIONS_ALIASES, args[0])) { + if (args.length < 2) throw new CommandException(Texts.of("Must specify a name!")); + int flag = 0; + Optional optWorld = FGHelper.parseWorld(args[1], FoxGuardMain.getInstance().getGame().getServer()); + World world; + if (optWorld != null && optWorld.isPresent()) { + world = optWorld.get(); + flag = 1; + } else world = player.getWorld(); + if (args.length < 2 + flag) throw new CommandException(Texts.of("Must specify a name!")); + int successes = 0; + int failures = 0; + for (String name : Arrays.copyOfRange(args, 1 + flag, args.length)) { + IRegion region = FGManager.getInstance().getRegion(world, name); + if (region == null) failures++; + else if (region instanceof GlobalRegion || region.isEnabled() == this.enableState) failures++; + else { + region.setIsEnabled(this.enableState); + successes++; + } + } + if (successes > 0) { + source.sendMessage(Texts.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " regions with " + + successes + " successes and " + failures + " failures!")); + return CommandResult.builder().successCount(successes).build(); + } else { + throw new CommandException(Texts.of(failures + " failures while trying to " + (this.enableState ? "enable" : "disable") + + " regions. Check to make sure you spelled their names correctly and that they are not already " + + (this.enableState ? "enabled." : "disabled."))); + } + + } else if (isAlias(HANDLERS_ALIASES, args[0])) { + if (args.length < 2) throw new CommandException(Texts.of("Must specify a name!")); + int successes = 0; + int failures = 0; + for (String name : Arrays.copyOfRange(args, 1, args.length)) { + IHandler handler = FGManager.getInstance().gethandler(name); + if (handler == null) failures++; + else if (handler instanceof GlobalHandler || handler.isEnabled() == this.enableState) { + failures++; + } else { + handler.setIsEnabled(this.enableState); + successes++; + } + } + if (successes > 0) { + source.sendMessage(Texts.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " handlers with " + + successes + " successes and " + failures + " failures!")); + return CommandResult.builder().successCount(successes).build(); + } else { + throw new CommandException(Texts.of(failures + " failures while trying to " + (this.enableState ? "enable" : "disable") + + " handlers. Check to make sure you spelled their names correctly and that they are not already " + + (this.enableState ? "enabled." : "disabled."))); + } + } else throw new ArgumentParseException(Texts.of("Not a valid category!"), args[0], 0); + } else { + + } + return CommandResult.empty(); + } + + @Override + public List getSuggestions(CommandSource source, String arguments) throws CommandException { + return null; + } + + @Override + public boolean testPermission(CommandSource source) { + return source.hasPermission("foxguard.command.modify.objects.enabledisable"); + } + + @Override + public Optional getShortDescription(CommandSource source) { + return Optional.empty(); + } + + @Override + public Optional getHelp(CommandSource source) { + return Optional.empty(); + } + + @Override + public Text getUsage(CommandSource source) { + if (source instanceof Player) + return Texts.of((this.enableState ? "enable" : "disable") + " ] | handler> [names]..."); + else return Texts.of((this.enableState ? "enable" : "disable") + " | handler> [names]..."); + } +} diff --git a/src/main/java/net/gravityfox/foxguard/factory/FGFactoryManager.java b/src/main/java/net/gravityfox/foxguard/factory/FGFactoryManager.java index cf1515c..e7a98fb 100644 --- a/src/main/java/net/gravityfox/foxguard/factory/FGFactoryManager.java +++ b/src/main/java/net/gravityfox/foxguard/factory/FGFactoryManager.java @@ -69,10 +69,10 @@ public IRegion createRegion(String name, String type, String args, InternalComma return null; } - public IRegion createRegion(DataSource source, String name, String type) throws SQLException { + public IRegion createRegion(DataSource source, String name, String type, boolean isEnabled) throws SQLException { for (IRegionFactory rf : regionFactories) { if (isAlias(rf.getTypes(), type)) { - IRegion region = rf.createRegion(source, name, type); + IRegion region = rf.createRegion(source, name, type, isEnabled); if (region != null) return region; } } @@ -90,10 +90,10 @@ public IHandler createHandler(String name, String type, int priority, String arg return null; } - public IHandler createHandler(DataSource source, String name, String type, int priority) throws SQLException { + public IHandler createHandler(DataSource source, String name, String type, int priority, boolean isEnabled) throws SQLException { for (IHandlerFactory fsf : handlerFactories) { if (isAlias(fsf.getTypes(), type)) { - IHandler handler = fsf.createHandler(source, name, type, priority); + IHandler handler = fsf.createHandler(source, name, type, priority, isEnabled); if (handler != null) return handler; } } diff --git a/src/main/java/net/gravityfox/foxguard/factory/FGHandlerFactory.java b/src/main/java/net/gravityfox/foxguard/factory/FGHandlerFactory.java index aa801c8..df7ccdb 100644 --- a/src/main/java/net/gravityfox/foxguard/factory/FGHandlerFactory.java +++ b/src/main/java/net/gravityfox/foxguard/factory/FGHandlerFactory.java @@ -36,6 +36,7 @@ import net.gravityfox.foxguard.util.FGHelper; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.entity.living.player.User; +import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.util.Tristate; import org.spongepowered.api.util.command.CommandSource; @@ -82,11 +83,11 @@ public IHandler createHandler(String name, String type, int priority, String arg return handler; } else if (Aliases.isAlias(permissionAliases, type)) { return new PermissionHandler(name, priority); - }else return null; + } else return null; } @Override - public IHandler createHandler(DataSource source, String name, String type, int priority) throws SQLException { + public IHandler createHandler(DataSource source, String name, String type, int priority, boolean isEnabled) throws SQLException { if (type.equalsIgnoreCase("simple")) { List ownerList = new LinkedList<>(); List memberList = new LinkedList<>(); @@ -157,6 +158,7 @@ public IHandler createHandler(DataSource source, String name, String type, int p handler.setOwners(ownerList); handler.setMembers(memberList); handler.setPassiveOption(po); + handler.setIsEnabled(isEnabled); return handler; } else if (type.equalsIgnoreCase("passive")) { List ownerList = new LinkedList<>(); @@ -183,9 +185,12 @@ public IHandler createHandler(DataSource source, String name, String type, int p } PassiveHandler handler = new PassiveHandler(name, priority, flagMap); handler.setOwners(ownerList); + handler.setIsEnabled(isEnabled); return handler; } else if (type.equalsIgnoreCase("permission")) { - return new PermissionHandler(name, priority); + PermissionHandler handler = new PermissionHandler(name, priority); + handler.setIsEnabled(isEnabled); + return handler; } else return null; } } diff --git a/src/main/java/net/gravityfox/foxguard/factory/FGRegionFactory.java b/src/main/java/net/gravityfox/foxguard/factory/FGRegionFactory.java index 969210b..ef389ab 100644 --- a/src/main/java/net/gravityfox/foxguard/factory/FGRegionFactory.java +++ b/src/main/java/net/gravityfox/foxguard/factory/FGRegionFactory.java @@ -84,7 +84,7 @@ public IRegion createRegion(String name, String type, String arguments, Internal } @Override - public IRegion createRegion(DataSource source, String name, String type) throws SQLException { + public IRegion createRegion(DataSource source, String name, String type, boolean isEnabled) throws SQLException { if (type.equalsIgnoreCase("rectangular")) { Vector2i a, b; List userList = new LinkedList<>(); @@ -107,6 +107,7 @@ public IRegion createRegion(DataSource source, String name, String type) throws } RectangularRegion region = new RectangularRegion(name, new BoundingBox2(a, b)); region.setOwners(userList); + region.setIsEnabled(isEnabled); return region; } else if (type.equalsIgnoreCase("cuboid")) { Vector3i a, b; @@ -129,6 +130,7 @@ public IRegion createRegion(DataSource source, String name, String type) throws } CuboidRegion region = new CuboidRegion(name, new BoundingBox3(a, b)); region.setOwners(userList); + region.setIsEnabled(isEnabled); return region; } else if (type.equalsIgnoreCase("elevation")) { int lowerBound, upperBound; @@ -152,6 +154,7 @@ public IRegion createRegion(DataSource source, String name, String type) throws } ElevationRegion region = new ElevationRegion(name, lowerBound, upperBound); region.setOwners(userList); + region.setIsEnabled(isEnabled); return region; } else return null; } diff --git a/src/main/java/net/gravityfox/foxguard/factory/IHandlerFactory.java b/src/main/java/net/gravityfox/foxguard/factory/IHandlerFactory.java index 93ee1e2..d3cb02c 100644 --- a/src/main/java/net/gravityfox/foxguard/factory/IHandlerFactory.java +++ b/src/main/java/net/gravityfox/foxguard/factory/IHandlerFactory.java @@ -40,6 +40,6 @@ public interface IHandlerFactory extends IFGFactory { IHandler createHandler(String name, String type, int priority, String arguments, InternalCommandState state, CommandSource source); - IHandler createHandler(DataSource source, String name, String type, int priority) throws SQLException; + IHandler createHandler(DataSource source, String name, String type, int priority, boolean isEnabled) throws SQLException; } diff --git a/src/main/java/net/gravityfox/foxguard/factory/IRegionFactory.java b/src/main/java/net/gravityfox/foxguard/factory/IRegionFactory.java index e61ca61..d0c25c7 100644 --- a/src/main/java/net/gravityfox/foxguard/factory/IRegionFactory.java +++ b/src/main/java/net/gravityfox/foxguard/factory/IRegionFactory.java @@ -42,6 +42,6 @@ public interface IRegionFactory extends IFGFactory { IRegion createRegion(String name, String type, String arguments, InternalCommandState state, World world, CommandSource source) throws CommandException; - IRegion createRegion(DataSource source, String name, String type) throws SQLException; + IRegion createRegion(DataSource source, String name, String type, boolean isEnabled) throws SQLException; } diff --git a/src/main/java/net/gravityfox/foxguard/handlers/GlobalHandler.java b/src/main/java/net/gravityfox/foxguard/handlers/GlobalHandler.java index e0fbb5a..09a7b87 100644 --- a/src/main/java/net/gravityfox/foxguard/handlers/GlobalHandler.java +++ b/src/main/java/net/gravityfox/foxguard/handlers/GlobalHandler.java @@ -79,7 +79,16 @@ public Text getDetails(String arguments) { @Override public void writeToDatabase(DataSource dataSource) { + } + @Override + public boolean isEnabled() { + return true; + } + + @Override + public void setIsEnabled(boolean state) { + this.isEnabled = true; } @Override diff --git a/src/main/java/net/gravityfox/foxguard/handlers/IHandler.java b/src/main/java/net/gravityfox/foxguard/handlers/IHandler.java index a762c99..a4ee2e4 100644 --- a/src/main/java/net/gravityfox/foxguard/handlers/IHandler.java +++ b/src/main/java/net/gravityfox/foxguard/handlers/IHandler.java @@ -40,10 +40,6 @@ public interface IHandler extends IFGObject, Comparable { Tristate handle(@Nullable User user, Flags flag, Event event); - boolean isEnabled(); - - void setIsEnabled(boolean state); - int getPriority(); void setPriority(int priority); diff --git a/src/main/java/net/gravityfox/foxguard/handlers/PassiveHandler.java b/src/main/java/net/gravityfox/foxguard/handlers/PassiveHandler.java index 8ae4820..bd015d3 100644 --- a/src/main/java/net/gravityfox/foxguard/handlers/PassiveHandler.java +++ b/src/main/java/net/gravityfox/foxguard/handlers/PassiveHandler.java @@ -71,8 +71,7 @@ public PassiveHandler(String name, int priority, CallbackHashMap= lowerBound && y <= upperBound; + return isEnabled && y >= lowerBound && y <= upperBound; } @Override diff --git a/src/main/java/net/gravityfox/foxguard/regions/GlobalRegion.java b/src/main/java/net/gravityfox/foxguard/regions/GlobalRegion.java index a7bf9f3..4365c7c 100644 --- a/src/main/java/net/gravityfox/foxguard/regions/GlobalRegion.java +++ b/src/main/java/net/gravityfox/foxguard/regions/GlobalRegion.java @@ -72,7 +72,16 @@ public Text getDetails(String arguments) { @Override public void writeToDatabase(DataSource dataSource) { + } + + @Override + public boolean isEnabled() { + return true; + } + @Override + public void setIsEnabled(boolean state) { + this.isEnabled = true; } @Override diff --git a/src/main/java/net/gravityfox/foxguard/regions/RectangularRegion.java b/src/main/java/net/gravityfox/foxguard/regions/RectangularRegion.java index d6199c7..f679e9e 100644 --- a/src/main/java/net/gravityfox/foxguard/regions/RectangularRegion.java +++ b/src/main/java/net/gravityfox/foxguard/regions/RectangularRegion.java @@ -115,7 +115,7 @@ public boolean isInRegion(int x, int y, int z) { @Override public boolean isInRegion(double x, double y, double z) { - return boundingBox.contains(x, z); + return isEnabled && boundingBox.contains(x, z); } @Override diff --git a/src/main/java/net/gravityfox/foxguard/regions/RegionBase.java b/src/main/java/net/gravityfox/foxguard/regions/RegionBase.java index be59a74..e781797 100644 --- a/src/main/java/net/gravityfox/foxguard/regions/RegionBase.java +++ b/src/main/java/net/gravityfox/foxguard/regions/RegionBase.java @@ -42,6 +42,7 @@ public abstract class RegionBase implements IRegion { protected final List handlers; protected String name; protected World world; + protected boolean isEnabled = true; public RegionBase(String name) { this.name = name; @@ -97,4 +98,14 @@ public void setName(String name) { this.name = name; } + @Override + public boolean isEnabled() { + return isEnabled; + } + + @Override + public void setIsEnabled(boolean state) { + this.isEnabled = state; + } + } diff --git a/src/main/java/net/gravityfox/foxguard/util/Aliases.java b/src/main/java/net/gravityfox/foxguard/util/Aliases.java index 31664af..3f9e279 100644 --- a/src/main/java/net/gravityfox/foxguard/util/Aliases.java +++ b/src/main/java/net/gravityfox/foxguard/util/Aliases.java @@ -35,12 +35,12 @@ public class Aliases { public static final String[] REGIONS_ALIASES = {"regions", "region", "reg", "r"}; public static final String[] HANDLERS_ALIASES = {"handlers", "handler", "handles", "handle", "hands", "hand", "h", "flagsets", "flagset", "flags", "flag", "f"}; public static final String[] POSITIONS_ALIASES = {"positions", "position", "points", "point", "locations", "location", "pos", "loc", "locs", "p"}; - public static final String[] OWNER_GROUP_ALIASES = {"owners", "owner", "master", "masters", "creator", "creators", - "admin", "admins", "administrator", "administrators", "mod", "mods"}; + public static final String[] OWNER_GROUP_ALIASES = {"owners", "owner", "masters", "master", "creators", "creator", + "admins", "admin", "administrators", "administrator", "mods", "mod"}; + public static final String[] MEMBER_GROUP_ALIASES = {"members", "member", "users", "user", "players", "player"}; + public static final String[] DEFAULT_GROUP_ALIASES = {"default", "nonmember", "nonmembers", "everyone", "other"}; public static final String[] SET_ALIASES = {"set", "perm", "flag", "rule", "perms", "flags", "rules", "permission", "permissions"}; public static final String[] PASSIVE_ALIASES = {"passive", "causeless", "userless", "environment"}; - public static final String[] MEMBER_GROUP_ALIASES = {"member", "members", "user", "users", "player", "players"}; - public static final String[] DEFAULT_GROUP_ALIASES = {"default", "nonmember", "nonmembers", "everyone", "other"}; public static final String[] GROUPS_ALIASES = {"group", "groups"}; public static final String[] TRUE_ALIASES = {"true", "t", "allow", "a"}; public static final String[] FALSE_ALIASES = {"false", "f", "deny", "d"}; diff --git a/src/main/java/net/gravityfox/foxguard/util/CallbackHashMap.java b/src/main/java/net/gravityfox/foxguard/util/CallbackHashMap.java index 6efa83a..37ffe10 100644 --- a/src/main/java/net/gravityfox/foxguard/util/CallbackHashMap.java +++ b/src/main/java/net/gravityfox/foxguard/util/CallbackHashMap.java @@ -41,7 +41,7 @@ public CallbackHashMap(BiFunction, V> callback) { @Override public V get(Object key) { - if(containsKey(key)){ + if (containsKey(key)) { return super.get(key); } else { return callback.apply(key, this); diff --git a/src/main/java/net/gravityfox/foxguard/util/DeferredObject.java b/src/main/java/net/gravityfox/foxguard/util/DeferredObject.java index be152c3..ac11a12 100644 --- a/src/main/java/net/gravityfox/foxguard/util/DeferredObject.java +++ b/src/main/java/net/gravityfox/foxguard/util/DeferredObject.java @@ -49,6 +49,8 @@ public class DeferredObject { public String type; public String listName; public String metaName; + public boolean listEnabled = true; + public boolean metaEnabled = true; public World listWorld = null; public String metaWorld = null; public int priority = 0; @@ -67,7 +69,8 @@ else if (FGManager.getInstance().gethandler(listName) == null) } name = metaName + x; } - IHandler handler = FGFactoryManager.getInstance().createHandler(dataSource, name, type, priority); + IHandler handler = FGFactoryManager.getInstance().createHandler(dataSource, name, type, priority, metaEnabled); + handler.setIsEnabled(metaEnabled); FGStorageManager.getInstance().markForDeletion(databaseDir); FGManager.getInstance().addHandler(handler); FoxGuardMain.getInstance().getLogger().info("Successfully force loaded Handler: " + @@ -94,7 +97,8 @@ else if (FGManager.getInstance().getRegion(world, listName) == null) } name = metaName + x; } - IRegion region = FGFactoryManager.getInstance().createRegion(dataSource, name, type); + IRegion region = FGFactoryManager.getInstance().createRegion(dataSource, name, type, metaEnabled); + region.setIsEnabled(metaEnabled); FGStorageManager.getInstance().markForDeletion(databaseDir); FGManager.getInstance().addRegion(world, region); FoxGuardMain.getInstance().getLogger().info("Successfully force loaded Region: " + @@ -114,6 +118,8 @@ public String toString() { builder.append("Type: ").append(type).append("\n"); builder.append("Listed name: ").append(listName).append("\n"); builder.append("Metadata name: ").append(metaName).append("\n"); + builder.append("Listed Enabled: ").append(listEnabled).append("\n"); + builder.append("Metadata Enabled: ").append(metaEnabled).append("\n"); if (listWorld != null) builder.append("Listed world: ").append(listWorld.getName()).append("\n"); if (metaWorld != null) builder.append("Metadata world: ").append(metaWorld).append("\n"); if (!category.equalsIgnoreCase("region")) builder.append("Priority: ").append(priority).append("\n"); diff --git a/src/main/java/net/gravityfox/foxguard/util/FGHelper.java b/src/main/java/net/gravityfox/foxguard/util/FGHelper.java index 408d39e..05476ba 100644 --- a/src/main/java/net/gravityfox/foxguard/util/FGHelper.java +++ b/src/main/java/net/gravityfox/foxguard/util/FGHelper.java @@ -61,11 +61,11 @@ public static Optional parseWorld(String arg, Server server) { } public static TextColor getColorForRegion(IRegion region) { - return region.getName().equals(GlobalRegion.NAME) ? TextColors.YELLOW : TextColors.RESET; + return region instanceof GlobalRegion ? TextColors.YELLOW : (region.isEnabled() ? TextColors.RESET : TextColors.GRAY); } public static TextColor getColorForHandler(IHandler handler) { - return handler.getName().equals(GlobalHandler.NAME) ? TextColors.YELLOW : TextColors.RESET; + return handler instanceof GlobalHandler ? TextColors.YELLOW : (handler.isEnabled() ? TextColors.RESET : TextColors.GRAY); } public static boolean contains(T[] array, T value) {