Merge pull request #26 from JonnyD/master

enhanced solution to dynamite bug. trying to get it working with DAO.
This commit is contained in:
Justin
2012-03-21 03:58:01 -07:00
3 changed files with 101 additions and 49 deletions

View File

@@ -19,9 +19,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Door;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;
@@ -104,10 +102,6 @@ public class BlockListener implements Listener {
@EventHandler
public void checkForExplosion(EntityExplodeEvent event){
if(event.isCancelled()){
return;
}
//Store blocks that are inside explosion's blast radius
List<Block> blastRadiusBlocks = event.blockList();
@@ -117,6 +111,7 @@ public class BlockListener implements Listener {
return;
}
List<Coordinate> reinforcedBlocksCoords = new ArrayList<Coordinate>();
HashMap<Coordinate, Block> affectedBlocks = new HashMap<Coordinate, Block>();
//Initialize min & max X,Y,Z coordinates
@@ -170,44 +165,41 @@ public class BlockListener implements Listener {
//Query database for any reinforced blocks that may be in the blast radius
//Reinforced blocks should have a durability > 0 (aka >= 1)
ResultSet result = dao.selectReinforcements(worldName, smallestX, largestX, smallestY, largestY, smallestZ, largestZ);
reinforcedBlocksCoords = dao.selectReinforcements(worldName, smallestX, largestX, smallestY, largestY, smallestZ, largestZ);
if(reinforcedBlocksCoords.size() < 1){
return;
}
//If there was some found, loop through each one
try {
while(result.next()){
//Get X,Y,Z coords of reinforced block
int x = result.getInt(1);
int y = result.getInt(2);
int z = result.getInt(3);
//Pass in x, y, z of reinforced block into affectedBlocks HashMap to instantiate a Block
Block protectedBlock = affectedBlocks.get(new Coordinate(world, x, y, z));
//Then remove the protectedBlock from explosion list
event.blockList().remove(protectedBlock);
}
} catch (SQLException e) {
System.err.println("Citadel - explosion error");
for(int i = 0; i < reinforcedBlocksCoords.size(); i++){
Coordinate rbCoords = reinforcedBlocksCoords.get(i);
Block reinforcedBlock = affectedBlocks.get(rbCoords);
//Then remove it from explosion
event.blockList().remove(reinforcedBlock);
}
//Update reinforcements to set durability of blocks that are within blast radius
//Explosion should decrement durability by 1
dao.updateReinforcements(worldName, smallestX, largestX, smallestY, largestY, smallestZ, largestZ);
}
@EventHandler
public void controlAccess(PlayerInteractEvent pie) {
if (!pie.hasBlock()) return;
public void onPlayerInteract(PlayerInteractEvent event) {
if (!event.hasBlock()) return;
if(event.isCancelled()) return;
Block block = pie.getClickedBlock();
Block block = event.getClickedBlock();
Material matl = block.getType();
Player p = pie.getPlayer();
Player p = event.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);
event.setCancelled(true);
}
}
}

View File

@@ -9,6 +9,9 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;
@@ -19,6 +22,7 @@ public class Citadel extends JavaPlugin {
public HashMap<Player, Integer> playerPlacementState;
public CitadelDao dao;
public Logger log;
public Connection connection;
public void onEnable() {
log = this.getLogger();
@@ -46,14 +50,41 @@ public class Citadel extends JavaPlugin {
materialRequirements.put(Material.getMaterial(keys[i]), requirements[i]);
}
try {
dao = new CitadelDao();
try
{
Class.forName("org.sqlite.JDBC");
this.connection = DriverManager.getConnection("jdbc:sqlite:plugins/Citadel/Citadel.db");
this.connection.createStatement().execute(
"CREATE TABLE IF NOT EXISTS REINFORCEMENTS (x INTEGER,y INTEGER,z INTEGER,world TEXT,durability INTEGER);");
this.connection.createStatement().execute(
"CREATE TABLE IF NOT EXISTS GROUPS (grpName TEXT,member TEXT);");
this.connection.createStatement().execute(
"CREATE TABLE IF NOT EXISTS REGISTRY (x INTEGER,y INTEGER,z INTEGER,world TEXT,grp TEXT);");
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());
}
Bukkit.getServer().getPluginManager().registerEvents(new BlockListener(this), this);
log.info("Citadel - Hi folks, Citadel is now on :D.");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public void onDisable() {

View File

@@ -1,11 +1,16 @@
package com.untamedears.Citadel.dao;
import com.untamedears.Citadel.Citadel;
import com.untamedears.Citadel.Coordinate;
import com.untamedears.Citadel.DelayedReinforcement;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
@@ -34,6 +39,7 @@ public class CitadelDao {
private static final String SELECT_REGISTRY = "SELECT grp FROM REGISTRY WHERE x=? AND y=? AND z=? AND world=?";
private Connection conn;
private JavaPlugin myPlugin;
public CitadelDao() throws ClassNotFoundException {
Class.forName("org.sqlite.JDBC");
@@ -142,18 +148,38 @@ public class CitadelDao {
}
}
public ResultSet selectReinforcements(String worldName, Integer smallestX, Integer largestX,
public List<Coordinate> selectReinforcements(String worldName, Integer smallestX, Integer largestX,
Integer smallestY, Integer largestY, Integer smallestZ, Integer largestZ){
List<Coordinate> reinforcedBlocks = new ArrayList<Coordinate>();
World world = myPlugin.getServer().getWorld(worldName);
try {
PreparedStatement tmp = conn.prepareStatement(SELECT_REINFORCEMENTS);
tmp.setString(1, worldName);
tmp.setInt(2, largestX);
tmp.setInt(3, smallestX);
tmp.setInt(4, largestY);
tmp.setInt(5, smallestY);
tmp.setInt(1, largestX);
tmp.setInt(2, smallestX);
tmp.setInt(3, largestY);
tmp.setInt(4, smallestY);
tmp.setInt(5, largestZ);
tmp.setInt(6, smallestZ);
tmp.setString(7, worldName);
tmp.execute();
ResultSet result = tmp.getResultSet();
return result;
while(result.next()){
//Get X,Y,Z coords of reinforced block
int x = result.getInt(1);
int y = result.getInt(2);
int z = result.getInt(3);
//Pass in x, y, z of reinforced block into affectedBlocks HashMap to instantiate a Block
reinforcedBlocks.add(new Coordinate(world, x, y, z));
}
tmp.close();
result.close();
return reinforcedBlocks;
} catch (SQLException e){
throw new RuntimeException(e);
}
@@ -163,13 +189,16 @@ public class CitadelDao {
Integer smallestY, Integer largestY, Integer smallestZ, Integer largestZ){
try {
PreparedStatement tmp = conn.prepareStatement(UPDATE_REINFORCEMENTS);
tmp.setString(1, worldName);
tmp.setInt(2, largestX);
tmp.setInt(3, smallestX);
tmp.setInt(4, largestY);
tmp.setInt(5, smallestY);
tmp.setInt(1, largestX);
tmp.setInt(2, smallestX);
tmp.setInt(3, largestY);
tmp.setInt(4, smallestY);
tmp.setInt(5, largestZ);
tmp.setInt(6, smallestZ);
tmp.setString(7, worldName);
tmp.execute();
} catch (SQLException e){
tmp.close();
} catch (SQLException e){
throw new RuntimeException(e);
}
}