From 6f7b10bfb0c3135ad44cac79267ff4382a7782a1 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Thu, 18 Apr 2013 14:17:07 +0200 Subject: [PATCH] Some random additions. Factions stuff that fit into the STD. --- src/com/massivecraft/mcore/MCore.java | 3 +- .../massivecraft/mcore/ModuloRepeatTask.java | 79 ++++++++++++++++++ .../mcore/WorldNameSetEngine.java | 63 ++++++++++++++ src/com/massivecraft/mcore/ps/PS.java | 10 +++ .../mcore/util/FirstTeleportUtil.java | 82 ------------------- src/com/massivecraft/mcore/util/MUtil.java | 28 ++++++- 6 files changed, 178 insertions(+), 87 deletions(-) create mode 100644 src/com/massivecraft/mcore/ModuloRepeatTask.java create mode 100644 src/com/massivecraft/mcore/WorldNameSetEngine.java delete mode 100644 src/com/massivecraft/mcore/util/FirstTeleportUtil.java diff --git a/src/com/massivecraft/mcore/MCore.java b/src/com/massivecraft/mcore/MCore.java index c92a6e32..ba48add4 100644 --- a/src/com/massivecraft/mcore/MCore.java +++ b/src/com/massivecraft/mcore/MCore.java @@ -31,7 +31,6 @@ import com.massivecraft.mcore.store.MStore; import com.massivecraft.mcore.usys.AspectColl; import com.massivecraft.mcore.usys.MultiverseColl; import com.massivecraft.mcore.usys.cmd.CmdUsys; -import com.massivecraft.mcore.util.FirstTeleportUtil; import com.massivecraft.mcore.util.PlayerUtil; import com.massivecraft.mcore.util.TimeDiffUtil; import com.massivecraft.mcore.util.TimeUnit; @@ -128,8 +127,8 @@ public class MCore extends MPlugin // Register events InternalListener.get().setup(); ScheduledTeleportEngine.get().setup(); - FirstTeleportUtil.get().setup(); TeleportMixinCauseEngine.get().setup(); + WorldNameSetEngine.get().setup(); // Schedule the collection ticker. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this.collTickTask, 1, 1); diff --git a/src/com/massivecraft/mcore/ModuloRepeatTask.java b/src/com/massivecraft/mcore/ModuloRepeatTask.java new file mode 100644 index 00000000..4a253e00 --- /dev/null +++ b/src/com/massivecraft/mcore/ModuloRepeatTask.java @@ -0,0 +1,79 @@ +package com.massivecraft.mcore; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.Plugin; + +/** + * This class will allow you to create non-tps-dependent repeating tasks. + * It makes use of the Bukkit scheduler internally. + */ +public abstract class ModuloRepeatTask implements Runnable +{ + // -------------------------------------------- // + // FIELDS: RAW + // -------------------------------------------- // + + // How many milliseconds should approximately pass between each invocation? + private long delayMillis; + public long getDelayMillis() { return this.delayMillis; } + public void setDelayMillis(long delayMillis) { this.delayMillis = delayMillis; } + + // When did the last invocation occur? + private long previousMillis; + public long getPreviousMillis() { return this.previousMillis; } + public void setPreviousMillis(long previousMillis) { this.previousMillis = previousMillis; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public ModuloRepeatTask() + { + this(0); + } + + public ModuloRepeatTask(long delayMilis) + { + this(delayMilis, System.currentTimeMillis()); + } + + public ModuloRepeatTask(long delayMilis, long previousMillis) + { + this.delayMillis = delayMilis; + this.previousMillis = previousMillis; + } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public void run() + { + long now = System.currentTimeMillis(); + long nowInvocationNumber = now / this.getDelayMillis(); + long previousInvocationNumber = this.getPreviousMillis() / this.getDelayMillis(); + + if (nowInvocationNumber == previousInvocationNumber) return; + + this.invoke(); + + this.setPreviousMillis(now); + } + + // -------------------------------------------- // + // EIGEN + // -------------------------------------------- // + + public int schedule(Plugin plugin) + { + return Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this, 1, 1); + } + + // -------------------------------------------- // + // ABSTRACT + // -------------------------------------------- // + + public abstract void invoke(); + +} diff --git a/src/com/massivecraft/mcore/WorldNameSetEngine.java b/src/com/massivecraft/mcore/WorldNameSetEngine.java new file mode 100644 index 00000000..62e091c6 --- /dev/null +++ b/src/com/massivecraft/mcore/WorldNameSetEngine.java @@ -0,0 +1,63 @@ +package com.massivecraft.mcore; + +import java.util.Collections; +import java.util.Set; +import java.util.TreeSet; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.event.world.WorldUnloadEvent; + +public class WorldNameSetEngine implements Listener +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static WorldNameSetEngine i = new WorldNameSetEngine(); + public static WorldNameSetEngine get() { return i; } + + // -------------------------------------------- // + // SETUP + // -------------------------------------------- // + + public void setup() + { + this.worldNamesInner.clear(); + for (World world : Bukkit.getWorlds()) + { + this.worldNamesInner.add(world.getName()); + } + + Bukkit.getPluginManager().registerEvents(this, MCore.get()); + } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final TreeSet worldNamesInner = new TreeSet(String.CASE_INSENSITIVE_ORDER); + private final Set worldNamesOuter = Collections.unmodifiableSet(this.worldNamesInner); + public Set getWorldNames() { return this.worldNamesOuter; } + + // -------------------------------------------- // + // LISTENER + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.NORMAL) + public void onWorldLoad(WorldLoadEvent event) + { + this.worldNamesInner.add(event.getWorld().getName()); + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onWorldUnload(WorldUnloadEvent event) + { + this.worldNamesInner.remove(event.getWorld().getName()); + } + +} diff --git a/src/com/massivecraft/mcore/ps/PS.java b/src/com/massivecraft/mcore/ps/PS.java index dd1af413..6e9c2616 100644 --- a/src/com/massivecraft/mcore/ps/PS.java +++ b/src/com/massivecraft/mcore/ps/PS.java @@ -227,6 +227,16 @@ public final class PS implements Cloneable, Serializable, Comparable return builder.build(); } + // -------------------------------------------- // + // FIELDS: IS + // -------------------------------------------- // + + public boolean isWorldLoadedOnThisServer() + { + if (this.world == null) return true; + return MUtil.getLoadedWorldNames().contains(this.world); + } + // -------------------------------------------- // // PRIVATE CONSTRUCTOR // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore/util/FirstTeleportUtil.java b/src/com/massivecraft/mcore/util/FirstTeleportUtil.java deleted file mode 100644 index 8aac8d5b..00000000 --- a/src/com/massivecraft/mcore/util/FirstTeleportUtil.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.massivecraft.mcore.util; - -import java.util.HashMap; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerLoginEvent; -import org.bukkit.event.player.PlayerTeleportEvent; - -import com.massivecraft.mcore.MCore; - -/** - * The first teleport to ever occur for a player happens soon after the player logged in. - * This is a system one that should not be cancelled since it happens as the player is assigned their entrance position. - * Altering the to-location is possible but altering the world will not matter. Only x, y, z, pitch and yaw are taken into consideration. - * This tool can be used to check if a PlayerTeleportEvent is the first one for the player. - */ -public class FirstTeleportUtil implements Listener -{ - // -------------------------------------------- // - // INSTANCE & CONSTRUCT - // -------------------------------------------- // - - private static FirstTeleportUtil i = new FirstTeleportUtil(); - public static FirstTeleportUtil get() { return i; } - - // -------------------------------------------- // - // STATIC - // -------------------------------------------- // - - private Map playerToFirstTeleport; - - public static boolean isFirstTeleport(PlayerTeleportEvent event) - { - Player player = event.getPlayer(); - - PlayerTeleportEvent stored = i.playerToFirstTeleport.get(player); - if (stored == null) - { - i.playerToFirstTeleport.put(player, event); - return true; - } - else - { - return stored == event; - } - } - - // -------------------------------------------- // - // SETUP - // -------------------------------------------- // - - public void setup() - { - playerToFirstTeleport = new HashMap(); - Bukkit.getPluginManager().registerEvents(this, MCore.get()); - } - - // -------------------------------------------- // - // LISTENER - // -------------------------------------------- // - - @EventHandler(priority = EventPriority.MONITOR) - public void onPlayerLoginEventMonitor(PlayerLoginEvent event) - { - Player player = event.getPlayer(); - this.playerToFirstTeleport.remove(player); - } - - @EventHandler(priority = EventPriority.LOWEST) - public void onPlayerTeleportLowest(PlayerTeleportEvent event) - { - Player player = event.getPlayer(); - if (this.playerToFirstTeleport.containsKey(player)) return; - this.playerToFirstTeleport.put(player, event); - } - -} diff --git a/src/com/massivecraft/mcore/util/MUtil.java b/src/com/massivecraft/mcore/util/MUtil.java index 28f69428..facb557a 100644 --- a/src/com/massivecraft/mcore/util/MUtil.java +++ b/src/com/massivecraft/mcore/util/MUtil.java @@ -40,6 +40,7 @@ import org.bukkit.event.player.PlayerQuitEvent; import com.massivecraft.mcore.InternalListener; import com.massivecraft.mcore.MCore; +import com.massivecraft.mcore.WorldNameSetEngine; import com.massivecraft.mcore.util.extractor.Extractor; import com.massivecraft.mcore.util.extractor.ExtractorPlayer; import com.massivecraft.mcore.util.extractor.ExtractorPlayerName; @@ -137,9 +138,14 @@ public class MUtil } // -------------------------------------------- // - // BLOCK COMPARISON BY LOCATIONS + // LOCATIONS COMPARISON // -------------------------------------------- // + public static boolean isSameBlock(PlayerMoveEvent event) + { + return isSameBlock(event.getFrom(), event.getTo()); + } + public static boolean isSameBlock(Location one, Location two) { if (one.getBlockX() != two.getBlockX()) return false; @@ -148,9 +154,16 @@ public class MUtil return one.getWorld().equals(two.getWorld()); } - public static boolean isSameBlock(PlayerMoveEvent event) + public static boolean isSameChunk(PlayerMoveEvent event) { - return isSameBlock(event.getFrom(), event.getTo()); + return isSameChunk(event.getFrom(), event.getTo()); + } + + public static boolean isSameChunk(Location one, Location two) + { + if (one.getBlockX() >> 4 != one.getBlockX() >> 4) return false; + if (one.getBlockZ() >> 4 != one.getBlockZ() >> 4) return false; + return one.getWorld().equals(two.getWorld()); } // -------------------------------------------- // @@ -265,6 +278,15 @@ public class MUtil return kickReason(event) != null; } + // -------------------------------------------- // + // WORLD DERP + // -------------------------------------------- // + + public static Set getLoadedWorldNames() + { + return WorldNameSetEngine.get().getWorldNames(); + } + // -------------------------------------------- // // SIMPLE CONSTRUCTORS // -------------------------------------------- //