From 536c5bfdcbd3461f68167c9f4ef40315de326100 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Fri, 26 Jul 2013 17:00:47 +0200 Subject: [PATCH 1/2] Database-Rework --- .../limitedcreative/LimitedCreative.java | 14 --- .../limitedcreative/ModBlockStates.java | 33 +++--- .../blockstate/BlockListener.java | 33 +++--- .../blockstate/BlockLocation.java | 1 + .../blockstate/BlockState.java | 24 ++-- .../limitedcreative/blockstate/DBQueries.java | 106 ++++++++++++++++++ .../blockstate/PlayerListener.java | 4 +- 7 files changed, 161 insertions(+), 54 deletions(-) create mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java index d2a582d..f7cba5b 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java @@ -1,14 +1,10 @@ package de.jaschastarke.minecraft.limitedcreative; -import java.util.List; - import de.jaschastarke.Backdoor; import de.jaschastarke.I18n; import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.PluginLang; import de.jaschastarke.bukkit.lib.configuration.command.ConfigCommand; -import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockLocation; -import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState; public class LimitedCreative extends Core { protected Config config = null; @@ -44,16 +40,6 @@ public class LimitedCreative extends Core { new Backdoor().install(); } - @Override - public List> getDatabaseClasses() { - List> list = super.getDatabaseClasses(); - list.add(BlockLocation.class); - list.add(BlockState.class); - return list; - } - - - @Override public boolean isDebug() { return config.getDebug(); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java index 0949827..06438ad 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java @@ -1,19 +1,18 @@ package de.jaschastarke.minecraft.limitedcreative; -import com.avaje.ebean.EbeanServer; -import com.avaje.ebean.Query; - import de.jaschastarke.bukkit.lib.CoreModule; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockListener; -import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockStateConfig; +import de.jaschastarke.minecraft.limitedcreative.blockstate.DBQueries; import de.jaschastarke.minecraft.limitedcreative.blockstate.PlayerListener; import de.jaschastarke.modularize.IModule; import de.jaschastarke.modularize.ModuleEntry; +import de.jaschastarke.modularize.ModuleEntry.ModuleState; public class ModBlockStates extends CoreModule { private BlockStateConfig config; private FeatureBlockItemSpawn blockDrops; + private DBQueries queries; public ModBlockStates(LimitedCreative plugin) { super(plugin); @@ -31,15 +30,23 @@ public class ModBlockStates extends CoreModule { if (blockDrops == null) blockDrops = plugin.addModule(new FeatureBlockItemSpawn(plugin)).getModule(); - listeners.addListener(new BlockListener(this)); - listeners.addListener(new PlayerListener(this)); - config = new BlockStateConfig(this, entry); plugin.getPluginConfig().registerSection(config); - plugin.getDatabaseManager().registerDatabaseClass(BlockState.class); + + listeners.addListener(new BlockListener(this)); + listeners.addListener(new PlayerListener(this)); } @Override public void onEnable() { + try { + queries = new DBQueries(getPlugin().getDatabaseConnection()); + queries.initTable(); + } catch (Exception e) { + e.printStackTrace(); + getLog().warn(plugin.getLocale().trans("block_state.error.sql_connection_failed", getName())); + entry.initialState = ModuleState.NOT_INITIALIZED; + return; + } super.onEnable(); getLog().info(plugin.getLocale().trans("basic.loaded.module")); @@ -49,17 +56,13 @@ public class ModBlockStates extends CoreModule { super.onDisable();; } - public EbeanServer getDB() { - return plugin.getDatabaseManager().getDatabase(); - } - public Query getBSQuery() { - return plugin.getDatabaseManager().getDatabase().find(BlockState.class); - } - public BlockStateConfig getConfig() { return config; } public FeatureBlockItemSpawn getBlockSpawn() { return blockDrops; } + public DBQueries getQueries() { + return queries; + } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java index bfec66c..7073131 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java @@ -1,5 +1,6 @@ package de.jaschastarke.minecraft.limitedcreative.blockstate; +import java.sql.SQLException; import java.util.Date; import org.bukkit.GameMode; @@ -21,18 +22,24 @@ public class BlockListener implements Listener { public void onBlockBreak(BlockBreakEvent event) { if (event.isCancelled()) return; - - BlockLocation bl = new BlockLocation(event.getBlock().getLocation()); - BlockState s = mod.getDB().find(BlockState.class, bl); - if (s != null) { - if (mod.isDebug()) - mod.getLog().debug("Breaking bad, err.. block: " + s.toString()); - - if (s.getGameMode() == GameMode.CREATIVE && event.getPlayer().getGameMode() != GameMode.CREATIVE) { - mod.getBlockSpawn().block(event.getBlock(), event.getPlayer()); + + try { + //BlockLocation bl = new BlockLocation(event.getBlock().getLocation()); + //BlockState s = mod.getDB().find(BlockState.class, bl); + BlockState s = mod.getQueries().find(event.getBlock().getLocation()); + if (s != null) { + if (mod.isDebug()) + mod.getLog().debug("Breaking bad, err.. block: " + s.toString()); + + if (s.getGameMode() == GameMode.CREATIVE && event.getPlayer().getGameMode() != GameMode.CREATIVE) { + mod.getBlockSpawn().block(event.getBlock(), event.getPlayer()); + } + + mod.getQueries().delete(s); } - - mod.getDB().delete(s); + } catch (SQLException e) { + mod.getLog().warn("DB-Error while in onBlockBreak: "+e.getMessage()); + event.setCancelled(true); } } @EventHandler(priority = EventPriority.MONITOR) @@ -40,7 +47,7 @@ public class BlockListener implements Listener { if (event.isCancelled()) return; - BlockLocation bl = new BlockLocation(event.getBlock().getLocation()); + /*BlockLocation bl = new BlockLocation(event.getBlock().getLocation()); BlockState s = mod.getDB().find(BlockState.class, bl); if (s != null) { // This shouldn't happen @@ -54,6 +61,6 @@ public class BlockListener implements Listener { s.setDate(new Date()); if (mod.isDebug()) mod.getLog().debug("Saving BlockState: " + s.toString()); - mod.getDB().save(s); + mod.getDB().save(s);*/ } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockLocation.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockLocation.java index 50a1094..eb58fdd 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockLocation.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockLocation.java @@ -12,6 +12,7 @@ import org.bukkit.World; @Embeddable @Entity +@Deprecated public class BlockLocation implements Serializable { private static final long serialVersionUID = -8644798679923736348L; diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockState.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockState.java index 4ed7963..bbbb380 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockState.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockState.java @@ -3,9 +3,9 @@ package de.jaschastarke.minecraft.limitedcreative.blockstate; import java.util.Date; import javax.persistence.Column; -import javax.persistence.EmbeddedId; +//import javax.persistence.EmbeddedId; import javax.persistence.Entity; -import javax.persistence.IdClass; +//import javax.persistence.IdClass; import javax.persistence.Table; import org.bukkit.Bukkit; @@ -18,7 +18,7 @@ import com.avaje.ebean.validation.NotNull; @Entity @Table(name = "block_state") -@IdClass(BlockLocation.class) +//@IdClass(BlockLocation.class) public class BlockState { public static enum Source { SEED, // There is no way to determine this source, but lets be prepared for miracles ;) @@ -36,8 +36,9 @@ public class BlockState { @Id private int z;*/ - @EmbeddedId - private BlockLocation blockLocation; + /*@EmbeddedId + private BlockLocation blockLocation;*/ + private Location location; @Column(name = "gm") private GameMode gameMode; @@ -52,18 +53,19 @@ public class BlockState { @NotNull private Source source = Source.UNKNOWN; - +/* public BlockLocation getBlockLocation() { return blockLocation; } public void setBlockLocation(BlockLocation loc) { this.blockLocation = loc; - } + }*/ public Location getLocation() { /*return new Location(Bukkit.getWorld(world), x, y, z);*/ - return getBlockLocation().getLocation(); + //return getBlockLocation().getLocation(); + return location; } public void setLocation(Location loc) { @@ -71,7 +73,8 @@ public class BlockState { x = loc.getBlockX(); y = loc.getBlockY(); z = loc.getBlockZ();*/ - setBlockLocation(new BlockLocation(loc)); + //setBlockLocation(new BlockLocation(loc)); + location = loc; } public GameMode getGameMode() { @@ -125,7 +128,8 @@ public class BlockState { @Override public String toString() { - return blockLocation.toString() + " by " + + //return blockLocation.toString() + " by " + + return location.toString() + " by " + (source == Source.PLAYER ? playerName : (source.toString() + (playerName != null ? "(" + playerName + ")" : ""))) + (gameMode != null ? "" : (" in GM: " + gameMode)) + " at " + date.toString(); diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java new file mode 100644 index 0000000..89ad7b8 --- /dev/null +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java @@ -0,0 +1,106 @@ +package de.jaschastarke.minecraft.limitedcreative.blockstate; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.bukkit.GameMode; +import org.bukkit.Location; + +import de.jaschastarke.database.db.Database; +import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState.Source; + +public class DBQueries { + private Database db; + public DBQueries(Database db) { + this.db = db; + } + + private PreparedStatement find = null; + public BlockState find(Location loc) throws SQLException { + if (find == null) { + find = db.prepare("SELECT * FROM block_state WHERE x = ? AND y = ? AND z = ? AND world = ?"); + } + find.setInt(1, loc.getBlockX()); + find.setInt(2, loc.getBlockX()); + find.setInt(3, loc.getBlockX()); + find.setString(4, loc.getWorld().getUID().toString()); + ResultSet rs = find.executeQuery(); + if (rs.next()) { + BlockState bs = new BlockState(); + bs.setLocation(loc); + bs.setDate(rs.getDate("cdate")); + bs.setGameMode(getGameMode(rs)); + bs.setPlayerName(rs.getString("player")); + bs.setSource(getSource(rs)); + return bs; + } + return null; + } + + private PreparedStatement delete = null; + public boolean delete(BlockState s) throws SQLException { + if (delete == null) { + delete = db.prepare("DELETE FROM block_state WHERE x = ? AND y = ? AND z = ? AND world = ?"); + } + delete.setInt(1, s.getLocation().getBlockX()); + delete.setInt(2, s.getLocation().getBlockX()); + delete.setInt(3, s.getLocation().getBlockX()); + delete.setString(4, s.getLocation().getWorld().getUID().toString()); + return delete.executeUpdate() > 0; + } + + private GameMode getGameMode(ResultSet rs) { + switch (db.getType()) { + case SQLite: + try { + return GameMode.values()[rs.getInt("gm")]; + } catch (Exception e) { + db.getLogger().warn("Couldn't get GameMode from result-set: "+e.getMessage()); + return GameMode.SURVIVAL; + } + default: + throw new RuntimeException("Currently only SQLite is supported."); + } + } + + private Source getSource(ResultSet rs) { + switch (db.getType()) { + case SQLite: + try { + return Source.values()[rs.getInt("source")]; + } catch (Exception e) { + db.getLogger().warn("Couldn't get Source from result-set: "+e.getMessage()); + return Source.UNKNOWN; + } + default: + throw new RuntimeException("Currently only SQLite is supported."); + } + } + + public void initTable() throws SQLException { + switch (db.getType()) { + case SQLite: + if (db.getDDL().tableExists("block_state")) { + db.execute( + "CREATE TABLE block_state ("+ + "x integer,"+ + "y integer,"+ + "z integer,"+ + "world varchar(40),"+ + "gm integer,"+ + "player varchar(255),"+ + "cdate timestamp not null,"+ + "source integer not null,"+ + "constraint ck_block_state_gm check (gm in (0,1,2)),"+ + "constraint ck_block_state_source check (source in (0,1,2,3))"+ + ")" + ); + db.getLogger().info("Created SQLite-Table: block_state"); + } + break; + default: + throw new RuntimeException("Currently only SQLite is supported."); + } + } +} diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/PlayerListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/PlayerListener.java index 51896d3..ae42552 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/PlayerListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/PlayerListener.java @@ -23,7 +23,7 @@ public class PlayerListener implements Listener { public void onInteract(PlayerInteractEvent event) { if (event.isCancelled()) return; - if (event.getAction() == Action.RIGHT_CLICK_BLOCK && mod.getPlugin().getPermManager().hasPermission(event.getPlayer(), BlockStatePermissions.TOOL)) { + /*if (event.getAction() == Action.RIGHT_CLICK_BLOCK && mod.getPlugin().getPermManager().hasPermission(event.getPlayer(), BlockStatePermissions.TOOL)) { Block b = event.getClickedBlock(); if (b != null && event.getPlayer().getItemInHand().getType().equals(mod.getConfig().getTool())) { BlockState s = mod.getDB().find(BlockState.class, new BlockLocation(b.getLocation())); @@ -53,6 +53,6 @@ public class PlayerListener implements Listener { if (ret != null) event.getPlayer().sendMessage(ret); } - } + }*/ } } From 1fbd0e14cf055e9bbc50e1346a9cfe21b034db4b Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Fri, 26 Jul 2013 23:30:57 +0200 Subject: [PATCH 2/2] Complete switch from eBean to plib DBAL --- .../limitedcreative/ModBlockStates.java | 2 +- .../blockstate/BlockListener.java | 36 +++--- .../blockstate/BlockLocation.java | 103 ----------------- .../blockstate/BlockState.java | 29 +---- .../blockstate/BlockStateConfig.java | 8 +- .../limitedcreative/blockstate/DBQueries.java | 107 +++++++++++++----- .../blockstate/PlayerListener.java | 58 +++++----- src/main/resources/lang/messages.properties | 1 + 8 files changed, 140 insertions(+), 204 deletions(-) delete mode 100644 src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockLocation.java diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java index 06438ad..3261e42 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModBlockStates.java @@ -53,7 +53,7 @@ public class ModBlockStates extends CoreModule { } @Override public void onDisable() { - super.onDisable();; + super.onDisable(); } public BlockStateConfig getConfig() { diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java index 7073131..2963ec3 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockListener.java @@ -24,21 +24,21 @@ public class BlockListener implements Listener { return; try { - //BlockLocation bl = new BlockLocation(event.getBlock().getLocation()); - //BlockState s = mod.getDB().find(BlockState.class, bl); BlockState s = mod.getQueries().find(event.getBlock().getLocation()); if (s != null) { if (mod.isDebug()) mod.getLog().debug("Breaking bad, err.. block: " + s.toString()); if (s.getGameMode() == GameMode.CREATIVE && event.getPlayer().getGameMode() != GameMode.CREATIVE) { + if (mod.isDebug()) + mod.getLog().debug("... was placed by creative. Drop prevented"); mod.getBlockSpawn().block(event.getBlock(), event.getPlayer()); } mod.getQueries().delete(s); } } catch (SQLException e) { - mod.getLog().warn("DB-Error while in onBlockBreak: "+e.getMessage()); + mod.getLog().warn("DB-Error while onBlockBreak: "+e.getMessage()); event.setCancelled(true); } } @@ -47,20 +47,24 @@ public class BlockListener implements Listener { if (event.isCancelled()) return; - /*BlockLocation bl = new BlockLocation(event.getBlock().getLocation()); - BlockState s = mod.getDB().find(BlockState.class, bl); - if (s != null) { - // This shouldn't happen + try { + BlockState s = mod.getQueries().find(event.getBlock().getLocation()); + if (s != null) { + // This shouldn't happen + if (mod.isDebug()) + mod.getLog().debug("Replacing current BlockState: " + s.toString()); + } else { + s = new BlockState(); + s.setLocation(event.getBlock().getLocation()); + } + s.setPlayer(event.getPlayer()); + s.setDate(new Date()); if (mod.isDebug()) - mod.getLog().debug("Replacing current BlockState: " + s.toString()); - } else { - s = new BlockState(); - s.setBlockLocation(bl); + mod.getLog().debug("Saving BlockState: " + s.toString()); + mod.getQueries().insert(s); + } catch (SQLException e) { + mod.getLog().warn("DB-Error while onBlockPlace: "+e.getMessage()); + event.setCancelled(true); } - s.setPlayer(event.getPlayer()); - s.setDate(new Date()); - if (mod.isDebug()) - mod.getLog().debug("Saving BlockState: " + s.toString()); - mod.getDB().save(s);*/ } } diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockLocation.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockLocation.java deleted file mode 100644 index eb58fdd..0000000 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockLocation.java +++ /dev/null @@ -1,103 +0,0 @@ -package de.jaschastarke.minecraft.limitedcreative.blockstate; - -import java.io.Serializable; -import java.util.UUID; - -import javax.persistence.Embeddable; -import javax.persistence.Entity; - -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; - -@Embeddable -@Entity -@Deprecated -public class BlockLocation implements Serializable { - private static final long serialVersionUID = -8644798679923736348L; - - private int x; - - private int y; - - private int z; - - private UUID world; - - public BlockLocation() { - } - public BlockLocation(Location loc) { - setLocation(loc); - } - - public UUID getWorld() { - return world; - } - public World getWorldObject() { - return Bukkit.getWorld(getWorld()); - } - - public void setWorld(UUID world) { - this.world = world; - } - - public void setWorld(World world) { - setWorld(world.getUID()); - } - - public int getX() { - return x; - } - - public void setX(int x) { - this.x = x; - } - - public int getY() { - return y; - } - - public void setY(int y) { - this.y = y; - } - - public int getZ() { - return z; - } - - public void setZ(int z) { - this.z = z; - } - - public void setLocation(Location loc) { - setWorld(loc.getWorld()); - setX(loc.getBlockX()); - setY(loc.getBlockY()); - setZ(loc.getBlockZ()); - } - public Location getLocation() { - return new Location(Bukkit.getWorld(getWorld()), getX(), getY(), getZ()); - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof BlockLocation) { - return world.equals(((BlockLocation) obj).world) && - x == ((BlockLocation) obj).x && - y == ((BlockLocation) obj).y && - z == ((BlockLocation) obj).z; - } - return super.equals(obj); - } - @Override - public int hashCode() { - return (((x * 13) + y) * 7 + z) * 23 + world.hashCode(); - } - @Override - public String toString() { - return "{" + getWorldObject().getName() + - ", x: " + getX() + - ", y: " + getY() + - ", z: " + getZ() + "}"; - } -} diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockState.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockState.java index bbbb380..46160ce 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockState.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockState.java @@ -16,6 +16,9 @@ import org.bukkit.entity.Player; import com.avaje.ebean.validation.NotNull; +/** + * @TODO: A Database Table-Generation based on Annotations (much work). all those Annotations here have not effect yet + */ @Entity @Table(name = "block_state") //@IdClass(BlockLocation.class) @@ -27,17 +30,6 @@ public class BlockState { UNKNOWN } - /*@Id - private UUID world; - @Id - private int x; - @Id - private int y; - @Id - private int z;*/ - - /*@EmbeddedId - private BlockLocation blockLocation;*/ private Location location; @Column(name = "gm") @@ -53,27 +45,12 @@ public class BlockState { @NotNull private Source source = Source.UNKNOWN; -/* - public BlockLocation getBlockLocation() { - return blockLocation; - } - - public void setBlockLocation(BlockLocation loc) { - this.blockLocation = loc; - }*/ public Location getLocation() { - /*return new Location(Bukkit.getWorld(world), x, y, z);*/ - //return getBlockLocation().getLocation(); return location; } public void setLocation(Location loc) { - /*world = loc.getWorld().getUID(); - x = loc.getBlockX(); - y = loc.getBlockY(); - z = loc.getBlockZ();*/ - //setBlockLocation(new BlockLocation(loc)); location = loc; } 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 dcea994..97f9ca8 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/BlockStateConfig.java @@ -63,9 +63,13 @@ public class BlockStateConfig extends Configuration implements IConfigurationSub /** * BlockStateEnabled * - * ... + * This experimental Feature stores the GameMode a Block was created in, and prevents drops if a Block was created + * in creative mode. * - * default: true + * Due to the Experimental state this Feature isn't enabled by default. It uses the Database-credentials from + * bukkit.yml (http://wiki.bukkit.org/Bukkit.yml#database) in the server-directory. + * + * default: false */ @IsConfigurationNode(order = 100) public boolean getEnabled() { 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 89ad7b8..394647c 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/DBQueries.java @@ -7,6 +7,7 @@ import java.sql.SQLException; import org.bukkit.GameMode; import org.bukkit.Location; +import de.jaschastarke.database.Type; import de.jaschastarke.database.db.Database; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState.Source; @@ -19,11 +20,11 @@ public class DBQueries { private PreparedStatement find = null; public BlockState find(Location loc) throws SQLException { if (find == null) { - find = db.prepare("SELECT * FROM block_state WHERE x = ? AND y = ? AND z = ? AND world = ?"); + find = db.prepare("SELECT * FROM lc_block_state WHERE x = ? AND y = ? AND z = ? AND world = ?"); } find.setInt(1, loc.getBlockX()); - find.setInt(2, loc.getBlockX()); - find.setInt(3, loc.getBlockX()); + find.setInt(2, loc.getBlockY()); + find.setInt(3, loc.getBlockZ()); find.setString(4, loc.getWorld().getUID().toString()); ResultSet rs = find.executeQuery(); if (rs.next()) { @@ -41,49 +42,76 @@ public class DBQueries { private PreparedStatement delete = null; public boolean delete(BlockState s) throws SQLException { if (delete == null) { - delete = db.prepare("DELETE FROM block_state WHERE x = ? AND y = ? AND z = ? AND world = ?"); + delete = db.prepare("DELETE FROM lc_block_state WHERE x = ? AND y = ? AND z = ? AND world = ?"); } delete.setInt(1, s.getLocation().getBlockX()); - delete.setInt(2, s.getLocation().getBlockX()); - delete.setInt(3, s.getLocation().getBlockX()); + delete.setInt(2, s.getLocation().getBlockY()); + delete.setInt(3, s.getLocation().getBlockZ()); delete.setString(4, s.getLocation().getWorld().getUID().toString()); return delete.executeUpdate() > 0; } + private PreparedStatement insert = null; + public boolean insert(BlockState s) throws SQLException { + if (insert == null) { + insert = db.prepare("INSERT INTO lc_block_state (x, y, z, world, gm, player, cdate, source)"+ + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); + } + insert.setInt(1, s.getLocation().getBlockX()); + insert.setInt(2, s.getLocation().getBlockY()); + insert.setInt(3, s.getLocation().getBlockZ()); + insert.setString(4, s.getLocation().getWorld().getUID().toString()); + if (db.getType() == Type.MySQL) + insert.setString(5, s.getGameMode().name()); + else + insert.setInt(5, s.getGameMode().getValue()); + insert.setString(6, s.getPlayerName()); + insert.setTimestamp(7, new java.sql.Timestamp(s.getDate().getTime())); + if (db.getType() == Type.MySQL) + insert.setString(8, s.getSource().name()); + else + insert.setInt(8, s.getSource().ordinal()); + return insert.executeUpdate() > 0; + } + private GameMode getGameMode(ResultSet rs) { - switch (db.getType()) { - case SQLite: - try { - return GameMode.values()[rs.getInt("gm")]; - } catch (Exception e) { - db.getLogger().warn("Couldn't get GameMode from result-set: "+e.getMessage()); - return GameMode.SURVIVAL; - } - default: - throw new RuntimeException("Currently only SQLite is supported."); + try { + switch (db.getType()) { + case SQLite: + return GameMode.getByValue(rs.getInt("gm")); + case MySQL: + return GameMode.valueOf(rs.getString("gm")); + default: + throw new RuntimeException("Unsupported Database-Type."); + } + } catch (SQLException e) { + db.getLogger().warn("Couldn't get GameMode from result-set: "+e.getMessage()); + return GameMode.SURVIVAL; } } private Source getSource(ResultSet rs) { - switch (db.getType()) { - case SQLite: - try { + try { + switch (db.getType()) { + case SQLite: return Source.values()[rs.getInt("source")]; - } catch (Exception e) { - db.getLogger().warn("Couldn't get Source from result-set: "+e.getMessage()); - return Source.UNKNOWN; - } - default: - throw new RuntimeException("Currently only SQLite is supported."); + case MySQL: + return Source.valueOf(rs.getString("source")); + default: + throw new RuntimeException("Unsupported Database-Type."); + } + } catch (Exception e) { + db.getLogger().warn("Couldn't get Source from result-set: "+e.getMessage()); + return Source.UNKNOWN; } } public void initTable() throws SQLException { switch (db.getType()) { case SQLite: - if (db.getDDL().tableExists("block_state")) { + if (!db.getDDL().tableExists("lc_block_state")) { db.execute( - "CREATE TABLE block_state ("+ + "CREATE TABLE lc_block_state ("+ "x integer,"+ "y integer,"+ "z integer,"+ @@ -92,11 +120,30 @@ public class DBQueries { "player varchar(255),"+ "cdate timestamp not null,"+ "source integer not null,"+ - "constraint ck_block_state_gm check (gm in (0,1,2)),"+ - "constraint ck_block_state_source check (source in (0,1,2,3))"+ + "primary key (x, y, z, world),"+ + "constraint ck_lc_block_state_gm check (gm in (0,1,2)),"+ + "constraint ck_lc_block_state_source check (source in (0,1,2,3))"+ ")" ); - db.getLogger().info("Created SQLite-Table: block_state"); + db.getLogger().info("Created SQLite-Table: lc_block_state"); + } + break; + case MySQL: + if (!db.getDDL().tableExists("lc_block_state")) { + db.execute( + "CREATE TABLE IF NOT EXISTS lc_block_state ("+ + "x INT NOT NULL,"+ + "y INT NOT NULL,"+ + "z INT NOT NULL,"+ + "world VARCHAR(40) NOT NULL,"+ + "gm ENUM('CREATIVE', 'SURVIVAL', 'ADVENTURE'),"+ + "player VARCHAR(255),"+ + "cdate TIMESTAMP NOT NULL,"+ + "source ENUM('SEED','PLAYER','EDIT','UNKNOWN'),"+ + "PRIMARY KEY (x, y, z, world)"+ + ")" + ); + db.getLogger().info("Created MySQL-Table: lc_block_state"); } break; default: diff --git a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/PlayerListener.java b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/PlayerListener.java index ae42552..40fae9c 100644 --- a/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/PlayerListener.java +++ b/src/main/java/de/jaschastarke/minecraft/limitedcreative/blockstate/PlayerListener.java @@ -1,5 +1,7 @@ package de.jaschastarke.minecraft.limitedcreative.blockstate; +import java.sql.SQLException; + import org.bukkit.ChatColor; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; @@ -23,36 +25,40 @@ public class PlayerListener implements Listener { public void onInteract(PlayerInteractEvent event) { if (event.isCancelled()) return; - /*if (event.getAction() == Action.RIGHT_CLICK_BLOCK && mod.getPlugin().getPermManager().hasPermission(event.getPlayer(), BlockStatePermissions.TOOL)) { + if (event.getAction() == Action.RIGHT_CLICK_BLOCK && mod.getPlugin().getPermManager().hasPermission(event.getPlayer(), BlockStatePermissions.TOOL)) { Block b = event.getClickedBlock(); if (b != null && event.getPlayer().getItemInHand().getType().equals(mod.getConfig().getTool())) { - BlockState s = mod.getDB().find(BlockState.class, new BlockLocation(b.getLocation())); - InGameFormatter f = new InGameFormatter(mod.getPlugin().getLang()); - String ret = null; - if (s == null || s.getSource() == Source.UNKNOWN) { - ret = f.formatString(ChatFormattings.ERROR, f.getString("block_state.tool_info.unknown", b.getType().toString())); - } else { - String k = "block_state.tool_info." + s.getSource().name().toLowerCase(); - String gm = s.getGameMode().toString().toLowerCase(); - switch (s.getGameMode()) { - case CREATIVE: - gm = ChatColor.GOLD + gm + ChatColor.RESET; - case SURVIVAL: - gm = ChatColor.GREEN + gm + ChatColor.RESET; - case ADVENTURE: - gm = ChatColor.DARK_GREEN + gm + ChatColor.RESET; - default: - break; + try { + BlockState s = mod.getQueries().find(b.getLocation()); + InGameFormatter f = new InGameFormatter(mod.getPlugin().getLang()); + String ret = null; + if (s == null || s.getSource() == Source.UNKNOWN) { + ret = f.formatString(ChatFormattings.ERROR, f.getString("block_state.tool_info.unknown", b.getType().toString())); + } else { + String k = "block_state.tool_info." + s.getSource().name().toLowerCase(); + String gm = s.getGameMode().toString().toLowerCase(); + switch (s.getGameMode()) { + case CREATIVE: + gm = ChatColor.GOLD + gm + ChatColor.RESET; + case SURVIVAL: + gm = ChatColor.GREEN + gm + ChatColor.RESET; + case ADVENTURE: + gm = ChatColor.DARK_GREEN + gm + ChatColor.RESET; + default: + break; + } + + ret = f.formatString(ChatFormattings.INFO, f.getString(k, b.getType().toString(), + s.getPlayerName(), + gm, + s.getDate())); } - - ret = f.formatString(ChatFormattings.INFO, f.getString(k, b.getType().toString(), - s.getPlayerName(), - gm, - s.getDate())); + if (ret != null) + event.getPlayer().sendMessage(ret); + } catch (SQLException e) { + mod.getLog().warn("DB-Error while onPlayerInteract: "+e.getMessage()); } - if (ret != null) - event.getPlayer().sendMessage(ret); } - }*/ + } } } diff --git a/src/main/resources/lang/messages.properties b/src/main/resources/lang/messages.properties index c6e77c9..4010789 100644 --- a/src/main/resources/lang/messages.properties +++ b/src/main/resources/lang/messages.properties @@ -49,3 +49,4 @@ block_state.tool_info.seed: This {0}-Block is generated by the god who created t block_state.tool_info.player: This {0}-Block was created by ''{1}'' in {2}-mode on {3} block_state.tool_info.edit: This {0}-Block was modified by the tool ''{1}'' or such at {3} block_state.tool_info.unknown: The origin of this {0}-Block is unknown +block_state.error.sql_connection_failed: Failed to connect to Database. Check bukkit.yml \ No newline at end of file