From 11833821eb1d98f29d2f91b694541038ee8202c3 Mon Sep 17 00:00:00 2001 From: gravityfox Date: Sat, 16 Jan 2016 17:06:48 -0800 Subject: [PATCH] Added a bunch of things. Added movement flags. Bug fixes. A lot of changes. --- FoxCore | 2 +- .../foxguard/plugin/FGConfigManager.java | 25 +++ .../sponge/foxguard/plugin/FGManager.java | 63 +++++++- .../foxguard/plugin/FGStorageManager.java | 81 +++++----- .../plugin/{handler/util => }/Flag.java | 20 ++- .../sponge/foxguard/plugin/FoxGuardMain.java | 15 +- .../plugin/command/CommandEnableDisable.java | 39 +++++ .../plugin/command/CommandModify.java | 25 +++ .../foxguard/plugin/event/FGUpdateEvent.java | 30 ++++ .../plugin/event/FGUpdateObjectEvent.java | 34 ++++ .../foxguard/plugin/event/FoxGuardEvent.java | 31 ++++ .../plugin/handler/GlobalHandler.java | 53 +++--- .../foxguard/plugin/handler/IHandler.java | 6 +- .../plugin/handler/PassiveHandler.java | 11 +- .../plugin/handler/PermissionHandler.java | 18 +-- .../plugin/handler/SimpleHandler.java | 24 +-- .../plugin/listener/BlockEventListener.java | 16 +- .../listener/ExplosionEventListener.java | 36 +++++ .../plugin/listener/InteractListener.java | 7 +- .../plugin/listener/PlayerMoveListener.java | 152 +++++++++++++++--- .../listener/SpawnEntityEventListener.java | 7 +- .../plugin/listener/util/EventResult.java | 74 +++++++++ .../object/factory/FGHandlerFactory.java | 2 +- .../plugin/region/ElevationRegion.java | 2 +- .../plugin/region/util/BoundingBox2.java | 4 +- .../plugin/region/util/BoundingBox3.java | 6 +- 26 files changed, 641 insertions(+), 142 deletions(-) rename src/main/java/net/foxdenstudio/sponge/foxguard/plugin/{handler/util => }/Flag.java (85%) create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FGUpdateEvent.java create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FGUpdateObjectEvent.java create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FoxGuardEvent.java create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/ExplosionEventListener.java create mode 100644 src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/util/EventResult.java diff --git a/FoxCore b/FoxCore index 88e0425..a5f127b 160000 --- a/FoxCore +++ b/FoxCore @@ -1 +1 @@ -Subproject commit 88e0425b66476be462c83d2f439bbc52b8e4986b +Subproject commit a5f127b0407880deae724547c55e85a746cccd6b diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGConfigManager.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGConfigManager.java index d78a032..465f929 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGConfigManager.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGConfigManager.java @@ -32,6 +32,8 @@ import java.io.File; import java.io.IOException; +import java.util.EnumMap; +import java.util.Map; public final class FGConfigManager { @@ -41,6 +43,7 @@ public final class FGConfigManager { private boolean forceLoad; private boolean purgeDatabases; + private Map modules = new EnumMap<>(Module.class); public FGConfigManager() { if (instance == null) instance = this; @@ -71,6 +74,9 @@ private void load() { forceLoad = root.getNode("storage", "forceLoad").getBoolean(false); purgeDatabases = root.getNode("storage", "purgeDatabases").getBoolean(true); + for (Module m : Module.values()) { + this.modules.put(m, root.getNode("module", m.name).getBoolean(true)); + } //-------------------------------------------------------------------------------------------------------------- } @@ -108,6 +114,11 @@ public void save() { "However, they will still be overwritten if a new database is made with the same name.") .setValue(purgeDatabases); + for (Module m : Module.values()) { + root.getNode("module", m.name).setValue(this.modules.get(m)); + } + + //-------------------------------------------------------------------------------------------------------------- try { loader.save(root); @@ -124,4 +135,18 @@ public boolean purgeDatabases() { return purgeDatabases; } + public Map getModules() { + return this.modules; + } + + public enum Module { + MOVEMENT("movement"); + + String name; + + Module(String name) { + this.name = name; + } + } + } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java index 86d5bc9..b66a83d 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGManager.java @@ -28,12 +28,16 @@ import com.flowpowered.math.vector.Vector3i; import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; +import net.foxdenstudio.sponge.foxguard.plugin.event.FGUpdateEvent; +import net.foxdenstudio.sponge.foxguard.plugin.event.FGUpdateObjectEvent; import net.foxdenstudio.sponge.foxguard.plugin.handler.GlobalHandler; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import net.foxdenstudio.sponge.foxguard.plugin.region.GlobalRegion; import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion; import org.spongepowered.api.Server; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.event.cause.Cause; import org.spongepowered.api.world.World; import java.util.ArrayList; @@ -92,6 +96,17 @@ public boolean addRegion(World world, IRegion region) { region.setWorld(world); this.regions.get(world).add(region); this.regionCache.get(world).clear(); + Sponge.getGame().getEventManager().post(new FGUpdateObjectEvent() { + @Override + public IFGObject getTarget() { + return region; + } + + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); FGStorageManager.getInstance().unmarkForDeletion(region); FGStorageManager.getInstance().updateRegion(region); return true; @@ -133,6 +148,17 @@ public boolean addHandler(IHandler handler) { if (handler == null) return false; if (gethandler(handler.getName()) != null) return false; handlers.add(handler); + Sponge.getGame().getEventManager().post(new FGUpdateObjectEvent() { + @Override + public IFGObject getTarget() { + return handler; + } + + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); FGStorageManager.getInstance().unmarkForDeletion(handler); FGStorageManager.getInstance().updateHandler(handler); return true; @@ -161,6 +187,17 @@ private boolean removeHandler(IHandler handler) { if (!this.handlers.contains(handler)) { return false; } + Sponge.getGame().getEventManager().post(new FGUpdateObjectEvent() { + @Override + public IFGObject getTarget() { + return handler; + } + + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); FGStorageManager.getInstance().markForDeletion(handler); handlers.remove(handler); return true; @@ -180,6 +217,17 @@ private boolean removeRegion(World world, IRegion region) { if (!this.regions.get(world).contains(region)) { return false; } + Sponge.getGame().getEventManager().post(new FGUpdateObjectEvent() { + @Override + public IFGObject getTarget() { + return region; + } + + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); FGStorageManager.getInstance().markForDeletion(region); this.regions.get(world).remove(region); this.regionCache.get(world).clear(); @@ -199,6 +247,12 @@ public boolean link(World world, String regionName, String handlerName) { public boolean link(IRegion region, IHandler handler) { if (region == null || handler == null || region.getHandlers().contains(handler)) return false; + Sponge.getGame().getEventManager().post(new FGUpdateEvent() { + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); return !(handler instanceof GlobalHandler && !(region instanceof GlobalRegion)) && region.addHandler(handler); } @@ -215,6 +269,12 @@ private boolean unlink(World world, String regionName, String handlerName) { public boolean unlink(IRegion region, IHandler handler) { if (region == null || handler == null || !region.getHandlers().contains(handler)) return false; + Sponge.getGame().getEventManager().post(new FGUpdateEvent() { + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); return !(handler instanceof GlobalHandler && region instanceof GlobalRegion) && region.removeHandler(handler); } @@ -255,7 +315,7 @@ public void populateWorld(World world) { addRegion(world, gr); } - public void unloadWorld(World world){ + public void unloadWorld(World world) { this.regions.remove(world); } @@ -266,6 +326,7 @@ public GlobalHandler getGlobalHandler() { private List calculateRegionsForChunk(Vector3i chunk, World world) { List cache = new ArrayList<>(); this.getRegionsList(world).stream() + .filter(IFGObject::isEnabled) .filter(region -> region.isInChunk(chunk)) .forEach(cache::add); return cache; diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGStorageManager.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGStorageManager.java index 84ccaee..aa043a3 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGStorageManager.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FGStorageManager.java @@ -58,7 +58,7 @@ public static FGStorageManager getInstance() { public synchronized void initHandlers() { Server server = Sponge.getGame().getServer(); - try (Connection conn = FoxGuardMain.instance().getDataSource("jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/foxguard").getConnection()) { + try (Connection conn = FoxGuardMain.instance().getDataSource("jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/foxguard").getConnection()) { try (Statement statement = conn.createStatement()) { statement.execute( "CREATE TABLE IF NOT EXISTS HANDLERS (" + @@ -77,9 +77,9 @@ public synchronized void initWorld(World world) { String dataBaseDir; Server server = Sponge.getGame().getServer(); if (world.getProperties().equals(server.getDefaultWorld().get())) { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/"; } else { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/" + world.getName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/" + world.getName() + "/foxguard/"; } try (Connection conn = FoxGuardMain.instance().getDataSource(dataBaseDir + "foxguard").getConnection()) { try (Statement statement = conn.createStatement()) { @@ -100,14 +100,13 @@ public synchronized void initWorld(World world) { public synchronized void loadHandlers() { Server server = Sponge.getGame().getServer(); - try (Connection conn = FoxGuardMain.instance().getDataSource("jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/foxguard").getConnection()) { + try (Connection conn = FoxGuardMain.instance().getDataSource("jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/foxguard").getConnection()) { try (Statement statement = conn.createStatement()) { try (ResultSet handlerDataSet = statement.executeQuery("SELECT * FROM HANDLERS;")) { while (handlerDataSet.next()) { try { - String databaseDir = "jdbc:h2:./" + - server.getDefaultWorld().get().getWorldName() + "/foxguard/handlers/" + - handlerDataSet.getString("NAME"); + String databaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + + "/foxguard/handlers/" + handlerDataSet.getString("NAME"); DataSource source = FoxGuardMain.instance().getDataSource(databaseDir); try (Connection metaConn = source.getConnection()) { try (Statement metaStatement = metaConn.createStatement()) { @@ -155,16 +154,16 @@ public synchronized void loadHandlers() { deferredHandler.metaEnabled = metaSet.getBoolean("ENABLED"); this.deferedObjects.add(deferredHandler); } else { - FoxGuardMain.instance().logger().info("Found potentially corrupted database."); + FoxGuardMain.instance().logger().warn("Found potentially corrupted database."); markForDeletion(databaseDir); } } else { - FoxGuardMain.instance().logger().info("Found potentially corrupted database."); + FoxGuardMain.instance().logger().warn("Found potentially corrupted database."); markForDeletion(databaseDir); } } } else { - FoxGuardMain.instance().logger().info("Found potentially corrupted database."); + FoxGuardMain.instance().logger().warn("Found potentially corrupted database."); markForDeletion(databaseDir); } } @@ -185,9 +184,9 @@ public synchronized void loadWorldRegions(World world) { Server server = Sponge.getGame().getServer(); String worldDatabaseDir; if (world.getProperties().equals(server.getDefaultWorld().get())) { - worldDatabaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/"; + worldDatabaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/"; } else { - worldDatabaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/" + world.getName() + "/foxguard/"; + worldDatabaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/" + world.getName() + "/foxguard/"; } try (Connection conn = FoxGuardMain.instance().getDataSource(worldDatabaseDir + "foxguard").getConnection()) { @@ -246,23 +245,23 @@ public synchronized void loadWorldRegions(World world) { deferredHandler.metaEnabled = metaSet.getBoolean("ENABLED"); this.deferedObjects.add(deferredHandler); } else { - FoxGuardMain.instance().logger().info("Found potentially corrupted database."); + FoxGuardMain.instance().logger().warn("Found potentially corrupted database."); markForDeletion(databaseDir); } } else { - FoxGuardMain.instance().logger().info("Found potentially corrupted database."); + FoxGuardMain.instance().logger().warn("Found potentially corrupted database."); markForDeletion(databaseDir); } } } else { - FoxGuardMain.instance().logger().info("Found potentially corrupted database."); + FoxGuardMain.instance().logger().warn("Found potentially corrupted database."); markForDeletion(databaseDir); } } } } } catch (SQLException e) { - FoxGuardMain.instance().logger().info("Unable to load Region in world \"" + world.getName() + "\". Perhaps the database is corrupted?"); + FoxGuardMain.instance().logger().error("Unable to load Region in world \"" + world.getName() + "\". Perhaps the database is corrupted?"); e.printStackTrace(); } } @@ -277,9 +276,9 @@ public synchronized void loadWorldLinks(World world) { Server server = Sponge.getGame().getServer(); String dataBaseDir; if (world.getProperties().equals(server.getDefaultWorld().get())) { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/"; } else { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/" + world.getName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/" + world.getName() + "/foxguard/"; } try (Connection conn = FoxGuardMain.instance().getDataSource(dataBaseDir + "foxguard").getConnection()) { @@ -301,9 +300,15 @@ public synchronized void loadLinks() { public synchronized void writeHandlers() { Server server = Sponge.getGame().getServer(); - try (Connection conn = FoxGuardMain.instance().getDataSource("jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/foxguard").getConnection()) { + try (Connection conn = FoxGuardMain.instance().getDataSource("jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/foxguard").getConnection()) { try (Statement statement = conn.createStatement()) { - statement.addBatch("DELETE FROM HANDLERS;"); + statement.addBatch("DROP TABLE IF EXISTS HANDLERS;"); + statement.addBatch( + "CREATE TABLE HANDLERS (" + + "NAME VARCHAR(256), " + + "TYPE VARCHAR(256)," + + "PRIORITY INTEGER," + + "ENABLED BOOLEAN);"); for (IHandler handler : FGManager.getInstance().getHandlerList()) { if (handler.autoSave()) { statement.addBatch("INSERT INTO HANDLERS(NAME, TYPE, PRIORITY, ENABLED) VALUES ('" + @@ -322,7 +327,7 @@ public synchronized void writeHandlers() { FGManager.getInstance().getHandlerList().stream().filter(IFGObject::autoSave).forEach(handler -> { try { DataSource source = FoxGuardMain.instance().getDataSource( - "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + + "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/handlers/" + handler.getName()); try (Connection conn = source.getConnection()) { try (Statement statement = conn.createStatement()) { @@ -355,9 +360,9 @@ public synchronized void writeWorld(World world) { Server server = Sponge.getGame().getServer(); String dataBaseDir; if (world.getProperties().equals(server.getDefaultWorld().get())) { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/"; } else { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/" + world.getName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/" + world.getName() + "/foxguard/"; } try (Connection conn = FoxGuardMain.instance().getDataSource(dataBaseDir + "foxguard").getConnection()) { try (Statement statement = conn.createStatement()) { @@ -423,9 +428,9 @@ public synchronized void updateRegion(IRegion region) { World world = region.getWorld(); String dataBaseDir; if (world.getProperties().equals(server.getDefaultWorld().get())) { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/"; } else { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/" + world.getName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/" + world.getName() + "/foxguard/"; } try { DataSource source = FoxGuardMain.instance().getDataSource(dataBaseDir + "regions/" + region.getName()); @@ -462,7 +467,7 @@ public synchronized void updateHandler(IHandler handler) { Server server = Sponge.getGame().getServer(); try { DataSource source = FoxGuardMain.instance().getDataSource( - "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + + "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/handlers/" + handler.getName()); try (Connection conn = source.getConnection()) { try (Statement statement = conn.createStatement()) { @@ -497,9 +502,9 @@ private synchronized void updateLists() { for (World world : Sponge.getGame().getServer().getWorlds()) { String dataBaseDir; if (world.getProperties().equals(server.getDefaultWorld().get())) { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/"; } else { - dataBaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/" + world.getName() + "/foxguard/"; + dataBaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/" + world.getName() + "/foxguard/"; } try (Connection conn = FoxGuardMain.instance().getDataSource(dataBaseDir + "foxguard").getConnection()) { try (Statement statement = conn.createStatement()) { @@ -523,7 +528,7 @@ private synchronized void updateLists() { e.printStackTrace(); } } - try (Connection conn = FoxGuardMain.instance().getDataSource("jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/foxguard").getConnection()) { + try (Connection conn = FoxGuardMain.instance().getDataSource("jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/foxguard").getConnection()) { try (Statement statement = conn.createStatement()) { statement.addBatch("DELETE FROM HANDLERS;"); for (IHandler handler : FGManager.getInstance().getHandlerList()) { @@ -562,14 +567,14 @@ public synchronized void markForDeletion(IFGObject object) { IRegion region = (IRegion) object; String databaseDir; if (region.getWorld().getProperties().equals(server.getDefaultWorld().get())) { - databaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/regions/"; + databaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/regions/"; } else { - databaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/" + region.getWorld().getName() + "/foxguard/regions/"; + databaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/" + region.getWorld().getName() + "/foxguard/regions/"; } databaseDir += region.getName(); markForDeletion(databaseDir); } else if (object instanceof IHandler) { - String databaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/handlers/" + object.getName(); + String databaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/handlers/" + object.getName(); markForDeletion(databaseDir); } } @@ -591,15 +596,15 @@ public synchronized void unmarkForDeletion(IFGObject object) { IRegion region = (IRegion) object; String databaseDir; if (region.getWorld().getProperties().equals(server.getDefaultWorld().get())) { - databaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/regions/"; + databaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/regions/"; } else { - databaseDir = "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/" + region.getWorld().getName() + "/foxguard/regions/"; + databaseDir = "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/" + region.getWorld().getName() + "/foxguard/regions/"; } databaseDir += region.getName(); unmarkForDeletion(databaseDir); } else if (object instanceof IHandler) { if (server.getDefaultWorld().isPresent()) { - unmarkForDeletion("jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/handlers/" + object.getName()); + unmarkForDeletion("jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/handlers/" + object.getName()); } } } @@ -623,9 +628,9 @@ public synchronized void resolveDeferredObjects() { try { IFGObject result = o.resolve(); if (result == null) - FoxGuardMain.instance().logger().info("Unable to resolve deferred object:\n" + o.toString()); + FoxGuardMain.instance().logger().warn("Unable to resolve deferred object:\n" + o.toString()); } catch (SQLException e) { - FoxGuardMain.instance().logger().info("Unable to resolve deferred object:\n" + o.toString()); + FoxGuardMain.instance().logger().warn("Unable to resolve deferred object:\n" + o.toString()); e.printStackTrace(); } } @@ -635,7 +640,7 @@ public synchronized void loadGlobalHandler() { Server server = Sponge.getGame().getServer(); try { FGManager.getInstance().getGlobalHandler().loadFromDatabase(FoxGuardMain.instance().getDataSource( - "jdbc:h2:./" + server.getDefaultWorld().get().getWorldName() + "/foxguard/handlers/" + GlobalHandler.NAME + "jdbc:h2:" + Sponge.getGame().getSavesDirectory().resolve(server.getDefaultWorldName()) + "/foxguard/handlers/" + GlobalHandler.NAME )); } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/Flag.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/Flag.java similarity index 85% rename from src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/Flag.java rename to src/main/java/net/foxdenstudio/sponge/foxguard/plugin/Flag.java index 310a5b3..5fd30b4 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/util/Flag.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/Flag.java @@ -23,28 +23,36 @@ * THE SOFTWARE. */ -package net.foxdenstudio.sponge.foxguard.plugin.handler.util; +package net.foxdenstudio.sponge.foxguard.plugin; import org.spongepowered.api.util.Tristate; public enum Flag { + ROOT(null, true, "root", "Everything"), BLOCK(ROOT, true, "block", "Blocks"), - BLOCK_PLACE(BLOCK, true, "blockplace", "Place-Blocks"), - BLOCK_BREAK(BLOCK, true, "blockbreak", "Break-Blocks"), - BLOCK_MODIFY(BLOCK, true, "blockmodify", "Modify-Blocks"), + BLOCK_CHANGE(BLOCK, true, "blockchange", "Change-Blocks"), + BLOCK_PLACE(BLOCK_CHANGE, true, "blockplace", "Place-Blocks"), + BLOCK_BREAK(BLOCK_CHANGE, true, "blockbreak", "Break-Blocks"), + BLOCK_MODIFY(BLOCK_CHANGE, true, "blockmodify", "Modify-Blocks"), BLOCK_INTERACT(BLOCK, true, "blockclick", "Click-Blocks"), BLOCK_INTERACT_PRIMARY(BLOCK_INTERACT, true, "blockattack", "Attack-Blocks"), BLOCK_INTERACT_SECONDARY(BLOCK_INTERACT, true, "blockinteract", "Interact-Blocks"), + ENTITY_INTERACT(ROOT, true, "entityclick", "Click-Entities"), ENTITY_INTERACT_PRIMARY(ENTITY_INTERACT, true, "entityattack", "Attack-Entities"), ENTITY_INTERACT_SECONDARY(ENTITY_INTERACT, true, "entityinteract", "Interact-Entities"), - BLOCK_FLUID(BLOCK, true, "fluids", "Fluids"), PLAYER_INTERACT_PRIMARY(ENTITY_INTERACT_PRIMARY, true, "playerattack", "Attack-Player"), + SPAWN_MOB(ROOT, true, "spawnmob", "Spawn-Mobs"), SPAWN_MOB_HOSTILE(SPAWN_MOB, true, "spawnmobhostile", "Spawn-Hostile-Mobs"), - SPAWN_MOB_PASSIVE(SPAWN_MOB, true, "spawnmobpassive", "Spawn-Passive-Mobs"); + SPAWN_MOB_PASSIVE(SPAWN_MOB, true, "spawnmobpassive", "Spawn-Passive-Mobs"), + + PLAYER_PASS(ROOT, true, "playerpass", "Player-Pass-Borders"), + PLAYER_ENTER(PLAYER_PASS, true, "playerenter", "Player-Enter"), + PLAYER_EXIT(PLAYER_PASS, true, "playerexit", "Player-Exit"); + final String humanName; final String flagName; diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java index 907895c..9bc1de5 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/FoxGuardMain.java @@ -36,6 +36,7 @@ import net.foxdenstudio.sponge.foxguard.plugin.command.handlers.CommandPriority; import net.foxdenstudio.sponge.foxguard.plugin.listener.BlockEventListener; import net.foxdenstudio.sponge.foxguard.plugin.listener.InteractListener; +import net.foxdenstudio.sponge.foxguard.plugin.listener.PlayerMoveListener; import net.foxdenstudio.sponge.foxguard.plugin.listener.SpawnEntityEventListener; import net.foxdenstudio.sponge.foxguard.plugin.state.HandlersStateField; import net.foxdenstudio.sponge.foxguard.plugin.state.RegionsStateField; @@ -50,6 +51,7 @@ import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.action.InteractEvent; import org.spongepowered.api.event.block.ChangeBlockEvent; +import org.spongepowered.api.event.entity.DisplaceEntityEvent; import org.spongepowered.api.event.entity.SpawnEntityEvent; import org.spongepowered.api.event.game.state.*; import org.spongepowered.api.event.world.LoadWorldEvent; @@ -166,13 +168,14 @@ public void serverStarted(GameStartedServerEvent event) { logger.info("Loading Handlers"); FGStorageManager.getInstance().loadHandlers(); FGStorageManager.getInstance().loadGlobalHandler(); - loaded = true; - logger.info("Loading Links"); - FGStorageManager.getInstance().loadLinks(); + if (FGConfigManager.getInstance().forceLoad()) { logger.info("Resolving deferred objects"); FGStorageManager.getInstance().resolveDeferredObjects(); } + loaded = true; + logger.info("Loading linkages"); + FGStorageManager.getInstance().loadLinks(); logger.info("Saving Handlers"); FGStorageManager.getInstance().writeHandlers(); logger.info("Saving World data"); @@ -180,6 +183,7 @@ public void serverStarted(GameStartedServerEvent event) { logger.info("Saving data for World: \"" + world.getName() + "\""); FGStorageManager.getInstance().writeWorld(world); } + } @Listener @@ -303,6 +307,11 @@ private void registerListeners() { eventManager.registerListener(this, ChangeBlockEvent.class, new BlockEventListener()); eventManager.registerListener(this, InteractEvent.class, new InteractListener()); eventManager.registerListener(this, SpawnEntityEvent.class, new SpawnEntityEventListener()); + if (FGConfigManager.getInstance().getModules().get(FGConfigManager.Module.MOVEMENT)) { + PlayerMoveListener pml = new PlayerMoveListener(); + eventManager.registerListener(this, DisplaceEntityEvent.Move.TargetPlayer.class, pml); + eventManager.registerListeners(this, pml.new Listeners()); + } } /** diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java index 02ba1ef..fad15e9 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandEnableDisable.java @@ -29,6 +29,8 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.state.FCStateManager; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; +import net.foxdenstudio.sponge.foxguard.plugin.event.FGUpdateEvent; import net.foxdenstudio.sponge.foxguard.plugin.handler.GlobalHandler; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; @@ -44,6 +46,7 @@ import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.args.ArgumentParseException; import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.cause.Cause; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; @@ -101,9 +104,21 @@ public CommandResult process(CommandSource source, String arguments) throws Comm } FCStateManager.instance().getStateMap().get(source).flush(RegionsStateField.ID, HandlersStateField.ID); if (successes == 1 && failures == 0) { + Sponge.getGame().getEventManager().post(new FGUpdateEvent() { + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " object!")); return CommandResult.success(); } else if (successes > 0) { + Sponge.getGame().getEventManager().post(new FGUpdateEvent() { + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " objects with " + successes + " successes" + (failures > 0 ? " and " + failures + " failures!" : "!"))); return CommandResult.builder().successCount(successes).build(); @@ -147,9 +162,21 @@ public CommandResult process(CommandSource source, String arguments) throws Comm } FCStateManager.instance().getStateMap().get(source).flush(RegionsStateField.ID); if (successes == 1 && failures == 0) { + Sponge.getGame().getEventManager().post(new FGUpdateEvent() { + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " region!")); return CommandResult.success(); } else if (successes > 0) { + Sponge.getGame().getEventManager().post(new FGUpdateEvent() { + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " regions with " + successes + " successes" + (failures > 0 ? " and " + failures + " failures!" : "!"))); return CommandResult.builder().successCount(successes).build(); @@ -182,9 +209,21 @@ public CommandResult process(CommandSource source, String arguments) throws Comm } FCStateManager.instance().getStateMap().get(source).flush(HandlersStateField.ID); if (successes == 1 && failures == 0) { + Sponge.getGame().getEventManager().post(new FGUpdateEvent() { + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " handler!")); return CommandResult.success(); } else if (successes > 0) { + Sponge.getGame().getEventManager().post(new FGUpdateEvent() { + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); source.sendMessage(Text.of(TextColors.GREEN, "Successfully " + (this.enableState ? "enabled" : "disabled") + " handlers with " + successes + " successes" + (failures > 0 ? " and " + failures + " failures!" : "!"))); return CommandResult.builder().successCount(successes).build(); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandModify.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandModify.java index 70b70a6..ce4a06b 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandModify.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/command/CommandModify.java @@ -30,6 +30,8 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; +import net.foxdenstudio.sponge.foxguard.plugin.event.FGUpdateObjectEvent; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import net.foxdenstudio.sponge.foxguard.plugin.region.IRegion; @@ -40,6 +42,7 @@ import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.args.ArgumentParseException; import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.cause.Cause; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.util.GuavaCollectors; @@ -99,6 +102,17 @@ public CommandResult process(CommandSource source, String arguments) throws Comm ProcessResult result = region.modify(source, parse.args.length < 3 ? "" : parse.args[2]); if (result.isSuccess()) { FGManager.getInstance().clearCache(world); + Sponge.getGame().getEventManager().post(new FGUpdateObjectEvent() { + @Override + public IFGObject getTarget() { + return region; + } + + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); if (result.getMessage().isPresent()) { if (!FCHelper.hasColor(result.getMessage().get())) { source.sendMessage(result.getMessage().get().toBuilder().color(TextColors.GREEN).build()); @@ -126,6 +140,17 @@ public CommandResult process(CommandSource source, String arguments) throws Comm throw new CommandException(Text.of("No Handler with name \"" + parse.args[1] + "\"!")); ProcessResult result = handler.modify(source, parse.args.length < 3 ? "" : parse.args[2]); if (result.isSuccess()) { + Sponge.getGame().getEventManager().post(new FGUpdateObjectEvent() { + @Override + public IFGObject getTarget() { + return handler; + } + + @Override + public Cause getCause() { + return Cause.of(FoxGuardMain.instance()); + } + }); if (result.getMessage().isPresent()) { if (!FCHelper.hasColor(result.getMessage().get())) { source.sendMessage(result.getMessage().get().toBuilder().color(TextColors.GREEN).build()); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FGUpdateEvent.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FGUpdateEvent.java new file mode 100644 index 0000000..19e5fc3 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FGUpdateEvent.java @@ -0,0 +1,30 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) 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.foxdenstudio.sponge.foxguard.plugin.event; + +public interface FGUpdateEvent extends FoxGuardEvent { + +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FGUpdateObjectEvent.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FGUpdateObjectEvent.java new file mode 100644 index 0000000..43f9e05 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FGUpdateObjectEvent.java @@ -0,0 +1,34 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) 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.foxdenstudio.sponge.foxguard.plugin.event; + +import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; + +public interface FGUpdateObjectEvent extends FGUpdateEvent { + + IFGObject getTarget(); + +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FoxGuardEvent.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FoxGuardEvent.java new file mode 100644 index 0000000..636a39d --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/event/FoxGuardEvent.java @@ -0,0 +1,31 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) 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.foxdenstudio.sponge.foxguard.plugin.event; + +import org.spongepowered.api.event.Event; + +public interface FoxGuardEvent extends Event { +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GlobalHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GlobalHandler.java index 6b8dcf7..5d82293 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GlobalHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/GlobalHandler.java @@ -30,7 +30,8 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.source.ProxySource; @@ -89,13 +90,13 @@ public String getUniqueTypeString() { } @Override - public Tristate handle(User user, Flag flag, Event event) { + public EventResult handle(User user, Flag flag, Event event) { Flag temp = flag; while (temp != null && !map.containsKey(temp)) { temp = temp.getParent(); } - if (temp != null) return map.get(temp); - else return map.get(flag); + if (temp != null) return EventResult.of(map.get(temp)); + else return EventResult.of(map.get(flag)); } @@ -118,29 +119,33 @@ public ProcessResult modify(CommandSource source, String arguments) throws Comma return ProcessResult.of(false, Text.of("Not a valid flag!")); } } - if (isIn(CLEAR_ALIASES, parse.args[2])) { - if (flag == null) { - this.map.clear(); - return ProcessResult.of(true, Text.of("Successfully cleared flags!")); - } else { - this.map.remove(flag); - return ProcessResult.of(true, Text.of("Successfully cleared flag!")); - } - } else { - Tristate tristate = tristateFrom(parse.args[2]); - if (tristate == null) { - return ProcessResult.of(false, Text.of("Not a valid value!")); - } - if (flag == null) { - for (Flag thatExist : Flag.values()) { - this.map.put(thatExist, tristate); + if (parse.args.length > 2) { + if (isIn(CLEAR_ALIASES, parse.args[2])) { + if (flag == null) { + this.map.clear(); + return ProcessResult.of(true, Text.of("Successfully cleared flags!")); + } else { + this.map.remove(flag); + return ProcessResult.of(true, Text.of("Successfully cleared flag!")); } - return ProcessResult.of(true, Text.of("Successfully set flags!")); } else { - this.map.put(flag, tristate); - return ProcessResult.of(true, Text.of("Successfully set flag!")); - } + Tristate tristate = tristateFrom(parse.args[2]); + if (tristate == null) { + return ProcessResult.of(false, Text.of("Not a valid value!")); + } + if (flag == null) { + for (Flag thatExist : Flag.values()) { + this.map.put(thatExist, tristate); + } + return ProcessResult.of(true, Text.of("Successfully set flags!")); + } else { + this.map.put(flag, tristate); + return ProcessResult.of(true, Text.of("Successfully set flag!")); + } + } + } else { + return ProcessResult.of(false, Text.of("Must specify a value!")); } } else { return ProcessResult.of(false, Text.of("Must specify a flag!")); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/IHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/IHandler.java index e6f8510..2416482 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/IHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/IHandler.java @@ -25,17 +25,17 @@ package net.foxdenstudio.sponge.foxguard.plugin.handler; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import org.spongepowered.api.entity.living.player.User; import org.spongepowered.api.event.Event; -import org.spongepowered.api.util.Tristate; import javax.annotation.Nullable; public interface IHandler extends IFGObject, Comparable { - Tristate handle(@Nullable User user, Flag flag, Event event); + EventResult handle(@Nullable User user, Flag flag, Event event); int getPriority(); diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PassiveHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PassiveHandler.java index a665b83..15a0d4d 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PassiveHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PassiveHandler.java @@ -30,8 +30,9 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; @@ -70,16 +71,16 @@ public PassiveHandler(String name, int priority, CallbackHashMap } @Override - public Tristate handle(@Nullable User user, Flag flag, Event event) { + public EventResult handle(@Nullable User user, Flag flag, Event event) { if (user != null) { - return Tristate.UNDEFINED; + return EventResult.pass(); } Flag temp = flag; while (temp != null && !map.containsKey(temp)) { temp = temp.getParent(); } - if (temp != null) return map.get(temp); - else return map.get(flag); + if (temp != null) return EventResult.of(map.get(temp)); + else return EventResult.of(map.get(flag)); } @Override diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java index babdd95..02c28de 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/PermissionHandler.java @@ -27,13 +27,13 @@ import com.google.common.collect.ImmutableList; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.entity.living.player.User; import org.spongepowered.api.event.Event; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; -import org.spongepowered.api.util.Tristate; import javax.annotation.Nullable; import javax.sql.DataSource; @@ -47,18 +47,18 @@ public PermissionHandler(String name, int priority) { } @Override - public Tristate handle(@Nullable User user, Flag flag, Event event) { - if (user == null) return Tristate.UNDEFINED; + public EventResult handle(@Nullable User user, Flag flag, Event event) { + if (user == null) return EventResult.pass(); while (flag != null) { if (user.hasPermission("foxguard.handler." + this.name.toLowerCase() + "." + flag.flagName() + ".allow")) - return Tristate.TRUE; + return EventResult.allow(); else if (user.hasPermission("foxguard.handler." + this.name.toLowerCase() + "." + flag.flagName() + ".deny")) - return Tristate.FALSE; + return EventResult.deny(); else if (user.hasPermission("foxguard.handler." + this.name.toLowerCase() + "." + flag.flagName() + ".pass")) - return Tristate.UNDEFINED; + return EventResult.pass(); flag = flag.getParent(); } - return Tristate.UNDEFINED; + return EventResult.pass(); } @Override @@ -79,7 +79,7 @@ public String getUniqueTypeString() { @Override public Text getDetails(String arguments) { return Text.builder() - .append(Text.of(TextColors.GOLD, "Permission: ")) + .append(Text.of(TextColors.GOLD, "Permission:\n")) .append(Text.of(TextColors.RESET, "foxguard.handler.")) .append(Text.of(TextColors.YELLOW, this.getName())) .append(Text.of(TextColors.RESET, ".")) diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/SimpleHandler.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/SimpleHandler.java index f7b1b69..f183f89 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/SimpleHandler.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/handler/SimpleHandler.java @@ -30,8 +30,9 @@ import net.foxdenstudio.sponge.foxcore.plugin.command.util.AdvCmdParse; import net.foxdenstudio.sponge.foxcore.plugin.command.util.ProcessResult; import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.listener.util.EventResult; import net.foxdenstudio.sponge.foxguard.plugin.object.IMembership; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandException; @@ -345,26 +346,27 @@ public List modifySuggestions(CommandSource source, String arguments) th } @Override - public Tristate handle(User user, Flag flag, Event event) { + public EventResult handle(User user, Flag flag, Event event) { if (user == null) { switch (this.passiveOption) { case OWNER: - return getResult(this.ownerPermissions, flag); + return EventResult.of(getResult(this.ownerPermissions, flag)); case MEMBER: - return getResult(this.memberPermissions, flag); + return EventResult.of(getResult(this.memberPermissions, flag)); case DEFAULT: - return getResult(this.defaultPermissions, flag); + return EventResult.of(getResult(this.defaultPermissions, flag)); case ALLOW: - return Tristate.TRUE; + return EventResult.allow(); case DENY: - return Tristate.FALSE; + return EventResult.deny(); case PASSTHROUGH: - return Tristate.UNDEFINED; + return EventResult.pass(); } } - if (FCHelper.isUserOnList(this.ownerList, user)) return getResult(this.ownerPermissions, flag); - else if (FCHelper.isUserOnList(this.memberList, user)) return getResult(this.memberPermissions, flag); - else return getResult(this.defaultPermissions, flag); + if (FCHelper.isUserOnList(this.ownerList, user)) return EventResult.of(getResult(this.ownerPermissions, flag)); + else if (FCHelper.isUserOnList(this.memberList, user)) + return EventResult.of(getResult(this.memberPermissions, flag)); + else return EventResult.of(getResult(this.defaultPermissions, flag)); } private Tristate getResult(Map map, Flag flag) { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/BlockEventListener.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/BlockEventListener.java index c890f03..f56f01f 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/BlockEventListener.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/BlockEventListener.java @@ -29,8 +29,8 @@ import com.flowpowered.math.vector.Vector3i; import net.foxdenstudio.sponge.foxcore.plugin.command.CommandDebug; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import org.spongepowered.api.block.BlockSnapshot; import org.spongepowered.api.block.BlockTypes; @@ -40,6 +40,7 @@ import org.spongepowered.api.event.EventListener; import org.spongepowered.api.event.block.ChangeBlockEvent; import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.chat.ChatTypes; import org.spongepowered.api.util.Tristate; import org.spongepowered.api.world.World; @@ -74,7 +75,6 @@ public void handle(ChangeBlockEvent event) throws Exception { else if (event instanceof ChangeBlockEvent.Place) typeFlag = Flag.BLOCK_PLACE; else return; - //FoxGuardMain.instance().getLogger().info(player.getName()); List handlerList = new ArrayList<>(); @@ -102,14 +102,16 @@ public void handle(ChangeBlockEvent event) throws Exception { if (handler.getPriority() < currPriority && flagState != Tristate.UNDEFINED) { break; } - flagState = flagState.and(handler.handle(user, typeFlag, event)); + flagState = flagState.and(handler.handle(user, typeFlag, event).getState()); currPriority = handler.getPriority(); } flagState = typeFlag.resolve(flagState); if (user instanceof Player && CommandDebug.instance().getDebug().get(user)) { - Vector3i vec = event.getTransactions().get(0).getOriginal().getPosition(); - ((Player) user).sendMessage(Text.of("Block action denied at (" + vec.getX() + ", " + vec.getY() + ", " + vec.getZ() + ")" - + (event.getTransactions().size() > 1 ? " and " + (event.getTransactions().size() - 1) + " other positions" : "") + "!")); + if (flagState == Tristate.FALSE) { + Vector3i vec = event.getTransactions().get(0).getOriginal().getPosition(); + ((Player) user).sendMessage(Text.of("Block action denied at (" + vec.getX() + ", " + vec.getY() + ", " + vec.getZ() + ")" + + (event.getTransactions().size() > 1 ? " and " + (event.getTransactions().size() - 1) + " other positions" : "") + "!")); + } } else { if (flagState == Tristate.FALSE) { if (user instanceof Player) { @@ -122,7 +124,7 @@ public void handle(ChangeBlockEvent event) throws Exception { break; } } - if (flag) player.sendMessage(Text.of("You don't have permission!")); + if (flag) player.sendMessage(ChatTypes.ACTION_BAR, Text.of("You don't have permission!")); } } else { diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/ExplosionEventListener.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/ExplosionEventListener.java new file mode 100644 index 0000000..c58ece2 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/ExplosionEventListener.java @@ -0,0 +1,36 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) 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.foxdenstudio.sponge.foxguard.plugin.listener; + +import org.spongepowered.api.event.EventListener; +import org.spongepowered.api.event.world.ExplosionEvent; + +public class ExplosionEventListener implements EventListener { + @Override + public void handle(ExplosionEvent event) throws Exception { + + } +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/InteractListener.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/InteractListener.java index debd118..267c8b3 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/InteractListener.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/InteractListener.java @@ -29,8 +29,8 @@ import com.flowpowered.math.vector.Vector3d; import com.flowpowered.math.vector.Vector3i; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.entity.living.player.User; @@ -39,6 +39,7 @@ import org.spongepowered.api.event.block.InteractBlockEvent; import org.spongepowered.api.event.entity.InteractEntityEvent; import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.chat.ChatTypes; import org.spongepowered.api.util.Tristate; import org.spongepowered.api.world.World; @@ -99,13 +100,13 @@ public void handle(InteractEvent event) throws Exception { if (handler.getPriority() < currPriority && flagState != Tristate.UNDEFINED) { break; } - flagState = flagState.and(handler.handle(user, typeFlag, event)); + flagState = flagState.and(handler.handle(user, typeFlag, event).getState()); currPriority = handler.getPriority(); } flagState = typeFlag.resolve(flagState); if (flagState == Tristate.FALSE) { if (user instanceof Player) - ((Player) user).sendMessage(Text.of("You don't have permission!")); + ((Player) user).sendMessage(ChatTypes.ACTION_BAR, Text.of("You don't have permission!")); event.setCancelled(true); } else { //makes sure that handlers are unable to cancel the event directly. diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/PlayerMoveListener.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/PlayerMoveListener.java index 14f55ce..6241777 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/PlayerMoveListener.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/PlayerMoveListener.java @@ -4,40 +4,64 @@ import com.flowpowered.math.vector.Vector3d; import com.flowpowered.math.vector.Vector3i; import com.google.common.collect.ImmutableList; +import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; +import net.foxdenstudio.sponge.foxguard.plugin.event.FGUpdateEvent; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.EventListener; +import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.entity.DisplaceEntityEvent; +import org.spongepowered.api.event.network.ClientConnectionEvent; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.chat.ChatTypes; +import org.spongepowered.api.util.Tristate; import org.spongepowered.api.world.World; +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; +import java.util.List; +import java.util.Map; /** * Created by Fox on 1/4/2016. * Project: SpongeForge */ -public class PlayerMoveListener implements EventListener { +public class PlayerMoveListener implements EventListener { + + private final LastWrapper EMPTY_LAST_WRAPPER = new LastWrapper(null, null); + + private final Map last = new CallbackHashMap<>((key, map) -> EMPTY_LAST_WRAPPER); + @Override - public void handle(DisplaceEntityEvent.Move.TargetPlayer event) throws Exception { + public void handle(DisplaceEntityEvent event) throws Exception { if (event.isCancelled()) return; - Player player = event.getTargetEntity(); - World world = player.getWorld(); - Vector3d from = event.getFromTransform().getPosition(), to = event.getToTransform().getPosition(); - LinkedList fromList = new LinkedList<>(), toList = new LinkedList<>(); - - FGManager.getInstance().getRegionsList(world, new Vector3i( - GenericMath.floor(from.getX() / 16.0), - GenericMath.floor(from.getY() / 16.0), - GenericMath.floor(from.getZ() / 16.0))).stream() - .filter(region -> region.isInRegion(from)) - .filter(IFGObject::isEnabled) - .forEach(region -> region.getHandlers().stream() - .filter(IFGObject::isEnabled) - .filter(handler -> !fromList.contains(handler)) - .forEach(fromList::add)); + Player player; + if (event instanceof DisplaceEntityEvent.TargetPlayer) + player = ((DisplaceEntityEvent.TargetPlayer) event).getTargetEntity(); + else if (event.getTargetEntity().getPassenger().isPresent() && event.getTargetEntity().getPassenger().get() instanceof Player) + player = (Player) event.getTargetEntity().getPassenger().get(); + else return; + World world = event.getTargetEntity().getWorld(); + List fromList = last.get(player).list, toList = new ArrayList<>(); + Vector3d to = event.getToTransform().getPosition().add(0, 0.1, 0); + if (fromList == null) { + fromList = new ArrayList<>(); + final List temp = fromList; + Vector3d from = event.getFromTransform().getPosition().add(0, 0.1, 0); + FGManager.getInstance().getRegionsList(world, new Vector3i( + GenericMath.floor(from.getX() / 16.0), + GenericMath.floor(from.getY() / 16.0), + GenericMath.floor(from.getZ() / 16.0))).stream() + .filter(region -> region.isInRegion(from)) + .filter(IFGObject::isEnabled) + .forEach(region -> region.getHandlers().stream() + .filter(IFGObject::isEnabled) + .filter(handler -> !temp.contains(handler)) + .forEach(temp::add)); + } FGManager.getInstance().getRegionsList(world, new Vector3i( GenericMath.floor(to.getX() / 16.0), @@ -50,17 +74,103 @@ public void handle(DisplaceEntityEvent.Move.TargetPlayer event) throws Exception .filter(handler -> !toList.contains(handler)) .forEach(toList::add)); + //System.out.println(fromList); + //System.out.println(toList); + + final List toComplete = new ArrayList<>(toList); + final List temp = fromList; ImmutableList.copyOf(fromList).stream() .filter(toList::contains) .forEach(handler -> { - fromList.remove(handler); + temp.remove(handler); toList.remove(handler); }); + List finalList = new ArrayList<>(); + fromList.stream() + .map(handler -> new HandlerWrapper(handler, Type.FROM)) + .forEach(finalList::add); + toList.stream() + .map(handler -> new HandlerWrapper(handler, Type.TO)) + .forEach(finalList::add); + if (finalList.size() == 0) { + this.last.put(player, new LastWrapper(toComplete, event.getToTransform().getPosition())); + return; + } + Collections.sort(finalList); + int currPriority = finalList.get(0).handler.getPriority(); + Tristate flagState = Tristate.UNDEFINED; + for (HandlerWrapper wrap : finalList) { + if (wrap.handler.getPriority() < currPriority && flagState != Tristate.UNDEFINED) { + break; + } + if (wrap.type == Type.FROM) { + flagState = flagState.and(wrap.handler.handle(player, Flag.PLAYER_EXIT, event).getState()); + } else { + flagState = flagState.and(wrap.handler.handle(player, Flag.PLAYER_ENTER, event).getState()); + } + currPriority = wrap.handler.getPriority(); + } + flagState = Flag.PLAYER_PASS.resolve(flagState); - Collections.sort(fromList); - Collections.sort(toList); + if (flagState == Tristate.FALSE) { + player.sendMessage(ChatTypes.ACTION_BAR, Text.of("You don't have permission to pass!")); + Vector3d position = this.last.get(player).position; + if (position == null) position = event.getFromTransform().getPosition(); + event.setToTransform(event.getToTransform().setPosition(position)); + } else { + this.last.put(player, new LastWrapper(toComplete, event.getToTransform().getPosition())); + //makes sure that handlers are unable to cancel the event directly. + event.setCancelled(false); + } + + } + + private enum Type { + FROM, TO + } + + private class HandlerWrapper implements Comparable { + public IHandler handler; + public Type type; + + public HandlerWrapper(IHandler handler, Type type) { + this.handler = handler; + this.type = type; + } + + @Override + public int compareTo(HandlerWrapper w) { + int val = handler.compareTo(w.handler); + if (val != 0) return val; + else return type.compareTo(w.type); + } + + @Override + public String toString() { + return this.type + ":" + this.handler; + } + } + + private class LastWrapper { + public List list; + public Vector3d position; + + public LastWrapper(List list, Vector3d position) { + this.list = list; + this.position = position; + } + } + public class Listeners { + @Listener + public void onJoin(ClientConnectionEvent.Join event) { + last.put(event.getTargetEntity(), new LastWrapper(null, event.getTargetEntity().getTransform().getPosition())); + } + @Listener + public void onChange(FGUpdateEvent event) { + last.clear(); + } } } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/SpawnEntityEventListener.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/SpawnEntityEventListener.java index f74cae9..e3e2959 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/SpawnEntityEventListener.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/SpawnEntityEventListener.java @@ -29,8 +29,8 @@ import com.flowpowered.math.vector.Vector3d; import com.flowpowered.math.vector.Vector3i; import net.foxdenstudio.sponge.foxguard.plugin.FGManager; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; import net.foxdenstudio.sponge.foxguard.plugin.object.IFGObject; import org.spongepowered.api.entity.Entity; import org.spongepowered.api.entity.living.Creature; @@ -40,6 +40,7 @@ import org.spongepowered.api.event.EventListener; import org.spongepowered.api.event.entity.SpawnEntityEvent; import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.chat.ChatTypes; import org.spongepowered.api.util.Tristate; import org.spongepowered.api.world.World; @@ -95,12 +96,12 @@ public void handle(SpawnEntityEvent event) throws Exception { if (handler.getPriority() < currPriority && flagState != Tristate.UNDEFINED) { break; } - flagState = flagState.and(handler.handle(user, typeFlag, event)); + flagState = flagState.and(handler.handle(user, typeFlag, event).getState()); currPriority = handler.getPriority(); } if (flagState == Tristate.FALSE) { if (user instanceof Player) - ((Player) user).sendMessage(Text.of("You don't have permission!")); + ((Player) user).sendMessage(ChatTypes.ACTION_BAR, Text.of("You don't have permission!")); event.setCancelled(true); } else { //makes sure that handlers are unable to cancel the event directly. diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/util/EventResult.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/util/EventResult.java new file mode 100644 index 0000000..2960b32 --- /dev/null +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/listener/util/EventResult.java @@ -0,0 +1,74 @@ +/* + * This file is part of FoxGuard, licensed under the MIT License (MIT). + * + * Copyright (c) gravityfox - https://gravityfox.net/ + * Copyright (c) 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.foxdenstudio.sponge.foxguard.plugin.listener.util; + +import org.spongepowered.api.text.Text; +import org.spongepowered.api.util.Tristate; + +import java.util.Optional; + +public final class EventResult { + + private static final EventResult SUCCESS = of(Tristate.TRUE); + private static final EventResult PASSTHROUGH = of(Tristate.UNDEFINED); + private static final EventResult FAILURE = of(Tristate.FALSE); + + private final Tristate state; + private final Optional message; + + private EventResult(Tristate success, Optional message) { + this.state = success; + this.message = message; + } + + public static EventResult of(Tristate state) { + return new EventResult(state, Optional.empty()); + } + + public static EventResult of(Tristate success, Text message) { + return new EventResult(success, Optional.of(message)); + } + + public static EventResult allow() { + return SUCCESS; + } + + public static EventResult pass() { + return PASSTHROUGH; + } + + public static EventResult deny() { + return FAILURE; + } + + public Tristate getState() { + return state; + } + + public Optional getMessage() { + return message; + } +} diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGHandlerFactory.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGHandlerFactory.java index d77e32a..3c3f0c9 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGHandlerFactory.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/object/factory/FGHandlerFactory.java @@ -29,12 +29,12 @@ import net.foxdenstudio.sponge.foxcore.common.FCHelper; import net.foxdenstudio.sponge.foxcore.plugin.util.Aliases; import net.foxdenstudio.sponge.foxcore.plugin.util.CallbackHashMap; +import net.foxdenstudio.sponge.foxguard.plugin.Flag; import net.foxdenstudio.sponge.foxguard.plugin.FoxGuardMain; import net.foxdenstudio.sponge.foxguard.plugin.handler.IHandler; import net.foxdenstudio.sponge.foxguard.plugin.handler.PassiveHandler; import net.foxdenstudio.sponge.foxguard.plugin.handler.PermissionHandler; import net.foxdenstudio.sponge.foxguard.plugin.handler.SimpleHandler; -import net.foxdenstudio.sponge.foxguard.plugin.handler.util.Flag; import org.spongepowered.api.command.CommandException; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.entity.living.player.Player; diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/ElevationRegion.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/ElevationRegion.java index 9c612ea..d2fa2b2 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/ElevationRegion.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/ElevationRegion.java @@ -108,7 +108,7 @@ public boolean isInRegion(int x, int y, int z) { @Override public boolean isInRegion(double x, double y, double z) { - return y >= lowerBound && y <= upperBound; + return y >= lowerBound && y <= upperBound + 1; } @Override diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/util/BoundingBox2.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/util/BoundingBox2.java index 37cbb70..95bcbb2 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/util/BoundingBox2.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/util/BoundingBox2.java @@ -95,8 +95,8 @@ public boolean contains(int x, int y) { } public boolean contains(double x, double y) { - return (x > this.a.getX() && x < this.b.getX() + 1 && - y > this.a.getY() && y < this.b.getY() + 1); + return (x >= this.a.getX() && x <= this.b.getX() + 1 && + y >= this.a.getY() && y <= this.b.getY() + 1); } diff --git a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/util/BoundingBox3.java b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/util/BoundingBox3.java index 7a9073b..49c42c8 100644 --- a/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/util/BoundingBox3.java +++ b/src/main/java/net/foxdenstudio/sponge/foxguard/plugin/region/util/BoundingBox3.java @@ -82,9 +82,9 @@ public boolean contains(int x, int y, int z) { } public boolean contains(double x, double y, double z) { - return (x > this.a.getX() && x < this.b.getX() + 1 && - z > this.a.getZ() && z < this.b.getZ() + 1 && - y > this.a.getY() && y < this.b.getY() + 1); + return (x >= this.a.getX() && x <= this.b.getX() + 1 && + z >= this.a.getZ() && z <= this.b.getZ() + 1 && + y >= this.a.getY() && y <= this.b.getY() + 1); }