Commits

Anonymous committed 4ac81a8

- command calling system
- waypoint command + full command set/persistence
- help screen

  • Participants
  • Parent commits 4bbf0c3

Comments (0)

Files changed (10)

+syntax:regexp
+^target/
+^\.project
+^\.settings
+^\.classpath
+
+

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/Bootstrap.java

         if (Database.isNew()) {
             Database.Handle handle = Database.getHandle();
             try {
-                handle.execStatement("CREATE TABLE waypoints (" +
+                handle.execNonParameterizedStatement("CREATE TABLE waypoint (" +
                         "id INTEGER PRIMARY KEY, " +
                         "player_name VARCHAR, " +
                         "waypoint_name VARCHAR, "+

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/ZeekTools.java

 package com.zzzcomputing.bukkit.ZeekTools;
 
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.entity.Player;
 import org.bukkit.plugin.java.JavaPlugin;
 
+import com.zzzcomputing.bukkit.ZeekTools.commands.GiveCommand;
+import com.zzzcomputing.bukkit.ZeekTools.commands.GotoCommand;
+import com.zzzcomputing.bukkit.ZeekTools.commands.HelpCommand;
+import com.zzzcomputing.bukkit.ZeekTools.commands.UnknownCommand;
+import com.zzzcomputing.bukkit.ZeekTools.commands.WaypointCommand;
 import com.zzzcomputing.bukkit.ZeekTools.util.Logger;
 
 
         }
     }
 
+    @Override
+    public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
+        try {
+            String commandName = command.getName().toLowerCase();
+            if (sender instanceof Player) {
+                if (commandName.equals("zt")) {
+                    return processCommand((Player) sender, args);
+                }
+            }
+            
+            return false;
+        } catch (Throwable ex) {
+            ex.printStackTrace();
+            return true;
+        }
+    }
+
+    private boolean processCommand(Player player, String [] args) {
+        String cmd = args[0];
+        if ("give".equals(cmd)) {
+            new GiveCommand(player).execute(args);
+        }
+        else if ("goto".equals(cmd)) {
+            new GotoCommand(player).execute(args);
+        }
+        else if ("list".equals(cmd) || "set".equals(cmd) || "del".equals(cmd)) {
+            new WaypointCommand(player).execute(args);
+        }
+        else if ("help".equals(cmd)) {
+            new HelpCommand(player).execute(args);
+        }
+        else {
+            new UnknownCommand(player).execute(args);
+        }
+        return true;
+    }
 }

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/commands/Command.java

+package com.zzzcomputing.bukkit.ZeekTools.commands;
+
+import org.bukkit.ChatColor;
+import org.bukkit.entity.Player;
+
+public abstract class Command {
+    protected Player player;
+    
+    public Command(Player player) {
+        this.player = player;
+    }
+
+    public abstract void execute(String [] args);
+    
+    protected void sendPlayerMessage(Object ... tokens) {
+        String msg = ChatColor.DARK_PURPLE + "[ZT] " + ChatColor.AQUA;
+        for (Object token: tokens) {
+            msg = msg.concat(token.toString());
+        }
+        player.sendMessage(msg);
+    }
+    
+    protected void sendError(String message) {
+        sendPlayerMessage(ChatColor.RED, message);
+    }
+}

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/commands/GiveCommand.java

+package com.zzzcomputing.bukkit.ZeekTools.commands;
+
+import org.bukkit.ChatColor;
+import org.bukkit.entity.Player;
+
+public class GiveCommand extends Command {
+
+    public GiveCommand(Player player) {
+        super(player);
+        // TODO Auto-generated constructor stub
+    }
+
+    @Override
+    public void execute(String[] args) {
+        sendPlayerMessage(ChatColor.DARK_PURPLE,  "Give command");
+
+    }
+
+}

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/commands/GotoCommand.java

+package com.zzzcomputing.bukkit.ZeekTools.commands;
+
+import org.bukkit.ChatColor;
+import org.bukkit.entity.Player;
+
+public class GotoCommand extends Command {
+
+    public GotoCommand(Player player) {
+        super(player);
+    }
+
+    @Override
+    public void execute(String[] args) {
+        sendPlayerMessage(ChatColor.DARK_PURPLE, "Goto command");
+
+    }
+
+}

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/commands/HelpCommand.java

