Scheduled teleports should not depent on tickrate.

This commit is contained in:
Olof Larsson 2013-02-27 08:27:28 +01:00
parent 9a4280fad9
commit bd1e2e29de
4 changed files with 115 additions and 98 deletions

View File

@ -15,7 +15,7 @@ import com.massivecraft.mcore.adapter.ItemStackAdapter;
import com.massivecraft.mcore.adapter.MongoURIAdapter;
import com.massivecraft.mcore.adapter.PSAdapter;
import com.massivecraft.mcore.cmd.CmdMcore;
import com.massivecraft.mcore.mixin.ScheduledTeleportListener;
import com.massivecraft.mcore.mixin.ScheduledTeleportEngine;
import com.massivecraft.mcore.mixin.SenderIdMixinDefault;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.Db;
@ -113,7 +113,7 @@ public class MCore extends MPlugin
// Register events
InternalListener.get().setup();
ScheduledTeleportListener.get().setup();
ScheduledTeleportEngine.get().setup();
// Schedule the collection ticker.
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this.collTickTask, 1, 1);

View File

@ -1,39 +1,11 @@
package com.massivecraft.mcore.mixin;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.PS;
public class ScheduledTeleport implements Runnable
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public final static transient int NON_SCHEDULED_TASK_ID = -1;
// -------------------------------------------- //
// STATIC INDEX
// -------------------------------------------- //
public static Map<Player, ScheduledTeleport> teleporteeToScheduledTeleport = new ConcurrentHashMap<Player, ScheduledTeleport>();
public static void schedule(ScheduledTeleport scheduledTeleport)
{
if (isScheduled(scheduledTeleport)) return;
Bukkit.getScheduler().scheduleSyncDelayedTask(MCore.get(), scheduledTeleport, scheduledTeleport.getDelaySeconds()*20);
}
public static boolean isScheduled(ScheduledTeleport scheduledTeleport)
{
return teleporteeToScheduledTeleport.containsKey(scheduledTeleport.getTeleportee());
}
// -------------------------------------------- //
// FIELDS & RAW-DATA ACCESS
// -------------------------------------------- //
@ -50,8 +22,10 @@ public class ScheduledTeleport implements Runnable
private final int delaySeconds;
public int getDelaySeconds() { return this.delaySeconds; }
private int taskId;
public int getTaskId() { return this.taskId; }
private long dueMillis;
public long getDueMillis() { return this.dueMillis; }
public void setDueMillis(long dueMillis) { this.dueMillis = dueMillis; }
public boolean isDue(long now) { return now >= this.dueMillis; }
// -------------------------------------------- //
// CONSTRUCT
@ -63,7 +37,7 @@ public class ScheduledTeleport implements Runnable
this.destinationPs = destinationPs;
this.destinationDesc = destinationDesc;
this.delaySeconds = delaySeconds;
this.taskId = NON_SCHEDULED_TASK_ID;
this.dueMillis = 0;
}
// -------------------------------------------- //
@ -72,28 +46,17 @@ public class ScheduledTeleport implements Runnable
public boolean isScheduled()
{
return this.taskId != NON_SCHEDULED_TASK_ID;
return ScheduledTeleportEngine.get().isScheduled(this);
}
public ScheduledTeleport schedule()
{
ScheduledTeleport old = teleporteeToScheduledTeleport.get(this.getTeleportee());
if (old != null) old.unschedule();
teleporteeToScheduledTeleport.put(this.getTeleportee(), this);
this.taskId = Bukkit.getScheduler().scheduleSyncDelayedTask(MCore.get(), this, this.getDelaySeconds()*20);
return old;
return ScheduledTeleportEngine.get().schedule(this);
}
public boolean unschedule()
{
Bukkit.getScheduler().cancelTask(this.getTaskId());
this.taskId = NON_SCHEDULED_TASK_ID;
return teleporteeToScheduledTeleport.remove(this.getTeleportee()) != null;
return ScheduledTeleportEngine.get().unschedule(this);
}
// -------------------------------------------- //

