From 28a6ead1e30cfb07da2ed59e65977b25249139b3 Mon Sep 17 00:00:00 2001 From: Hassan Syyid Date: Sun, 8 Jan 2017 17:58:40 -0500 Subject: [PATCH] Update for 1.10.2 and move some MySQL calls to async thread --- build.gradle | 4 +- .../io/github/hsyyid/inspector/Inspector.java | 51 ++++++++------ .../github/hsyyid/inspector/PluginInfo.java | 4 +- .../listeners/ExplosionListener.java | 6 +- .../PlayerInteractBlockListener.java | 30 +++++---- .../inspector/utilities/DatabaseManager.java | 66 ++++++++++--------- .../hsyyid/inspector/utilities/Utils.java | 13 ++-- 7 files changed, 98 insertions(+), 76 deletions(-) diff --git a/build.gradle b/build.gradle index 139c60c..0e14a04 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'eclipse' sourceCompatibility = 1.8 targetCompatibility = 1.8 -version = '0.6.3' +version = '0.6.4' group = "io.github.hsyyid" archivesBaseName = "Inspector" @@ -26,7 +26,7 @@ repositories { } dependencies { - compile "org.spongepowered:spongeapi:4.2.0-SNAPSHOT" + compile "org.spongepowered:spongeapi:6.0.0-SNAPSHOT" compile "com.github.Flibio:Updatifier:v1.0.0" } diff --git a/src/main/java/io/github/hsyyid/inspector/Inspector.java b/src/main/java/io/github/hsyyid/inspector/Inspector.java index a2bd15a..842a689 100644 --- a/src/main/java/io/github/hsyyid/inspector/Inspector.java +++ b/src/main/java/io/github/hsyyid/inspector/Inspector.java @@ -1,7 +1,28 @@ package io.github.hsyyid.inspector; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +import org.slf4j.Logger; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.command.args.GenericArguments; +import org.spongepowered.api.command.spec.CommandSpec; +import org.spongepowered.api.config.DefaultConfig; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.game.state.GameInitializationEvent; +import org.spongepowered.api.plugin.Dependency; +import org.spongepowered.api.plugin.Plugin; +import org.spongepowered.api.plugin.PluginContainer; +import org.spongepowered.api.text.Text; + import com.google.common.collect.Sets; import com.google.inject.Inject; + import io.github.hsyyid.inspector.cmdexecutors.InspectorExecutor; import io.github.hsyyid.inspector.cmdexecutors.RollbackExecutor; import io.github.hsyyid.inspector.cmdexecutors.ToggleInspectorExecutor; @@ -16,24 +37,6 @@ import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.commented.CommentedConfigurationNode; import ninja.leaping.configurate.loader.ConfigurationLoader; -import org.slf4j.Logger; -import org.spongepowered.api.Sponge; -import org.spongepowered.api.command.args.GenericArguments; -import org.spongepowered.api.command.spec.CommandSpec; -import org.spongepowered.api.config.DefaultConfig; -import org.spongepowered.api.event.Listener; -import org.spongepowered.api.event.game.state.GameInitializationEvent; -import org.spongepowered.api.plugin.Dependency; -import org.spongepowered.api.plugin.Plugin; -import org.spongepowered.api.text.Text; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Set; -import java.util.UUID; @Updatifier(repoName = "Inspector", repoOwner = "hsyyid", version = "v" + PluginInfo.VERSION) @Plugin(id = PluginInfo.ID, name = PluginInfo.NAME, version = PluginInfo.VERSION, description = PluginInfo.DESCRIPTION, dependencies = @Dependency(id = "Updatifier", version = "1.0", optional = true) ) @@ -58,6 +61,14 @@ public static Inspector instance() return instance; } + @Inject + private PluginContainer pluginContainer; + + public PluginContainer getPluginContainer() + { + return pluginContainer; + } + @Inject private Logger logger; @@ -116,9 +127,7 @@ public void onGameInit(GameInitializationEvent event) inspectorSubcommands.put(Arrays.asList("rollback"), CommandSpec.builder() .description(Text.of("Rollback Command")) .permission("inspector.rollback") - .arguments(GenericArguments.seq( - GenericArguments.onlyOne(GenericArguments.user(Text.of("player"))), - GenericArguments.optional(GenericArguments.onlyOne(GenericArguments.string(Text.of("time")))))) + .arguments(GenericArguments.seq(GenericArguments.onlyOne(GenericArguments.user(Text.of("player"))), GenericArguments.optional(GenericArguments.onlyOne(GenericArguments.string(Text.of("time")))))) .executor(new RollbackExecutor()) .build()); diff --git a/src/main/java/io/github/hsyyid/inspector/PluginInfo.java b/src/main/java/io/github/hsyyid/inspector/PluginInfo.java index bdac693..0f8dd0b 100644 --- a/src/main/java/io/github/hsyyid/inspector/PluginInfo.java +++ b/src/main/java/io/github/hsyyid/inspector/PluginInfo.java @@ -5,8 +5,8 @@ */ public abstract class PluginInfo { - public static final String ID = "io.github.hsyyid.inspector"; + public static final String ID = "inspector"; public static final String NAME = "Inspector"; - public static final String VERSION = "0.6.3"; + public static final String VERSION = "0.6.4"; public static final String DESCRIPTION = "This plugin enables servers to monitor griefing and provides rollback."; } diff --git a/src/main/java/io/github/hsyyid/inspector/listeners/ExplosionListener.java b/src/main/java/io/github/hsyyid/inspector/listeners/ExplosionListener.java index bbab057..7b7731d 100644 --- a/src/main/java/io/github/hsyyid/inspector/listeners/ExplosionListener.java +++ b/src/main/java/io/github/hsyyid/inspector/listeners/ExplosionListener.java @@ -17,7 +17,7 @@ public class ExplosionListener { @Listener - public void onExplosion(ExplosionEvent.Detonate event, @First Player player) + public void onExplosion(ExplosionEvent.Post event, @First Player player) { for (Transaction transaction : event.getTransactions()) { @@ -27,9 +27,7 @@ public void onExplosion(ExplosionEvent.Detonate event, @First Player player) format.setTimeZone(TimeZone.getTimeZone("GMT")); String timeInGMT = format.format(cal.getTime()); - Inspector.instance().getDatabaseManager().updateBlockInformation(transactionLocation.getBlockX(), transactionLocation.getBlockY(), - transactionLocation.getBlockZ(), transactionLocation.getExtent().getUniqueId(), player.getUniqueId(), player.getName(), timeInGMT, - transaction.getOriginal(), transaction.getFinal()); + Inspector.instance().getDatabaseManager().updateBlockInformation(transactionLocation.getBlockX(), transactionLocation.getBlockY(), transactionLocation.getBlockZ(), transactionLocation.getExtent().getUniqueId(), player.getUniqueId(), player.getName(), timeInGMT, transaction.getOriginal(), transaction.getFinal()); } } } diff --git a/src/main/java/io/github/hsyyid/inspector/listeners/PlayerInteractBlockListener.java b/src/main/java/io/github/hsyyid/inspector/listeners/PlayerInteractBlockListener.java index 05573b2..1f2ff8f 100644 --- a/src/main/java/io/github/hsyyid/inspector/listeners/PlayerInteractBlockListener.java +++ b/src/main/java/io/github/hsyyid/inspector/listeners/PlayerInteractBlockListener.java @@ -1,16 +1,18 @@ package io.github.hsyyid.inspector.listeners; -import com.google.common.collect.Lists; -import io.github.hsyyid.inspector.Inspector; -import io.github.hsyyid.inspector.utilities.BlockInformation; -import io.github.hsyyid.inspector.utilities.Utils; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + import org.spongepowered.api.Sponge; import org.spongepowered.api.block.BlockSnapshot; +import org.spongepowered.api.data.type.HandTypes; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.Order; import org.spongepowered.api.event.block.InteractBlockEvent; import org.spongepowered.api.event.filter.cause.First; +import org.spongepowered.api.event.filter.cause.Root; import org.spongepowered.api.service.pagination.PaginationList; import org.spongepowered.api.service.pagination.PaginationService; import org.spongepowered.api.text.Text; @@ -20,9 +22,11 @@ import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; -import java.util.List; -import java.util.Optional; -import java.util.UUID; +import com.google.common.collect.Lists; + +import io.github.hsyyid.inspector.Inspector; +import io.github.hsyyid.inspector.utilities.BlockInformation; +import io.github.hsyyid.inspector.utilities.Utils; public class PlayerInteractBlockListener { @@ -81,9 +85,11 @@ public void onPlayerClickBlock(InteractBlockEvent event, @First Player player) } @Listener - public void onPlayerLeftClickBlock(InteractBlockEvent.Primary event, @First Player player) + public void onPlayerLeftClickBlock(InteractBlockEvent.Primary event, @Root Player player) { - if (player.hasPermission("inspector.region.use") && player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals((String) Utils.getConfigValue("inspector.select.tool"))) + if (player.hasPermission("inspector.region.use") && player.getItemInHand(HandTypes.MAIN_HAND).isPresent() + && player.getItemInHand(HandTypes.MAIN_HAND).get().getItem().getName() + .equals((String) Utils.getConfigValue("inspector.select.tool"))) { Location pointA = event.getTargetBlock().getLocation().get(); Utils.addPointOrCreateRegionOf(player.getUniqueId(), pointA, false); @@ -93,9 +99,11 @@ public void onPlayerLeftClickBlock(InteractBlockEvent.Primary event, @First Play } @Listener - public void onPlayerRightClickBlock(InteractBlockEvent.Secondary event, @First Player player) + public void onPlayerRightClickBlock(InteractBlockEvent.Secondary event, @Root Player player) { - if (player.hasPermission("inspector.region.use") && player.getItemInHand().isPresent() && player.getItemInHand().get().getItem().getName().equals((String) Utils.getConfigValue("inspector.select.tool"))) + if (player.hasPermission("inspector.region.use") && player.getItemInHand(HandTypes.MAIN_HAND).isPresent() + && player.getItemInHand(HandTypes.MAIN_HAND).get().getItem().getName() + .equals((String) Utils.getConfigValue("inspector.select.tool"))) { Location pointB = event.getTargetBlock().getLocation().get(); Utils.addPointOrCreateRegionOf(player.getUniqueId(), pointB, true); diff --git a/src/main/java/io/github/hsyyid/inspector/utilities/DatabaseManager.java b/src/main/java/io/github/hsyyid/inspector/utilities/DatabaseManager.java index 9df63d1..dcc2838 100644 --- a/src/main/java/io/github/hsyyid/inspector/utilities/DatabaseManager.java +++ b/src/main/java/io/github/hsyyid/inspector/utilities/DatabaseManager.java @@ -73,46 +73,50 @@ private Connection getDatabaseConnection() throws SQLException public void updateBlockInformation(int x, int y, int z, UUID worldUUID, UUID playerUUID, String playerName, String time, BlockSnapshot oldBlockSnapshot, BlockSnapshot newBlockSnapshot) { - try - { - Connection c = this.getDatabaseConnection(); - Statement stmt = c.createStatement(); + Sponge.getScheduler().createTaskBuilder().async().execute(() -> { + try + { + Connection c = this.getDatabaseConnection(); + Statement stmt = c.createStatement(); - String sql = "CREATE TABLE IF NOT EXISTS BLOCKINFO" + "(LOCATION TEXT NOT NULL," + " PLAYERID INT NOT NULL," + " TIME TEXT NOT NULL," + " OLDBLOCK TEXT NOT NULL," + " NEWBLOCK TEXT NOT NULL)"; - stmt.executeUpdate(sql); + String sql = "CREATE TABLE IF NOT EXISTS BLOCKINFO" + "(LOCATION TEXT NOT NULL," + " PLAYERID INT NOT NULL," + " TIME TEXT NOT NULL," + " OLDBLOCK TEXT NOT NULL," + " NEWBLOCK TEXT NOT NULL)"; + stmt.executeUpdate(sql); - Map serializedBlock = oldBlockSnapshot.toContainer().getMap(DataQuery.of()).get(); - String oldBlock = this.gson.toJson(serializedBlock); - serializedBlock = newBlockSnapshot.toContainer().getMap(DataQuery.of()).get(); - String newBlock = this.gson.toJson(serializedBlock); + Map serializedBlock = oldBlockSnapshot.toContainer().getMap(DataQuery.of()).get(); + String oldBlock = this.gson.toJson(serializedBlock); + serializedBlock = newBlockSnapshot.toContainer().getMap(DataQuery.of()).get(); + String newBlock = this.gson.toJson(serializedBlock); - sql = "INSERT INTO BLOCKINFO (LOCATION,PLAYERID,TIME,OLDBLOCK,NEWBLOCK) " + "VALUES ('" + x + ";" + y + ";" + z + ";" + worldUUID.toString() + "'," + this.getPlayerId(playerUUID) + ",'" + time + "','" + oldBlock + "','" + newBlock + "');"; - stmt.executeUpdate(sql); + sql = "INSERT INTO BLOCKINFO (LOCATION,PLAYERID,TIME,OLDBLOCK,NEWBLOCK) " + "VALUES ('" + x + ";" + y + ";" + z + ";" + worldUUID.toString() + "'," + this.getPlayerId(playerUUID) + ",'" + time + "','" + oldBlock + "','" + newBlock + "');"; + stmt.executeUpdate(sql); - stmt.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } + stmt.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + }).submit(Inspector.instance()); } public void addPlayerToDatabase(Player player) { - try - { - Connection c = this.getDatabaseConnection(); - Statement stmt = c.createStatement(); + Sponge.getScheduler().createTaskBuilder().async().execute(() -> { + try + { + Connection c = this.getDatabaseConnection(); + Statement stmt = c.createStatement(); - String sql = "INSERT INTO PLAYERS (UUID, NAME) " + "VALUES ('" + player.getUniqueId().toString() + "','" + player.getName() + "');"; - stmt.executeUpdate(sql); + String sql = "INSERT INTO PLAYERS (UUID, NAME) " + "VALUES ('" + player.getUniqueId().toString() + "','" + player.getName() + "');"; + stmt.executeUpdate(sql); - stmt.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - } + stmt.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + }).submit(Inspector.instance()); } public boolean isPlayerInDatabase(Player player) @@ -125,7 +129,7 @@ public boolean isPlayerInDatabase(Player player) Statement stmt = c.createStatement(); stmt.executeUpdate("CREATE TABLE IF NOT EXISTS PLAYERS" + "(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + " UUID TEXT NOT NULL," + " NAME TEXT NOT NULL)"); stmt.close(); - + PreparedStatement preparedStmt = c.prepareStatement("SELECT count(*) from PLAYERS WHERE uuid=?"); preparedStmt.setString(1, player.getUniqueId().toString()); ResultSet rs = preparedStmt.executeQuery(); diff --git a/src/main/java/io/github/hsyyid/inspector/utilities/Utils.java b/src/main/java/io/github/hsyyid/inspector/utilities/Utils.java index 9b33d2a..aa1ae0d 100644 --- a/src/main/java/io/github/hsyyid/inspector/utilities/Utils.java +++ b/src/main/java/io/github/hsyyid/inspector/utilities/Utils.java @@ -1,15 +1,18 @@ package io.github.hsyyid.inspector.utilities; -import io.github.hsyyid.inspector.Inspector; +import java.util.List; +import java.util.Optional; +import java.util.UUID; + import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.event.cause.Cause; +import org.spongepowered.api.event.cause.NamedCause; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.format.TextColors; import org.spongepowered.api.world.Location; import org.spongepowered.api.world.World; -import java.util.List; -import java.util.Optional; -import java.util.UUID; +import io.github.hsyyid.inspector.Inspector; public class Utils { @@ -21,7 +24,7 @@ public static void revertBlock(CommandSource src, int index, List