Commits

Anonymous committed e40f8ed

- new command names
- give / destroy / goto all implemented

Comments (0)

Files changed (10)

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

 import org.bukkit.command.Command;
 import org.bukkit.command.CommandSender;
 import org.bukkit.entity.Player;
+import org.bukkit.event.Event;
+import org.bukkit.event.Event.Priority;
+import org.bukkit.event.player.PlayerJoinEvent;
+import org.bukkit.event.player.PlayerListener;
+import org.bukkit.event.player.PlayerQuitEvent;
 import org.bukkit.plugin.java.JavaPlugin;
+import org.bukkit.util.config.Configuration;
 
+import com.zzzcomputing.bukkit.ZeekTools.commands.DestroyCommand;
 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;
+import com.zzzcomputing.bukkit.ZeekTools.util.Util;
 
 
 public class ZeekTools extends JavaPlugin {
-
+    Configuration config;
+    
     public void onDisable() {
         // TODO Auto-generated method stub
         
     }
     
+    class ZTPlayerListener extends PlayerListener {
+        public void onPlayerJoin(PlayerJoinEvent event) {
+            Util.sendPlayerMessage(event.getPlayer(), "ZeekTools enabled.");
+            Util.sendPlayerMessage(event.getPlayer(), "Enter '/helpzt' for help.");
+        }
+        
+        public void onPlayerQuit(PlayerQuitEvent event) {
+            
+        }
+    }
+    
     @Override
     public void onEnable() {
-        Logger.info("ZeekTools online hooray !");
+
+        config = getConfiguration();
+        ZTPlayerListener listener = new ZTPlayerListener();
+        this.getServer().getPluginManager().registerEvent(Event.Type.PLAYER_JOIN, listener, Priority.Normal, this);
+        this.getServer().getPluginManager().registerEvent(Event.Type.PLAYER_QUIT, listener, Priority.Normal, this);
         try {
             Bootstrap.setupEnvironment();
         } catch (Exception e) {
             Logger.severe("Environment setup failed", e);
         }
+        Logger.info("ZeekTools online hooray !");
     }
 
     @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);
-                }
+                String commandName = command.getName().toLowerCase();
+                return processCommand((Player) sender, commandName, args);
             }
             
             return false;
         }
     }
 
