diff --git a/plugins/citadel-paper/src/META-INF/MANIFEST.MF b/plugins/citadel-paper/src/META-INF/MANIFEST.MF new file mode 100644 index 000000000..59499bce4 --- /dev/null +++ b/plugins/citadel-paper/src/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/plugins/citadel-paper/src/com/untamedears/Citadel/BlockListener.java b/plugins/citadel-paper/src/com/untamedears/Citadel/BlockListener.java index 5095c2dbf..bc83d8dd0 100644 --- a/plugins/citadel-paper/src/com/untamedears/Citadel/BlockListener.java +++ b/plugins/citadel-paper/src/com/untamedears/Citadel/BlockListener.java @@ -17,205 +17,142 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.material.Door; import org.bukkit.plugin.java.JavaPlugin; -import java.sql.SQLException; import java.util.HashMap; +import java.util.logging.Logger; -public class BlockListener implements Listener -{ - private HashMap delayedReinforcements; - private HashMap taskMaterial; - private HashMap taskInitiator; - public static HashMap playerReenforcers; - private JavaPlugin myPlugin; - private CitadelDao dao; +public class BlockListener implements Listener { + private HashMap delayedReinforcements; + private HashMap taskMaterial; + private HashMap taskInitiator; + public static HashMap playerReinforcers; + private JavaPlugin myPlugin; + private CitadelDao dao; + private Logger log; - public BlockListener(JavaPlugin jp)throws SQLException, ClassNotFoundException - { - this.delayedReinforcements = new HashMap(); - playerReenforcers = new HashMap(); - this.taskMaterial = new HashMap(); - this.taskInitiator = new HashMap(); + public BlockListener(JavaPlugin jp) throws ClassNotFoundException { + this.delayedReinforcements = new HashMap(); + playerReinforcers = new HashMap(); + this.taskMaterial = new HashMap(); + this.taskInitiator = new HashMap(); - this.myPlugin = jp; - this.dao = ((Citadel)this.myPlugin).dao; - } + this.myPlugin = jp; + this.dao = ((Citadel) this.myPlugin).dao; + this.log = myPlugin.getLogger(); + } - @EventHandler - public void applyReinforcement(BlockPlaceEvent bpe) - { - Player placer = bpe.getPlayer(); - Block block = bpe.getBlock(); - Material matl = playerReenforcers.get(placer); - if ((matl != null) && (Citadel.materialStrengths.containsKey(matl)) && (Citadel.materialRequirements.containsKey(matl))) - { - System.out.println("matl: " + matl + " requires: " + Citadel.materialRequirements.get(matl)); - if (placer.getInventory().contains(matl, Citadel.materialRequirements.get(matl))) - { - try - { - int pid = this.myPlugin.getServer().getScheduler().scheduleSyncDelayedTask( - this.myPlugin, - dao.addReinforcement(block, matl), - 20L); + @EventHandler + public void applyReinforcement(BlockPlaceEvent bpe) { + Player placer = bpe.getPlayer(); + Block block = bpe.getBlock(); + Material matl = playerReinforcers.get(placer); + if ((matl != null) && (Citadel.materialStrengths.containsKey(matl)) && (Citadel.materialRequirements.containsKey(matl))) { + log.info("matl: " + matl + " requires: " + Citadel.materialRequirements.get(matl)); + if (placer.getInventory().contains(matl, Citadel.materialRequirements.get(matl))) { + int pid = this.myPlugin.getServer().getScheduler().scheduleSyncDelayedTask( + this.myPlugin, + dao.addReinforcement(block, matl), + 20L); - this.delayedReinforcements.put(block, pid); - placer.getInventory().removeItem(new ItemStack(matl, Citadel.materialRequirements.get(matl))); - this.taskInitiator.put(pid, placer); - this.taskMaterial.put(pid, matl); - } - catch (SQLException e) - { - System.err.println("Exception creating reinforcement:\n" + e); - return; - } - } - else - { - placer.sendMessage(ChatColor.YELLOW + "You require more " + matl + " to continue reinforcements."); - playerReenforcers.remove(placer); - placer.sendMessage("You are now out of reinforcement mode"); - } - } - } - - @EventHandler - public void gracefullyRemoveReinforcementModeOnLogout(PlayerQuitEvent pqe) - { - if (playerReenforcers.containsKey(pqe.getPlayer())) - playerReenforcers.remove(pqe.getPlayer()); - } - - @EventHandler - public void checkDurabilityAndDelayedEventCheck(BlockBreakEvent bbe) { - Block block = bbe.getBlock(); - try { - if (this.delayedReinforcements.containsKey(block)) - { - Integer pid = this.delayedReinforcements.get(block); - Material matl = this.taskMaterial.get(pid); - this.myPlugin.getServer().getScheduler().cancelTask(this.delayedReinforcements.get(block)); - this.taskInitiator.get(pid).getInventory().addItem(new ItemStack(matl, Citadel.materialRequirements.get(matl))); - - this.delayedReinforcements.remove(block); - this.taskInitiator.remove(pid); - this.taskMaterial.remove(pid); + this.delayedReinforcements.put(block, pid); + placer.getInventory().removeItem(new ItemStack(matl, Citadel.materialRequirements.get(matl))); + this.taskInitiator.put(pid, placer); + this.taskMaterial.put(pid, matl); + } else { + placer.sendMessage(ChatColor.YELLOW + "You require more " + matl + " to continue reinforcements."); + playerReinforcers.remove(placer); + placer.sendMessage("You are now out of reinforcement mode"); } - - Integer durability = dao.updateReinforcement(block, 1); - if (durability == null) return; - - if (durability <= 0) - { - dao.removeReinforcement(block); - } else - { - bbe.setCancelled(true); - } - } catch (SQLException e) { - System.err.println("Citadel - error accessing database:\n" + e); } } - @EventHandler - public void controlAccess(PlayerInteractEvent pie) { + @EventHandler + public void gracefullyRemoveReinforcementModeOnLogout(PlayerQuitEvent pqe) { + if (playerReinforcers.containsKey(pqe.getPlayer())) + playerReinforcers.remove(pqe.getPlayer()); + } + + @EventHandler + public void checkDurabilityAndDelayedEventCheck(BlockBreakEvent bbe) { + Block block = bbe.getBlock(); + if (this.delayedReinforcements.containsKey(block)) { + Integer pid = this.delayedReinforcements.get(block); + Material matl = this.taskMaterial.get(pid); + this.myPlugin.getServer().getScheduler().cancelTask(this.delayedReinforcements.get(block)); + this.taskInitiator.get(pid).getInventory().addItem(new ItemStack(matl, Citadel.materialRequirements.get(matl))); + + this.delayedReinforcements.remove(block); + this.taskInitiator.remove(pid); + this.taskMaterial.remove(pid); + } + + Integer durability = dao.updateReinforcement(block, 1); + if (durability == null) return; + + if (durability <= 0) { + dao.removeReinforcement(block); + } else { + bbe.setCancelled(true); + } + } + + @EventHandler + public void controlAccess(PlayerInteractEvent pie) { if (!pie.hasBlock()) return; - Block block = pie.getClickedBlock(); + Block block = pie.getClickedBlock(); Material matl = block.getType(); - Player p = pie.getPlayer(); - try - { - if ((matl == Material.CHEST) || (matl == Material.WOODEN_DOOR) || (matl == Material.IRON_DOOR)) { - String group = dao.getRegisteredGroup(block); - if (group != null && !dao.isPlayerInGroup(group, p.getDisplayName())) { - p.sendMessage(ChatColor.RED + "That door/chest is locked."); - pie.setCancelled(true); - } - } - } - catch (SQLException e) - { - System.err.println("Error when seeing if the door's part of a group:\n" + e); - } - } - - public void controlRedstone(BlockRedstoneEvent bre) - { - Block block = bre.getBlock(); - - if ((block instanceof Door)) - try - { - String group = dao.getRegisteredGroup(block); - if (group != null) { - bre.setNewCurrent(bre.getOldCurrent()); - } - } - catch (SQLException e) - { - System.err.println("Error in redstone protection checking:\n" + e); - } - } - - @EventHandler - public void makePrivateGroup(PlayerLoginEvent ple) - { - ((Citadel)this.myPlugin).playerPlacementState.put(ple.getPlayer(), 1); - try - { - String playerName = ple.getPlayer().getDisplayName(); - if (!dao.isPlayerInGroup(playerName, playerName)) { - dao.addPlayerToGroup(playerName, playerName); + Player p = pie.getPlayer(); + if ((matl == Material.CHEST) || (matl == Material.WOODEN_DOOR) || (matl == Material.IRON_DOOR)) { + String group = dao.getRegisteredGroup(block); + if (group != null && !dao.isPlayerInGroup(group, p.getDisplayName())) { + p.sendMessage(ChatColor.RED + "That door/chest is locked."); + pie.setCancelled(true); } - } - catch (SQLException e) - { - System.err.println("Error in seeing if player has self-group:\n" + e); - } - } + } + } - @EventHandler - public void secureDoorOrChest(BlockPlaceEvent bpe) - { - Block blk = bpe.getBlock(); - Material object = bpe.getBlock().getType(); + public void controlRedstone(BlockRedstoneEvent bre) { + Block block = bre.getBlock(); - if ((object != Material.CHEST) && (object != Material.WOODEN_DOOR) && (object != Material.IRON_DOOR)) - { - return; - } + if ((block instanceof Door)) { + String group = dao.getRegisteredGroup(block); + if (group != null) { + bre.setNewCurrent(bre.getOldCurrent()); + } + } + } - Integer playerState = ((Citadel)this.myPlugin).playerPlacementState.get(bpe.getPlayer()); - if (playerState == null) - { - ((Citadel)this.myPlugin).playerPlacementState.put(bpe.getPlayer(), 0); - playerState = 0; - } + @EventHandler + public void makePrivateGroup(PlayerLoginEvent ple) { + ((Citadel) this.myPlugin).playerPlacementState.put(ple.getPlayer(), 1); + String playerName = ple.getPlayer().getDisplayName(); + if (!dao.isPlayerInGroup(playerName, playerName)) { + dao.addPlayerToGroup(playerName, playerName); + } + } - if (playerState > 0) - { - try - { - dao.addRegisteredGroup(blk, bpe.getPlayer().getDisplayName()); - } - catch (SQLException e) - { - System.err.println("Error putting chest/door into protection database:\n" + e); - } - } - } + @EventHandler + public void secureDoorOrChest(BlockPlaceEvent bpe) { + Block blk = bpe.getBlock(); + Material object = bpe.getBlock().getType(); - public void close() - { - try - { - dao.close(); - } - catch (SQLException e) - { - System.err.println("Citadel - Sorry, I'll have to close the database ungracefully!"); - System.err.println(e.toString()); - } - } + if ((object != Material.CHEST) && (object != Material.WOODEN_DOOR) && (object != Material.IRON_DOOR)) { + return; + } + + Integer playerState = ((Citadel) this.myPlugin).playerPlacementState.get(bpe.getPlayer()); + if (playerState == null) { + ((Citadel) this.myPlugin).playerPlacementState.put(bpe.getPlayer(), 0); + playerState = 0; + } + + if (playerState > 0) { + dao.addRegisteredGroup(blk, bpe.getPlayer().getDisplayName()); + } + } + + public void close() { + dao.close(); + } } \ No newline at end of file diff --git a/plugins/citadel-paper/src/com/untamedears/Citadel/Citadel.java b/plugins/citadel-paper/src/com/untamedears/Citadel/Citadel.java index 142c61ad3..edc4a655e 100644 --- a/plugins/citadel-paper/src/com/untamedears/Citadel/Citadel.java +++ b/plugins/citadel-paper/src/com/untamedears/Citadel/Citadel.java @@ -9,164 +9,110 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import java.sql.SQLException; import java.util.HashMap; import java.util.List; +import java.util.logging.Logger; -public class Citadel extends JavaPlugin -{ - public static HashMap materialStrengths; - public static HashMap materialRequirements; - public HashMap playerPlacementState; +public class Citadel extends JavaPlugin { + public static HashMap materialStrengths; + public static HashMap materialRequirements; + public HashMap playerPlacementState; public CitadelDao dao; + public Logger log; - public void onEnable() - { - reloadConfig(); - materialStrengths = new HashMap(); - materialRequirements = new HashMap(); - this.playerPlacementState = new HashMap(); + public void onEnable() { + log = this.getLogger(); + reloadConfig(); + materialStrengths = new HashMap(); + materialRequirements = new HashMap(); + this.playerPlacementState = new HashMap(); - List tmp1 = getConfig().getIntegerList("materials.strengths.keys"); - List tmp2 = getConfig().getIntegerList("materials.strengths.strengths"); - List tmp3 = getConfig().getIntegerList("materials.strengths.requirements"); + List tmp1 = getConfig().getIntegerList("materials.strengths.keys"); + List tmp2 = getConfig().getIntegerList("materials.strengths.strengths"); + List tmp3 = getConfig().getIntegerList("materials.strengths.requirements"); - Integer[] keys = tmp1.toArray(new Integer[tmp1.size()]); - Integer[] strengths = tmp2.toArray(new Integer[tmp2.size()]); - Integer[] requirements = tmp3.toArray(new Integer[tmp3.size()]); + Integer[] keys = tmp1.toArray(new Integer[tmp1.size()]); + Integer[] strengths = tmp2.toArray(new Integer[tmp2.size()]); + Integer[] requirements = tmp3.toArray(new Integer[tmp3.size()]); - if ((keys.length != strengths.length) || (keys.length != requirements.length)) - { - System.err.println("Citadel - config lengths not matching!"); - System.err.println("File is most likely corrupted but I'll try to do the best I can"); - System.err.println("By interpreting the shortest of it all as the proper length"); - System.err.println("Proceed with caution and re-look-over your configuration file!"); - } - for (int i = 0; i < Math.min(keys.length, Math.min(strengths.length, requirements.length)); i++) - { - materialStrengths.put(Material.getMaterial(keys[i]), strengths[i]); - materialRequirements.put(Material.getMaterial(keys[i]), requirements[i]); - } + if ((keys.length != strengths.length) || (keys.length != requirements.length)) { + log.warning("Citadel - config lengths not matching!"); + log.warning("File is most likely corrupted but I'll try to do the best I can"); + log.warning("By interpreting the shortest of it all as the proper length"); + log.warning("Proceed with caution and re-look-over your configuration file!"); + } + for (int i = 0; i < Math.min(keys.length, Math.min(strengths.length, requirements.length)); i++) { + materialStrengths.put(Material.getMaterial(keys[i]), strengths[i]); + materialRequirements.put(Material.getMaterial(keys[i]), requirements[i]); + } - try - { + try { dao = new CitadelDao(); Bukkit.getServer().getPluginManager().registerEvents(new BlockListener(this), this); - } - catch (SQLException e) - { - System.err.println("sorry there was some sort of sql exception:"); - System.err.println(e.toString()); - System.err.println(e.getSQLState()); - return; - } - catch (ClassNotFoundException e) - { - System.err.println("sorry we couldn't find the sqlite driver:"); - System.err.println(e.toString()); - return; - } - catch (Exception e) - { - System.err.println("Other weird error when enabling Citadel: "); - System.err.println(e.toString()); - } + log.info("Citadel - Hi folks, Citadel is now on :D."); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } - System.out.println("Citadel - Hi folks, Citadel is now on :D."); - } - - public void onDisable() - { - System.out.println("Citadel - Hi folks, Citadel is disabled :(."); - } - - public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) - { - if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "You must be a player!"); - return true; - } + public void onDisable() { + log.info("Citadel - Hi folks, Citadel is disabled :(."); + } - if (cmd.getName().equalsIgnoreCase("ctfortify")) - { - if (args.length < 1) - { - sender.sendMessage(ChatColor.RED + "Please specify reinforcement block type."); - return true; - } - Material matl = Material.getMaterial(args[0].toUpperCase()); - if (materialStrengths.containsKey(matl)) - BlockListener.playerReenforcers.put((Player)sender, matl); - else - sender.sendMessage(ChatColor.YELLOW + "Material " + args[0] + " not found as applicable reinforcement material"); - } - else if (cmd.getName().equalsIgnoreCase("ctstop")) - { - BlockListener.playerReenforcers.remove((Player)sender); - sender.sendMessage(ChatColor.GREEN + "You are now out of reinforcement mode"); - } - else if (cmd.getName().equalsIgnoreCase("ctplink")) - { - sender.sendMessage(ChatColor.RED + "Not yet implemented, sorry"); - if (args.length < 1) - sender.sendMessage(ChatColor.RED + "Please specify reinforcement block type."); - } - else if (cmd.getName().equalsIgnoreCase("ctlist")) - { - if ((materialRequirements.keySet() == null) || (materialRequirements.keySet().size() == 0)) - { - sender.sendMessage(ChatColor.YELLOW + "No reinforcement materials available."); - } - for (Material m : materialRequirements.keySet()) - sender.sendMessage(ChatColor.GREEN + m.toString() + " has strength " + materialStrengths.get(m) + " and you'll need " + materialRequirements.get(m) + " of it."); - } - else if (cmd.getName().equalsIgnoreCase("ctadd")) - { - if (args.length < 1) - { - sender.sendMessage(ChatColor.RED + "Please specify player to add."); - return true; - } - try { - dao.addPlayerToGroup(((Player) sender).getDisplayName() + "Grp", args[0]); - sender.sendMessage(ChatColor.GREEN + " player " + args[0] + " added."); - } - catch (SQLException e) - { - System.err.println("Error adding player " + args[0] + " to group " + ((Player)sender).getDisplayName() + ":\n" + e); - } - } - else if (cmd.getName().equalsIgnoreCase("ctrm")) - { - if (args.length < 1) - { - sender.sendMessage(ChatColor.RED + "Please specify player to add."); - return true; - } - try - { - dao.removePlayerFromGroup(((Player)sender).getDisplayName() + "Grp", args[0]); - sender.sendMessage(ChatColor.GREEN + " player " + args[0] + " removed."); - } - catch (SQLException e) - { - System.err.println("Error removing player " + args[0] + " from group " + ((Player)sender).getDisplayName() + ":\n" + e); - } - } - else if (cmd.getName().equalsIgnoreCase("ctpublic")) - { - this.playerPlacementState.put((Player)sender, 0); - sender.sendMessage(ChatColor.GREEN + "Now in public mode."); - } - else if (cmd.getName().equalsIgnoreCase("ctprivate")) - { - this.playerPlacementState.put((Player)sender, 1); - sender.sendMessage(ChatColor.GREEN + "Now in private mode."); - } else if (cmd.getName().equalsIgnoreCase("ctgrp")) { - this.playerPlacementState.put((Player)sender, 2); - sender.sendMessage(ChatColor.GREEN + "Now in group mode."); - } - return true; - } + public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "You must be a player!"); + return true; + } + + if (cmd.getName().equalsIgnoreCase("ctfortify")) { + if (args.length < 1) { + sender.sendMessage(ChatColor.RED + "Please specify reinforcement block type."); + return true; + } + Material matl = Material.getMaterial(args[0].toUpperCase()); + if (materialStrengths.containsKey(matl)) + BlockListener.playerReinforcers.put((Player) sender, matl); + else + sender.sendMessage(ChatColor.YELLOW + "Material " + args[0] + " not found as applicable reinforcement material"); + } else if (cmd.getName().equalsIgnoreCase("ctstop")) { + BlockListener.playerReinforcers.remove((Player) sender); + sender.sendMessage(ChatColor.GREEN + "You are now out of reinforcement mode"); + } else if (cmd.getName().equalsIgnoreCase("ctplink")) { + sender.sendMessage(ChatColor.RED + "Not yet implemented, sorry"); + if (args.length < 1) + sender.sendMessage(ChatColor.RED + "Please specify reinforcement block type."); + } else if (cmd.getName().equalsIgnoreCase("ctlist")) { + if ((materialRequirements.keySet() == null) || (materialRequirements.keySet().size() == 0)) { + sender.sendMessage(ChatColor.YELLOW + "No reinforcement materials available."); + } + for (Material m : materialRequirements.keySet()) + sender.sendMessage(ChatColor.GREEN + m.toString() + " has strength " + materialStrengths.get(m) + " and you'll need " + materialRequirements.get(m) + " of it."); + } else if (cmd.getName().equalsIgnoreCase("ctadd")) { + if (args.length < 1) { + sender.sendMessage(ChatColor.RED + "Please specify player to add."); + return true; + } + dao.addPlayerToGroup(((Player) sender).getDisplayName(), args[0]); + sender.sendMessage(ChatColor.GREEN + " player " + args[0] + " added."); + } else if (cmd.getName().equalsIgnoreCase("ctrm")) { + if (args.length < 1) { + sender.sendMessage(ChatColor.RED + "Please specify player to add."); + return true; + } + dao.removePlayerFromGroup(((Player) sender).getDisplayName(), args[0]); + sender.sendMessage(ChatColor.GREEN + " player " + args[0] + " removed."); + } else if (cmd.getName().equalsIgnoreCase("ctpublic")) { + this.playerPlacementState.put((Player) sender, 0); + sender.sendMessage(ChatColor.GREEN + "Now in public mode."); + } else if (cmd.getName().equalsIgnoreCase("ctprivate")) { + this.playerPlacementState.put((Player) sender, 1); + sender.sendMessage(ChatColor.GREEN + "Now in private mode."); + } else if (cmd.getName().equalsIgnoreCase("ctgrp")) { + this.playerPlacementState.put((Player) sender, 2); + sender.sendMessage(ChatColor.GREEN + "Now in group mode."); + } + return true; + } } \ No newline at end of file diff --git a/plugins/citadel-paper/src/com/untamedears/Citadel/dao/CitadelDao.java b/plugins/citadel-paper/src/com/untamedears/Citadel/dao/CitadelDao.java index 474e359ce..c2de2fe85 100644 --- a/plugins/citadel-paper/src/com/untamedears/Citadel/dao/CitadelDao.java +++ b/plugins/citadel-paper/src/com/untamedears/Citadel/dao/CitadelDao.java @@ -14,130 +14,179 @@ import java.sql.*; * Time: 3:58 PM */ public class CitadelDao { - private static final String CREATE_REINFORCEMENTS = "CREATE TABLE IF NOT EXISTS REENFORCEMENTS (x INTEGER,y INTEGER,z INTEGER,world TEXT,durability INTEGER);"; + private static final String CREATE_REINFORCEMENTS = "CREATE TABLE IF NOT EXISTS REINFORCEMENTS (x INTEGER,y INTEGER,z INTEGER,world TEXT,durability INTEGER);"; private static final String CREATE_GROUPS = "CREATE TABLE IF NOT EXISTS GROUPS (grpName TEXT,member TEXT);"; private static final String CREATE_REGISTRY = "CREATE TABLE IF NOT EXISTS REGISTRY (x INTEGER,y INTEGER,z INTEGER,world TEXT,grp TEXT);"; - + private static final String INSERT_GROUP = "INSERT INTO GROUPS (grpName,member) values (?,?);"; private static final String SELECT_GROUP = "SELECT grpName FROM GROUPS WHERE grpName=? AND member=?;"; private static final String DELETE_GROUP = "DELETE FROM GROUPS WHERE grpName=? AND member=?;"; - private static final String INSERT_REINFORCEMENT = "INSERT INTO REENFORCEMENTS (x,y,z,world,durability) values (?,?,?,?,?);"; - private static final String UPDATE_REINFORCEMENT = "UPDATE REENFORCEMENTS SET DURABILITY=DURABILITY-? WHERE x=? AND y=? AND z=? AND world=?;"; - private static final String SELECT_REINFORCEMENT = "SELECT DURABILITY FROM REENFORCEMENTS WHERE x=? AND y=? AND z=? AND world=?;"; - private static final String DELETE_REINFORCEMENT = "DELETE FROM REENFORCEMENTS WHERE x=:x AND y=? AND z=? AND world=?;"; - + private static final String INSERT_REINFORCEMENT = "INSERT INTO REINFORCEMENTS (x,y,z,world,durability) values (?,?,?,?,?);"; + private static final String UPDATE_REINFORCEMENT = "UPDATE REINFORCEMENTS SET DURABILITY=DURABILITY-? WHERE x=? AND y=? AND z=? AND world=?;"; + private static final String SELECT_REINFORCEMENT = "SELECT DURABILITY FROM REINFORCEMENTS WHERE x=? AND y=? AND z=? AND world=?;"; + private static final String DELETE_REINFORCEMENT = "DELETE FROM REINFORCEMENTS WHERE x=:x AND y=? AND z=? AND world=?;"; + + private static final String INSERT_REGISTRY = "INSERT INTO REGISTRY (x,y,z,world,grp) VALUES (?,?,?,?,?)"; private static final String SELECT_REGISTRY = "SELECT grp FROM REGISTRY WHERE x=? AND y=? AND z=? AND world=?"; private Connection conn; - public CitadelDao() throws ClassNotFoundException, SQLException { + public CitadelDao() throws ClassNotFoundException { Class.forName("org.sqlite.JDBC"); - conn = DriverManager.getConnection("jdbc:sqlite:plugins/Citadel/Citadel.db"); - conn.createStatement().execute(CREATE_REINFORCEMENTS); - conn.createStatement().execute(CREATE_GROUPS); - conn.createStatement().execute(CREATE_REGISTRY); - } - - public void addPlayerToGroup(String group, String player) throws SQLException { - PreparedStatement tmp = conn.prepareStatement(INSERT_GROUP); - tmp.setString(1, group); - tmp.setString(2, player); - tmp.execute(); - tmp.close(); - } - - public void removePlayerFromGroup(String group, String player) throws SQLException { - PreparedStatement tmp = conn.prepareStatement(DELETE_GROUP); - tmp.setString(1, group); - tmp.setString(2, player); - tmp.execute(); - tmp.close(); - } - - public boolean isPlayerInGroup(String group, String player) throws SQLException { - boolean isInGroup; - PreparedStatement tmp = conn.prepareStatement(SELECT_GROUP); - tmp.setString(1, group); - tmp.setString(2, player); - tmp.execute(); - ResultSet result = tmp.getResultSet(); - isInGroup = result.next(); - - result.close(); - tmp.close(); - - return isInGroup; - } - - public DelayedReinforcement addReinforcement(Block block, Material material) throws SQLException { - PreparedStatement tmp = conn.prepareStatement(INSERT_REINFORCEMENT); - setBlockParameters(tmp, block); - tmp.setInt(5, Citadel.materialStrengths.get(material)); - - return new DelayedReinforcement(tmp); - } - - public Integer updateReinforcement(Block block, int durabilityDamage) throws SQLException { - Integer durability = null; - PreparedStatement tmp = conn.prepareStatement(UPDATE_REINFORCEMENT + SELECT_REINFORCEMENT); - tmp.setInt(1, durabilityDamage); - setBlockParameters(tmp, block); - setBlockParameters(tmp, block); - tmp.execute(); - ResultSet result = tmp.getResultSet(); - if (result.next()) { - durability = result.getInt(1); + try { + conn = DriverManager.getConnection("jdbc:sqlite:plugins/Citadel/Citadel.db"); + conn.createStatement().execute(CREATE_REINFORCEMENTS); + conn.createStatement().execute(CREATE_GROUPS); + conn.createStatement().execute(CREATE_REGISTRY); + } catch (SQLException e) { + throw new RuntimeException(e); } - - result.close(); - tmp.close(); - - return durability; } - public void removeReinforcement(Block block) throws SQLException { - PreparedStatement tmp = conn.prepareStatement(DELETE_REINFORCEMENT); - setBlockParameters(tmp, block); - tmp.execute(); - tmp.close(); - } - - public void addRegisteredGroup(Block block, String group) throws SQLException { - PreparedStatement tmp = this.conn.prepareStatement( - "INSERT INTO REGISTRY (x,y,z,world,grp) VALUES (?,?,?,?,?)"); - setBlockParameters(tmp, block); - tmp.setString(5, group); - tmp.execute(); - tmp.close(); - } - - public String getRegisteredGroup(Block block) throws SQLException { - String group = null; - PreparedStatement tmp = conn.prepareStatement(SELECT_REGISTRY); - setBlockParameters(tmp, block); - tmp.execute(); - ResultSet result = tmp.getResultSet(); - if (result.next()) { - group = result.getString(1); + public void addPlayerToGroup(String group, String player) { + try { + PreparedStatement tmp = conn.prepareStatement(INSERT_GROUP); + tmp.setString(1, group); + tmp.setString(2, player); + tmp.execute(); + tmp.close(); + } catch (SQLException e) { + throw new RuntimeException(e); } - - result.close(); - tmp.close(); - - return group; } - private void setBlockParameters(PreparedStatement tmp, Block block) throws SQLException { - int modifier = tmp.getParameterMetaData().getParameterCount(); - tmp.setInt(1 + modifier, block.getX()); - tmp.setInt(2 + modifier, block.getY()); - tmp.setInt(3 + modifier, block.getZ()); - tmp.setString(4 + modifier, block.getWorld().getName()); + public void removePlayerFromGroup(String group, String player) { + try { + PreparedStatement tmp = conn.prepareStatement(DELETE_GROUP); + tmp.setString(1, group); + tmp.setString(2, player); + tmp.execute(); + tmp.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } - public void close() throws SQLException { - conn.close(); + public boolean isPlayerInGroup(String group, String player) { + try { + boolean isInGroup; + PreparedStatement tmp = conn.prepareStatement(SELECT_GROUP); + tmp.setString(1, group); + tmp.setString(2, player); + tmp.execute(); + ResultSet result = tmp.getResultSet(); + isInGroup = result.next(); + + result.close(); + tmp.close(); + + return isInGroup; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public DelayedReinforcement addReinforcement(Block block, Material material) { + try { + PreparedStatement tmp = conn.prepareStatement(INSERT_REINFORCEMENT); + setBlockParameters(tmp, block, 1); + tmp.setInt(5, Citadel.materialStrengths.get(material)); + + return new DelayedReinforcement(tmp); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public Integer updateReinforcement(Block block, int durabilityDamage) { + try { + Integer durability = null; + PreparedStatement tmp = conn.prepareStatement(UPDATE_REINFORCEMENT); + tmp.setInt(1, durabilityDamage); + setBlockParameters(tmp, block, 2); + tmp.execute(); + if (tmp.getUpdateCount() > 0) { + tmp.close(); + tmp = conn.prepareStatement(SELECT_REINFORCEMENT); + setBlockParameters(tmp, block, 1); + tmp.execute(); + + ResultSet result = tmp.getResultSet(); + if (result.next()) { + durability = result.getInt(1); + } + + result.close(); + } + tmp.close(); + + return durability; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void removeReinforcement(Block block) { + try { + PreparedStatement tmp = conn.prepareStatement(DELETE_REINFORCEMENT); + setBlockParameters(tmp, block, 1); + tmp.execute(); + tmp.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void addRegisteredGroup(Block block, String group) { + try { + PreparedStatement tmp = this.conn.prepareStatement(INSERT_REGISTRY); + setBlockParameters(tmp, block, 1); + tmp.setString(5, group); + tmp.execute(); + tmp.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public String getRegisteredGroup(Block block) { + try { + String group = null; + PreparedStatement tmp = conn.prepareStatement(SELECT_REGISTRY); + setBlockParameters(tmp, block, 1); + tmp.execute(); + ResultSet result = tmp.getResultSet(); + if (result.next()) { + group = result.getString(1); + } + + result.close(); + tmp.close(); + + return group; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + private void setBlockParameters(PreparedStatement tmp, Block block, int index) { + try { + tmp.setInt(index++, block.getX()); + tmp.setInt(index++, block.getY()); + tmp.setInt(index++, block.getZ()); + tmp.setString(index, block.getWorld().getName()); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void close() { + try { + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } } }