Skip to content

Commit

Permalink
Allow schematic pastes to be delayed, fixes #1054
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 14, 2015
1 parent 201d7d9 commit 28190d1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 27 deletions.
Expand Up @@ -2120,7 +2120,7 @@ public void registerCoreMembers() {

// <--[command]
// @Name Schematic
// @Syntax schematic [create/load/unload/rotate/paste/save] [name:<name>] (angle:<#>) (<location>) (<cuboid>)
// @Syntax schematic [create/load/unload/rotate/paste/save] [name:<name>] (angle:<#>) (<location>) (<cuboid>) (delayed)
// @Group World
// @Required 2
// @Stable unstable
Expand Down Expand Up @@ -2164,7 +2164,7 @@ public void registerCoreMembers() {

// -->
registerCoreMember(SchematicCommand.class,
"SCHEMATIC", "schematic [create/load/unload/rotate/paste/save] [name:<name>] (angle:<#>) (<location>) (<cuboid>)", 2);
"SCHEMATIC", "schematic [create/load/unload/rotate/paste/save] [name:<name>] (angle:<#>) (<location>) (<cuboid>) (delayed)", 2);


// <--[command]
Expand Down
Expand Up @@ -4,6 +4,7 @@
import net.aufdemrand.denizen.utilities.blocks.CuboidBlockSet;
import net.aufdemrand.denizencore.scripts.ScriptHelper;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.scripts.commands.Holdable;
import net.aufdemrand.denizencore.tags.ReplaceableTagEvent;
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
Expand All @@ -24,7 +25,7 @@
import java.util.HashMap;
import java.util.Map;

public class SchematicCommand extends AbstractCommand {
public class SchematicCommand extends AbstractCommand implements Holdable {

@Override
public void onEnable() {
Expand All @@ -42,55 +43,65 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values()))
&& arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", new Element(arg.raw_value.toUpperCase()));

}
else if (!scriptEntry.hasObject("name")
&& arg.matchesPrefix("name"))
&& arg.matchesPrefix("name")) {
scriptEntry.addObject("name", arg.asElement());

}
else if (!scriptEntry.hasObject("angle")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer))
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("angle", arg.asElement());

}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class))
&& arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("location", arg.asType(dLocation.class));

}
else if (!scriptEntry.hasObject("cuboid")
&& arg.matchesArgumentType(dCuboid.class))
&& arg.matchesArgumentType(dCuboid.class)) {
scriptEntry.addObject("cuboid", arg.asType(dCuboid.class));

else
}
else if (!scriptEntry.hasObject("delayed")
&& arg.matches("delayed")) {
scriptEntry.addObject("delayed", new Element("true"));
}
else {
arg.reportUnhandled();
}
}

if (!scriptEntry.hasObject("type"))
throw new InvalidArgumentsException("Missing type argument!");

if (!scriptEntry.hasObject("name"))
throw new InvalidArgumentsException("Missing name argument!");

}


@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {

Element angle = scriptEntry.getElement("angle");
Element type = scriptEntry.getElement("type");
Element name = scriptEntry.getElement("name");
Element delayed = scriptEntry.getElement("delayed");
dLocation location = scriptEntry.getdObject("location");
dCuboid cuboid = scriptEntry.getdObject("cuboid");

dB.report(scriptEntry, getName(), type.debug()
+ name.debug()
+ (location != null ? location.debug(): "")
+ (cuboid != null ? cuboid.debug(): "")
+ (angle != null ? angle.debug(): ""));
+ (angle != null ? angle.debug(): "")
+ (delayed != null ? delayed.debug(): ""));