-    private boolean processCommand(Player player, String [] args) {
-        String cmd = args[0];
-        if ("give".equals(cmd)) {
-            new GiveCommand(player).execute(args);
+    private boolean processCommand(Player player, String cmd, String [] args) {
+        if ("giveitem".equals(cmd)) {
+            new GiveCommand(this, player).execute(cmd, args);
+        }
+        else if ("destroy".equals(cmd)) {
+            new DestroyCommand(this, player).execute(cmd, args);
         }
         else if ("goto".equals(cmd)) {
-            new GotoCommand(player).execute(args);
+            new GotoCommand(this, player).execute(cmd, args);
         }
-        else if ("list".equals(cmd) || "set".equals(cmd) || "del".equals(cmd)) {
-            new WaypointCommand(player).execute(args);
+        else if ("listwp".equals(cmd) || "setwp".equals(cmd) || "delwp".equals(cmd)) {
+            new WaypointCommand(this, player).execute(cmd, args);
         }
-        else if ("help".equals(cmd)) {
-            new HelpCommand(player).execute(args);
-        }
-        else {
-            new UnknownCommand(player).execute(args);
+        else if ("helpzt".equals(cmd)) {
+            new HelpCommand(this, player).execute(cmd, args);
         }
         return true;
     }

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

 
 import org.bukkit.ChatColor;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.java.JavaPlugin;
+
+import com.zzzcomputing.bukkit.ZeekTools.util.Util;
 
 public abstract class Command {
     protected Player player;
+    protected JavaPlugin plugin;
     
-    public Command(Player player) {
+    public Command(JavaPlugin plugin, Player player) {
+        this.plugin = plugin;
         this.player = player;
     }
 
-    public abstract void execute(String [] args);
+    public abstract void execute(String command, 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);
+        Util.sendPlayerMessage(player, tokens);
     }
     
     protected void sendError(String message) {

src/main/java/com/zzzcomputing/bukkit/ZeekTools/commands/DestroyCommand.java

+package com.zzzcomputing.bukkit.ZeekTools.commands;
+
+import org.bukkit.entity.Player;
+import org.bukkit.plugin.java.JavaPlugin;
+
+public class DestroyCommand extends Command {
+
+    public DestroyCommand(JavaPlugin plugin, Player player) {
+        super(plugin, player);
+        // TODO Auto-generated constructor stub
+    }
+
+    @Override
+    public void execute(String command, String[] args) {
+        player.getInventory().remove(player.getItemInHand());
+    }
+
+}

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

 package com.zzzcomputing.bukkit.ZeekTools.commands;
 
-import org.bukkit.ChatColor;
+import java.util.HashMap;
+
+import org.bukkit.Material;
 import org.bukkit.entity.Player;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.plugin.java.JavaPlugin;
+
+import com.zzzcomputing.bukkit.ZeekTools.util.Util;
 
 public class GiveCommand extends Command {
-
-    public GiveCommand(Player player) {
-        super(player);
+    public GiveCommand(JavaPlugin plugin, Player player) {
+        super(plugin, player);
         // TODO Auto-generated constructor stub
     }
 
+    static HashMap<String, Material> names;
+    
+    static {
+        names = new HashMap<String, Material>();
+        for (Material m : Material.values()) {
+            names.put(m.toString().toLowerCase().replaceAll("[\\s_-]", ""), m);
+            names.put(String.valueOf(m.getId()), m);
+        }
+    }
+
     @Override
-    public void execute(String[] args) {
-        sendPlayerMessage(ChatColor.DARK_PURPLE,  "Give command");
-
+    public void execute(String command, String[] args) {
+        if (args.length < 1) {
+            sendError("Give command accepts item id and optional quantity");
+        }
+        else {
+            String itemname = "";
+            int amount = 64;
+            if (args.length > 1) {
+                try {
+                    amount = Integer.valueOf(args[args.length - 1]);
+                    args = Util.sliceEnd(args, 1);
+                }
+                catch (NumberFormatException nfe) {}
+            }
+            for (String arg: args) {
+                itemname += arg;
+            }
+            itemname = itemname.toLowerCase().replaceAll("[\\s_-]", "");
+            if (names.containsKey(itemname)) {
+                Material material = names.get(itemname);
+                player.getInventory().addItem(new ItemStack(material, amount));
+            }
+            else {
+                sendError(String.format("Can't locate item name or number %s", args[0]));
+            }
+        }
     }
 
 }

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

 package com.zzzcomputing.bukkit.ZeekTools.commands;
 
+import java.util.UUID;
+
 import org.bukkit.ChatColor;
+import org.bukkit.Location;
+import org.bukkit.Server;
+import org.bukkit.World;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.java.JavaPlugin;
 
 public class GotoCommand extends Command {
 
-    public GotoCommand(Player player) {
-        super(player);
+    public GotoCommand(JavaPlugin plugin, Player player) {
+        super(plugin, player);
     }
 
     @Override
-    public void execute(String[] args) {
-        sendPlayerMessage(ChatColor.DARK_PURPLE, "Goto command");
+    public void execute(String command, String[] args) {
+        if (args.length == 3) {
+            gotoCoordinates(args);
+        }
+        else if (args.length == 2 && "player".equals(args[0])) {
+           gotoPlayer(args);
+        }
+        else if (args.length == 1 && "spawn".equals(args[0])) {
+            gotoSpawn(args);
+        }
+        else if (args.length == 1) {
+            gotoWaypoint(args);
+        }
+        else {
+            sendError("goto command requires '<waypoint>', " +
+                    "'<x> <y> <z>', 'spawn', or 'player <playername>' "+ 
+                    "as arguments");
+        }
 
     }
+    
+    private void gotoCoordinates(String [] args) {
+        double x;
+        double y;
+        double z;
+        try {
+            x = Double.parseDouble(args[0]);
+            y = Double.parseDouble(args[1]);
+            z = Double.parseDouble(args[2]);
+        }
+        catch (NumberFormatException nfe) {
+            sendError(String.format("Not numerical arguments: %s %s %s", args[0], args[1], args[2]));
+            return;
+        }
 
+        teleport(player.getWorld(), x, y, z, String.format("%.2f %.2f %.2f", x, y, z));
+    }
+    
+    private void gotoWaypoint(String [] args) {
+        WaypointCommand.Waypoint wp = new WaypointCommand(plugin, player).getWaypointByName(args[0]);
+        if (wp == null) {
+            sendError(String.format("No such waypoint '%s'", args[0]));
+        }
+        else {
+            if (player.getWorld().getUID().equals(wp.world_uid)) {
+                teleport(player.getWorld(), wp.x, wp.y, wp.z, wp.getDescription());
+            }
+            else {
+                teleport(plugin.getServer().getWorld(UUID.fromString(wp.world_uid)), wp.x, wp.y, wp.z, wp.getDescription());
+            }
+        }
+    }
+
+    private void teleport(World w, double x, double y, double z, String success) {
+        Location existingLocation = player.getLocation();
+        Location targetLocation = new Location(w, x, y, z, existingLocation.getYaw(), existingLocation.getPitch());
+        
+        player.teleport(targetLocation);
+        this.sendPlayerMessage(String.format("Teleported to %s", success));
+    }
+    
+    private void gotoPlayer(String [] args) {
+        String playername = args[1];
+        Player p = plugin.getServer().getPlayer(playername);
+        if (p == null) {
+            this.sendPlayerMessage(String.format("Can't find player '%s'", playername));
+        }
+        else {
+            player.teleport(p);
+            this.sendPlayerMessage(String.format("Teleported to '%s'", p.getName()));
+        }
+    }
+    
+    private void gotoSpawn(String [] args) {
+        sendError("goto spawn not yet implemented :(");
+    }
 }

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

 
 import org.bukkit.ChatColor;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.java.JavaPlugin;
 
 public class HelpCommand extends Command {
 
-    public HelpCommand(Player player) {
-        super(player);
+    public HelpCommand(JavaPlugin plugin, Player player) {
+        super(plugin, 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");
+    public void execute(String command, String[] args) {
+        sendPlayerMessage(ChatColor.WHITE, "/giveitem <name_or_id> [num] [player] - Give item.");
+        sendPlayerMessage(ChatColor.WHITE, "/goto <x> <y> <z> - goto coordinates");
+        sendPlayerMessage(ChatColor.WHITE, "/goto <waypoint> - goto saved waypoint");
+        sendPlayerMessage(ChatColor.WHITE, "/goto player <player> - goto player position");
+        sendPlayerMessage(ChatColor.WHITE, "/goto spawn - goto spawn point/bed");
+        sendPlayerMessage(ChatColor.WHITE, "/destroy - empty out the currently selected item stack");
+        sendPlayerMessage(ChatColor.WHITE, "/listwp - list waypoints");
+        sendPlayerMessage(ChatColor.WHITE, "/setwp <waypoint> - set waypoint");
+        sendPlayerMessage(ChatColor.WHITE, "/delwp <waypoint> - delete waypoint");
     }
 }

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]);
-    }
-
-}

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

 import java.util.ArrayList;
 import java.util.List;
 
-import org.bukkit.ChatColor;
 import org.bukkit.entity.Player;
+import org.bukkit.plugin.java.JavaPlugin;
 
 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);
+    public WaypointCommand(JavaPlugin plugin, Player player) {
+        super(plugin, player);
     }
 
     @Override
-    public void execute(String[] args) {
-        String subcommand = args[0];
-        if ("set".equals(subcommand)) {
+    public void execute(String command, String[] args) {
+        if ("setwp".equals(command)) {
             setWaypoint(args);
         }
-        else if ("del".equals(subcommand)) {
+        else if ("delwp".equals(command)) {
             deleteWaypoint(args);
         }
-        else if ("list".equals(subcommand)) {
+        else if ("listwp".equals(command)) {
             listWaypoints(args);
         }
-        else {
-            new UnknownCommand(player).execute(args);
-        }
     }
     
     private void listWaypoints(String [] args) {
-        if (args.length != 1) {
+        if (args.length > 0) {
             sendError("list command takes no arguments");
         }
         else {
     }
 
     private void deleteWaypoint(String [] args) {
-        if (args.length != 2) {
-            sendError("set command requires one argument");
+        if (args.length != 1) {
+            sendError("delete command requires one argument");
         }
         else {
             Database.Handle handle = Database.getHandle();
             try {
-                Waypoint wp = Waypoint.findByName(handle, player, args[1]);
+                Waypoint wp = Waypoint.findByName(handle, player, args[0]);
                 if (wp == null) {
-                    sendError(String.format("Waypoint '%s' does not exist", args[1]));
+                    sendError(String.format("Waypoint '%s' does not exist", args[0]));
                 }
                 else {
                     wp.delete(handle);
         }
     }
     private void setWaypoint(String [] args) {
-        if (args.length != 2) {
+        if (args.length != 1) {
             sendError("set command requires one argument");
         }
+        else if ("spawn".equals(args[0])) {
+            sendError("Can't use the name 'spawn' as a waypoint name");
+        }
         else {
             Database.Handle handle = Database.getHandle();
             try {
-                Waypoint wp = Waypoint.findByName(handle, player, args[1]);
+                Waypoint wp = Waypoint.findByName(handle, player, args[0]);
                 if (wp != null) {
-                    sendError(String.format("Waypoint '%s' already exists", args[1]));
+                    sendError(String.format("Waypoint '%s' already exists", args[0]));
                 }
                 else {
-                    wp = new Waypoint(player, args[1]);
+                    wp = new Waypoint(player, args[0]);
                     wp.persist(handle);
                     sendPlayerMessage(String.format("Waypoint %s saved", wp.getDescription()));
                 }
         }
     }
     
+    Waypoint getWaypointByName(String name) {
+        Database.Handle handle = Database.getHandle();
+        try {
+            return Waypoint.findByName(handle, player, name);
+        }
+        finally {
+            handle.close();
+        }
+    }
+
     static class Waypoint {
         String name;
         Player player;

src/main/java/com/zzzcomputing/bukkit/ZeekTools/util/Util.java

+package com.zzzcomputing.bukkit.ZeekTools.util;
+
+import org.bukkit.ChatColor;
+import org.bukkit.entity.Player;
+
+public class Util {
+    public static String [] slice(String [] src, int start) {
+        String [] dest = new String[src.length - start];
+        System.arraycopy(src, start, dest, 0, dest.length);
+        return dest;
+    }
+    
+    public static String [] sliceEnd(String [] src, int end) {
+        String [] dest = new String[src.length - end];
+        System.arraycopy(src, 0, dest, 0, dest.length);
+        return dest;
+    }
+
+    public static void sendPlayerMessage(Player player, Object ... tokens) {
+        String msg = ChatColor.DARK_PURPLE + "[ZT] " + ChatColor.AQUA;
+        for (Object token: tokens) {
+            msg = msg.concat(token.toString());
+        }
+        player.sendMessage(msg);
+    }
+
+}

src/main/resources/plugin.yml

 description: Provides some SPC-like features to Bukkit.
 author: zzzeek
 commands:
-    zt:
-        description: ZeekTools commands
-        usage: |
-            /<command> give <name_or_id> [num] [player] - Give item.
-            /<command> goto <x> <y> <z> - goto coordinates
-            /<command> goto <waypoint> - goto saved waypoint
-            /<command> goto spawn - goto spawn point/bed
-            /<command> list waypoints - list waypoints
-            /<command> set <waypoint> - set waypoint
-            /<command> del <waypoint> - delete waypoint
+    giveitem:
+        description: Give item.  Will give the max unless [num] given.
+        usage: give <name_or_id> [num]
+    destroy:
+        description: clear out the current item stack
+        usage: destroy
+    goto:
+        description: Goto waypoint, coordinates, player, or spawn point
+        usage: goto <waypoint>|<x> <y> <z>|player <playername>|spawn
+    listwp:
+        description: list waypoints
+        usage: listwp
+    setwp:
+        description: set waypoint
+        usage: setwp <waypoint>
+    delwp:
+        description: delete waypoint
+        usage: delwp <waypoint>
+    helpzt:
+        description: help
+        usage: helpzt
  
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.