From 9587b756c93bb91a927ac481b3a7ae416dad5f62 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 22 May 2015 14:17:30 +0200 Subject: [PATCH] Changes to the PS Destination teleporter system. --- .../MassiveCoreEngineDestination.java | 2 +- .../massivecore/PriorityComparator.java | 30 ++++++++++---- .../massivecore/cmd/arg/ARDestination.java | 3 +- .../mixin/TeleportMixinDefault.java | 14 +++++-- .../massivecore/teleport/Destination.java | 3 +- .../teleport/DestinationAbstract.java | 41 ++++++++++++++++++- .../massivecore/teleport/DestinationJump.java | 2 +- .../teleport/DestinationPlayer.java | 2 +- .../teleport/DestinationSimple.java | 2 +- .../massivecore/teleport/DestinationThat.java | 2 +- .../teleport/DestinationThere.java | 2 +- .../massivecore/teleport/DestinationTop.java | 2 +- .../teleport/DestinationWorld.java | 2 +- .../massivecore/util/InventoryUtil.java | 1 + 14 files changed, 84 insertions(+), 24 deletions(-) diff --git a/src/com/massivecraft/massivecore/MassiveCoreEngineDestination.java b/src/com/massivecraft/massivecore/MassiveCoreEngineDestination.java index 68384658..dbe7e306 100644 --- a/src/com/massivecraft/massivecore/MassiveCoreEngineDestination.java +++ b/src/com/massivecraft/massivecore/MassiveCoreEngineDestination.java @@ -95,7 +95,7 @@ public class MassiveCoreEngineDestination extends EngineAbstract public static final Set ALIASES_THERE = new MassiveSet("there"); public static final Set ALIASES_THAT = new MassiveSet("that"); public static final Set ALIASES_JUMP = new MassiveSet("jump"); - public static final Set ALIASES_WORLD = new MassiveSet("w", "world", "worldspawn", "wspawn"); + public static final Set ALIASES_WORLD = new MassiveSet("w", "world", "spawn", "wspawn", "worldspawn"); public static final Set ALIASES_PLAYER = new MassiveSet("p", "player", "here", "me", "self"); public Destination destinationArg(String arg, CommandSender sender) throws MassiveException diff --git a/src/com/massivecraft/massivecore/PriorityComparator.java b/src/com/massivecraft/massivecore/PriorityComparator.java index 58c76704..40f8c859 100644 --- a/src/com/massivecraft/massivecore/PriorityComparator.java +++ b/src/com/massivecraft/massivecore/PriorityComparator.java @@ -2,6 +2,9 @@ package com.massivecraft.massivecore; import java.util.Comparator; +import com.massivecraft.massivecore.store.ComparatorEntityId; +import com.massivecraft.massivecore.store.Entity; + public class PriorityComparator implements Comparator { // -------------------------------------------- // @@ -16,21 +19,30 @@ public class PriorityComparator implements Comparator // -------------------------------------------- // @Override - public int compare(Prioritized one, Prioritized two) + public int compare(Prioritized p1, Prioritized p2) { - if (one == null && two == null) return 0; - if (two == null) return 1; - if (one == null) return -1; + // Null + if (p1 == null && p2 == null) return 0; + if (p1 == null) return -1; + if (p2 == null) return +1; - int ret = Integer.valueOf(one.getPriority()).compareTo(two.getPriority()); + // Equals + if (p1.equals(p2)) return 0; - // We should only return 0 if the items actually are equal. - if (ret == 0 && ! one.equals(two)) + // Priority + int ret = Integer.compare(p1.getPriority(), p2.getPriority()); + if (ret != 0) return ret; + + // Entity Id + if (p1 instanceof Entity && p2 instanceof Entity) { - ret = two.hashCode() - one.hashCode(); + Entity e1 = (Entity)p1; + Entity e2 = (Entity)p2; + return ComparatorEntityId.get().compare(e1, e2); } - return ret; + // We should only return 0 if the items actually are equal. + return p2.hashCode() - p1.hashCode(); } } diff --git a/src/com/massivecraft/massivecore/cmd/arg/ARDestination.java b/src/com/massivecraft/massivecore/cmd/arg/ARDestination.java index fd709b12..49b29cf7 100644 --- a/src/com/massivecraft/massivecore/cmd/arg/ARDestination.java +++ b/src/com/massivecraft/massivecore/cmd/arg/ARDestination.java @@ -33,7 +33,8 @@ public class ARDestination extends ARAbstract Destination ret = event.getDestination(); if (ret == null) throw new MassiveException().addMsg("Unknown destination \"%s\".", arg); - if ( ! ret.hasPs()) throw new MassiveException().addMessage(ret.getMessagePsNull(sender)); + // Throw exeption if ps is null. + ret.getPs(sender); return ret; } diff --git a/src/com/massivecraft/massivecore/mixin/TeleportMixinDefault.java b/src/com/massivecraft/massivecore/mixin/TeleportMixinDefault.java index 10ae5332..1148ac5f 100644 --- a/src/com/massivecraft/massivecore/mixin/TeleportMixinDefault.java +++ b/src/com/massivecraft/massivecore/mixin/TeleportMixinDefault.java @@ -76,7 +76,15 @@ public class TeleportMixinDefault extends TeleportMixinAbstract String teleporteeId = IdUtil.getId(teleporteeObject); if (!IdUtil.isPlayerId(teleporteeId)) throw new TeleporterException(Txt.parse("%s is not a player.", Mixin.getDisplayName(teleporteeId, IdUtil.getConsole()))); - if ( ! destination.hasPs()) throw new TeleporterException(destination.getMessagePsNull(teleporteeId)); + PS ps; + try + { + ps = destination.getPs(teleporteeId); + } + catch (Exception e) + { + throw new TeleporterException(e.getMessage()); + } String desc = destination.getDesc(teleporteeId); if (delaySeconds > 0) @@ -112,11 +120,11 @@ public class TeleportMixinDefault extends TeleportMixinAbstract Player teleportee = IdUtil.getPlayer(teleporteeId); if (teleportee != null) { - teleportPlayer(teleportee, destination.getPs()); + teleportPlayer(teleportee, ps); } else { - Mixin.setSenderPs(teleporteeId, destination.getPs()); + Mixin.setSenderPs(teleporteeId, ps); } } } diff --git a/src/com/massivecraft/massivecore/teleport/Destination.java b/src/com/massivecraft/massivecore/teleport/Destination.java index 0ac48a13..a17e337e 100644 --- a/src/com/massivecraft/massivecore/teleport/Destination.java +++ b/src/com/massivecraft/massivecore/teleport/Destination.java @@ -2,11 +2,12 @@ package com.massivecraft.massivecore.teleport; import java.io.Serializable; +import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.ps.PS; public interface Destination extends Serializable { - public PS getPs(); + public PS getPs(Object watcherObject) throws MassiveException; public boolean hasPs(); public String getMessagePsNull(Object watcherObject); diff --git a/src/com/massivecraft/massivecore/teleport/DestinationAbstract.java b/src/com/massivecraft/massivecore/teleport/DestinationAbstract.java index fa8b67e5..badcfb6b 100644 --- a/src/com/massivecraft/massivecore/teleport/DestinationAbstract.java +++ b/src/com/massivecraft/massivecore/teleport/DestinationAbstract.java @@ -1,5 +1,7 @@ package com.massivecraft.massivecore.teleport; +import com.massivecraft.massivecore.MassiveException; +import com.massivecraft.massivecore.ps.PS; import com.massivecraft.massivecore.ps.PSFormatHumanSpace; import com.massivecraft.massivecore.util.Txt; @@ -13,14 +15,41 @@ public abstract class DestinationAbstract implements Destination protected String desc = null; + // -------------------------------------------- // + // ABSTRACT + // -------------------------------------------- // + + public PS getPsInner() + { + return null; + } + // -------------------------------------------- // // OVERRIDE // -------------------------------------------- // + @Override + public PS getPs(Object watcherObject) throws MassiveException + { + PS ret = this.getPsInner(); + if (ret == null) + { + throw new MassiveException().addMessage(this.getMessagePsNull(watcherObject)); + } + return ret; + } + @Override public boolean hasPs() { - return this.getPs() != null; + try + { + return this.getPs(null) != null; + } + catch (MassiveException e) + { + return false; + } } @Override @@ -34,7 +63,15 @@ public abstract class DestinationAbstract implements Destination public String getDesc(Object watcherObject) { if (this.desc != null) return this.desc; - return PSFormatHumanSpace.get().format(this.getPs()); + try + { + PS ps = this.getPs(watcherObject); + return PSFormatHumanSpace.get().format(ps); + } + catch (MassiveException e) + { + return "null"; + } } @Override diff --git a/src/com/massivecraft/massivecore/teleport/DestinationJump.java b/src/com/massivecraft/massivecore/teleport/DestinationJump.java index dc2a1f7a..c7e0bddc 100644 --- a/src/com/massivecraft/massivecore/teleport/DestinationJump.java +++ b/src/com/massivecraft/massivecore/teleport/DestinationJump.java @@ -28,7 +28,7 @@ public class DestinationJump extends DestinationPlayer // -------------------------------------------- // @Override - public PS getPs() + public PS getPsInner() { Player player = this.getPlayer(); if (player == null) return null; diff --git a/src/com/massivecraft/massivecore/teleport/DestinationPlayer.java b/src/com/massivecraft/massivecore/teleport/DestinationPlayer.java index 9f7d64db..236906d7 100644 --- a/src/com/massivecraft/massivecore/teleport/DestinationPlayer.java +++ b/src/com/massivecraft/massivecore/teleport/DestinationPlayer.java @@ -44,7 +44,7 @@ public class DestinationPlayer extends DestinationAbstract // -------------------------------------------- // @Override - public PS getPs() + public PS getPsInner() { return Mixin.getSenderPs(this.playerId); } diff --git a/src/com/massivecraft/massivecore/teleport/DestinationSimple.java b/src/com/massivecraft/massivecore/teleport/DestinationSimple.java index 3f43f648..92584157 100644 --- a/src/com/massivecraft/massivecore/teleport/DestinationSimple.java +++ b/src/com/massivecraft/massivecore/teleport/DestinationSimple.java @@ -38,7 +38,7 @@ public class DestinationSimple extends DestinationAbstract // -------------------------------------------- // @Override - public PS getPs() + public PS getPsInner() { return this.ps; } diff --git a/src/com/massivecraft/massivecore/teleport/DestinationThat.java b/src/com/massivecraft/massivecore/teleport/DestinationThat.java index 8a6f4cc8..37ad10cc 100644 --- a/src/com/massivecraft/massivecore/teleport/DestinationThat.java +++ b/src/com/massivecraft/massivecore/teleport/DestinationThat.java @@ -28,7 +28,7 @@ public class DestinationThat extends DestinationPlayer // -------------------------------------------- // @Override - public PS getPs() + public PS getPsInner() { Player player = this.getPlayer(); if (player == null) return null; diff --git a/src/com/massivecraft/massivecore/teleport/DestinationThere.java b/src/com/massivecraft/massivecore/teleport/DestinationThere.java index bf9a4d0d..3cbb32fd 100644 --- a/src/com/massivecraft/massivecore/teleport/DestinationThere.java +++ b/src/com/massivecraft/massivecore/teleport/DestinationThere.java @@ -28,7 +28,7 @@ public class DestinationThere extends DestinationPlayer // -------------------------------------------- // @Override - public PS getPs() + public PS getPsInner() { Player player = this.getPlayer(); if (player == null) return null; diff --git a/src/com/massivecraft/massivecore/teleport/DestinationTop.java b/src/com/massivecraft/massivecore/teleport/DestinationTop.java index 782ab223..32989c37 100644 --- a/src/com/massivecraft/massivecore/teleport/DestinationTop.java +++ b/src/com/massivecraft/massivecore/teleport/DestinationTop.java @@ -28,7 +28,7 @@ public class DestinationTop extends DestinationPlayer // -------------------------------------------- // @Override - public PS getPs() + public PS getPsInner() { Player player = this.getPlayer(); if (player == null) return null; diff --git a/src/com/massivecraft/massivecore/teleport/DestinationWorld.java b/src/com/massivecraft/massivecore/teleport/DestinationWorld.java index 445b29a4..0bc71567 100644 --- a/src/com/massivecraft/massivecore/teleport/DestinationWorld.java +++ b/src/com/massivecraft/massivecore/teleport/DestinationWorld.java @@ -55,7 +55,7 @@ public class DestinationWorld extends DestinationAbstract // -------------------------------------------- // @Override - public PS getPs() + public PS getPsInner() { String worldId = this.getWorldId(); if (worldId == null) return null; diff --git a/src/com/massivecraft/massivecore/util/InventoryUtil.java b/src/com/massivecraft/massivecore/util/InventoryUtil.java index 7aae1f5b..9f945d26 100644 --- a/src/com/massivecraft/massivecore/util/InventoryUtil.java +++ b/src/com/massivecraft/massivecore/util/InventoryUtil.java @@ -292,6 +292,7 @@ public class InventoryUtil public static ItemStack cloneItemStack(ItemStack itemStack) { + if (itemStack == null) return null; return new ItemStack(itemStack); }