CuboidBlockSet set;
switch (Type.valueOf(type.asString())) {
Type ttype = Type.valueOf(type.asString());
if (scriptEntry.shouldWaitFor() && ttype != Type.PASTE) {
scriptEntry.setFinished(true);
}
switch (ttype) {
case CREATE:
if (schematics.containsKey(name.asString().toUpperCase())) {
dB.echoError(scriptEntry.getResidingQueue(), "Schematic file " + name.asString() + " is already loaded.");
Expand All @@ -105,6 +116,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
return;
}
try {
// TODO: Make me waitable!
set = new CuboidBlockSet(cuboid, location);
schematics.put(name.asString().toUpperCase(), set);
}
Expand All @@ -127,7 +139,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
return;
}
InputStream fs = new FileInputStream(f);
//set = CuboidBlockSet.fromCompressedString(ScriptHelper.convertStreamToString(fs));
// TODO: Make me waitable!
set = CuboidBlockSet.fromMCEditStream(fs);
fs.close();
schematics.put(name.asString().toUpperCase(), set);
Expand Down Expand Up @@ -155,6 +167,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
return;
}
dB.echoError(scriptEntry.getResidingQueue(), "Schematic rotation is TODO!");
// TODO: Make me waitable!
int ang = angle.asInt();
if (ang < 0) {
ang = 360 + ang;
Expand All @@ -163,7 +176,6 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
ang -= 90;
schematics.get(name.asString().toUpperCase()).rotateOne();
}
//schematics.get(name.asString().toUpperCase()).rotate2D(angle.asInt());
break;
case PASTE:
if (!schematics.containsKey(name.asString().toUpperCase())) {
Expand All @@ -175,7 +187,17 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
return;
}
try {
schematics.get(name.asString().toUpperCase()).setBlocks(location);
if (delayed != null && delayed.asBoolean()) {
schematics.get(name.asString().toUpperCase()).setBlocksDelayed(location, new Runnable() {
@Override
public void run() {
scriptEntry.setFinished(true);
}
});
}
else {
schematics.get(name.asString().toUpperCase()).setBlocks(location);
}
}
catch (Exception ex) {
dB.echoError(scriptEntry.getResidingQueue(), "Exception pasting schematic file " + name.asString() + ".");
Expand All @@ -192,13 +214,9 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
set = schematics.get(name.asString().toUpperCase());
String directory = URLDecoder.decode(System.getProperty("user.dir"));
File f = new File(directory + "/plugins/Denizen/schematics/" + name.asString() + ".schematic");
//String output = set.toCompressedFormat();
// TODO: Make me waitable!
FileOutputStream fs = new FileOutputStream(f);
set.saveMCEditFormatToStream(fs);
/*OutputStreamWriter osw = new OutputStreamWriter(fs);
osw.write(output);
osw.flush();
osw.close();*/
fs.flush();
fs.close();
}
Expand Down
Expand Up @@ -8,6 +8,8 @@ public interface BlockSet {

public abstract List<BlockData> getBlocks();

public abstract void setBlocksDelayed(Location loc, Runnable runme);

public abstract void setBlocks(Location loc);

public abstract String toCompressedFormat();
Expand Down
Expand Up @@ -2,10 +2,13 @@

import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizen.utilities.jnbt.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.*;
import org.bukkit.util.Vector;

Expand Down Expand Up @@ -63,6 +66,36 @@ public dCuboid getCuboid(Location loc) {
return new dCuboid(low, high);
}

public class IntHolder {
public long theInt = 0;
}

@Override
public void setBlocksDelayed(final Location loc, final Runnable runme) {
final IntHolder index = new IntHolder();
final long goal = (long)(x_width * y_length * z_height);

new BukkitRunnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
while (index.theInt < goal) {
long z = index.theInt % ((long)(z_height));
long y = ((index.theInt - z) % ((long)(y_length * z_height))) / ((long)z_height);
long x = (index.theInt - y - z) / ((long)(y_length * z_height));
blocks.get((int)index.theInt).setBlock(loc.clone().add(x, y, z).getBlock());
index.theInt++;
if (System.currentTimeMillis() - start > 50) {
return;
}
}
runme.run();
cancel();

}
}.runTaskTimer(DenizenAPI.getCurrentInstance(), 1, 1);
}

@Override
public void setBlocks(Location loc) {
int index = 0;
Expand All @@ -77,7 +110,7 @@ public void setBlocks(Location loc) {
}

public CuboidBlockSet rotateOne() {
// TODO
// TODO: IMPLEMENT ME!
return new CuboidBlockSet();
}

Expand Down

0 comments on commit 28190d1

Please sign in to comment.