+package com.zzzcomputing.bukkit.ZeekTools.commands;
+
+import org.bukkit.ChatColor;
+import org.bukkit.entity.Player;
+
+public class HelpCommand extends Command {
+
+    public HelpCommand(Player player) {
+        super(player);
+    }
+    @Override
+    public void execute(String[] args) {
+        sendPlayerMessage(ChatColor.WHITE, "/zt give <name_or_id> [num] [player] - Give item.");
+        sendPlayerMessage(ChatColor.WHITE, "/zt goto <x> <y> <z> - goto coordinates");
+        sendPlayerMessage(ChatColor.WHITE, "/zt goto <waypoint> - goto saved waypoint");
+        sendPlayerMessage(ChatColor.WHITE, "/zt goto spawn - goto spawn point/bed");
+        sendPlayerMessage(ChatColor.WHITE, "/zt list - list waypoints");
+        sendPlayerMessage(ChatColor.WHITE, "/zt set <waypoint> - set waypoint");
+        sendPlayerMessage(ChatColor.WHITE, "/zt del <waypoint> - delete waypoint");
+    }
+}

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/commands/UnknownCommand.java

+package com.zzzcomputing.bukkit.ZeekTools.commands;
+
+import org.bukkit.ChatColor;
+import org.bukkit.entity.Player;
+
+public class UnknownCommand extends Command {
+    public UnknownCommand(Player player) {
+        super(player);
+    }
+
+    @Override
+    public void execute(String[] args) {
+        sendError("No such command: " + args[0]);
+    }
+
+}

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/commands/WaypointCommand.java

+package com.zzzcomputing.bukkit.ZeekTools.commands;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.bukkit.ChatColor;
+import org.bukkit.entity.Player;
+
+import com.zzzcomputing.bukkit.ZeekTools.util.Database;
+import com.zzzcomputing.bukkit.ZeekTools.util.Database.Handle;
+import com.zzzcomputing.bukkit.ZeekTools.util.Database.ProtectedOperation;
+import com.zzzcomputing.bukkit.ZeekTools.util.Logger;
+
+public class WaypointCommand extends Command {
+
+    public WaypointCommand(Player player) {
+        super(player);
+    }
+
+    @Override
+    public void execute(String[] args) {
+        String subcommand = args[0];
+        if ("set".equals(subcommand)) {
+            setWaypoint(args);
+        }
+        else if ("del".equals(subcommand)) {
+            deleteWaypoint(args);
+        }
+        else if ("list".equals(subcommand)) {
+            listWaypoints(args);
+        }
+        else {
+            new UnknownCommand(player).execute(args);
+        }
+    }
+    
+    private void listWaypoints(String [] args) {
+        if (args.length != 1) {
+            sendError("list command takes no arguments");
+        }
+        else {
+            Database.Handle handle = Database.getHandle();
+            try {
+                for (Waypoint wp : Waypoint.findByPlayer(handle, player)) {
+                    sendPlayerMessage(wp.getDescription());
+                }
+            }
+            finally {
+                handle.close();
+            }
+            
+        }
+    }
+
+    private void deleteWaypoint(String [] args) {
+        if (args.length != 2) {
+            sendError("set command requires one argument");
+        }
+        else {
+            Database.Handle handle = Database.getHandle();
+            try {
+                Waypoint wp = Waypoint.findByName(handle, player, args[1]);
+                if (wp == null) {
+                    sendError(String.format("Waypoint '%s' does not exist", args[1]));
+                }
+                else {
+                    wp.delete(handle);
+                    sendPlayerMessage(String.format("Waypoint %s deleted", wp.getDescription()));
+                }
+            }
+            finally {
+                handle.close();
+            }
+        }
+    }
+    private void setWaypoint(String [] args) {
+        if (args.length != 2) {
+            sendError("set command requires one argument");
+        }
+        else {
+            Database.Handle handle = Database.getHandle();
+            try {
+                Waypoint wp = Waypoint.findByName(handle, player, args[1]);
+                if (wp != null) {
+                    sendError(String.format("Waypoint '%s' already exists", args[1]));
+                }
+                else {
+                    wp = new Waypoint(player, args[1]);
+                    wp.persist(handle);
+                    sendPlayerMessage(String.format("Waypoint %s saved", wp.getDescription()));
+                }
+            }
+            finally {
+                handle.close();
+            }
+            
+        }
+    }
+    
+    static class Waypoint {
+        String name;
+        Player player;
+        double x;
+        double y;
+        double z;
+        String world_uid;
+        int id;
+        
+        public Waypoint(Player player, String name) {
+            this(player, name, 
+                       player.getLocation().getX(), 
+                       player.getLocation().getY(), 
+                       player.getLocation().getZ(), 
+                       player.getWorld().getUID().toString(), 
+                       -1);
+        }
+        public Waypoint(Player player, String name, double x, double y, double z, String world_uid) {
+            this(player, name, x, y, z, world_uid, -1);
+        }
+        public Waypoint(Player player, String name, double x, double y, double z, String world_uid, int id) {
+            this.player= player;
+            this.name = name;
+            this.x = x;
+            this.y = y;
+            this.z = z;
+            this.world_uid = world_uid;
+            this.id = id;
+        }
+        public Waypoint(Player player, ResultSet result) throws SQLException {
+            this(
+                    player, 
+                    result.getString(2), 
+                    result.getFloat(3),
+                    result.getFloat(4),
+                    result.getFloat(5),
+                    result.getString(6),
+                    result.getInt(1));
+
+        }
+        public String getDescription() {
+            return String.format("%s (%.2f, %.2f, %.2f)", name, x, y, z);
+        }
+        public void delete(Database.Handle handle) {
+            handle.invoke(
+                    new ProtectedOperation<Object>() {
+
+                        @Override
+                        public Object invoke(Handle handle) throws SQLException {
+                            handle.execStatement(
+                                    "DELETE FROM waypoint WHERE id=?", id);
+                            return null;
+                        }
+
+                    }
+                    );
+        }
+        public void persist(Database.Handle handle) {
+            handle.invoke(
+                    new ProtectedOperation<Object>() {
+
+                        @Override
+                        public Object invoke(Handle handle) throws SQLException {
+                            handle.execStatement(
+                                    "INSERT INTO waypoint (player_name, waypoint_name, x, y, z, world_uid) " +
+                                            "VALUES (?, ?, ?, ?, ?, ?)", 
+                                            player.getName(), name, new Float(x), new Float(y), new Float(z), world_uid);
+                            return null;
+                        }
+
+                    }
+                    );
+        }
+        public static Waypoint findByName(Database.Handle handle, final Player player, final String name) {
+            return (Waypoint)handle.invoke(
+                    new Database.ProtectedOperation<Waypoint>() {
+                        @Override
+                        public Waypoint invoke(Database.Handle handle) throws SQLException {
+                            ResultSet result = handle.execQuery(
+                                    "SELECT id, waypoint_name, x, y, z, world_uid FROM waypoint " +
+                                     "WHERE player_name=? AND waypoint_name=?", 
+                                    player.getName(), name);
+                            if (result.next()) {
+                                return new Waypoint(player, result);
+                            }
+                            else {
+                                return null;
+                            }
+                        }
+
+                    }
+                    );
+        }
+        public static Waypoint [] findByPlayer(Database.Handle handle, final Player player) {
+            return (Waypoint [])handle.invoke(
+                    new Database.ProtectedOperation<Waypoint []>() {
+                        @Override
+                        public Waypoint [] invoke(Database.Handle handle) throws SQLException {
+                            List<Waypoint> l = new ArrayList<Waypoint>();
+                            ResultSet result = handle.execQuery(
+                                    "SELECT id, waypoint_name, x, y, z, world_uid " +
+                                    "FROM waypoint WHERE player_name=?", 
+                                    player.getName());
+                            while (result.next()) {
+                                l.add(new Waypoint(player, result));
+                            }
+                            return l.toArray(new Waypoint[l.size()]);
+                        }
+                    }
+                    );
+            
+        }
+    }
+    
+}

