Remove LazyLocation and declare that Tasks are not Utils.

This commit is contained in:
Olof Larsson
2013-04-18 12:29:56 +02:00
parent 9a8f2071e0
commit bc0a8da76f
7 changed files with 8 additions and 115 deletions

View File

@ -1,24 +0,0 @@
package com.massivecraft.factions.util;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.FPlayerColl;
import com.massivecraft.factions.Factions;
public class AutoLeaveTask implements Runnable
{
double rate;
public AutoLeaveTask()
{
this.rate = ConfServer.autoLeaveRoutineRunsEveryXMinutes;
}
public void run()
{
FPlayerColl.get().autoLeaveOnInactivityRoutine();
// maybe setting has been changed? if so, restart task at new rate
if (this.rate != ConfServer.autoLeaveRoutineRunsEveryXMinutes)
Factions.get().startAutoLeaveTask(true);
}
}

View File

@ -1,31 +0,0 @@
package com.massivecraft.factions.util;
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.FactionColl;
import com.massivecraft.factions.Factions;
public class EconLandRewardTask implements Runnable
{
double rate;
public EconLandRewardTask()
{
this.rate = ConfServer.econLandRewardTaskRunsEveryXMinutes;
}
@Override
public void run()
{
FactionColl.get().econLandRewardRoutine();
// TODO: This technique is TPS dependent and wrong.
// Instead of restarting a TPS dependent task the task should poll every once in a while for the system millis.
// With such a setup the need for restarts are gone.
// maybe setting has been changed? if so, restart task at new rate
if (this.rate != ConfServer.econLandRewardTaskRunsEveryXMinutes)
{
Factions.get().startEconLandRewardTask(true);
}
}
}

View File

