Support async engine tasks and optimize MUtil.random a bit

This commit is contained in:
Olof Larsson 2014-02-20 12:05:25 +01:00
parent 890de8a294
commit 9ea73846e4
3 changed files with 42 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package com.massivecraft.mcore;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;
public interface Engine extends Listener, Runnable public interface Engine extends Listener, Runnable
{ {
@ -12,5 +13,10 @@ public interface Engine extends Listener, Runnable
public Long getDelay(); public Long getDelay();
public Long getPeriod(); public Long getPeriod();
public Integer getTaskId(); public Integer getTaskId();
public BukkitTask getBukkitTask();
public boolean isSync();
} }

View File

@ -2,6 +2,7 @@ package com.massivecraft.mcore;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.scheduler.BukkitTask;
public abstract class EngineAbstract implements Engine public abstract class EngineAbstract implements Engine
{ {
@ -9,8 +10,8 @@ public abstract class EngineAbstract implements Engine
// FIELDS // FIELDS
// -------------------------------------------- // // -------------------------------------------- //
private Integer taskId; private BukkitTask task;
@Override public Integer getTaskId() { return this.taskId; } @Override public Integer getTaskId() { return this.task.getTaskId(); }
// -------------------------------------------- // // -------------------------------------------- //
// OVERRIDE // OVERRIDE
@ -22,7 +23,14 @@ public abstract class EngineAbstract implements Engine
Bukkit.getPluginManager().registerEvents(this, this.getPlugin()); Bukkit.getPluginManager().registerEvents(this, this.getPlugin());
if (this.getPeriod() != null) if (this.getPeriod() != null)
{ {
this.taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(this.getPlugin(), this, this.getDelay(), this.getPeriod()); if (this.isSync())
{
Bukkit.getScheduler().runTaskTimer(this.getPlugin(), this, this.getDelay(), this.getPeriod());
}
else
{
Bukkit.getScheduler().runTaskTimerAsynchronously(this.getPlugin(), this, this.getDelay(), this.getPeriod());
}
} }
} }
@ -30,10 +38,10 @@ public abstract class EngineAbstract implements Engine
public void deactivate() public void deactivate()
{ {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
if (this.getTaskId() != null) if (this.task != null)
{ {
Bukkit.getScheduler().cancelTask(this.getTaskId()); this.task.cancel();
this.taskId = null; this.task = null;
} }
} }
@ -55,4 +63,16 @@ public abstract class EngineAbstract implements Engine
} }
@Override
public BukkitTask getBukkitTask()
{
return this.task;
}
@Override
public boolean isSync()
{
return true;
}
} }

View File

@ -487,7 +487,16 @@ public class MUtil
if (coll.size() == 0) return null; if (coll.size() == 0) return null;
if (coll.size() == 1) return coll.iterator().next(); if (coll.size() == 1) return coll.iterator().next();
int index = MCore.random.nextInt(coll.size()); int index = MCore.random.nextInt(coll.size());
return new ArrayList<T>(coll).get(index); List<T> list = null;
if (coll instanceof List<?>)
{
list = (List<T>)coll;
}
else
{
list = new ArrayList<T>(coll);
}
return list.get(index);
} }
public static <T> List<T> random(Collection<T> coll, int count) public static <T> List<T> random(Collection<T> coll, int count)