Permalink
Browse files

Cleaned up some generic-related warnings.

Moved all database functions to a separate class.
  • Loading branch information...
1 parent fcfdd96 commit 59a19e1014d6d75bbe0ffef4085a7a499a790154 @chrisrico chrisrico committed Mar 19, 2012
@@ -1,5 +1,6 @@
package com.untamedears.Citadel;
+import com.untamedears.Citadel.dao.CitadelDao;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
@@ -16,9 +17,6 @@
import org.bukkit.material.Door;
import org.bukkit.plugin.java.JavaPlugin;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
@@ -29,57 +27,47 @@
private HashMap<Integer, Player> taskInitiator;
public static HashMap<Player, Material> playerReenforcers;
private JavaPlugin myPlugin;
- private Connection conn;
+ private CitadelDao dao;
public BlockListener(JavaPlugin jp)throws SQLException, ClassNotFoundException
{
- this.delayedReenforcements = new HashMap();
- playerReenforcers = new HashMap();
- this.taskMaterial = new HashMap();
- this.taskInitiator = new HashMap();
+ this.delayedReenforcements = new HashMap<Block, Integer>();
+ playerReenforcers = new HashMap<Player, Material>();
+ this.taskMaterial = new HashMap<Integer, Material>();
+ this.taskInitiator = new HashMap<Integer, Player>();
this.myPlugin = jp;
- this.conn = ((Citadel)this.myPlugin).connection;
+ this.dao = ((Citadel)this.myPlugin).dao;
}
@EventHandler
public void applyReenforcement(BlockPlaceEvent bpe)
{
Player placer = bpe.getPlayer();
Block block = bpe.getBlock();
- Material matl = (Material)playerReenforcers.get(placer);
- Integer pid = null;
+ 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, ((Integer)Citadel.materialRequirements.get(matl)).intValue()))
+ if (placer.getInventory().contains(matl, Citadel.materialRequirements.get(matl)))
{
try
{
- PreparedStatement tmp = this.conn.prepareStatement(
- "INSERT INTO REENFORCEMENTS (x,y,z,world,durability) values (?,?,?,?,?)");
- tmp.setInt(1, block.getX());
- tmp.setInt(2, block.getY());
- tmp.setInt(3, block.getZ());
- tmp.setString(4, block.getWorld().getName());
- tmp.setInt(5, ((Integer)Citadel.materialStrengths.get(matl)).intValue());
-
- pid = Integer.valueOf(this.myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(
- this.myPlugin,
- new DelayedReenforcement(tmp),
- 20L));
+ int pid = this.myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(
+ this.myPlugin,
+ dao.addReinforcement(block, matl),
+ 20L);
this.delayedReenforcements.put(block, pid);
- placer.getInventory().removeItem(new ItemStack[] { new ItemStack(matl, ((Integer)Citadel.materialRequirements.get(matl)).intValue()) });
+ 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 reenforcement:\n" + e);
- return;
}
- }
+ }
else
{
placer.sendMessage(ChatColor.YELLOW + "You require more " + matl + " to continue reenforcements.");
@@ -98,70 +86,34 @@ public void gracefullyRemoveReenforcementModeOnLogout(PlayerQuitEvent pqe)
@EventHandler
public void checkDurabilityAndDelayedEventCheck(BlockBreakEvent bbe) {
- Block tmp = bbe.getBlock();
-
- if (this.delayedReenforcements.containsKey(tmp))
- {
- Integer pid = (Integer)this.delayedReenforcements.get(tmp);
- Material matl = (Material)this.taskMaterial.get(pid);
- this.myPlugin.getServer().getScheduler().cancelTask(((Integer)this.delayedReenforcements.get(tmp)).intValue());
- ((Player)this.taskInitiator.get(pid)).getInventory().addItem(new ItemStack[] { new ItemStack(matl, ((Integer)Citadel.materialRequirements.get(matl)).intValue()) });
-
- this.delayedReenforcements.remove(tmp);
- this.taskInitiator.remove(pid);
- this.taskMaterial.remove(pid);
- }
-
- try
- {
- PreparedStatement ask = this.conn.prepareStatement(
- "SELECT DURABILITY FROM REENFORCEMENTS WHERE x=? AND y=? AND z=? AND world=?");
- ask.setInt(1, tmp.getX());
- ask.setInt(2, tmp.getY());
- ask.setInt(3, tmp.getZ());
- ask.setString(4, tmp.getWorld().getName());
- ask.execute();
- ResultSet answer = ask.getResultSet();
- if (!answer.next())
- {
- answer.close();
- ask.close();
- return;
- }
- int durability = answer.getInt(1);
- durability--;
- ask.close();
- answer.close();
- if (durability <= 0)
- {
- PreparedStatement delete = this.conn.prepareStatement(
- "DELETE FROM REENFORCEMENTS WHERE x=? AND y=? AND z=? AND world=?");
- delete.setInt(1, tmp.getX());
- delete.setInt(2, tmp.getY());
- delete.setInt(3, tmp.getZ());
- delete.setString(4, tmp.getWorld().getName());
- delete.execute();
- delete.close();
- }
- else
- {
- bbe.setCancelled(true);
- PreparedStatement update = this.conn.prepareStatement(
- "UPDATE REENFORCEMENTS SET DURABILITY=? WHERE x=? AND y=? AND z=? AND world=?");
- update.setInt(1, durability);
- update.setInt(2, tmp.getX());
- update.setInt(3, tmp.getY());
- update.setInt(4, tmp.getZ());
- update.setString(5, tmp.getWorld().getName());
- update.execute();
- update.close();
- }
- }
- catch (SQLException e)
- {
- System.err.println("Citadel - error accessing database:\n" + e);
- }
- }
+ Block block = bbe.getBlock();
+ try {
+ if (this.delayedReenforcements.containsKey(block))
+ {
+ Integer pid = this.delayedReenforcements.get(block);
+ Material matl = this.taskMaterial.get(pid);
+ this.myPlugin.getServer().getScheduler().cancelTask(this.delayedReenforcements.get(block));
+ this.taskInitiator.get(pid).getInventory().addItem(new ItemStack(matl, Citadel.materialRequirements.get(matl)));
+
+ this.delayedReenforcements.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);
+ }
+ } catch (SQLException e) {
+ System.err.println("Citadel - error accessing database:\n" + e);
+ }
+ }
@EventHandler
public void controlAccess(PlayerInteractEvent pie) {
@@ -174,52 +126,12 @@ public void controlAccess(PlayerInteractEvent pie) {
try
{
if ((matl == Material.CHEST) || (matl == Material.WOODEN_DOOR) || (matl == Material.IRON_DOOR)) {
- PreparedStatement tmp = this.conn.prepareStatement(
- "SELECT grp FROM REGISTRY WHERE x=? AND y=? AND z=? AND world=?");
- tmp.setInt(1, block.getX());
- tmp.setInt(2, block.getY());
- tmp.setInt(3, block.getZ());
- tmp.setString(4, block.getWorld().getName());
- tmp.execute();
- ResultSet answer = tmp.getResultSet();
-
- if (!answer.next()) {
- answer.close();
- tmp.close();
- return;
- }
- do
- {
- PreparedStatement queryGroup = this.conn.prepareStatement(
- "SELECT grpName FROM GROUPS WHERE grpName=? AND member=?");
- queryGroup.setString(1, answer.getString(1));
- queryGroup.setString(2, p.getPlayerListName());
- queryGroup.execute();
- ResultSet tmpRs = queryGroup.getResultSet();
- if (tmpRs.next())
- {
- tmpRs.close();
- queryGroup.close();
- tmp.close();
- return;
- }
- tmpRs.close();
- queryGroup.close();
- }
- while (answer.next());
- tmp.close();
-
- p.sendMessage(ChatColor.RED + "That door/chest is locked.");
- /*
- p.sendMessage(ChatColor.RED + "That door/chest is locked. O NOZ!!!");
- p.sendMessage(ChatColor.MAGIC + "Hey Beavis, that was, like, cool.");
- p.sendMessage(ChatColor.YELLOW + "Huh huh.");
- p.sendMessage(ChatColor.GREEN + "Uh, huh huh. Oh yeah. Money is cool!");
- p.sendMessage(ChatColor.BLUE + "YEAH! It, like, uh, huh huh. Uh...");
- p.sendMessage(ChatColor.LIGHT_PURPLE + "Huh huh huh. Uh, hmmmm, heh, huh, hmmm huh huh. Yeah.");
- */
- pie.setCancelled(true);
- }
+ 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)
{
@@ -234,21 +146,10 @@ public void controlRedstone(BlockRedstoneEvent bre)
if ((block instanceof Door))
try
{
- PreparedStatement tmp = this.conn.prepareStatement(
- "SELECT group FROM REGISTRY WHERE x=? AND y=? AND z=? AND world=?");
- tmp.setInt(1, block.getX());
- tmp.setInt(2, block.getY());
- tmp.setInt(3, block.getZ());
- tmp.setString(4, block.getWorld().getName());
- tmp.execute();
- ResultSet answer = tmp.getResultSet();
-
- if (answer.next())
- {
- bre.setNewCurrent(bre.getOldCurrent());
- }
- answer.close();
- tmp.close();
+ String group = dao.getRegisteredGroup(block);
+ if (group != null) {
+ bre.setNewCurrent(bre.getOldCurrent());
+ }
}
catch (SQLException e)
{
@@ -259,23 +160,13 @@ public void controlRedstone(BlockRedstoneEvent bre)
@EventHandler
public void makePrivateGroup(PlayerLoginEvent ple)
{
- ((Citadel)this.myPlugin).playerPlacementState.put(ple.getPlayer(), Integer.valueOf(1));
+ ((Citadel)this.myPlugin).playerPlacementState.put(ple.getPlayer(), 1);
try
{
- PreparedStatement tmp = this.conn.prepareStatement("SELECT grpName FROM GROUPS WHERE grpName = ?");
- tmp.setString(1, ple.getPlayer().getDisplayName());
- tmp.execute();
- ResultSet rs = tmp.getResultSet();
- if (!rs.next())
- {
- tmp = this.conn.prepareStatement("INSERT INTO GROUPS (grpName,member) values (?,?)");
- tmp.setString(1, ple.getPlayer().getDisplayName());
- tmp.setString(2, ple.getPlayer().getDisplayName());
- tmp.execute();
- tmp.close();
- }
- rs.close();
- tmp.close();
+ String playerName = ple.getPlayer().getDisplayName();
+ if (!dao.isPlayerInGroup(playerName, playerName)) {
+ dao.addPlayerToGroup(playerName, playerName);
+ }
}
catch (SQLException e)
{
@@ -294,31 +185,18 @@ public void secureDoorOrChest(BlockPlaceEvent bpe)
return;
}
- Integer playerState = (Integer)((Citadel)this.myPlugin).playerPlacementState.get(bpe.getPlayer());
+ Integer playerState = ((Citadel)this.myPlugin).playerPlacementState.get(bpe.getPlayer());
if (playerState == null)
{
- ((Citadel)this.myPlugin).playerPlacementState.put(bpe.getPlayer(), Integer.valueOf(1));
- playerState = Integer.valueOf(1);
+ ((Citadel)this.myPlugin).playerPlacementState.put(bpe.getPlayer(), 0);
+ playerState = 0;
}
- if (playerState.intValue() > 0)
+ if (playerState > 0)
{
try
{
- PreparedStatement tmp = this.conn.prepareStatement(
- "INSERT INTO REGISTRY (x,y,z,world,grp) VALUES (?,?,?,?,?)");
- tmp.setInt(1, blk.getX());
- tmp.setInt(2, blk.getY());
- tmp.setInt(3, blk.getZ());
- tmp.setString(4, blk.getWorld().getName());
- tmp.setString(5, bpe.getPlayer().getDisplayName());
- tmp.execute();
- if (playerState.intValue() > 1)
- {
- tmp.setString(5, bpe.getPlayer().getDisplayName() + "Grp");
- tmp.execute();
- }
- tmp.close();
+ dao.addRegisteredGroup(blk, bpe.getPlayer().getDisplayName());
}
catch (SQLException e)
{
@@ -331,7 +209,7 @@ public void close()
{
try
{
- this.conn.close();
+ dao.close();
}
catch (SQLException e)
{
Oops, something went wrong. Retry.

0 comments on commit 59a19e1

Please sign in to comment.