View File

@ -0,0 +1,105 @@
package com.massivecraft.mcore.mixin;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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.PlayerMoveEvent;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.util.MUtil;
public class ScheduledTeleportEngine implements Listener, Runnable
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ScheduledTeleportEngine i = new ScheduledTeleportEngine();
public static ScheduledTeleportEngine get() { return i; }
// -------------------------------------------- //
// SCHEDULED TELEPORT INDEX
// -------------------------------------------- //
protected Map<Player, ScheduledTeleport> teleporteeToScheduledTeleport = new ConcurrentHashMap<Player, ScheduledTeleport>();
public boolean isScheduled(ScheduledTeleport st)
{
return this.teleporteeToScheduledTeleport.containsValue(st);
}
public ScheduledTeleport schedule(ScheduledTeleport st)
{
ScheduledTeleport old = this.teleporteeToScheduledTeleport.get(st.getTeleportee());
if (old != null) old.unschedule();
this.teleporteeToScheduledTeleport.put(st.getTeleportee(), st);
st.setDueMillis(System.currentTimeMillis() + st.getDelaySeconds()*1000);
return old;
}
public boolean unschedule(ScheduledTeleport st)
{
ScheduledTeleport old = this.teleporteeToScheduledTeleport.get(st.getTeleportee());
if (old == null) return false;
if (old != st) return false;
return this.teleporteeToScheduledTeleport.remove(st.getTeleportee()) != null;
}
// -------------------------------------------- //
// SETUP
// -------------------------------------------- //
public void setup()
{
Bukkit.getPluginManager().registerEvents(this, MCore.get());
Bukkit.getScheduler().scheduleSyncRepeatingTask(MCore.get(), this, 1, 1);
}
// -------------------------------------------- //
// LISTENER
// -------------------------------------------- //
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMoved(PlayerMoveEvent event)
{
// If the player moved from one block to another ...
if (MUtil.isSameBlock(event.getFrom(), event.getTo())) return;
// ... and there is a ScheduledTeleport ...
ScheduledTeleport scheduledTeleport = teleporteeToScheduledTeleport.get(event.getPlayer());
if (scheduledTeleport == null) return;
// ... unschedule it ...
scheduledTeleport.unschedule();
// ... and inform the teleportee.
Mixin.msg(scheduledTeleport.getTeleportee(), "<rose>Cancelled <i>teleport to <h>"+scheduledTeleport.getDestinationDesc()+"<i>.");
}
// -------------------------------------------- //
// LISTENER
// -------------------------------------------- //
@Override
public void run()
{
long now = System.currentTimeMillis();
for (ScheduledTeleport st : teleporteeToScheduledTeleport.values())
{
if (st.isDue(now))
{
st.run();
}
}
}
}

View File

@ -1,51 +0,0 @@
package com.massivecraft.mcore.mixin;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.util.MUtil;
public class ScheduledTeleportListener implements Listener
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static ScheduledTeleportListener i = new ScheduledTeleportListener();
public static ScheduledTeleportListener get() { return i; }
// -------------------------------------------- //
// SETUP
// -------------------------------------------- //
public void setup()
{
Bukkit.getPluginManager().registerEvents(this, MCore.get());
}
// -------------------------------------------- //
// LISTENER
// -------------------------------------------- //
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMoved(PlayerMoveEvent event)
{
// If the player moved from one block to another ...
if (MUtil.isSameBlock(event.getFrom(), event.getTo())) return;
// ... and there is a ScheduledTeleport ...
ScheduledTeleport scheduledTeleport = ScheduledTeleport.teleporteeToScheduledTeleport.get(event.getPlayer());
if (scheduledTeleport == null) return;
// ... unschedule it ...
scheduledTeleport.unschedule();
// ... and inform the teleportee.
Mixin.msg(scheduledTeleport.getTeleportee(), "<rose>Cancelled <i>teleport to <h>"+scheduledTeleport.getDestinationDesc()+"<i>.");
}
}