Rewrite cancel method as it is posing some issues

This commit is contained in:
md_5 2013-03-21 20:16:41 +11:00
parent 3d8143c36e
commit ffddcf939f

View File

@ -2,9 +2,10 @@ package net.md_5.bungee.scheduler;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import gnu.trove.TCollections; import gnu.trove.TCollections;
import gnu.trove.iterator.TIntObjectIterator;
import gnu.trove.map.TIntObjectMap; import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap; import gnu.trove.map.hash.TIntObjectHashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import net.md_5.bungee.BungeeCord; import net.md_5.bungee.BungeeCord;
@ -21,31 +22,32 @@ public class BungeeScheduler implements TaskScheduler
@Override @Override
public void cancel(int id) public void cancel(int id)
{ {
cancel( tasks.remove( id ) ); BungeeTask task = tasks.remove( id );
task.getFuture().cancel( false );
} }
@Override @Override
public void cancel(ScheduledTask task) public void cancel(ScheduledTask task)
{ {
Preconditions.checkArgument( task instanceof BungeeTask, "Don't know how to handle task %s", task ); cancel( task.getId() );
tasks.remove( task.getId() ).getFuture().cancel( false );
} }
@Override @Override
public int cancel(Plugin plugin) public int cancel(Plugin plugin)
{ {
int cancelled = 0; Set<ScheduledTask> toRemove = new HashSet<>();
for ( TIntObjectIterator<BungeeTask> iter = tasks.iterator(); iter.hasNext(); ) for ( ScheduledTask task : tasks.valueCollection() )
{ {
BungeeTask task = iter.value();
if ( task.getOwner() == plugin ) if ( task.getOwner() == plugin )
{ {
task.getFuture().cancel( false ); toRemove.add( task );
iter.remove();
cancelled++;
} }
} }
return cancelled; for ( ScheduledTask task : toRemove )
{
cancel( task );
}
return toRemove.size();
} }
@Override @Override