Skip to content

Commit

Permalink
Update chunkload
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 21, 2013
1 parent 528e5ad commit 5e4bf9b
Showing 1 changed file with 56 additions and 38 deletions.
Expand Up @@ -5,6 +5,8 @@

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Duration;
Expand Down Expand Up @@ -37,88 +39,104 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
Duration length = new Duration(0);
Action action = Action.ADD;

for (String arg : scriptEntry.getArguments()) {
if(aH.matchesArg("ADD, REMOVE, REMOVEALL", arg)) {
action = Action.valueOf(aH.getStringFrom(arg).toUpperCase());
} else if (aH.matchesLocation(arg)) {
chunk = aH.getLocationFrom(arg).getChunk();
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matchesEnum(Action.values())
&& !scriptEntry.hasObject("action"))
scriptEntry.addObject("action", new Element(arg.getValue().toUpperCase()));

} else if(aH.matchesInteger(arg) || aH.matchesDouble(arg) || aH.matchesDuration(arg)) {
length = Duration.valueOf(arg);
} else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
else if (arg.matchesArgumentType(dLocation.class)
&& !scriptEntry.hasObject("location"))
scriptEntry.addObject("location", arg.asType(dLocation.class));

else if (arg.matchesArgumentType(Duration.class)
&& !scriptEntry.hasObject("duration"))
scriptEntry.addObject("duration", arg.asType(Duration.class));

else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg.raw_value);
}

if (chunk == null) throw new InvalidArgumentsException(Messages.DEBUG_SET_LOCATION);
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException(Messages.DEBUG_SET_LOCATION);

if (!scriptEntry.hasObject("action"))
scriptEntry.addObject("action", new Element("ADD"));

scriptEntry.addObject("action", action)
.addObject("chunk", chunk)
.addObject("length", length);
if (!scriptEntry.hasObject("duration"))
scriptEntry.addObject("duration", new Duration(0));
}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
Action action = (Action) scriptEntry.getObject("action");
Chunk chunk = (Chunk) scriptEntry.getObject("chunk");
Duration length = (Duration) scriptEntry.getObject("length");
Element action = scriptEntry.getElement("action");
dLocation chunkloc = (dLocation) scriptEntry.getObject("location");
Duration length = (Duration) scriptEntry.getObject("duration");

dB.report(getName(),
action.debug()
+ chunkloc.debug()
+ length.debug());

switch (action) {
Chunk chunk = chunkloc.getChunk();
String chunkString = chunk.getX()+", "+chunk.getZ();

switch (Action.valueOf(action.asString())) {
case ADD:
if(length.getSeconds() != 0)
chunkDelays.put(chunk.getX()+", "+chunk.getZ(), System.currentTimeMillis() + length.getMillis());
chunkDelays.put(chunkString, System.currentTimeMillis() + length.getMillis());
else
chunkDelays.put(chunk.getX()+", "+chunk.getZ(), (long) 0);
dB.echoDebug("...added chunk "+chunk.getX() + ", "+ chunk.getZ() + " with a " + length.getSeconds() + "delay");
chunkDelays.put(chunkString, (long) 0);
dB.echoDebug("...added chunk "+chunk.getX() + ", "+ chunk.getZ() + " with a delay of " + length.getSeconds() + " seconds.");
if(!chunk.isLoaded())
chunk.load();
break;
case REMOVE:
if(chunkDelays.containsKey(chunk)) {
chunkDelays.remove(chunk);
if(chunkDelays.containsKey(chunkString)) {
chunkDelays.remove(chunkString);
dB.echoDebug("...allowing unloading of chunk "+chunk.getX() + ", "+ chunk.getZ());
}
else
dB.echoError("Chunk was not on the load list!");
break;
case REMOVEALL:
dB.echoDebug("...allowing unloading of all stored chunks");
chunkDelays.clear();
break;
}

// Report to dB
dB.report(getName(), aH.debugObj("Action", action.toString())
+ aH.debugObj("Chunk", chunk.toString())
+ aH.debugObj("Length", length.toString()));

}

// Map of chunks with delays
Map<String, Long> chunkDelays = new HashMap<String, Long>();

@EventHandler
public void stopUnload(ChunkUnloadEvent e) {
if(chunkDelays.containsKey(e.getChunk().getX()+ ", "+e.getChunk().getZ())) {
if(chunkDelays.get(e.getChunk().getX()+ ", "+e.getChunk().getZ()) == 0) {
String chunkString = e.getChunk().getX()+", "+ e.getChunk().getZ();
if(chunkDelays.containsKey(chunkString)) {
if(chunkDelays.get(chunkString) == 0)
e.setCancelled(true);
return;
} else if(System.currentTimeMillis() < chunkDelays.get(e.getChunk().getX()+ ", "+e.getChunk().getZ())) {

else if(System.currentTimeMillis() < chunkDelays.get(chunkString))
e.setCancelled(true);
return;
} else chunkDelays.remove(e.getChunk().getX()+ ", "+e.getChunk().getZ());

else
chunkDelays.remove(chunkString);
}
}

@EventHandler
public void stopDespawn(NPCDespawnEvent e) {
Chunk chnk = e.getNPC().getBukkitEntity().getLocation().getChunk();
if(chunkDelays.containsKey(chnk.getX() + ", "+chnk.getZ())) {
if(chunkDelays.get(chnk.getX() + ", "+chnk.getZ()) == 0) {
String chunkString = chnk.getX()+", "+ chnk.getZ();
if(chunkDelays.containsKey(chunkString)) {
if(chunkDelays.get(chunkString) == 0)
e.setCancelled(true);
return;
} else if(System.currentTimeMillis() < chunkDelays.get(chnk.getX() + ", "+chnk.getZ())) {

else if(System.currentTimeMillis() < chunkDelays.get(chunkString))
e.setCancelled(true);
return;
} else chunkDelays.remove(chnk.getX() + ", "+chnk.getZ());

else
chunkDelays.remove(chunkString);
}
}

Expand Down

0 comments on commit 5e4bf9b

Please sign in to comment.