Skip to content

Commit

Permalink
Updated for the new CommandExecutor interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceManiac authored and grum committed Mar 25, 2011
1 parent ea24df7 commit 8419fdb
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 38 deletions.
31 changes: 31 additions & 0 deletions src/main/java/com/dinnerbone/bukkit/sample/SampleDebugCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

package com.dinnerbone.bukkit.sample;

import org.bukkit.entity.Player;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

/**
* Handler for the /debug sample command.
* @author SpaceManiac
*/
public class SampleDebugCommand implements CommandExecutor {
private final SamplePlugin plugin;

public SampleDebugCommand(SamplePlugin plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] split) {
if (sender instanceof Player) {
Player player = (Player) sender;
plugin.setDebugging(player, !plugin.isDebugging(player));

return true;
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
package com.dinnerbone.bukkit.sample;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
Expand All @@ -29,38 +27,6 @@ public void onPlayerQuit(PlayerEvent event) {
System.out.println(event.getPlayer().getName() + " left the server! :'(");
}

@Override
public void onPlayerCommand(PlayerChatEvent event) {
String[] split = event.getMessage().split(" ");
Player player = event.getPlayer();

if (split[0].equalsIgnoreCase("/pos")) {
if (split.length == 1) {
Location location = player.getLocation();
player.sendMessage("You are currently at " + location.getX() +"," + location.getY() + "," + location.getZ() +
" with " + location.getYaw() + " yaw and " + location.getPitch() + " pitch");
} else if (split.length == 4) {
try {
double x = Double.parseDouble(split[1]);
double y = Double.parseDouble(split[2]);
double z = Double.parseDouble(split[3]);

player.teleportTo(new Location(player.getWorld(), x, y, z));
} catch (NumberFormatException ex) {
player.sendMessage("Given location is invalid");
}
} else {
player.sendMessage("Usage: '/pos' to get current position, or '/pos x y z' to teleport to x,y,z");
}

event.setCancelled(true);
} else if (split[0].equalsIgnoreCase("/debug")) {
plugin.setDebugging(player, !plugin.isDebugging(player));

event.setCancelled(true);
}
}

@Override
public void onPlayerMove(PlayerMoveEvent event) {
if (plugin.isDebugging(event.getPlayer())) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/dinnerbone/bukkit/sample/SamplePlugin.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@

package com.dinnerbone.bukkit.sample;

import java.io.File;
import java.util.HashMap;
import org.bukkit.entity.Player;
import org.bukkit.Server;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;

Expand Down Expand Up @@ -42,11 +39,14 @@ public void onEnable() {
PluginManager pm = getServer().getPluginManager();
pm.registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.Normal, this);
pm.registerEvent(Event.Type.BLOCK_PHYSICS, blockListener, Priority.Normal, this);
pm.registerEvent(Event.Type.BLOCK_CANBUILD, blockListener, Priority.Normal, this);

// Register our commands
getCommand("pos").setExecutor(new SamplePosCommand(this));
getCommand("debug").setExecutor(new SampleDebugCommand(this));

// EXAMPLE: Custom code, here we just output some info so we can check all is well
PluginDescriptionFile pdfFile = this.getDescription();
System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/com/dinnerbone/bukkit/sample/SamplePosCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

package com.dinnerbone.bukkit.sample;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

/**
* Handler for the /pos sample command.
* @author SpaceManiac
*/
public class SamplePosCommand implements CommandExecutor {
private final SamplePlugin plugin;

public SamplePosCommand(SamplePlugin plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] split) {
if (!(sender instanceof Player)) {
return false;
}
Player player = (Player) sender;

if (split.length == 0) {
Location location = player.getLocation();
player.sendMessage("You are currently at " + location.getX() +"," + location.getY() + "," + location.getZ() +
" with " + location.getYaw() + " yaw and " + location.getPitch() + " pitch");
return true;
} else if (split.length == 3) {
try {
double x = Double.parseDouble(split[0]);
double y = Double.parseDouble(split[1]);
double z = Double.parseDouble(split[2]);

player.teleportTo(new Location(player.getWorld(), x, y, z));
} catch (NumberFormatException ex) {
player.sendMessage("Given location is invalid");
}
return true;
} else {
return false;
}
}
}
7 changes: 7 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
name: Sample Plugin
main: com.dinnerbone.bukkit.sample.SamplePlugin
version: 0.4
commands:
pos:
description: Displays your position or teleports you to another.
usage: "Usage: '/pos' to get current position, or '/pos x y z' to teleport to x,y,z"
debug:
description: Toggles your debugging status for this plugin.
usage: /debug

0 comments on commit 8419fdb

Please sign in to comment.