Database-Rework

This commit is contained in:
Jascha Starke 2013-07-26 17:00:47 +02:00
parent 85a0008fbe
commit 536c5bfdcb
7 changed files with 161 additions and 54 deletions

View file

@ -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<Class<?>> getDatabaseClasses() {
List<Class<?>> list = super.getDatabaseClasses();
list.add(BlockLocation.class);
list.add(BlockState.class);
return list;
}
@Override
public boolean isDebug() {
return config.getDebug();

View file

@ -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<LimitedCreative> {
private BlockStateConfig config;
private FeatureBlockItemSpawn blockDrops;
private DBQueries queries;
public ModBlockStates(LimitedCreative plugin) {
super(plugin);
@ -31,15 +30,23 @@ public class ModBlockStates extends CoreModule<LimitedCreative> {
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<LimitedCreative> {
super.onDisable();;
}
public EbeanServer getDB() {
return plugin.getDatabaseManager().getDatabase();
}
public Query<BlockState> getBSQuery() {
return plugin.getDatabaseManager().getDatabase().find(BlockState.class);
}
public BlockStateConfig getConfig() {
return config;
}
public FeatureBlockItemSpawn getBlockSpawn() {
return blockDrops;
}
public DBQueries getQueries() {
return queries;
}
}

View file

@ -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);*/
}
}

View file

@ -12,6 +12,7 @@ import org.bukkit.World;
@Embeddable
@Entity
@Deprecated
public class BlockLocation implements Serializable {
private static final long serialVersionUID = -8644798679923736348L;

View file

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

View file

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

View file

@ -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);
}
}
}*/
}
}