From 9e1f814646c374e2d881bf9c6238896a78bbb135 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Thu, 30 Dec 2010 04:52:26 +0000 Subject: [PATCH] Added sendMessage() to player, added /pos to sample plugin By: Dinnerbone --- .../bukkit/sample/SamplePlayerListener.java | 31 +++++++++++++++++++ .../bukkit/sample/SamplePlugin.java | 1 + paper-api/src/org/bukkit/Player.java | 7 +++++ 3 files changed, 39 insertions(+) diff --git a/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlayerListener.java b/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlayerListener.java index 0e28a95569..75589bfc55 100644 --- a/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlayerListener.java +++ b/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlayerListener.java @@ -1,6 +1,9 @@ package com.dinnerbone.bukkit.sample; +import org.bukkit.Location; +import org.bukkit.Player; +import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerListener; @@ -24,4 +27,32 @@ public class SamplePlayerListener extends PlayerListener { 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); + } + } } diff --git a/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlugin.java b/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlugin.java index 95fe2299ce..53bb1317e8 100644 --- a/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlugin.java +++ b/paper-api/sample/src/com/dinnerbone/bukkit/sample/SamplePlugin.java @@ -36,5 +36,6 @@ public class SamplePlugin extends JavaPlugin { private void registerEvents() { getServer().getPluginManager().registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Normal, this); getServer().getPluginManager().registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this); + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.Normal, this); } } diff --git a/paper-api/src/org/bukkit/Player.java b/paper-api/src/org/bukkit/Player.java index 1958caf994..7d4c5ed722 100644 --- a/paper-api/src/org/bukkit/Player.java +++ b/paper-api/src/org/bukkit/Player.java @@ -12,4 +12,11 @@ public interface Player extends HumanEntity { * @return true if they are online */ public boolean isOnline(); + + /** + * Sends this player a message, which will be displayed in their chat + * + * @param message Message to be displayed + */ + public void sendMessage(String message); }