From e07241d4d7a77c8d19aa933b813087a9c882faf4 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 5 Mar 2013 09:52:08 +0100 Subject: [PATCH] Adding in an event on PS teleportation. --- .../event/MCorePlayerPSTeleportEvent.java | 61 +++++++++++++++++++ .../mcore/mixin/TeleportMixinDefault.java | 9 ++- .../massivecraft/mcore/util/TimeDiffUtil.java | 17 ++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/com/massivecraft/mcore/event/MCorePlayerPSTeleportEvent.java diff --git a/src/com/massivecraft/mcore/event/MCorePlayerPSTeleportEvent.java b/src/com/massivecraft/mcore/event/MCorePlayerPSTeleportEvent.java new file mode 100644 index 00000000..81652d25 --- /dev/null +++ b/src/com/massivecraft/mcore/event/MCorePlayerPSTeleportEvent.java @@ -0,0 +1,61 @@ +package com.massivecraft.mcore.event; + +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import com.massivecraft.mcore.PS; + +public class MCorePlayerPSTeleportEvent extends Event implements Cancellable, Runnable +{ + // -------------------------------------------- // + // REQUIRED EVENT CODE + // -------------------------------------------- // + + private static final HandlerList handlers = new HandlerList(); + @Override public HandlerList getHandlers() { return handlers; } + public static HandlerList getHandlerList() { return handlers; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private boolean cancelled; + @Override public boolean isCancelled() { return this.cancelled; } + @Override public void setCancelled(boolean cancelled) { this.cancelled = cancelled; } + + private final Player player; + public Player getPlayer() { return this.player; } + + private final Location from; + public Location getFrom() { return this.from; } + + private PS to; + public PS getTo() { return this.to; } + public void setTo(PS to) { this.to = to; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public MCorePlayerPSTeleportEvent(Player player, Location from, PS to) + { + this.player = player; + this.from = from; + this.to = to; + } + + // -------------------------------------------- // + // RUN + // -------------------------------------------- // + + @Override + public void run() + { + Bukkit.getPluginManager().callEvent(this); + } + +} diff --git a/src/com/massivecraft/mcore/mixin/TeleportMixinDefault.java b/src/com/massivecraft/mcore/mixin/TeleportMixinDefault.java index 87e8a114..3e915f30 100644 --- a/src/com/massivecraft/mcore/mixin/TeleportMixinDefault.java +++ b/src/com/massivecraft/mcore/mixin/TeleportMixinDefault.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import org.bukkit.util.Vector; import com.massivecraft.mcore.PS; +import com.massivecraft.mcore.event.MCorePlayerPSTeleportEvent; import com.massivecraft.mcore.util.SenderUtil; import com.massivecraft.mcore.util.Txt; @@ -24,7 +25,6 @@ public class TeleportMixinDefault extends TeleportMixinAbstract public static void teleportEntity(Entity entity, PS ps) throws TeleporterException { - // Use a local clone of the ps to avoid altering original ps = ps.clone(); // Ensure the ps has a world name @@ -58,6 +58,13 @@ public class TeleportMixinDefault extends TeleportMixinAbstract } else { + // Run event + MCorePlayerPSTeleportEvent event = new MCorePlayerPSTeleportEvent(teleportee, teleportee.getLocation(), destinationPs.clone()); + event.run(); + if (event.isCancelled()) return; + if (event.getTo() == null) return; + destinationPs = event.getTo().clone(); + teleportEntity(teleportee, destinationPs); } } diff --git a/src/com/massivecraft/mcore/util/TimeDiffUtil.java b/src/com/massivecraft/mcore/util/TimeDiffUtil.java index a9bf2735..3fd0ced8 100644 --- a/src/com/massivecraft/mcore/util/TimeDiffUtil.java +++ b/src/com/massivecraft/mcore/util/TimeDiffUtil.java @@ -1,6 +1,7 @@ package com.massivecraft.mcore.util; import java.util.ArrayList; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -128,6 +129,22 @@ public class TimeDiffUtil return unitcounts(millis, TimeUnit.getAll()); } + public static LinkedHashMap limit(LinkedHashMap unitcounts, int limit) + { + LinkedHashMap ret = new LinkedHashMap(); + + Iterator> iter = unitcounts.entrySet().iterator(); + int i = 0; + while (iter.hasNext() && i < limit) + { + Entry entry = iter.next(); + ret.put(entry.getKey(), entry.getValue()); + i++; + } + + return ret; + } + // -------------------------------------------- // // FORMAT // -------------------------------------------- //