Skip to content

Commit

Permalink
Update for 1.10.2 and move some MySQL calls to async thread
Browse files Browse the repository at this point in the history
  • Loading branch information
hsyyid committed Jan 8, 2017
1 parent bd10d52 commit 28a6ead
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 76 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
}

Expand Down
51 changes: 30 additions & 21 deletions src/main/java/io/github/hsyyid/inspector/Inspector.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) )
Expand All @@ -58,6 +61,14 @@ public static Inspector instance()
return instance;
}

@Inject
private PluginContainer pluginContainer;

public PluginContainer getPluginContainer()
{
return pluginContainer;
}

@Inject
private Logger logger;

Expand Down Expand Up @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/hsyyid/inspector/PluginInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockSnapshot> transaction : event.getTransactions())
{
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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<World> pointA = event.getTargetBlock().getLocation().get();
Utils.addPointOrCreateRegionOf(player.getUniqueId(), pointA, false);
Expand All @@ -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<World> pointB = event.getTargetBlock().getLocation().get();
Utils.addPointOrCreateRegionOf(player.getUniqueId(), pointB, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/io/github/hsyyid/inspector/utilities/Utils.java
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -21,7 +24,7 @@ public static void revertBlock(CommandSource src, int index, List<BlockInformati

if (blockInfo.getLocation().getBlock() != blockInfo.getOldBlockSnapshot().getState())
{
blockInfo.getLocation().setBlock(blockInfo.getOldBlockSnapshot().getState());
blockInfo.getLocation().setBlock(blockInfo.getOldBlockSnapshot().getState(), Cause.of(NamedCause.source(Inspector.instance().getPluginContainer())));
}
}

Expand Down

0 comments on commit 28a6ead

Please sign in to comment.