Some random additions. Factions stuff that fit into the STD.

This commit is contained in:
Olof Larsson 2013-04-18 14:17:07 +02:00
parent 0ffa24b15e
commit 6f7b10bfb0
6 changed files with 178 additions and 87 deletions

View File

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

View File

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

View File

@ -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<String> worldNamesInner = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
private final Set<String> worldNamesOuter = Collections.unmodifiableSet(this.worldNamesInner);
public Set<String> 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());
}
}

View File

@ -227,6 +227,16 @@ public final class PS implements Cloneable, Serializable, Comparable<PS>
return builder.build();
}
// -------------------------------------------- //
// FIELDS: IS
// -------------------------------------------- //
public boolean isWorldLoadedOnThisServer()
{
if (this.world == null) return true;
return MUtil.getLoadedWorldNames().contains(this.world);
}
// -------------------------------------------- //
// PRIVATE CONSTRUCTOR
// -------------------------------------------- //

View File

@ -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<Player, PlayerTeleportEvent> 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<Player, PlayerTeleportEvent>();
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);
}
}

View File

@ -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<String> getLoadedWorldNames()
{
return WorldNameSetEngine.get().getWorldNames();
}
// -------------------------------------------- //
// SIMPLE CONSTRUCTORS
// -------------------------------------------- //