From 764b050baad504f2f0da19f90bcb488da7026a1f Mon Sep 17 00:00:00 2001 From: Simon Rigby Date: Mon, 7 Mar 2011 09:14:02 +0000 Subject: [PATCH] Refactoring... Cleaning Up... Adding some Commands... Fixing up some mistakes etc etc etc. --- .../MultiVerseCore/MVEntityListener.java | 3 +- .../MultiVerseCore/MVPlayerListener.java | 5 ++ .../MultiVerseCore/MVTeleport.java | 6 +- .../onarandombox/MultiVerseCore/MVWorld.java | 2 +- .../MultiVerseCore/MultiVerseCore.java | 31 +++---- .../MultiVerseCore/commands/MVCoord.java | 17 +++- .../MultiVerseCore/commands/MVCreate.java | 34 +++++++- .../MultiVerseCore/commands/MVWho.java | 63 ++++++++++++++ ...uration.java => DefaultConfiguration.java} | 4 +- .../{blockSafety.java => BlockSafety.java} | 2 +- .../utils/LocationManipulation.java | 82 +++++++++++++++++++ .../onarandombox/utils/stringLocation.java | 28 ------- src/plugin.yml | 48 ++++++++--- 13 files changed, 260 insertions(+), 65 deletions(-) create mode 100644 src/com/onarandombox/MultiVerseCore/commands/MVWho.java rename src/com/onarandombox/MultiVerseCore/configuration/{defaultConfiguration.java => DefaultConfiguration.java} (91%) rename src/com/onarandombox/utils/{blockSafety.java => BlockSafety.java} (95%) create mode 100644 src/com/onarandombox/utils/LocationManipulation.java delete mode 100644 src/com/onarandombox/utils/stringLocation.java diff --git a/src/com/onarandombox/MultiVerseCore/MVEntityListener.java b/src/com/onarandombox/MultiVerseCore/MVEntityListener.java index f0b6c196..84cf59f4 100644 --- a/src/com/onarandombox/MultiVerseCore/MVEntityListener.java +++ b/src/com/onarandombox/MultiVerseCore/MVEntityListener.java @@ -63,8 +63,9 @@ public class MVEntityListener extends EntityListener { } if (defender instanceof Player && attacker instanceof Player){ + Player player = (Player) attacker; if (!(this.plugin.worlds.get(w.getName()).pvp)) { - this.plugin.playerSessions.get(((Player) attacker)).message(ChatColor.RED + "PVP is disabled in this World."); + this.plugin.getPlayerSession(player).message(ChatColor.RED + "PVP is disabled in this World."); event.setCancelled(true); return; } diff --git a/src/com/onarandombox/MultiVerseCore/MVPlayerListener.java b/src/com/onarandombox/MultiVerseCore/MVPlayerListener.java index aa0a2b55..ff5a4563 100644 --- a/src/com/onarandombox/MultiVerseCore/MVPlayerListener.java +++ b/src/com/onarandombox/MultiVerseCore/MVPlayerListener.java @@ -14,6 +14,11 @@ public class MVPlayerListener extends PlayerListener { this.plugin = plugin; } + public void onPlayerTeleport(PlayerMoveEvent event){ + //MultiVerseCore.debugMsg(event.getPlayer().getName() + " just tried to Teleport"); + //event.setCancelled(true); + } + public void onPlayerMove(PlayerMoveEvent event){ } diff --git a/src/com/onarandombox/MultiVerseCore/MVTeleport.java b/src/com/onarandombox/MultiVerseCore/MVTeleport.java index f63e0bb4..786ae7be 100644 --- a/src/com/onarandombox/MultiVerseCore/MVTeleport.java +++ b/src/com/onarandombox/MultiVerseCore/MVTeleport.java @@ -50,8 +50,6 @@ public class MVTeleport { z = location.getZ() / (srcComp != 0 ? srcComp : 1) * trgComp + 0.5; } - world.loadChunk(world.getChunkAt(new Location(world,x,y,z))); - if (y < 1 && world.getEnvironment() == Environment.NORMAL) y = 1; @@ -77,7 +75,9 @@ public class MVTeleport { if (aux == -1) return null; MultiVerseCore.log.info("Target location (safe): " + x + ", " + aux + ", " + z); - + + world.loadChunk(world.getChunkAt(new Location(world,x,aux,z))); // Try load the destination to chunk first before teleporting. + return new Location(world, x, aux, z, location.getYaw(),location.getPitch()); } diff --git a/src/com/onarandombox/MultiVerseCore/MVWorld.java b/src/com/onarandombox/MultiVerseCore/MVWorld.java index e0d5e8e6..f787b1a8 100644 --- a/src/com/onarandombox/MultiVerseCore/MVWorld.java +++ b/src/com/onarandombox/MultiVerseCore/MVWorld.java @@ -9,7 +9,7 @@ import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.util.config.Configuration; -import com.onarandombox.utils.stringLocation; +import com.onarandombox.utils.LocationManipulation; @SuppressWarnings("unused") public class MVWorld { diff --git a/src/com/onarandombox/MultiVerseCore/MultiVerseCore.java b/src/com/onarandombox/MultiVerseCore/MultiVerseCore.java index 2c4b6368..6a1baa8a 100644 --- a/src/com/onarandombox/MultiVerseCore/MultiVerseCore.java +++ b/src/com/onarandombox/MultiVerseCore/MultiVerseCore.java @@ -31,7 +31,7 @@ import org.bukkit.event.Event.Priority; //import com.nijiko.permissions.PermissionHandler; import com.onarandombox.MultiVerseCore.commands.*; -import com.onarandombox.MultiVerseCore.configuration.defaultConfiguration; +import com.onarandombox.MultiVerseCore.configuration.DefaultConfiguration; @SuppressWarnings("unused") public class MultiVerseCore extends JavaPlugin { @@ -95,8 +95,8 @@ public class MultiVerseCore extends JavaPlugin { }*/ // Call the defaultConfiguration class to create the config files if they don't already exist. - new defaultConfiguration(dataFolder, "config.yml"); - new defaultConfiguration(dataFolder, "worlds.yml"); + new DefaultConfiguration(dataFolder, "config.yml"); + new DefaultConfiguration(dataFolder, "worlds.yml"); // Now grab the Configuration Files. configMV = new Configuration(new File(dataFolder, "config.yml")); @@ -109,20 +109,21 @@ public class MultiVerseCore extends JavaPlugin { // Setup all the Events the plugin needs to Monitor. PluginManager pm = getServer().getPluginManager(); pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.Low, this); // Low so it acts above any other. - pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.High, this); // To Add World Prefixing to Chat. - pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener, Priority.Normal, this); // Respawn Players at the right point. - + pm.registerEvent(Event.Type.PLAYER_TELEPORT, playerListener, Priority.Normal, this); // Cancel Teleports if needed. + //pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.High, this); // To Add World Prefixing to Chat. -- Separate Plugin, maybe... + //pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener, Priority.Normal, this); // Respawn Players at the right point. -- No need to handle it anymore with setSpawnLocation() + pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener,Priority.Normal, this); // To remove Player Sessions pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, Priority.Normal, this); // To prevent Blocks being destroyed. pm.registerEvent(Event.Type.BLOCK_PLACED, blockListener, Priority.Normal, this); // To prevent Blocks being placed. - pm.registerEvent(Event.Type.ENTITY_DAMAGED, entityListener, Priority.Normal, this); // To Allow/Disallow PVP. + pm.registerEvent(Event.Type.ENTITY_DAMAGED, entityListener, Priority.Normal, this); // To Allow/Disallow PVP as well as EnableHealth. - pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Priority.Normal, this); + pm.registerEvent(Event.Type.CREATURE_SPAWN, entityListener, Priority.Normal, this); // To prevent all or certain animals/monsters from spawning. - pm.registerEvent(Event.Type.ENTITY_EXPLODE, entityListener, Priority.Normal, this); - pm.registerEvent(Event.Type.EXPLOSION_PRIMED, entityListener, Priority.Normal, this); + pm.registerEvent(Event.Type.ENTITY_EXPLODE, entityListener, Priority.Normal, this); // Try to prevent Ghasts from blowing up structures. + pm.registerEvent(Event.Type.EXPLOSION_PRIMED, entityListener, Priority.Normal, this); // Try to prevent Ghasts from blowing up structures. pm.registerEvent(Event.Type.PLUGIN_ENABLE, pluginListener, Priority.Normal, this); // Monitor for Permissions Plugin etc. @@ -169,6 +170,7 @@ public class MultiVerseCore extends JavaPlugin { commands.put("mvsetspawn", new MVSetSpawn(this)); commands.put("mvspawn", new MVSpawn(this)); commands.put("mvcoord", new MVCoord(this)); + commands.put("mvwho", new MVWho(this)); } /** @@ -248,7 +250,7 @@ public class MultiVerseCore extends JavaPlugin { MVCommandHandler handler = commands.get(command.getName().toLowerCase()); - if (handler != null) { + if (handler!=null) { return handler.perform(sender, args); } else { return false; @@ -258,6 +260,9 @@ public class MultiVerseCore extends JavaPlugin { /** * Basic Debug Output function, if we've enabled debugging we'll output more information. */ + public static void debugMsg(String msg){ + debugMsg(msg,null); + } public static void debugMsg(String msg, Player p){ if(debug){ log.info(msg); @@ -266,8 +271,4 @@ public class MultiVerseCore extends JavaPlugin { } } } - - public static void debugMsg(String msg){ - debugMsg(msg,null); - } } \ No newline at end of file diff --git a/src/com/onarandombox/MultiVerseCore/commands/MVCoord.java b/src/com/onarandombox/MultiVerseCore/commands/MVCoord.java index f339c079..6cad51bf 100644 --- a/src/com/onarandombox/MultiVerseCore/commands/MVCoord.java +++ b/src/com/onarandombox/MultiVerseCore/commands/MVCoord.java @@ -1,12 +1,17 @@ package com.onarandombox.MultiVerseCore.commands; +import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import com.onarandombox.MultiVerseCore.MVCommandHandler; import com.onarandombox.MultiVerseCore.MultiVerseCore; +import com.onarandombox.utils.LocationManipulation; public class MVCoord extends MVCommandHandler { + private LocationManipulation locMan = new LocationManipulation(); + public MVCoord(MultiVerseCore plugin) { super(plugin); // TODO Auto-generated constructor stub @@ -14,8 +19,16 @@ public class MVCoord extends MVCommandHandler { @Override public boolean perform(CommandSender sender, String[] args) { - // TODO Auto-generated method stub - return false; + // TODO: Add Permissions + if(sender instanceof Player){ + Player p = (Player) sender; + + p.sendMessage(ChatColor.RED + "World: " + ChatColor.WHITE + p.getWorld().getName()); + p.sendMessage(ChatColor.RED + "Compression: " + ChatColor.WHITE + plugin.worlds.get(p.getWorld().getName()).compression); + p.sendMessage(ChatColor.RED + "Coordinates: " + ChatColor.WHITE + locMan.strCoords(p.getLocation())); + p.sendMessage(ChatColor.RED + "Direction: " + ChatColor.WHITE + locMan.getDirection(p.getLocation())); + } + return true; } } diff --git a/src/com/onarandombox/MultiVerseCore/commands/MVCreate.java b/src/com/onarandombox/MultiVerseCore/commands/MVCreate.java index 6bdf3ded..ec9dabcf 100644 --- a/src/com/onarandombox/MultiVerseCore/commands/MVCreate.java +++ b/src/com/onarandombox/MultiVerseCore/commands/MVCreate.java @@ -1,5 +1,10 @@ package com.onarandombox.MultiVerseCore.commands; +import java.io.File; + +import org.bukkit.ChatColor; +import org.bukkit.World; +import org.bukkit.World.Environment; import org.bukkit.command.CommandSender; import com.onarandombox.MultiVerseCore.MVCommandHandler; @@ -13,7 +18,34 @@ public class MVCreate extends MVCommandHandler { @Override public boolean perform(CommandSender sender, String[] args) { - // TODO Auto-generated method stub + + if (args.length != 2) { + sender.sendMessage("Not enough parameters to create a new world"); + sender.sendMessage(ChatColor.RED + "/mvcreate WORLDNAME ENVIRONMENT - Create a new World."); + sender.sendMessage(ChatColor.RED + "Example - /mvcreate hellworld nether"); + return true; + } + if (new File(args[0].toString()).exists()) { + sender.sendMessage(ChatColor.RED + "A Folder/World already exists with this name!"); + sender.sendMessage(ChatColor.RED + "If you are confident it is a world you can import with /mvimport"); + return true; + } + String name = args[0].toString(); + String env = args[1].toString(); + Environment environment = null; + if (env.equalsIgnoreCase("NETHER")) + environment = Environment.NETHER; + + if (env.equalsIgnoreCase("NORMAL")) + environment = Environment.NORMAL; + + if (environment == null) { + sender.sendMessage(ChatColor.RED + + "Environment type does not exist!"); + sender.sendMessage(ChatColor.RED + + "Only Normal & Nether exist as Environments"); + return false; + } return false; } diff --git a/src/com/onarandombox/MultiVerseCore/commands/MVWho.java b/src/com/onarandombox/MultiVerseCore/commands/MVWho.java new file mode 100644 index 00000000..fc490da9 --- /dev/null +++ b/src/com/onarandombox/MultiVerseCore/commands/MVWho.java @@ -0,0 +1,63 @@ +package com.onarandombox.MultiVerseCore.commands; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.ChatColor; +import org.bukkit.World; +import org.bukkit.World.Environment; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.onarandombox.MultiVerseCore.MVCommandHandler; +import com.onarandombox.MultiVerseCore.MultiVerseCore; + +public class MVWho extends MVCommandHandler { + + public MVWho(MultiVerseCore plugin) { + super(plugin); + } + + @Override + public boolean perform(CommandSender sender, String[] args) { + List worlds = new ArrayList(); + + if(args.length>1){ + return false; + } + + if(args.length>0){ + World world = plugin.getServer().getWorld(args[0].toString()); + if(world!=null){ + worlds.add(world); + } else { + sender.sendMessage(ChatColor.RED + "World does not exist"); + return true; + } + } else { + worlds = plugin.getServer().getWorlds(); + } + + for(World world : worlds){ + ChatColor color = ChatColor.BLUE; + if(world.getEnvironment()==Environment.NETHER){ + color = ChatColor.RED; + } else if(world.getEnvironment()==Environment.NORMAL){ + color = ChatColor.GREEN; + } + List players = world.getPlayers(); + + String result = ""; + if(players.size() <= 0){ + result = "Empty"; + } else { + for(Player player : players){ + result += player.getName() + " "; + } + } + sender.sendMessage(color + world.getName() + ChatColor.WHITE + " - " + result); + } + return true; + } + +} diff --git a/src/com/onarandombox/MultiVerseCore/configuration/defaultConfiguration.java b/src/com/onarandombox/MultiVerseCore/configuration/DefaultConfiguration.java similarity index 91% rename from src/com/onarandombox/MultiVerseCore/configuration/defaultConfiguration.java rename to src/com/onarandombox/MultiVerseCore/configuration/DefaultConfiguration.java index 4007a00f..b479a006 100644 --- a/src/com/onarandombox/MultiVerseCore/configuration/defaultConfiguration.java +++ b/src/com/onarandombox/MultiVerseCore/configuration/DefaultConfiguration.java @@ -10,9 +10,9 @@ import com.onarandombox.MultiVerseCore.MultiVerseCore; * https://github.com/Nijikokun/iConomy3/blob/master/com/nijiko/coelho/iConomy/iConomy.java * @author Nijikokun & Coelho */ -public class defaultConfiguration { +public class DefaultConfiguration { - public defaultConfiguration(File folder, String name){ + public DefaultConfiguration(File folder, String name){ File actual = new File(folder, name); if (!actual.exists()) { diff --git a/src/com/onarandombox/utils/blockSafety.java b/src/com/onarandombox/utils/BlockSafety.java similarity index 95% rename from src/com/onarandombox/utils/blockSafety.java rename to src/com/onarandombox/utils/BlockSafety.java index 352402c5..f0ec1c3f 100644 --- a/src/com/onarandombox/utils/blockSafety.java +++ b/src/com/onarandombox/utils/BlockSafety.java @@ -3,7 +3,7 @@ package com.onarandombox.utils; import org.bukkit.Material; import org.bukkit.World; -public class blockSafety { +public class BlockSafety { /** * This function checks whether the block at the given coordinates are above * air or not. diff --git a/src/com/onarandombox/utils/LocationManipulation.java b/src/com/onarandombox/utils/LocationManipulation.java new file mode 100644 index 00000000..32219a43 --- /dev/null +++ b/src/com/onarandombox/utils/LocationManipulation.java @@ -0,0 +1,82 @@ +package com.onarandombox.utils; + +import org.bukkit.Location; +import org.bukkit.World; + +public class LocationManipulation { + + /** + * Convert a Location into a Colon separated string to allow us to store it in text. + * @param location + * @return + */ + public String locationToString(Location location) { + StringBuilder l = new StringBuilder(); + l.append(location.getBlockX() + ":"); + l.append(location.getBlockY() + ":"); + l.append(location.getBlockZ() + ":"); + l.append(location.getYaw() + ":"); + l.append(location.getPitch()); + return l.toString(); + } + /** + * Convert a String to a Location. + * @param world + * @param xStr + * @param yStr + * @param zStr + * @param yawStr + * @param pitchStr + * @return + */ + public Location stringToLocation(World world, String xStr, String yStr, String zStr, String yawStr, String pitchStr){ + double x = Double.parseDouble(xStr); + double y = Double.parseDouble(yStr); + double z = Double.parseDouble(zStr); + float yaw = Float.valueOf(yawStr).floatValue(); + float pitch = Float.valueOf(pitchStr).floatValue(); + + return new Location(world, x, y, z, yaw, pitch); + } + /** + * Convert a Location to XYZ Coordinates. + * @param l + * @return + */ + public String strCoords(Location l){ + String result = ""; + result += "X: " + l.getBlockX() + " "; + result += "Y: " + l.getBlockY() + " "; + result += "Z: " + l.getBlockZ() + " "; + return result; + } + /** + * Return the NESW Direction a Location is facing. + * @param location + * @return + */ + public String getDirection(Location location) { + int r = (int) Math.abs((location.getYaw() - 90) % 360); + String dir; + if (r < 23) + dir = "N"; + else if (r < 68) + dir = "NE"; + else if (r < 113) + dir = "E"; + else if (r < 158) + dir = "SE"; + else if (r < 203) + dir = "S"; + else if (r < 248) + dir = "SW"; + else if (r < 293) + dir = "W"; + else if (r < 338) + dir = "NW"; + else + dir = "N"; + + return dir; + } +} diff --git a/src/com/onarandombox/utils/stringLocation.java b/src/com/onarandombox/utils/stringLocation.java deleted file mode 100644 index 8e7062f1..00000000 --- a/src/com/onarandombox/utils/stringLocation.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.onarandombox.utils; - -import org.bukkit.Location; -import org.bukkit.World; - -public class stringLocation { - - public Location stringToLocation(World world, String xStr, String yStr, String zStr, String yawStr, String pitchStr){ - double x = Double.parseDouble(xStr); - double y = Double.parseDouble(yStr); - double z = Double.parseDouble(zStr); - float yaw = Float.valueOf(yawStr).floatValue(); - float pitch = Float.valueOf(pitchStr).floatValue(); - - return new Location(world, x, y, z, yaw, pitch); - } - - public String locationToString(Location location) { - StringBuilder l = new StringBuilder(); - l.append(location.getBlockX() + ":"); - l.append(location.getBlockY() + ":"); - l.append(location.getBlockZ() + ":"); - l.append(location.getYaw() + ":"); - l.append(location.getPitch()); - return l.toString(); - } - -} diff --git a/src/plugin.yml b/src/plugin.yml index 09b6273a..a3e8cf59 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -5,28 +5,54 @@ version: 0.1 commands: mvcreate: description: World create command - usage: / + usage: | + / + / creative normal -- Creates a world called 'creative' with a NORMAL environment. + / hellworld nether -- Creates a world called 'hellworld' with a NETHER environment. mvimport: description: World import command - usage: / + usage: | + / + / creative normal -- Imports an existing world called 'creative' with a NORMAL environment. + / hellworld nether -- Imports an existing world called 'hellworld' with a NETHER environment. mvremove: description: World delete command - usage: / + usage: | + / + / creative -- Removes the world 'creative' from the MultiVerse setup. + / hellworld -- Removes the world 'hellworld' from the MultiVerse setup. mvmodify: description: Modify the settings of an existing world - usage: / + usage: | + /