Complete switch from eBean to plib DBAL

This commit is contained in:
Jascha Starke 2013-07-26 23:30:57 +02:00
parent 536c5bfdcb
commit 1fbd0e14cf
8 changed files with 140 additions and 204 deletions

View file

@ -53,7 +53,7 @@ public class ModBlockStates extends CoreModule<LimitedCreative> {
} }
@Override @Override
public void onDisable() { public void onDisable() {
super.onDisable();; super.onDisable();
} }
public BlockStateConfig getConfig() { public BlockStateConfig getConfig() {

View file

@ -24,21 +24,21 @@ public class BlockListener implements Listener {
return; return;
try { try {
//BlockLocation bl = new BlockLocation(event.getBlock().getLocation());
//BlockState s = mod.getDB().find(BlockState.class, bl);
BlockState s = mod.getQueries().find(event.getBlock().getLocation()); BlockState s = mod.getQueries().find(event.getBlock().getLocation());
if (s != null) { if (s != null) {
if (mod.isDebug()) if (mod.isDebug())
mod.getLog().debug("Breaking bad, err.. block: " + s.toString()); mod.getLog().debug("Breaking bad, err.. block: " + s.toString());
if (s.getGameMode() == GameMode.CREATIVE && event.getPlayer().getGameMode() != GameMode.CREATIVE) { 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.getBlockSpawn().block(event.getBlock(), event.getPlayer());
} }
mod.getQueries().delete(s); mod.getQueries().delete(s);
} }
} catch (SQLException e) { } 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); event.setCancelled(true);
} }
} }
@ -47,20 +47,24 @@ public class BlockListener implements Listener {
if (event.isCancelled()) if (event.isCancelled())
return; return;
/*BlockLocation bl = new BlockLocation(event.getBlock().getLocation()); try {
BlockState s = mod.getDB().find(BlockState.class, bl); BlockState s = mod.getQueries().find(event.getBlock().getLocation());
if (s != null) { if (s != null) {
// This shouldn't happen // This shouldn't happen
if (mod.isDebug()) if (mod.isDebug())
mod.getLog().debug("Replacing current BlockState: " + s.toString()); mod.getLog().debug("Replacing current BlockState: " + s.toString());
} else { } else {
s = new BlockState(); s = new BlockState();
s.setBlockLocation(bl); s.setLocation(event.getBlock().getLocation());
} }
s.setPlayer(event.getPlayer()); s.setPlayer(event.getPlayer());
s.setDate(new Date()); s.setDate(new Date());
if (mod.isDebug()) if (mod.isDebug())
mod.getLog().debug("Saving BlockState: " + s.toString()); mod.getLog().debug("Saving BlockState: " + s.toString());
mod.getDB().save(s);*/ mod.getQueries().insert(s);
} catch (SQLException e) {
mod.getLog().warn("DB-Error while onBlockPlace: "+e.getMessage());
event.setCancelled(true);
}
} }
} }

View file

@ -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() + "}";
}
}

View file