@ -1,107 +0,0 @@
package com.massivecraft.factions.util;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
/*
* This class provides a lazy-load Location, so that World doesn't need to be initialized
* yet when an object of this class is created, only when the Location is first accessed.
*/
public class LazyLocation
{
private Location location = null;
private String worldName;
private double x;
private double y;
private double z;
private float pitch;
private float yaw;
public LazyLocation(Location loc)
{
setLocation(loc);
}
public LazyLocation(final String worldName, final double x, final double y, final double z)
{
this(worldName, x, y, z, 0, 0);
}
public LazyLocation(final String worldName, final double x, final double y, final double z, final float yaw, final float pitch)
{
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
// This returns the actual Location
public final Location getLocation()
{
// make sure Location is initialized before returning it
initLocation();
return location;
}
// change the Location
public final void setLocation(Location loc)
{
this.location = loc;
this.worldName = loc.getWorld().getName();
this.x = loc.getX();
this.y = loc.getY();
this.z = loc.getZ();
this.yaw = loc.getYaw();
this.pitch = loc.getPitch();
}
// This initializes the Location
private void initLocation()
{
// if location is already initialized, simply return
if (location != null) return;
// get World; hopefully it's initialized at this point
World world = Bukkit.getWorld(worldName);
if (world == null) return;
// store the Location for future calls, and pass it on
location = new Location(world, x, y, z, yaw, pitch);
}
public final String getWorldName()
{
return worldName;
}
public final double getX()
{
return x;
}
public final double getY()
{
return y;
}
public final double getZ()
{
return z;
}
public final double getPitch()
{
return pitch;
}
public final double getYaw()
{
return yaw;
}
}

View File

@ -1,216 +0,0 @@
package com.massivecraft.factions.util;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import com.massivecraft.factions.Factions;
import com.massivecraft.mcore.ps.PS;
/*
* reference diagram, task should move in this pattern out from chunk 0 in the center.
* 8 [>][>][>][>][>] etc.
* [^][6][>][>][>][>][>][6]
* [^][^][4][>][>][>][4][v]
* [^][^][^][2][>][2][v][v]
* [^][^][^][^][0][v][v][v]
* [^][^][^][1][1][v][v][v]
* [^][^][3][<][<][3][v][v]
* [^][5][<][<][<][<][5][v]
* [7][<][<][<][<][<][<][7]
*/
public abstract class SpiralTask implements Runnable
{
// general task-related reference data
private transient World world = null;
private transient boolean readyToGo = false;
private transient int taskID = -1;
private transient int limit = 0;
// values for the spiral pattern routine
private transient int x = 0;
private transient int z = 0;
private transient boolean isZLeg = false;
private transient boolean isNeg = false;
private transient int length = -1;
private transient int current = 0;
// @SuppressWarnings("LeakingThisInConstructor") This actually triggers a warning in Eclipse xD Could we find another way to suppress the error please? :)
public SpiralTask(PS chunk, int radius)
{
chunk = chunk.getChunk(true);
// limit is determined based on spiral leg length for given radius; see insideRadius()
this.limit = (radius - 1) * 2;
this.world = Bukkit.getWorld(chunk.getWorld());
if (this.world == null)
{
Factions.get().log(Level.WARNING, "[SpiralTask] A valid world must be specified!");
this.stop();
return;
}
this.x = (int)chunk.getChunkX();
this.z = (int)chunk.getChunkZ();
this.readyToGo = true;
// get this party started
this.setTaskID(Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Factions.get(), this, 2, 2));
}
/*
* This is where the necessary work is done; you'll need to override this method with whatever you want
* done at each chunk in the spiral pattern.
* Return false if the entire task needs to be aborted, otherwise return true to continue.
*/
public abstract boolean work();
/*
* Returns a PS pointing at the current chunk X and Z values.
*/
public final PS currentChunk()
{
return PS.valueOf(this.world.getName(), null, null, null, null, null, null, this.x, this.z, null, null, null, null, null);
}
/*
* Returns a Location pointing at the current chunk X and Z values.
* note that the Location is at the corner of the chunk, not the center.
*/
public final Location currentLocation()
{
return new Location(world, this.x * 16, 65.0, this.z * 16);
}
/*
* Returns current chunk X and Z values.
*/
public final int getX()
{
return x;
}
public final int getZ()
{
return z;
}
/*
* Below are the guts of the class, which you normally wouldn't need to mess with.
*/
public final void setTaskID(int ID)
{
if (ID == -1)
this.stop();
taskID = ID;
}
public final void run()
{
if (!this.valid() || !readyToGo) return;
// this is set so it only does one iteration at a time, no matter how frequently the timer fires
readyToGo = false;
// make sure we're still inside the specified radius
if ( ! this.insideRadius()) return;
// track this to keep one iteration from dragging on too long and possibly choking the system
long loopStartTime = now();
// keep going until the task has been running for 20ms or more, then stop to take a breather
while (now() < loopStartTime + 20)
{
// run the primary task on the current X/Z coordinates
if ( ! this.work())
{
this.finish();
return;
}
// move on to next chunk in spiral
if ( ! this.moveToNext())
return;
}
// ready for the next iteration to run
readyToGo = true;
}
// step through chunks in spiral pattern from center; returns false if we're done, otherwise returns true
public final boolean moveToNext()
{
if ( ! this.valid()) return false;
// make sure we don't need to turn down the next leg of the spiral
if (current < length)
{
current++;
// if we're outside the radius, we're done
if ( ! this.insideRadius()) return false;
}
else
{ // one leg/side of the spiral down...
current = 0;
isZLeg ^= true;
// every second leg (between X and Z legs, negative or positive), length increases
if (isZLeg)
{
isNeg ^= true;
length++;
}
}
// move one chunk further in the appropriate direction
if (isZLeg)
z += (isNeg) ? -1 : 1;
else
x += (isNeg) ? -1 : 1;
return true;
}
public final boolean insideRadius()
{
boolean inside = current < limit;
if (!inside)
this.finish();
return inside;
}
// for successful completion
public void finish()
{
// P.p.log("SpiralTask successfully completed!");
this.stop();
}
// we're done, whether finished or cancelled
public final void stop()
{
if (!this.valid()) return;
readyToGo = false;
Bukkit.getServer().getScheduler().cancelTask(taskID);
taskID = -1;
}
// is this task still valid/workable?
public final boolean valid()
{
return taskID != -1;
}
private static long now()
{
return System.currentTimeMillis();
}
}