Skip to content

Commit

Permalink
Map command: dot input, and modernize
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 17, 2022
1 parent 450f60a commit e5e9c6a
Showing 1 changed file with 51 additions and 103 deletions.
@@ -1,13 +1,16 @@
package com.denizenscript.denizen.scripts.commands.item;

import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.scripts.containers.core.MapScriptContainer;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.maps.DenizenMapManager;
import com.denizenscript.denizen.utilities.maps.DenizenMapRenderer;
import com.denizenscript.denizen.utilities.maps.MapDot;
import com.denizenscript.denizen.utilities.maps.MapImage;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.WorldTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
Expand All @@ -24,26 +27,29 @@ public class MapCommand extends AbstractCommand {

public MapCommand() {
setName("map");
setSyntax("map [<#>/new:<world>] (reset:<location>) (scale:<value>) (tracking) (image:<file>) (resize) (script:<script>) (x:<#>) (y:<#>)");
setRequiredArguments(2, 7);
setSyntax("map [<#>/new:<world>] (reset:<location>) (scale:<value>) (tracking) (image:<file>) (resize) (script:<script>) (dot:<color>) (radius:<radius>) (x:<#>) (y:<#>)");
setRequiredArguments(2, 9);
isProcedural = false;
setPrefixesHandled("dot", "radius", "image", "script", "x", "y", "reset", "new");
setBooleansHandled("resize", "tracking");
}

// <--[command]
// @Name Map
// @Syntax map [<#>/new:<world>] (reset:<location>) (scale:<value>) (tracking) (image:<file>) (resize) (script:<script>) (x:<#>) (y:<#>)
// @Syntax map [<#>/new:<world>] (reset:<location>) (scale:<value>) (tracking) (image:<file>) (resize) (script:<script>) (dot:<color>) (radius:<radius>) (x:<#>) (y:<#>)
// @Required 2
// @Maximum 7
// @Maximum 9
// @Short Modifies a new or existing map by adding images or text.
// @Group item
//
// @Description
// This command modifies an existing map, or creates a new one. Using this will override existing non-Denizen map renderers with Denizen's custom map renderer.
//
// You must specify at least one of 'reset', 'script', or 'image'. You can specify multiple at once if you prefer.
// You must specify at least one of 'reset', 'script', 'image', or 'pixel'. You can specify multiple at once if you prefer.
//
// When using 'reset', you can specify optionally 'scale' and/or 'tracking'.
// When using 'image' you can optionally specify 'resize'.
// When using 'dot' you can optionally specify 'radius'.
//
// You can reset this at any time by using the 'reset:<location>' argument, which will remove all
// images and texts on the map and show the default world map at the specified location.
Expand Down Expand Up @@ -85,149 +91,91 @@ public MapCommand() {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("new")
&& arg.matchesPrefix("new")
&& arg.matchesArgumentType(WorldTag.class)) {
scriptEntry.addObject("new", arg.asType(WorldTag.class));
}
else if (!scriptEntry.hasObject("reset-loc")
&& arg.matchesPrefix("r", "reset")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("reset-loc", arg.asType(LocationTag.class));
scriptEntry.addObject("reset", new ElementTag(true));
}
else if (!scriptEntry.hasObject("reset")
&& arg.matches("reset")) {
scriptEntry.addObject("reset", new ElementTag(true));
}
else if (!scriptEntry.hasObject("image")
&& arg.matchesPrefix("i", "img", "image")) {
scriptEntry.addObject("image", arg.asElement());
}
else if (!scriptEntry.hasObject("resize")
&& arg.matches("resize")) {
scriptEntry.addObject("resize", new ElementTag(true));
}
else if (!scriptEntry.hasObject("width")
&& arg.matchesPrefix("width")
&& arg.matchesInteger()) {
scriptEntry.addObject("width", arg.asElement());
}
else if (!scriptEntry.hasObject("height")
&& arg.matchesPrefix("height")
&& arg.matchesInteger()) {
scriptEntry.addObject("height", arg.asElement());
}
else if (!scriptEntry.hasObject("scale")
&& arg.matchesPrefix("scale")
&& arg.matchesEnum(MapView.Scale.class)) {
scriptEntry.addObject("scale", arg.asElement());
}
else if (!scriptEntry.hasObject("tracking")
&& arg.matches("tracking")) {
scriptEntry.addObject("tracking", new ElementTag("true"));
}
else if (!scriptEntry.hasObject("script")
&& arg.matchesPrefix("s", "script")
&& arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else if (!scriptEntry.hasObject("x-value")
&& arg.matchesPrefix("x")
&& arg.matchesFloat()) {
scriptEntry.addObject("x-value", arg.asElement());
}
else if (!scriptEntry.hasObject("y-value")
&& arg.matchesPrefix("y")
&& arg.matchesFloat()) {
scriptEntry.addObject("y-value", arg.asElement());
}
else if (!scriptEntry.hasObject("map-id")
if (!scriptEntry.hasObject("map-id")
&& arg.matchesInteger()) {
scriptEntry.addObject("map-id", arg.asElement());
}
else {
arg.reportUnhandled();
}

}
if (!scriptEntry.hasObject("map-id") && !scriptEntry.hasObject("new")) {
throw new InvalidArgumentsException("Must specify a map ID or create a new map!");
}
if (!scriptEntry.hasObject("reset")
&& !scriptEntry.hasObject("reset-loc")
&& !scriptEntry.hasObject("image")
&& !scriptEntry.hasObject("script")) {
throw new InvalidArgumentsException("Must specify a valid action to perform!");
}
scriptEntry.defaultObject("reset", new ElementTag(false)).defaultObject("resize", new ElementTag(false))
.defaultObject("x-value", new ElementTag(0)).defaultObject("y-value", new ElementTag(0));
}

@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("map-id");
WorldTag create = scriptEntry.getObjectTag("new");
ElementTag reset = scriptEntry.getElement("reset");
LocationTag resetLoc = scriptEntry.getObjectTag("reset-loc");
ElementTag image = scriptEntry.getElement("image");
ScriptTag script = scriptEntry.getObjectTag("script");
ElementTag resize = scriptEntry.getElement("resize");
ElementTag width = scriptEntry.getElement("width");
ElementTag height = scriptEntry.getElement("height");
ElementTag scale = scriptEntry.getElement("scale");
ElementTag tracking = scriptEntry.getElement("tracking");
ElementTag x = scriptEntry.getElement("x-value");
ElementTag y = scriptEntry.getElement("y-value");
WorldTag create = scriptEntry.argForPrefix("new", WorldTag.class, true);
LocationTag resetLoc = scriptEntry.argForPrefix("reset", LocationTag.class, true);
ElementTag image = scriptEntry.argForPrefixAsElement("image", null);
boolean resize = scriptEntry.argAsBoolean("resize");
ScriptTag script = scriptEntry.argForPrefix("script", ScriptTag.class, true);
ElementTag width = scriptEntry.argForPrefixAsElement("width", null);
ElementTag height = scriptEntry.argForPrefixAsElement("height", null);
ElementTag scale = scriptEntry.argForPrefixAsElement("scale", null);
boolean tracking = scriptEntry.argAsBoolean("tracking");
ElementTag x = scriptEntry.argForPrefixAsElement("x", "0");
ElementTag y = scriptEntry.argForPrefixAsElement("y", "0");
ColorTag dot = scriptEntry.argForPrefix("dot", ColorTag.class, true);
ElementTag radius = scriptEntry.argForPrefixAsElement("radius", null);
if (!x.isFloat() || !y.isFloat()) {
throw new InvalidArgumentsRuntimeException("Invalid X or Y value!");
}
if (scale != null && !scale.matchesEnum(MapView.Scale.class)) {
throw new InvalidArgumentsRuntimeException("Invalid scale - doesn't match scale enum!");
}
if (create == null && id == null) {
throw new InvalidArgumentsRuntimeException("Must specify a map ID or create a new map!");
}
if (resetLoc == null && image == null && script == null && dot == null) {
throw new InvalidArgumentsRuntimeException("Must specify a valid action to perform!");
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), id, create, reset, resetLoc, image, script, resize, width, height, x, y);
Debug.report(scriptEntry, getName(), id, create, resetLoc, image, script, dot, radius, scale, db("resize", resize), db("tracking", tracking), width, height, x, y);
}
MapView map;
if (create != null) {
map = Bukkit.getServer().createMap(create.getWorld());
scriptEntry.addObject("created_map", new ElementTag(map.getId()));
Debug.echoDebug(scriptEntry, "Created map with id " + map.getId() + ".");
}
else if (id != null) {
else { // id != null
map = Bukkit.getServer().getMap((short) id.asInt());
if (map == null) {
Debug.echoError("No map found for ID '" + id.asInt() + "'!");
return;
}
}
else { // not possible
return;
}
if (reset.asBoolean()) {
if (tracking != null) {
map.setTrackingPosition(true);
}
if (resetLoc != null) {
map.setTrackingPosition(tracking);
if (scale != null) {
map.setScale(MapView.Scale.valueOf(scale.asString().toUpperCase()));
}
List<MapRenderer> oldRenderers = DenizenMapManager.removeDenizenRenderers(map);
for (MapRenderer renderer : oldRenderers) {
map.addRenderer(renderer);
}
if (resetLoc != null) {
map.setCenterX(resetLoc.getBlockX());
map.setCenterZ(resetLoc.getBlockZ());
map.setWorld(resetLoc.getWorld());
}
map.setCenterX(resetLoc.getBlockX());
map.setCenterZ(resetLoc.getBlockZ());
map.setWorld(resetLoc.getWorld());
}
if (script != null) {
DenizenMapManager.removeDenizenRenderers(map);
((MapScriptContainer) script.getContainer()).applyTo(map);
}
if (image != null) {
DenizenMapRenderer dmr = DenizenMapManager.getDenizenRenderer(map);
int wide = width != null ? width.asInt() : resize.asBoolean() ? 128 : 0;
int high = height != null ? height.asInt() : resize.asBoolean() ? 128 : 0;
int wide = width != null ? width.asInt() : resize ? 128 : 0;
int high = height != null ? height.asInt() : resize ? 128 : 0;
if (CoreUtilities.toLowerCase(image.asString()).endsWith(".gif")) {
dmr.autoUpdate = true;
}
dmr.addObject(new MapImage(dmr, x.asString(), y.asString(), "true", false, image.asString(), wide, high));
dmr.hasChanged = true;
}
if (dot != null) {
DenizenMapRenderer dmr = DenizenMapManager.getDenizenRenderer(map);
dmr.addObject(new MapDot(x.asString(), y.asString(), "true", false, radius == null ? "1" : radius.toString(), dot.toString()));
dmr.hasChanged = true;
}
}
}

0 comments on commit e5e9c6a

Please sign in to comment.