Support async engine tasks and optimize MUtil.random a bit
This commit is contained in:
parent
890de8a294
commit
9ea73846e4
@ -2,6 +2,7 @@ package com.massivecraft.mcore;
|
||||
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
public interface Engine extends Listener, Runnable
|
||||
{
|
||||
@ -12,5 +13,10 @@ public interface Engine extends Listener, Runnable
|
||||
|
||||
public Long getDelay();
|
||||
public Long getPeriod();
|
||||
|
||||
public Integer getTaskId();
|
||||
|
||||
public BukkitTask getBukkitTask();
|
||||
public boolean isSync();
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.massivecraft.mcore;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
public abstract class EngineAbstract implements Engine
|
||||
{
|
||||
@ -9,8 +10,8 @@ public abstract class EngineAbstract implements Engine
|
||||
// FIELDS
|
||||
// -------------------------------------------- //
|
||||
|
||||
private Integer taskId;
|
||||
@Override public Integer getTaskId() { return this.taskId; }
|
||||
private BukkitTask task;
|
||||
@Override public Integer getTaskId() { return this.task.getTaskId(); }
|
||||
|
||||
// -------------------------------------------- //
|
||||
// OVERRIDE
|
||||
@ -22,7 +23,14 @@ public abstract class EngineAbstract implements Engine
|
||||
Bukkit.getPluginManager().registerEvents(this, this.getPlugin());
|
||||
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()
|
||||
{
|
||||
HandlerList.unregisterAll(this);
|
||||
if (this.getTaskId() != null)
|
||||
if (this.task != null)
|
||||
{
|
||||
Bukkit.getScheduler().cancelTask(this.getTaskId());
|
||||
this.taskId = null;
|
||||
this.task.cancel();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -487,7 +487,16 @@ public class MUtil
|
||||
if (coll.size() == 0) return null;
|
||||
if (coll.size() == 1) return coll.iterator().next();
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user