Random scripting stuff

This commit is contained in:
boy0001 2015-08-02 04:01:41 +10:00
parent 2a3c6ab615
commit e314f46c47
7 changed files with 105 additions and 14 deletions

28
pom.xml
View File

@ -40,7 +40,33 @@
<plugin> <plugin>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<executions> <executions>
<execution>
<id>bukkit</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<finalName>PlotSquared-Bukkit</finalName>
<excludes>
<exclude>**/com/plotsquared/sponge/**</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>sponge</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<finalName>PlotSquared-Sponge</finalName>
<excludes>
<exclude>**/com/plotsquared/bukkit/**</exclude>
<exclude>**/com/intellectualcrafters/plot/api/*</exclude>
</excludes>
</configuration>
</execution>
</executions> </executions>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -7,6 +7,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
@ -265,6 +266,7 @@ public class PS {
} }
// Copy files // Copy files
copyFile("automerge.js", "scripts");
copyFile("town.template", "templates"); copyFile("town.template", "templates");
copyFile("skyblock.template", "templates"); copyFile("skyblock.template", "templates");
copyFile("german.yml", "translations"); copyFile("german.yml", "translations");
@ -317,8 +319,8 @@ public class PS {
* @param message Message to log * @param message Message to log
* @see IPlotMain#log(String) * @see IPlotMain#log(String)
*/ */
public static void log(final String message) { public static void log(Object message) {
get().IMP.log(message); get().IMP.log(StringMan.getString(message));
} }
/** /**

View File

@ -40,6 +40,8 @@ import javax.script.ScriptEngineManager;
import javax.script.ScriptException; import javax.script.ScriptException;
import javax.script.SimpleScriptContext; import javax.script.SimpleScriptContext;
import org.bukkit.ChatColor;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.intellectualcrafters.plot.PS; import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
@ -120,7 +122,6 @@ public class DebugExec extends SubCommand {
scope.put("UUIDHandler", UUIDHandler.implementation); scope.put("UUIDHandler", UUIDHandler.implementation);
scope.put("DBFunc", DBFunc.dbManager); scope.put("DBFunc", DBFunc.dbManager);
scope.put("HybridUtils", HybridUtils.manager); scope.put("HybridUtils", HybridUtils.manager);
scope.put("HybridUtils", HybridUtils.manager);
scope.put("IMP", PS.get().IMP); scope.put("IMP", PS.get().IMP);
scope.put("MainCommand", MainCommand.getInstance()); scope.put("MainCommand", MainCommand.getInstance());
@ -366,7 +367,7 @@ public class DebugExec extends SubCommand {
} }
case "run": { case "run": {
try { try {
script = StringMan.join(Files.readLines(new File(PS.get().IMP.getDirectory(), args[1]), StandardCharsets.UTF_8), ""); script = StringMan.join(Files.readLines(new File(new File(PS.get().IMP.getDirectory() + File.separator + "scripts"), args[1]), StandardCharsets.UTF_8), System.getProperty("line.separator"));
if (args.length > 2) { if (args.length > 2) {
HashMap<String, String> replacements = new HashMap<>(); HashMap<String, String> replacements = new HashMap<>();
for (int i = 2; i < args.length; i++) { for (int i = 2; i < args.length; i++) {
@ -384,9 +385,13 @@ public class DebugExec extends SubCommand {
script = StringMan.join(args, " "); script = StringMan.join(args, " ");
} }
} }
if (!ConsolePlayer.isConsole(player)) {
MainUtil.sendMessage(player, C.NOT_CONSOLE);
return false;
}
init(); init();
scope.put("PlotPlayer", player); scope.put("PlotPlayer", player);
PS.log("> " + script); System.out.print("> " + script);
try { try {
if (async) { if (async) {
final String toExec = script; final String toExec = script;

View File

@ -476,9 +476,10 @@ public class Plot {
/** /**
* Auto merge the plot with any adjacent plots of the same owner * Auto merge the plot with any adjacent plots of the same owner
* @see MainUtil#autoMerge(Plot, UUID) to specify the owner * @see MainUtil#autoMerge(Plot, UUID) to specify the owner
* @param removeRoads If to remove roads when merging
*/ */
public void autoMerge() { public void autoMerge(boolean removeRoads) {
MainUtil.autoMerge(this, owner); MainUtil.autoMerge(this, owner, removeRoads);
} }
/** /**

View File

@ -821,7 +821,7 @@ public class MainUtil {
return string; return string;
} }
public static void autoMerge(final Plot plot, final UUID uuid) { public static void autoMerge(final Plot plot, final UUID uuid, boolean removeRoads) {
if (plot == null) { if (plot == null) {
return; return;
} }
@ -843,7 +843,7 @@ public class MainUtil {
final PlotId top = getTopPlot(plot).id; final PlotId top = getTopPlot(plot).id;
plots = getPlotSelectionIds(new PlotId(bot.x, bot.y - 1), new PlotId(top.x, top.y)); plots = getPlotSelectionIds(new PlotId(bot.x, bot.y - 1), new PlotId(top.x, top.y));
if (ownsPlots(plot.world, plots, uuid, 0)) { if (ownsPlots(plot.world, plots, uuid, 0)) {
final boolean result = mergePlots(plot.world, plots, true); final boolean result = mergePlots(plot.world, plots, removeRoads);
if (result) { if (result) {
merge = true; merge = true;
continue; continue;
@ -851,7 +851,7 @@ public class MainUtil {
} }
plots = getPlotSelectionIds(new PlotId(bot.x, bot.y), new PlotId(top.x + 1, top.y)); plots = getPlotSelectionIds(new PlotId(bot.x, bot.y), new PlotId(top.x + 1, top.y));
if (ownsPlots(plot.world, plots, uuid, 1)) { if (ownsPlots(plot.world, plots, uuid, 1)) {
final boolean result = mergePlots(plot.world, plots, true); final boolean result = mergePlots(plot.world, plots, removeRoads);
if (result) { if (result) {
merge = true; merge = true;
continue; continue;
@ -859,7 +859,7 @@ public class MainUtil {
} }
plots = getPlotSelectionIds(new PlotId(bot.x, bot.y), new PlotId(top.x, top.y + 1)); plots = getPlotSelectionIds(new PlotId(bot.x, bot.y), new PlotId(top.x, top.y + 1));
if (ownsPlots(plot.world, plots, uuid, 2)) { if (ownsPlots(plot.world, plots, uuid, 2)) {
final boolean result = mergePlots(plot.world, plots, true); final boolean result = mergePlots(plot.world, plots, removeRoads);
if (result) { if (result) {
merge = true; merge = true;
continue; continue;
@ -867,7 +867,7 @@ public class MainUtil {
} }
plots = getPlotSelectionIds(new PlotId(bot.x - 1, bot.y), new PlotId(top.x, top.y)); plots = getPlotSelectionIds(new PlotId(bot.x - 1, bot.y), new PlotId(top.x, top.y));
if (ownsPlots(plot.world, plots, uuid, 3)) { if (ownsPlots(plot.world, plots, uuid, 3)) {
final boolean result = mergePlots(plot.world, plots, true); final boolean result = mergePlots(plot.world, plots, removeRoads);
if (result) { if (result) {
merge = true; merge = true;
continue; continue;
@ -928,7 +928,7 @@ public class MainUtil {
} }
final PlotWorld plotworld = PS.get().getPlotWorld(plot.world); final PlotWorld plotworld = PS.get().getPlotWorld(plot.world);
if (plotworld.AUTO_MERGE) { if (plotworld.AUTO_MERGE) {
autoMerge(p, uuid); autoMerge(p, uuid, true);
} }
return true; return true;
} }

View File

@ -1,6 +1,8 @@
package com.intellectualcrafters.plot.util; package com.intellectualcrafters.plot.util;
import java.lang.reflect.Array;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -26,6 +28,37 @@ public class StringMan {
return sb.toString(); return sb.toString();
} }
public static String getString(Object obj) {
if (obj == null) {
return "null";
}
if (obj.getClass() == String.class) {
return (String) obj;
}
if (obj.getClass().isArray()) {
String result = "";
String prefix = "";
for(int i=0; i<Array.getLength(obj); i++){
result += prefix + getString(Array.get(obj, i));
prefix = ",";
}
return "( " + result + " )";
}
else if (obj instanceof Collection<?>) {
String result = "";
String prefix = "";
for (Object element : (List<?>) obj) {
result += prefix + getString(element);
prefix = ",";
}
return "[ " + result + " ]";
}
else {
return obj.toString();
}
}
public static String replaceFirst(char c, String s) { public static String replaceFirst(char c, String s) {
if (s == null) { if (s == null) {
return ""; return "";

View File

@ -0,0 +1,24 @@
/*
Need to script something quick with PlotSquared?
/plot debugexec runasync automerge.js <removeroads>
This is an example script that will auto merge all plots
*/
var plots = PS.sortPlots(PS.getPlots());
PS.log("Attempting to auto merge " + plots.size() + " plots");
if ("%s0" === "true") {
for (var i = 0; i < plots.size(); i++) {
var plot = plots.get(i);
plot.autoMerge(false);
}
}
else if ("%s0" === "false") {
for (var i = 0; i < plots.size(); i++) {
var plot = plots.get(i);
plot.autoMerge(false);
}
}
else {
C_COMMAND_SYNTAX.send(PlotPlayer, "/plot debugexec automerge.js <removeroads>");
MainUtil.sendMessage(PlotPlayer, "$1<removeroads> is true or false if you want to remove roads when auto merging");
}