File src/main/java/com/zzzcomputing/bukkit/ZeekTools/util/Database.java

 import java.io.File;
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 
                 throw new DatabaseException("Exception occurred connecting to database", sqle);
             }
         }
-        public boolean execStatement(String statement) throws DatabaseException {
+        public boolean execNonParameterizedStatement(String statement) throws DatabaseException {
             try {
                 Statement sql = connection.createStatement();
                 boolean result = sql.execute(statement);
                 throw new DatabaseException("Exception occurred executing statement", sqle);
             }
         }
+        public boolean execStatement(String statement, Object ...params) throws DatabaseException {
+            try {
+                PreparedStatement sql = connection.prepareStatement(statement);
+                for (int i=0; i <params.length; i++) {
+                    sql.setObject(i+1, params[i]);
+                }
+                boolean result = sql.execute();
+                return result;
+            }
+            catch (SQLException sqle) {
+                throw new DatabaseException("Exception occurred executing statement", sqle);
+            }
+        }
+        public ResultSet execQuery(String statement, Object ...params) throws DatabaseException {
+            try {
+                PreparedStatement sql = connection.prepareStatement(statement);
+                for (int i=0; i <params.length; i++) {
+                    sql.setObject(i+1, params[i]);
+                }
+                ResultSet result = sql.executeQuery();
+                return result;
+            }
+            catch (SQLException sqle) {
+                throw new DatabaseException("Exception occurred executing statement", sqle);
+            }
+        }
+        public <T> Object invoke(ProtectedOperation<T> op) {
+            try {
+                return op.invoke(this);
+            }
+            catch (SQLException sqle) {
+                throw new DatabaseException("Database exception occurred", sqle);
+            }
+        }
         public void close() throws DatabaseException {
             try {
                 connection.close();
             }
         }
     }
+    public interface ProtectedOperation<T> {
+        public <T> Object invoke(Handle handle) throws SQLException;
+    }
     public static class DatabaseException extends RuntimeException {
         DatabaseException(String message) {
             super(message);