Adding in an event on PS teleportation.

This commit is contained in:
Olof Larsson 2013-03-05 09:52:08 +01:00
parent 2543fc306e
commit e07241d4d7
3 changed files with 86 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -6,6 +6,7 @@ import org.bukkit.entity.Player;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import com.massivecraft.mcore.PS; import com.massivecraft.mcore.PS;
import com.massivecraft.mcore.event.MCorePlayerPSTeleportEvent;
import com.massivecraft.mcore.util.SenderUtil; import com.massivecraft.mcore.util.SenderUtil;
import com.massivecraft.mcore.util.Txt; 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 public static void teleportEntity(Entity entity, PS ps) throws TeleporterException
{ {
// Use a local clone of the ps to avoid altering original
ps = ps.clone(); ps = ps.clone();
// Ensure the ps has a world name // Ensure the ps has a world name
@ -58,6 +58,13 @@ public class TeleportMixinDefault extends TeleportMixinAbstract
} }
else 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); teleportEntity(teleportee, destinationPs);
} }
} }

View File

@ -1,6 +1,7 @@
package com.massivecraft.mcore.util; package com.massivecraft.mcore.util;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -128,6 +129,22 @@ public class TimeDiffUtil
return unitcounts(millis, TimeUnit.getAll()); return unitcounts(millis, TimeUnit.getAll());
} }
public static LinkedHashMap<TimeUnit, Long> limit(LinkedHashMap<TimeUnit, Long> unitcounts, int limit)
{
LinkedHashMap<TimeUnit, Long> ret = new LinkedHashMap<TimeUnit, Long>();
Iterator<Entry<TimeUnit, Long>> iter = unitcounts.entrySet().iterator();
int i = 0;
while (iter.hasNext() && i < limit)
{
Entry<TimeUnit, Long> entry = iter.next();
ret.put(entry.getKey(), entry.getValue());
i++;
}
return ret;
}
// -------------------------------------------- // // -------------------------------------------- //
// FORMAT // FORMAT
// -------------------------------------------- // // -------------------------------------------- //