Skip to content

Commit

Permalink
map scripts: text, coordinates, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 18, 2021
1 parent 15078dc commit 3bb2f52
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 88 deletions.
Expand Up @@ -25,7 +25,7 @@ public class BlockSpreadsScriptEvent extends BukkitScriptEvent implements Listen
//
// @Cancellable true
//
// @Triggers when a block spreads based on world conditions, EG, when fire spreads, or when mushrooms spread.
// @Triggers when a block spreads based on world conditions, EG, when fire spreads, or when mushrooms spread, or when vines grow.
//
// @Context
// <context.source_location> returns the LocationTag of the block that spread.
Expand Down
Expand Up @@ -56,8 +56,10 @@ public MapScriptContainer(YamlConfiguration configurationSection, String scriptC
//
// 2:
// type: text
// # Specify any text to display.
// # Specify any text to display. Color codes not permitted (unless you know how to format CraftMapCanvas byte-ID color codes).
// text: Hello <player.name>
// # Specify the color of the text as any valid ColorTag.
// color: red
// # Specify a tag to show or hide custom content! Valid for all objects.
// # Note that all inputs other than 'type' for all objects support tags that will be dynamically reparsed per-player each time the map updates.
// visible: <player.name.contains[bob].not>
Expand Down Expand Up @@ -129,7 +131,7 @@ public void applyTo(MapView mapView) {
return;
}
String text = objectSection.getString("text");
added = new MapText(x, y, visible, shouldDebug(), text);
added = new MapText(x, y, visible, shouldDebug(), text, objectSection.getString("color", "black"));
break;
case "cursor":
if (!objectSection.contains("cursor")) {
Expand Down
Expand Up @@ -81,9 +81,7 @@ public static void reloadMaps() {
MapObject object = null;
switch (type) {
case "CURSOR":
object = new MapCursor(xTag, yTag, visibilityTag, debug,
objectsData.getString(objectKey + ".direction"),
objectsData.getString(objectKey + ".cursor"));
object = new MapCursor(xTag, yTag, visibilityTag, debug, objectsData.getString(objectKey + ".direction"), objectsData.getString(objectKey + ".cursor"));
break;
case "IMAGE":
String file = objectsData.getString(objectKey + ".image");
Expand All @@ -92,8 +90,10 @@ public static void reloadMaps() {
object = new MapImage(xTag, yTag, visibilityTag, debug, file, width, height);
break;
case "TEXT":
object = new MapText(xTag, yTag, visibilityTag, debug,
objectsData.getString(objectKey + ".text"));
object = new MapText(xTag, yTag, visibilityTag, debug, objectsData.getString(objectKey + ".text"), objectsData.getString(objectKey + ".color"));
break;
case "DOT":
object = new MapDot(xTag, yTag, visibilityTag, debug, objectsData.getString(objectKey + ".radius"), objectsData.getString(objectKey + ".color"));
break;
}
if (object != null) {
Expand Down
@@ -1,6 +1,7 @@
package com.denizenscript.denizen.utilities.maps;

import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapView;

Expand Down Expand Up @@ -56,11 +57,14 @@ public Map<String, Object> getSaveData() {

@Override
public void render(MapView mapView, MapCanvas mapCanvas, PlayerTag player, UUID uuid) {
org.bukkit.map.MapCursor cursor = new org.bukkit.map.MapCursor((byte) getX(player),
(byte) getY(player), getDirection(player), getType(player).getValue(),
isVisibleTo(player));
mapCanvas.getCursors().addCursor(cursor);
cursors.put(uuid, cursor);
try {
org.bukkit.map.MapCursor cursor = new org.bukkit.map.MapCursor((byte) (getX(player) * 2), (byte) (getY(player) * 2), getDirection(player), getType(player).getValue(), isVisibleTo(player));
mapCanvas.getCursors().addCursor(cursor);
cursors.put(uuid, cursor);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}

}
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import org.bukkit.Color;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapPalette;
Expand Down Expand Up @@ -32,24 +33,29 @@ public Map<String, Object> getSaveData() {

@Override
public void render(MapView mapView, MapCanvas mapCanvas, PlayerTag player, UUID uuid) {
int baseX = getX(player);
int baseY = getY(player);
int radius = (int) Double.parseDouble(tag(radiusTag, player));
Color color = ColorTag.valueOf(tag(colorTag, player), getTagContext(player)).getColor();
for (int x = -radius; x < radius; x++) {
int finalX = baseX + x;
if (finalX >= 128) {
continue;
}
for (int y = -radius; y < radius; y++) {
int finalY = baseY + y;
if (finalY >= 128) {
try {
int baseX = getX(player);
int baseY = getY(player);
int radius = (int) Double.parseDouble(tag(radiusTag, player));
Color color = ColorTag.valueOf(tag(colorTag, player), getTagContext(player)).getColor();
for (int x = -radius; x < radius; x++) {
int finalX = baseX + x;
if (finalX >= 128) {
continue;
}
if (((x + 0.5) * (x + 0.5)) + ((y + 0.5) * (y + 0.5)) <= (radius * radius)) {
mapCanvas.setPixel(finalX, finalY, MapPalette.matchColor(color.getRed(), color.getGreen(), color.getBlue()));
for (int y = -radius; y < radius; y++) {
int finalY = baseY + y;
if (finalY >= 128) {
continue;
}
if (((x + 0.5) * (x + 0.5)) + ((y + 0.5) * (y + 0.5)) <= (radius * radius)) {
mapCanvas.setPixel(finalX, finalY, MapPalette.matchColor(color.getRed(), color.getGreen(), color.getBlue()));
}
}
}
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
}
Expand Up @@ -50,73 +50,84 @@ public Map<String, Object> getSaveData() {

@Override
public void render(MapView mapView, MapCanvas mapCanvas, PlayerTag player, UUID uuid) {
if (actualFile == null) {
actualFile = DenizenMapManager.getActualFile(fileTag);
try {
if (actualFile == null) {
disabled = true;
return;
}
imageIcon = new ImageIcon(actualFile);
image = imageIcon.getImage();
image.getSource().addConsumer(new ImageConsumer() {
@Override
public void setDimensions(int width, int height) {
}
@Override
public void setProperties(Hashtable<?, ?> props) {
}
@Override
public void setColorModel(ColorModel model) {
actualFile = DenizenMapManager.getActualFile(fileTag);
if (actualFile == null) {
disabled = true;
return;
}
@Override
public void setHints(int hintflags) {
}
@Override
public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
// When the internal pixels are updated, the cache is no longer currently.
cachedImageData = null;
imageIcon = new ImageIcon(actualFile);
image = imageIcon.getImage();
image.getSource().addConsumer(new ImageConsumer() {
@Override
public void setDimensions(int width, int height) {
}

@Override
public void setProperties(Hashtable<?, ?> props) {
}

@Override
public void setColorModel(ColorModel model) {
}

@Override
public void setHints(int hintflags) {
}

@Override
public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) {
// When the internal pixels are updated, the cache is no longer currently.
cachedImageData = null;
}

@Override
public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {
}

@Override
public void imageComplete(int status) {
}
});
if (width == 0) {
width = image.getWidth(null);
}
@Override
public void setPixels(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {
if (height == 0) {
height = image.getHeight(null);
}
@Override
public void imageComplete(int status) {
if (width == -1 || height == -1) {
Debug.echoError("Image loading failed (bad width/height) for image " + fileTag);
disabled = true;
return;
}
});
if (width == 0) {
width = image.getWidth(null);
disabled = false;
}
if (height == 0) {
height = image.getHeight(null);
}
if (width == -1 || height == -1) {
Debug.echoError("Image loading failed (bad width/height) for image " + fileTag);
disabled = true;
if (disabled) {
return;
}
disabled = false;
}
if (disabled) {
return;
}
// Use custom functions to draw image to allow transparency and reduce lag intensely
byte[] bytes;
if (cachedImageData == null || image != imageForCache) {
bytes = imageToBytes(image, width, height);
if (bytes == null) {
Debug.echoError("Image loading failed (bad imageToBytes) for image " + fileTag);
disabled = true;
return;
// Use custom functions to draw image to allow transparency and reduce lag intensely
byte[] bytes;
if (cachedImageData == null || image != imageForCache) {
bytes = imageToBytes(image, width, height);
if (bytes == null) {
Debug.echoError("Image loading failed (bad imageToBytes) for image " + fileTag);
disabled = true;
return;
}
cachedImageData = bytes;
imageForCache = image;
}
else {
bytes = cachedImageData;
}
cachedImageData = bytes;
imageForCache = image;
int x = getX(player);
int y = getY(player);
NMSHandler.getPacketHelper().setMapData(mapCanvas, bytes, x, y, this);
}
else {
bytes = cachedImageData;
catch (Throwable ex) {
Debug.echoError(ex);
}
int x = getX(player);
int y = getY(player);
NMSHandler.getPacketHelper().setMapData(mapCanvas, bytes, x, y, this);
}

private static final Color[] bukkitColors;
Expand Down
@@ -1,7 +1,12 @@
package com.denizenscript.denizen.utilities.maps;

import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapPalette;
import org.bukkit.map.MapView;
import org.bukkit.map.MinecraftFont;

Expand All @@ -11,12 +16,13 @@

public class MapText extends MapObject {

protected String textTag;
protected String textTag, colorTag;
protected Map<UUID, String> playerTexts = new HashMap<>();

public MapText(String xTag, String yTag, String visibilityTag, boolean debug, String textTag) {
public MapText(String xTag, String yTag, String visibilityTag, boolean debug, String textTag, String colorTag) {
super(xTag, yTag, visibilityTag, debug);
this.textTag = textTag;
this.colorTag = colorTag;
}

@Override
Expand All @@ -38,15 +44,24 @@ public Map<String, Object> getSaveData() {
Map<String, Object> data = super.getSaveData();
data.put("type", "TEXT");
data.put("text", textTag);
data.put("color", colorTag);
return data;
}

@Override
public void render(MapView mapView, MapCanvas mapCanvas, PlayerTag player, UUID uuid) {
if (!playerTexts.containsKey(uuid)) {
playerTexts.put(uuid, tag(textTag, player));
try {
if (!playerTexts.containsKey(uuid)) {
playerTexts.put(uuid, tag(textTag, player));
}
Color color = ColorTag.valueOf(colorTag == null ? "black" : tag(colorTag, player), getTagContext(player)).getColor();
byte b = MapPalette.matchColor(color.getRed(), color.getGreen(), color.getBlue());
String text = ((char) 167) + Byte.toString(b) + ((char) 59) + getText(player);
mapCanvas.drawText(getX(player), getY(player), MinecraftFont.Font, text);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
mapCanvas.drawText(getX(player), getY(player), MinecraftFont.Font, getText(player));
}

}

0 comments on commit 3bb2f52

Please sign in to comment.