Skip to content

Commit

Permalink
Implement markers
Browse files Browse the repository at this point in the history
- Implement Markers
- Update bukkit api jar
  • Loading branch information
borzilleri committed Apr 3, 2011
1 parent 94e4b52 commit 8d44009
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 20 deletions.
Binary file modified lib/bukkit-0.0.1-SNAPSHOT.jar
Binary file not shown.
8 changes: 8 additions & 0 deletions src/com/asylumsw/bukkit/waypoints/Gates.java
Expand Up @@ -184,6 +184,10 @@ public static boolean activateGate(Player player, String gateName) {
player.sendMessage(ChatColor.RED + "Error: Gate name 'home' is reserved.");
return false;
}
if (gateName.equalsIgnoreCase("reset")) {
player.sendMessage(ChatColor.RED + "Error: Gate name 'reset' is reserved.");
return false;
}

Location loc;
Warp gate = gateList.containsKey(gateName) ? gateList.get(gateName) : null;
Expand Down Expand Up @@ -287,4 +291,8 @@ public boolean doBlockAction(Material structureMaterial,
player.sendMessage(ChatColor.GOLD + "*** Deactivating Home Point ***");
return true;
}

public static boolean gateExists(String name) {
return gateList.containsKey(name);
}
}
32 changes: 16 additions & 16 deletions src/com/asylumsw/bukkit/waypoints/Homes.java
Expand Up @@ -12,33 +12,33 @@
*/
public class Homes {
public final static int WARP_DELAY = 15;
private static HashMap<String,Warp> homeList;
protected static int[] homePointStart = new int[] {0,1,1};
protected static Material[][][] homePointPattern = new Material[][][] {
private static HashMap<String,Warp> markerList;
protected static int[] markerPointStart = new int[] {0,1,1};
protected static Material[][][] markerPointPattern = new Material[][][] {
{
{Material.GLASS, Material.GLASS, Material.GLASS},
{Material.GLASS, Material.IRON_BLOCK, Material.GLASS},
{Material.GLASS, Material.GLASS, Material.GLASS},
}
};

public static void loadHomes() {
public static void loadMarkers() {
HomeData.initTable();
homeList = HomeData.getHomes();
markerList = HomeData.getHomes();
}

public static void sendPlayerHome(Player player, boolean debug, boolean override) {
// If the player does not have a home point, send them to the spawn.
if( !homeList.containsKey(player.getName()) ) {
if( !markerList.containsKey(player.getName()) ) {
player.sendMessage(ChatColor.GOLD + "*** Returning to Spawn Point ***");
Waypoints.warpPlayerTo(player, player.getWorld().getSpawnLocation(), WARP_DELAY);
return;
}

Warp home = homeList.get(player.getName());
Warp home = markerList.get(player.getName());

Structure.Validator validator = new Structure.Validator();
Structure.parse(homePointPattern, homePointStart, home.getLocation(), validator, debug);
Structure.parse(markerPointPattern, markerPointStart, home.getLocation(), validator, debug);

if( 0 < validator.invalidBlockCount ) {
player.sendMessage(ChatColor.RED+"ERROR: Home point is invalid.");
Expand All @@ -56,18 +56,18 @@ public static void sendPlayerHome(Player player, boolean debug, boolean override
}

public static boolean setHomePoint(Player player) {
if( homeList.containsKey(player.getName()) ) {
if( markerList.containsKey(player.getName()) ) {
player.sendMessage(ChatColor.RED+"Error: Home Point already active.");
return false;
}

Structure.Validator validator = new Structure.Validator();
Structure.parse(homePointPattern, homePointStart, player.getLocation(), validator,true);
Structure.parse(markerPointPattern, markerPointStart, player.getLocation(), validator,true);

if( 0 >= validator.invalidBlockCount ) {
Warp home = new Warp(player.getName(), player, Waypoint.HOME);
if( HomeData.addHome(home) ) {
homeList.put(player.getName(), home);
markerList.put(player.getName(), home);
player.sendMessage(ChatColor.GOLD+"*** Activating Home Point ***");
return true;
}
Expand All @@ -83,16 +83,16 @@ public static boolean setHomePoint(Player player) {
}

public static boolean unsetHomePoint(Player player) {
if( !homeList.containsKey(player.getName()) ) {
if( !markerList.containsKey(player.getName()) ) {
// Player has no active home point.
player.sendMessage(ChatColor.RED+"ERROR: No active home point.");
return false;
}

Warp home = homeList.get(player.getName());
Warp home = markerList.get(player.getName());

Structure.Validator validator = new Structure.Validator();
Structure.parse(homePointPattern, homePointStart, home.getLocation(), validator);
Structure.parse(markerPointPattern, markerPointStart, home.getLocation(), validator);

if( 0 < validator.invalidBlockCount ) {
// Player's home point is invalid
Expand Down Expand Up @@ -121,10 +121,10 @@ public boolean doBlockAction(Material structureBlockType,
return doBlockAction(structureBlockType, worldBlock);
}
};
Structure.parse(homePointPattern, homePointStart, homeList.get(player.getName()).getLocation(), remover);
Structure.parse(markerPointPattern, markerPointStart, markerList.get(player.getName()).getLocation(), remover);

// Remove home point from the list, and save the list.
homeList.remove(player.getName());
markerList.remove(player.getName());

player.sendMessage(ChatColor.GOLD+"*** Deactivating Home Point ***");
return true;
Expand Down
227 changes: 227 additions & 0 deletions src/com/asylumsw/bukkit/waypoints/MarkerData.java
@@ -0,0 +1,227 @@
package com.asylumsw.bukkit.waypoints;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author jonathan
*/
public class MarkerData {

public final static String MARKER_TABLE = "CREATE TABLE `markerList` ("
+ "`id` INTEGER PRIMARY KEY,"
+ "`player` varchar(255) NOT NULL,"
+ "`name` varchar(255) NOT NULL UNIQUE"
+ "`x` int NOT NULL, `y` int NOT NULL, `z` int NOT NULL,"
+ "`world` varchar(255) NOT NULL )";

public static void initTable() {
if (!tableExists()) {
createTable();
}
}

public static HashMap<String, Warp> getMarkers() {
HashMap<String, Warp> markerList = new HashMap<String, Warp>();
Connection conn = null;
Statement statement = null;
ResultSet set = null;
Logger log = Logger.getLogger("Minecraft");

try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);

statement = conn.createStatement();
set = statement.executeQuery("SELECT player,x,y,z,world FROM markerList");
while (set.next()) {
String name = set.getString("name");
Warp mark = new Warp(name, set.getInt("x"), set.getInt("y"),
set.getInt("z"), 0, 0, set.getString("world"), Waypoint.MARKER);
mark.setOwner(set.getString("player"));
markerList.put(name, mark);
}
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[MYWARP]: Warp Load Exception");
}
catch (ClassNotFoundException e) {
log.log(Level.SEVERE, "[MYWARP]: Error loading org.sqlite.JDBC");
}
finally {
try {
if (statement != null) {
statement.close();
}
if (set != null) {
set.close();
}
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[MYWARP]: Warp Load Exception (on close)");
}
}
return markerList;
}

private static boolean tableExists() {
Connection conn = null;
ResultSet rs = null;

try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
DatabaseMetaData dbm = conn.getMetaData();
rs = dbm.getTables(null, null, "markerList", null);
return (!rs.next() ? false : true);
}
catch (SQLException ex) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Table Check Exception");
return false;
}
catch (ClassNotFoundException ex2) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Class Not Found Exception");
return false;
}
finally {
try {
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Table Check Exception (on closing)");
}
}
}

private static void createTable() {
Connection conn = null;
Statement st = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
st = conn.createStatement();
st.executeUpdate(MARKER_TABLE);
}
catch (SQLException e) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Create Table Exception", e);
}
catch (ClassNotFoundException e) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Error loading org.sqlite.JDBC");
}
finally {
try {
if (conn != null) {
conn.close();
}
if (st != null) {
st.close();
}
}
catch (SQLException e) {
Logger log = Logger.getLogger("Minecraft");
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Could not create the table (on close)");
}
}
}

public static boolean addMarker(Warp loc) {
Connection conn = null;
PreparedStatement ps = null;
Logger log = Logger.getLogger("Minecraft");
boolean success = false;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
ps = conn.prepareStatement("INSERT INTO markerList (player, name, x, y, z, world) "
+ "VALUES (?,?,?,?,?,?)");
ps.setString(1, loc.getOwnerName());
ps.setString(2, loc.getName());
ps.setInt(3, loc.getX());
ps.setInt(4, loc.getY());
ps.setInt(5, loc.getZ());
ps.setString(6, loc.getWorldName());

ps.executeUpdate();
success = true;
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Warp Insert Exception", ex);
}
catch (ClassNotFoundException ex2) {
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Error loading org.sqlite.JDBC");
}
finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Warp Insert Exception (on close)", ex);

}
}

return success;
}

public static boolean deleteMarker(String markerName) {
Connection conn = null;
PreparedStatement ps = null;
Logger log = Logger.getLogger("Minecraft");
boolean success = false;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection(Waypoints.DATABASE);
ps = conn.prepareStatement("DELETE FROM markerList WHERE name = ?");
ps.setString(1, markerName);
ps.executeUpdate();
success = true;
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Warp Insert Exception", ex);
}
catch (ClassNotFoundException ex2) {
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Error loading org.sqlite.JDBC");
}
finally {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
log.log(Level.SEVERE, "[WAYPOINTS:MARKERS]: Warp Insert Exception (on close)", ex);
}
}
return success;
}
}

0 comments on commit 8d44009

Please sign in to comment.