From b9355db350e6996ecae974114c8590cfe5503f51 Mon Sep 17 00:00:00 2001 From: Pierce Corcoran Date: Thu, 18 Sep 2014 19:52:17 -0700 Subject: [PATCH 1/2] Fix for MCPC+ and other general errors Added particle effect when entering a portal, Added a Not enough XP message, Added Wait time config, fixed Yaml error in local-EM.yml, Added onBlockPlace cool down so there aren't double block place events on MCPC+, Fixed setPirvacy Stack Overflow Error, made portal name regex prettier, Removed Dependency on CraftWorld, Subtracts XP only if there is a destination for a portal, Fixed cast of Object to int --- MyPortals/config.yml | 4 +++ MyPortals/locale-EN.yml | 3 +- .../src/cl/netgamer/myportals/MyCmd.java | 4 +-- .../src/cl/netgamer/myportals/MyListener.java | 25 ++++++++++++++--- .../src/cl/netgamer/myportals/MyPortals.java | 28 +++++++++++++++++-- .../src/cl/netgamer/myportals/Portal.java | 2 +- .../src/cl/netgamer/myportals/Shape.java | 8 +++--- .../src/cl/netgamer/myportals/WarpTask.java | 15 ++++++---- .../src/cl/netgamer/tabtext/TabText.java | 10 +++---- 9 files changed, 73 insertions(+), 26 deletions(-) diff --git a/MyPortals/config.yml b/MyPortals/config.yml index 9a2ae22..74f34f2 100644 --- a/MyPortals/config.yml +++ b/MyPortals/config.yml @@ -5,6 +5,10 @@ disabled: false debug: false +# time in ticks for the player to wait until they are teleported +# don't set this too low or they won't be able to get out of the portal before thery are teleported again +waitTime: 80 + # locale prefix. you can also copy, rename and edit lang-EN.yml locale: EN diff --git a/MyPortals/locale-EN.yml b/MyPortals/locale-EN.yml index 0e66a54..cef6f72 100644 --- a/MyPortals/locale-EN.yml +++ b/MyPortals/locale-EN.yml @@ -85,7 +85,7 @@ hidden: can't display somebody's hidden portal info locked: can't modify somebody's locked portal invalidName: invalid name, only 1 to 12 ascii letters and numbers busyName: the reciever has a same name portal -nameNotFound: portal name not found, try with "owner:portalName" +nameNotFound: "portal name not found, try with \"owner:portalName\"" diffrentWorlds: you can not link diffrent world portals for now noName: give the portal a name first notYours: you can not give a portal that is not yours @@ -100,3 +100,4 @@ privacyOk: privacy successfully changed giveOk: portal successfully transferred recieveOk: player %$1s has gived you the portal %$2s rebuilded: portals rebuilding finished +notEnoughXP: You need more XP to use this portal \ No newline at end of file diff --git a/MyPortals/src/cl/netgamer/myportals/MyCmd.java b/MyPortals/src/cl/netgamer/myportals/MyCmd.java index 410b92b..4635dd9 100644 --- a/MyPortals/src/cl/netgamer/myportals/MyCmd.java +++ b/MyPortals/src/cl/netgamer/myportals/MyCmd.java @@ -36,7 +36,7 @@ public class MyCmd implements CommandExecutor{ // METHODS - @Override + //@Override public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args){ if(cmd.getName().equalsIgnoreCase("portal")){ @@ -304,7 +304,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String alias, String // UTILITY - String msg(String key){ + public String msg(String key){ if (key.length() < 20 && lang.msg.containsKey(key)) return lang.msg.get(key); else return key; } diff --git a/MyPortals/src/cl/netgamer/myportals/MyListener.java b/MyPortals/src/cl/netgamer/myportals/MyListener.java index 4a46b3c..4beff51 100644 --- a/MyPortals/src/cl/netgamer/myportals/MyListener.java +++ b/MyPortals/src/cl/netgamer/myportals/MyListener.java @@ -1,13 +1,13 @@ package cl.netgamer.myportals; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.bukkit.Effect; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; @@ -20,9 +20,7 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockRedstoneEvent; import org.bukkit.event.entity.EntityExplodeEvent; -import org.bukkit.event.player.PlayerBucketEvent; import org.bukkit.event.player.PlayerBucketFillEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -109,11 +107,22 @@ public void onPlayerInteract(PlayerInteractEvent event){ @EventHandler public void onBlockPlace(BlockPlaceEvent event){ // check materials, from simple to complex, step by step + if(plugin.blockplacecooldown) return; + plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + public void run() { + plugin.blockplacecooldown = false; + } + }, 2L); + + //MyPortals.log("Placed Block: " + event.getBlock().getType().getId()); // check material of block placed, check only integer part is faster if (event.getBlock().getTypeId() != ((Number) chargeId).intValue()){ return; } + plugin.blockplacecooldown = true; + + //MyPortals.log("Block is charge"); // check material of block below if (event.getBlock().getLocation().clone().add(0, -1, 0).getBlock().getTypeId() != ((Number) baseId).intValue()){ @@ -121,14 +130,20 @@ public void onBlockPlace(BlockPlaceEvent event){ return; } + //MyPortals.log("Block is on base"); + // possible portal, get location we will use a lot Location loc = event.getBlock().getLocation().clone().add(0, -1, 0); int facing = plugin.shape.getFacing(loc); if (facing < 0) return; + //MyPortals.log("Facing = " + facing); + // looks like a portal, try to create if (!plugin.create(event.getPlayer(), loc, facing)) return; + //MyPortals.log("Looks like a portal"); + // add portal blocks, block -> base, base... for (Location l: plugin.shape.getPortalBlocks(loc, facing)){ if (!portalBlocks.containsKey(l)) portalBlocks.put(l, new ArrayList()); @@ -219,6 +234,7 @@ public void onPlayerMove(PlayerMoveEvent event){ @EventHandler public void onStepBlock(StepBlockEvent event){ // player move?, cancel warp task if exists + //MyPortals.log("Step"); if (warps.containsKey(event.getPlayer().getName())){ warps.remove(event.getPlayer().getName()); event.getPlayer().removePotionEffect(PotionEffectType.getById(9)); @@ -228,9 +244,10 @@ public void onStepBlock(StepBlockEvent event){ if (plugin.getPortalByLocation(event.getTo()) == null) return; WarpTask warp = new WarpTask(this, event.getPlayer(), event.getTo()); - warp.runTaskLater(plugin, 80); + warp.runTaskLater(plugin, MyPortals.waitTime); warps.put(event.getPlayer().getName(), warp.getTaskId()); event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.getById(9), 160, 1)); + event.getPlayer().playEffect(event.getPlayer().getEyeLocation(), Effect.ENDER_SIGNAL, 10); } // UTILITY diff --git a/MyPortals/src/cl/netgamer/myportals/MyPortals.java b/MyPortals/src/cl/netgamer/myportals/MyPortals.java index a0367b7..d10a75d 100644 --- a/MyPortals/src/cl/netgamer/myportals/MyPortals.java +++ b/MyPortals/src/cl/netgamer/myportals/MyPortals.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Bukkit; @@ -24,6 +25,7 @@ public static void log(String msg){ logger.info(msg); } + public static int waitTime; // PROPERTIES static boolean debug; @@ -36,15 +38,27 @@ public static void log(String msg){ MyCmd cmd; private String[] tags; + public boolean blockplacecooldown = false; + + private static Lang lang; + public static String msg(String key){ + if (key.length() < 20 && lang.msg.containsKey(key)) return lang.msg.get(key); + else return key; + } // ENABLE PLUGIN public void onEnable(){ this.saveDefaultConfig(); + if (getConfig().getBoolean("disabled")){ getLogger().info("MyPortals disabled (does nothing)"); return; } + + lang = new Lang(this, getConfig().getString("locale")); + + waitTime = getConfig().getInt("waitTime"); debug = getConfig().getBoolean("debug"); logger = getLogger(); @@ -60,6 +74,8 @@ public void onEnable(){ getCommand("portal").setExecutor(cmd); tags = cmd.tags; new MyListener(this, shape.getBaseId(), shape.getChargeId(), shape.getMaterials(), shape.getPortalsBlocks(portals)); + + logger.log(Level.INFO, Bukkit.getServer().getWorlds().toString()); } // PORTAL INFORMATION @@ -122,7 +138,13 @@ protected String info(Portal portal, String sender){ // PORTAL EVENTS protected boolean create(Player owner, Location baseLoc, int facing){ - if (!allowedWorlds.containsKey(baseLoc.getWorld().getName())) return false; + //MyPortals.log("Is this world (" + baseLoc.getWorld().getName() + ") Authorized?"); + //MyPortals.log("Well, the list of autorized world is " + allowedWorlds.toString()); + if (!allowedWorlds.containsKey(baseLoc.getWorld().getName())) { + //MyPortals.log("So, no."); + return false; + } + //MyPortals.log("So, yes."); //Portal portal = new Portal(baseLoc, owner, facing); Portal portal = new Portal(baseLoc, facing); data.savePortal(portal); @@ -168,7 +190,7 @@ protected String dest(Portal portal, String destName, Player player){ protected String setPrivacy(Portal portal, int privacy, Player player){ // try to change privacy - String status = setPrivacy(portal, privacy, player); + String status = portal.setPrivacy(privacy, player); if (status.equals("privacyOk")) data.savePortal(portal); return status; } @@ -262,7 +284,7 @@ static Location locationDecode(String data){ World w = null; for (String s: allowedWorlds.keySet()){ if (allowedWorlds.get(s).equals(d[0])){ - w = Bukkit.getWorld(s); + w = Bukkit.getServer().getWorld(s); break; } } diff --git a/MyPortals/src/cl/netgamer/myportals/Portal.java b/MyPortals/src/cl/netgamer/myportals/Portal.java index 48f3aa7..8150dbb 100644 --- a/MyPortals/src/cl/netgamer/myportals/Portal.java +++ b/MyPortals/src/cl/netgamer/myportals/Portal.java @@ -93,7 +93,7 @@ String setName(String portalName, Player player){ // can change name? if (!player.getName().equalsIgnoreCase(owner) && privacy > 0) return "locked"; // check name syntax - if (!name.matches("[a-zA-Z0-9]{1,12}")) return "invalidName"; + if (!portalName.matches("\\w{1,12}")) return "invalidName"; name = portalName; // on first naming give ownership if (owner.length() < 1) owner = player.getName(); diff --git a/MyPortals/src/cl/netgamer/myportals/Shape.java b/MyPortals/src/cl/netgamer/myportals/Shape.java index 0b16188..d7c8cb1 100644 --- a/MyPortals/src/cl/netgamer/myportals/Shape.java +++ b/MyPortals/src/cl/netgamer/myportals/Shape.java @@ -9,7 +9,7 @@ import java.util.Map.Entry; import org.bukkit.Location; // 179 -import org.bukkit.craftbukkit.v1_7_R3.CraftWorld; +//import org.bukkit.craftbukkit.v1_6_R3.CraftWorld; // 1710 //import org.bukkit.craftbukkit.v1_7_R4.CraftWorld; @@ -226,10 +226,10 @@ class ShapeReplace implements ShapeInterface{ public boolean use(Location loc, Number ref, Location base){ // if replacing by red lamp on if (ref.intValue() == 124){ - boolean Static = ((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic; - ((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic = true; + //boolean Static = ((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic; + //((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic = true; loc.getBlock().setTypeId(124); - ((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic = Static; + //((CraftWorld) loc.getBlock().getWorld()).getHandle().isStatic = Static; return true; } loc.getBlock().setTypeIdAndData(ref.intValue(), (byte) ((ref.doubleValue() % 1) * 100), false); diff --git a/MyPortals/src/cl/netgamer/myportals/WarpTask.java b/MyPortals/src/cl/netgamer/myportals/WarpTask.java index de87c1e..bc85e6c 100644 --- a/MyPortals/src/cl/netgamer/myportals/WarpTask.java +++ b/MyPortals/src/cl/netgamer/myportals/WarpTask.java @@ -5,7 +5,7 @@ import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitRunnable; -public class WarpTask extends BukkitRunnable{ +public class WarpTask extends BukkitRunnable { private MyListener parent; private Player player; @@ -18,17 +18,13 @@ public WarpTask(MyListener parent, Player player, Location location){ } // this runs every tick? - @Override + //@Override public void run(){ // canceled? if (!parent.warps.containsValue(this.getTaskId())) return; // remove nausea effect before player.removePotionEffect(PotionEffectType.getById(9)); - - // player has enough experience points? - int xp = player.getTotalExperience(); - if (xp < parent.plugin.xpCost) return; // source portal still exists? Portal portal = parent.plugin.getPortalByLocation(location); @@ -38,6 +34,13 @@ public void run(){ portal = parent.plugin.getPortalByLocation(portal.getDestination()); if (portal == null) return; + // player has enough experience points? + int xp = player.getTotalExperience(); + if (xp < parent.plugin.xpCost) { + player.sendMessage(MyPortals.msg("notEnoughXP")); + return; + } + // allow teleport? //if (!portal.canWarp(player)) return; diff --git a/MyPortals/src/cl/netgamer/tabtext/TabText.java b/MyPortals/src/cl/netgamer/tabtext/TabText.java index 2016fa5..3cb2bd3 100644 --- a/MyPortals/src/cl/netgamer/tabtext/TabText.java +++ b/MyPortals/src/cl/netgamer/tabtext/TabText.java @@ -18,7 +18,7 @@ * * - // example * - multilineString = "PLAYER------`RATE------`RANK------\n"; - * - multilineString += "§EJohn`10.01`1§R\n"; + * - multilineString += "�EJohn`10.01`1�R\n"; * - multilineString += "Doe`-9.30`2"; * * - TabText tt = new TabText(multilineString); @@ -40,7 +40,7 @@ public class TabText{ private int numPages; private String[] lines; private Map charList = new HashMap(){{ - put(-6, "§"); + put(-6, "�"); put(2, "!.,:;i|"); put(3, "'`l"); put(4, " I[]t"); @@ -147,7 +147,7 @@ public String getPage(int page, boolean monospace){ tab = (monospace)? tabs[fieldPos]: tabs[fieldPos] * 6; field = pxSubstring(fields[fieldPos], tab, monospace); line += (String)field[0]; - lineLen += (int)field[1]; + lineLen += (Integer)field[1]; lineLen2 += tab; } lines2 += (lines2.length() < 1)? line: '\n'+line; @@ -186,7 +186,7 @@ private Object[] pxSubstring(String str, int len, boolean mono){ * @return character width depending of "mono" */ private int pxLen(char ch, boolean mono){ - if (mono) return (ch == '§')? -1: 1; + if (mono) return (ch == '�')? -1: 1; // character list iteration, 6 = default int l = 6; for (int px: charList.keySet()){ @@ -225,7 +225,7 @@ public void sortByFields(int... keys){ for (int i = 0; i < lines.length; ++i){ // iterate fields - fields = lines[i].replaceAll("§.", "").split("`", -1); + fields = lines[i].replaceAll("�.", "").split("`", -1); line = ""; for (int j = 0; j < keys.length; ++j){ From cc79a06b11f0bc9ca6f2eee5eedbfe5b8f9f6074 Mon Sep 17 00:00:00 2001 From: Pierce Corcoran Date: Mon, 22 Sep 2014 22:28:10 -0700 Subject: [PATCH 2/2] More Bug fixes and portal compass Fixed Pistons not breaking portals Not enough Exp now plays sound Right clicking a portal base with a compass points it to the destination portal Shift right clicking with a compass resets it to the spawn point --- .../src/cl/netgamer/myportals/MyListener.java | 60 +++++++++++++++---- .../src/cl/netgamer/myportals/Portal.java | 4 ++ .../src/cl/netgamer/myportals/WarpTask.java | 5 +- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/MyPortals/src/cl/netgamer/myportals/MyListener.java b/MyPortals/src/cl/netgamer/myportals/MyListener.java index 4beff51..e6d7c78 100644 --- a/MyPortals/src/cl/netgamer/myportals/MyListener.java +++ b/MyPortals/src/cl/netgamer/myportals/MyListener.java @@ -7,20 +7,25 @@ import java.util.Map; import java.util.Map.Entry; +import org.bukkit.ChatColor; import org.bukkit.Effect; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBurnEvent; import org.bukkit.event.block.BlockCanBuildEvent; import org.bukkit.event.block.BlockFromToEvent; import org.bukkit.event.block.BlockPhysicsEvent; +import org.bukkit.event.block.BlockPistonExtendEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockRedstoneEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerBucketFillEvent; +import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -194,24 +199,34 @@ public void onBlockCanBuild(BlockCanBuildEvent event){ // on grab water portal with bucket @EventHandler public void onPlayerBucketFill(PlayerBucketFillEvent event){ - checkPortalDestroyed(event.getBlockClicked()); + if(isPartOfPortal(event.getBlockClicked())) { + event.setCancelled(true); + //checkPortalDestroyed(event.getBlockClicked()); + } } // POSSIBLE PORTAL BLOCK DELETED - // on block event, check portal shape - private void checkPortalDestroyed(Block block){ - // was portal material? - if (!(materials.contains(block.getTypeId()))){ - return; + private boolean isPartOfPortal(Block block) { + // was portal material? + if (materials.contains(block.getTypeId())){ + return true; } // belonged some portal? - if (!(portalBlocks.containsKey(block.getLocation()))){ - return; + if (portalBlocks.containsKey(block.getLocation())){ + return true; } + return false; + } + + // on block event, check portal shape + private void checkPortalDestroyed(Block block){ + + if(!isPartOfPortal(block)) return; + // what portals belonged to? (clone to avoid removing while iterating problems) ArrayList ofPortals = (ArrayList)(portalBlocks.get(block.getLocation())).clone(); @@ -237,7 +252,7 @@ public void onStepBlock(StepBlockEvent event){ //MyPortals.log("Step"); if (warps.containsKey(event.getPlayer().getName())){ warps.remove(event.getPlayer().getName()); - event.getPlayer().removePotionEffect(PotionEffectType.getById(9)); + event.getPlayer().removePotionEffect(PotionEffectType.CONFUSION); } // portal enter?: create warp task, schedule, store id and play nausea effect @@ -246,10 +261,35 @@ public void onStepBlock(StepBlockEvent event){ WarpTask warp = new WarpTask(this, event.getPlayer(), event.getTo()); warp.runTaskLater(plugin, MyPortals.waitTime); warps.put(event.getPlayer().getName(), warp.getTaskId()); - event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.getById(9), 160, 1)); + event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 160, 1)); event.getPlayer().playEffect(event.getPlayer().getEyeLocation(), Effect.ENDER_SIGNAL, 10); } + @EventHandler + public void onPistonExtend(BlockPistonExtendEvent event) { + for(Block block : event.getBlocks()) { + checkPortalDestroyed(block); + } + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + Portal p = plugin.getPortalByLocation(event.getClickedBlock().getLocation()); + if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getItem().getType() == Material.COMPASS) { + if (p != null) { + if (p.canWarp(event.getPlayer())) return; + Location dest = p.getDestination(); + if (dest != null) { + event.getPlayer().setCompassTarget(dest); + } + } + if (event.getPlayer().isSneaking()) { + Location spawn = event.getPlayer().getWorld().getSpawnLocation(); + event.getPlayer().setCompassTarget(spawn); + } + } + } + // UTILITY /** with iterator to avoid remove while iterating problems */ diff --git a/MyPortals/src/cl/netgamer/myportals/Portal.java b/MyPortals/src/cl/netgamer/myportals/Portal.java index 8150dbb..7822ec0 100644 --- a/MyPortals/src/cl/netgamer/myportals/Portal.java +++ b/MyPortals/src/cl/netgamer/myportals/Portal.java @@ -133,6 +133,10 @@ String give(Player currentOwner, String recipient){ return "giveOk"; } + public boolean canWarp(Player player) { + return player.getName().equalsIgnoreCase(getOwner()) && privacy > 2; + } + boolean warp(Player player){ // allow teleport? //if (!canWarp(player)) return false; diff --git a/MyPortals/src/cl/netgamer/myportals/WarpTask.java b/MyPortals/src/cl/netgamer/myportals/WarpTask.java index bc85e6c..2c4bf55 100644 --- a/MyPortals/src/cl/netgamer/myportals/WarpTask.java +++ b/MyPortals/src/cl/netgamer/myportals/WarpTask.java @@ -1,6 +1,7 @@ package cl.netgamer.myportals; import org.bukkit.Location; +import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitRunnable; @@ -24,7 +25,7 @@ public void run(){ if (!parent.warps.containsValue(this.getTaskId())) return; // remove nausea effect before - player.removePotionEffect(PotionEffectType.getById(9)); + player.removePotionEffect(PotionEffectType.CONFUSION); // source portal still exists? Portal portal = parent.plugin.getPortalByLocation(location); @@ -37,7 +38,7 @@ public void run(){ // player has enough experience points? int xp = player.getTotalExperience(); if (xp < parent.plugin.xpCost) { - player.sendMessage(MyPortals.msg("notEnoughXP")); + player.playSound(player.getLocation(), Sound.VILLAGER_NO, 5, 0); return; }