@ -16,6 +16,9 @@ import org.bukkit.entity.Player;
import com.avaje.ebean.validation.NotNull; 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 @Entity
@Table(name = "block_state") @Table(name = "block_state")
//@IdClass(BlockLocation.class) //@IdClass(BlockLocation.class)
@ -27,17 +30,6 @@ public class BlockState {
UNKNOWN UNKNOWN
} }
/*@Id
private UUID world;
@Id
private int x;
@Id
private int y;
@Id
private int z;*/
/*@EmbeddedId
private BlockLocation blockLocation;*/
private Location location; private Location location;
@Column(name = "gm") @Column(name = "gm")
@ -53,27 +45,12 @@ public class BlockState {
@NotNull @NotNull
private Source source = Source.UNKNOWN; private Source source = Source.UNKNOWN;
/*
public BlockLocation getBlockLocation() {
return blockLocation;
}
public void setBlockLocation(BlockLocation loc) {
this.blockLocation = loc;
}*/
public Location getLocation() { public Location getLocation() {
/*return new Location(Bukkit.getWorld(world), x, y, z);*/
//return getBlockLocation().getLocation();
return location; return location;
} }
public void setLocation(Location loc) { public void setLocation(Location loc) {
/*world = loc.getWorld().getUID();
x = loc.getBlockX();
y = loc.getBlockY();
z = loc.getBlockZ();*/
//setBlockLocation(new BlockLocation(loc));
location = loc; location = loc;
} }

View file

@ -63,9 +63,13 @@ public class BlockStateConfig extends Configuration implements IConfigurationSub
/** /**
* BlockStateEnabled * 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) @IsConfigurationNode(order = 100)
public boolean getEnabled() { public boolean getEnabled() {

View file

@ -7,6 +7,7 @@ import java.sql.SQLException;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
import de.jaschastarke.database.Type;
import de.jaschastarke.database.db.Database; import de.jaschastarke.database.db.Database;
import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState.Source; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState.Source;
@ -19,11 +20,11 @@ public class DBQueries {
private PreparedStatement find = null; private PreparedStatement find = null;
public BlockState find(Location loc) throws SQLException { public BlockState find(Location loc) throws SQLException {
if (find == null) { 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(1, loc.getBlockX());
find.setInt(2, loc.getBlockX()); find.setInt(2, loc.getBlockY());
find.setInt(3, loc.getBlockX()); find.setInt(3, loc.getBlockZ());
find.setString(4, loc.getWorld().getUID().toString()); find.setString(4, loc.getWorld().getUID().toString());
ResultSet rs = find.executeQuery(); ResultSet rs = find.executeQuery();
if (rs.next()) { if (rs.next()) {
@ -41,49 +42,76 @@ public class DBQueries {
private PreparedStatement delete = null; private PreparedStatement delete = null;
public boolean delete(BlockState s) throws SQLException { public boolean delete(BlockState s) throws SQLException {
if (delete == null) { 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(1, s.getLocation().getBlockX());
delete.setInt(2, s.getLocation().getBlockX()); delete.setInt(2, s.getLocation().getBlockY());
delete.setInt(3, s.getLocation().getBlockX()); delete.setInt(3, s.getLocation().getBlockZ());
delete.setString(4, s.getLocation().getWorld().getUID().toString()); delete.setString(4, s.getLocation().getWorld().getUID().toString());
return delete.executeUpdate() > 0; 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) { private GameMode getGameMode(ResultSet rs) {
try {
switch (db.getType()) { switch (db.getType()) {
case SQLite: case SQLite:
try { return GameMode.getByValue(rs.getInt("gm"));
return GameMode.values()[rs.getInt("gm")]; case MySQL:
} catch (Exception e) { 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()); db.getLogger().warn("Couldn't get GameMode from result-set: "+e.getMessage());
return GameMode.SURVIVAL; return GameMode.SURVIVAL;
} }
default:
throw new RuntimeException("Currently only SQLite is supported.");
}
} }
private Source getSource(ResultSet rs) { private Source getSource(ResultSet rs) {
try {
switch (db.getType()) { switch (db.getType()) {
case SQLite: case SQLite:
try {
return Source.values()[rs.getInt("source")]; return Source.values()[rs.getInt("source")];
case MySQL:
return Source.valueOf(rs.getString("source"));
default:
throw new RuntimeException("Unsupported Database-Type.");
}
} catch (Exception e) { } catch (Exception e) {
db.getLogger().warn("Couldn't get Source from result-set: "+e.getMessage()); db.getLogger().warn("Couldn't get Source from result-set: "+e.getMessage());
return Source.UNKNOWN; return Source.UNKNOWN;
} }
default:
throw new RuntimeException("Currently only SQLite is supported.");
}
} }
public void initTable() throws SQLException { public void initTable() throws SQLException {
switch (db.getType()) { switch (db.getType()) {
case SQLite: case SQLite:
if (db.getDDL().tableExists("block_state")) { if (!db.getDDL().tableExists("lc_block_state")) {
db.execute( db.execute(
"CREATE TABLE block_state ("+ "CREATE TABLE lc_block_state ("+
"x integer,"+ "x integer,"+
"y integer,"+ "y integer,"+
"z integer,"+ "z integer,"+
@ -92,11 +120,30 @@ public class DBQueries {
"player varchar(255),"+ "player varchar(255),"+
"cdate timestamp not null,"+ "cdate timestamp not null,"+
"source integer not null,"+ "source integer not null,"+
"constraint ck_block_state_gm check (gm in (0,1,2)),"+ "primary key (x, y, z, world),"+
"constraint ck_block_state_source check (source in (0,1,2,3))"+ "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; break;
default: default:

View file

@ -1,5 +1,7 @@
package de.jaschastarke.minecraft.limitedcreative.blockstate; package de.jaschastarke.minecraft.limitedcreative.blockstate;
import java.sql.SQLException;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -23,10 +25,11 @@ public class PlayerListener implements Listener {
public void onInteract(PlayerInteractEvent event) { public void onInteract(PlayerInteractEvent event) {
if (event.isCancelled()) if (event.isCancelled())
return; 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(); Block b = event.getClickedBlock();
if (b != null && event.getPlayer().getItemInHand().getType().equals(mod.getConfig().getTool())) { if (b != null && event.getPlayer().getItemInHand().getType().equals(mod.getConfig().getTool())) {
BlockState s = mod.getDB().find(BlockState.class, new BlockLocation(b.getLocation())); try {
BlockState s = mod.getQueries().find(b.getLocation());
InGameFormatter f = new InGameFormatter(mod.getPlugin().getLang()); InGameFormatter f = new InGameFormatter(mod.getPlugin().getLang());
String ret = null; String ret = null;
if (s == null || s.getSource() == Source.UNKNOWN) { if (s == null || s.getSource() == Source.UNKNOWN) {
@ -52,7 +55,10 @@ public class PlayerListener implements Listener {
} }
if (ret != null) if (ret != null)
event.getPlayer().sendMessage(ret); event.getPlayer().sendMessage(ret);
} catch (SQLException e) {
mod.getLog().warn("DB-Error while onPlayerInteract: "+e.getMessage());
}
}
} }
}*/
} }
} }

View file

@ -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.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.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.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