From 4498d8343fe6b052b11fd2a8ec02a5c1bf53bfaf Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Sat, 19 Oct 2013 16:29:40 +0200 Subject: [PATCH] Slimmer BlockState-Database - Default logSurvival to false - "/lcbs cleanup" command to delete survival items, with logSurvival: false Rename TabCompletion --- .../FeatureSwitchGameMode.java | 4 +-- .../limitedcreative/MainCommand.java | 4 +-- .../blockstate/BlockStateCommand.java | 34 +++++++++++++++++-- .../blockstate/BlockStateConfig.java | 4 +-- .../limitedcreative/blockstate/DBModel.java | 6 ++++ .../limitedcreative/blockstate/DBQueries.java | 18 ++++++++++ .../blockstate/SyncronizedModel.java | 11 ++++++ .../blockstate/ThreadedModel.java | 18 ++++++++++ .../blockstate/thread/ThreadLink.java | 14 ++++++++ .../regions/RegionsCommand.java | 10 +++--- src/main/resources/lang/messages.properties | 3 ++ .../resources/lang/messages_de.properties | 3 ++ 12 files changed, 116 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java index 2802ef7..0309f7d 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/FeatureSwitchGameMode.java @@ -18,12 +18,12 @@ import de.jaschastarke.bukkit.lib.commands.MethodCommand; import de.jaschastarke.bukkit.lib.commands.IMethodCommandContainer; import de.jaschastarke.bukkit.lib.commands.MissingPermissionCommandException; import de.jaschastarke.bukkit.lib.commands.NeedsPlayerArgumentCommandException; -import de.jaschastarke.bukkit.lib.commands.TabCompletionHelper; import de.jaschastarke.bukkit.lib.commands.annotations.Alias; import de.jaschastarke.bukkit.lib.commands.annotations.Description; import de.jaschastarke.bukkit.lib.commands.annotations.IsCommand; import de.jaschastarke.bukkit.lib.commands.annotations.NeedsPermission; import de.jaschastarke.bukkit.lib.commands.annotations.Usages; +import de.jaschastarke.bukkit.lib.commands.parser.TabCompletion; import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission; import de.jaschastarke.minecraft.lib.permissions.IPermission; import de.jaschastarke.modularize.ModuleEntry.ModuleState; @@ -139,7 +139,7 @@ public class FeatureSwitchGameMode extends CoreModule { } @Override - public List getTabCompleter(MethodCommand cmd) { + public List getTabCompleter(MethodCommand cmd) { return null; } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/MainCommand.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/MainCommand.java index d1729b8..a770319 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/MainCommand.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/MainCommand.java @@ -8,10 +8,10 @@ import de.jaschastarke.bukkit.lib.commands.CommandContext; import de.jaschastarke.bukkit.lib.commands.IHelpDescribed; import de.jaschastarke.bukkit.lib.commands.IMethodCommandContainer; import de.jaschastarke.bukkit.lib.commands.MethodCommand; -import de.jaschastarke.bukkit.lib.commands.TabCompletionHelper; import de.jaschastarke.bukkit.lib.commands.annotations.Description; import de.jaschastarke.bukkit.lib.commands.annotations.IsCommand; import de.jaschastarke.bukkit.lib.commands.annotations.NeedsPermission; +import de.jaschastarke.bukkit.lib.commands.parser.TabCompletion; import de.jaschastarke.maven.ArchiveDocComments; import de.jaschastarke.maven.PluginCommand; import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission; @@ -95,7 +95,7 @@ public class MainCommand extends BukkitCommand implements IHelpDescribed, IMetho } @Override - public List getTabCompleter(MethodCommand cmd) { + public List getTabCompleter(MethodCommand cmd) { return null; } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateCommand.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateCommand.java index b754a2e..6a28282 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateCommand.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateCommand.java @@ -94,9 +94,33 @@ public class BlockStateCommand extends BukkitCommand implements IHelpDescribed { throw new CommandException("Module " + mod.getName() + " is disabled"); return super.execute(context, args); } + + /** + * Deletes no longer used data from the BlockState-Database. Currently it only removes non-creative entries + * from the database, if you changed to "logSurvival"-config from true to false. + */ + @IsCommand("cleanup") + @Usages("") + public boolean cleanupDatabase(final CommandContext context, String... args) throws CommandException { + if (mod.getConfig().getLogSurvival()) { + context.responseFormatted(ChatFormattings.INFO, L("command.blockstate.nothing_to_cleanup")); + } else { + mod.getPlugin().getServer().getScheduler().runTaskAsynchronously(mod.getPlugin(), new BukkitRunnable() { + @Override + public void run() { + int countDeleted = mod.getModel().cleanUp(DBModel.Cleanup.SURVIVAL); + if (countDeleted < 0) + context.responseFormatted(ChatFormattings.ERROR, L("command.blockstate.cleanup_error")); + else + context.responseFormatted(ChatFormattings.SUCCESS, L("command.blockstate.cleanup_success", countDeleted)); + } + }); + } + return true; + } /** - * Modifies the Block-GameMode-Database and sets all blocks in the selection to the provided gamemode. Set it + * Modifies the BlockState-Database and sets all blocks in the selection to the provided gamemode. Set it * to "creative" to disable drop of this block on destroying. Set it to "survival" to allow it. * WorldEdit is required, because the selection Region is used. * gamemode can be: survival / creative / adventure / s / c / a / 0 / 1 / 2 @@ -247,7 +271,13 @@ public class BlockStateCommand extends BukkitCommand implements IHelpDescribed { mod.getModuleEntry().disable(); thread.start(); - context.response(L("command.blockstate.migrate_started", source.getType(), target.getType())); + String sourceType = source.getType().toString(); + if (params.getParameter("import") != null) { + if (params.getParameter("import").equals("cc")) { + sourceType = "CreativeControl-" + sourceType; + } + } + context.response(L("command.blockstate.migrate_started", sourceType, target.getType())); return true; } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java index 6500b13..0a6f905 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java @@ -130,11 +130,11 @@ public class BlockStateConfig extends Configuration implements IConfigurationSub * Log all Block-Places to the database. Disable to make the database more slim by not adding blocks placed in * survival-mode. * - * default: true + * default: false */ @IsConfigurationNode(order = 400) public boolean getLogSurvival() { - return config.getBoolean("logSurvival", true); + return config.getBoolean("logSurvival", false); } private StringList ignoredWorlds = null; diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBModel.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBModel.java index 196bfab..efc75db 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBModel.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBModel.java @@ -64,6 +64,10 @@ public interface DBModel { } } + public enum Cleanup { + SURVIVAL; + } + public void onEnable() throws Exception; public void onDisable(); public void moveState(Block from, Block to); @@ -83,4 +87,6 @@ public interface DBModel { public void removeState(Block block); public void finish(); } + + public int cleanUp(Cleanup target); } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java index 7a70e01..c7dc323 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java @@ -14,6 +14,7 @@ import de.jaschastarke.bukkit.lib.database.ResultIterator; import de.jaschastarke.database.Type; import de.jaschastarke.database.db.Database; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState.Source; +import de.jaschastarke.minecraft.limitedcreative.blockstate.DBModel.Cleanup; import de.jaschastarke.utils.IDebugLogHolder; public class DBQueries { @@ -300,6 +301,23 @@ public class DBQueries { throw new RuntimeException("Currently only SQLite is supported."); } } + + public int cleanup(Cleanup q) throws SQLException { + PreparedStatement delete; + if (dbg.isDebug()) + dbg.getLog().debug("DBQuery: cleanup: " + q.toString()); + if (q == Cleanup.SURVIVAL) { + delete = db.prepare("DELETE FROM lc_block_state WHERE gm = ?"); + if (db.getType() == Type.MySQL) + delete.setString(1, GameMode.SURVIVAL.name()); + else + delete.setInt(1, GameMode.SURVIVAL.getValue()); + } else { + return -1; + } + return delete.executeUpdate(); + } + public Database getDB() { return db; } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/SyncronizedModel.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/SyncronizedModel.java index 40beffe..242316d 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/SyncronizedModel.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/SyncronizedModel.java @@ -198,4 +198,15 @@ public class SyncronizedModel extends AbstractModel implements DBModel { } } } + + @Override + public int cleanUp(Cleanup target) { + try { + return q.cleanup(target); + } catch (SQLException e) { + mod.getLog().severe(e.getMessage()); + mod.getLog().warn("Failed to cleanup BlockState-DB"); + return -1; + } + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/ThreadedModel.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/ThreadedModel.java index 3025b8b..a3a6c03 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/ThreadedModel.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/ThreadedModel.java @@ -18,6 +18,7 @@ import org.bukkit.metadata.Metadatable; import de.jaschastarke.database.DatabaseConfigurationException; import de.jaschastarke.minecraft.limitedcreative.ModBlockStates; +import de.jaschastarke.minecraft.limitedcreative.blockstate.thread.CallableAction; import de.jaschastarke.minecraft.limitedcreative.blockstate.thread.ThreadLink; import de.jaschastarke.minecraft.limitedcreative.blockstate.thread.Transaction; @@ -259,4 +260,21 @@ public class ThreadedModel extends AbstractModel implements DBModel, Listener { public ModBlockStates getModel() { return mod; } + + @Override + public int cleanUp(final Cleanup target) { + return threads.call(new CallableAction() { + @Override + public void process(ThreadLink link, DBQueries q) { + this.returnSet = true; + try { + this.returnValue = q.cleanup(target); + } catch (SQLException e) { + this.returnValue = -1; + mod.getLog().severe(e.getMessage()); + mod.getLog().warn("Failed to cleanup BlockState-DB"); + } + } + }); + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/thread/ThreadLink.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/thread/ThreadLink.java index 5846811..b64bc8d 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/thread/ThreadLink.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/thread/ThreadLink.java @@ -134,6 +134,20 @@ public class ThreadLink { return action.getValue(); } + public void queue(Action act) { + synchronized (updateQueue) { + updateQueue.add(act); + updateQueue.notify(); + } + } + public T call(CallableAction act) { + synchronized (updateQueue) { + updateQueue.push(act); + updateQueue.notify(); + } + return act.getValue(); + } + public List callUpdate(Cuboid c) { FetchCuboidAction action = new FetchCuboidAction(c); synchronized (updateQueue) { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java index 7acea75..b4e517f 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/regions/RegionsCommand.java @@ -26,12 +26,12 @@ import de.jaschastarke.bukkit.lib.commands.ICommand; import de.jaschastarke.bukkit.lib.commands.IHelpDescribed; import de.jaschastarke.bukkit.lib.commands.MethodCommand; import de.jaschastarke.bukkit.lib.commands.MissingPermissionCommandException; -import de.jaschastarke.bukkit.lib.commands.TabCompletionHelper; -import de.jaschastarke.bukkit.lib.commands.TabCompletionHelper.Completer; -import de.jaschastarke.bukkit.lib.commands.TabCompletionHelper.Context; import de.jaschastarke.bukkit.lib.commands.annotations.IsCommand; import de.jaschastarke.bukkit.lib.commands.annotations.Usages; import de.jaschastarke.bukkit.lib.commands.parser.DefinedParameterParser; +import de.jaschastarke.bukkit.lib.commands.parser.TabCompletion; +import de.jaschastarke.bukkit.lib.commands.parser.TabCompletion.Completer; +import de.jaschastarke.bukkit.lib.commands.parser.TabCompletion.Context; import de.jaschastarke.maven.ArchiveDocComments; import de.jaschastarke.maven.PluginCommand; import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission; @@ -114,9 +114,9 @@ public class RegionsCommand extends BukkitCommand implements IHelpDescribed { for (ICommand cmd : handler.getCommands()) { if (cmd instanceof MethodCommand) { if (cmd.getName().equals("info")) { - ((MethodCommand) cmd).getCompleter().add(TabCompletionHelper.forUsageLine("[region]")); + ((MethodCommand) cmd).getCompleter().add(TabCompletion.forUsageLine("[region]")); } - for (TabCompletionHelper c : ((MethodCommand) cmd).getCompleter()) { + for (TabCompletion c : ((MethodCommand) cmd).getCompleter()) { c.setCompleter("region", new RegionCompleter()); } } diff --git a/src/main/resources/lang/messages.properties b/src/main/resources/lang/messages.properties index bc54e5c..697e98d 100644 --- a/src/main/resources/lang/messages.properties +++ b/src/main/resources/lang/messages.properties @@ -38,6 +38,9 @@ command.blockstate.migration_finished_restart: A Server-Restart is required! command.blockstate.migration_error: Migration failed with error: {0} command.blockstate.migrate_useronline_error: There are players on the Server. The migration shouldn't be run with active players. command.blockstate.migrate_confirm: Are you sure you want to start the migration? It may take a very long time and much CPU. To confirm execution run the command with the following added: {0} +command.blockstate.nothing_to_cleanup: Nothing to cleanup +command.blockstate.cleanup_success: Successfully removed {0} entries from database +command.blockstate.cleanup_error: Error while cleaning database. Check server.log cmdblock.blocked: This command is blocked while in creative mode. cmdblock.blocked.requires_worldedit: WorlEdit is required to use this command diff --git a/src/main/resources/lang/messages_de.properties b/src/main/resources/lang/messages_de.properties index 0db65d8..a9e5c6c 100644 --- a/src/main/resources/lang/messages_de.properties +++ b/src/main/resources/lang/messages_de.properties @@ -38,6 +38,9 @@ command.blockstate.migration_finished_restart: Server-Neustart notwendig! command.blockstate.migration_error: Migration mit folgendem Fehler fehlgeschlagen: {0} command.blockstate.migrate_useronline_error: Es sind Spieler auf dem Server. Die Migration sollte nicht mit aktiven Spielern ausgeführt werden. command.blockstate.migrate_confirm: Bist du sicher. dass du die Migration starten willst? Dies dauert vermutlich sehr lange und benötigt viel Prozessor-Leistung. Um die Ausführung zu bestätigen führe den Befehl erneut mit folgendem Parameter aus: {0} +command.blockstate.nothing_to_cleanup: Nichts aufzuräumen +command.blockstate.cleanup_success: Erfolgreich {0} Datensätze aus der Datenbank gelöscht +command.blockstate.cleanup_error: Fehler beim Aufräumen der Datenbank. server.log prüfen cmdblock.blocked: Dieses Kommando ist im Kreativ-Modus blockiert cmdblock.blocked.requires_worldedit: WorldEdit wird für diesen Befehl benötigt