Skip to content

Commit

Permalink
Config-defined custom colors
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Apr 10, 2021
1 parent 8befcef commit c2cb976
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 10 deletions.
7 changes: 4 additions & 3 deletions plugin/src/main/java/com/denizenscript/denizen/Denizen.java
Expand Up @@ -311,6 +311,7 @@ else if (javaVersion.startsWith("9") || javaVersion.startsWith("1.9") || javaVer
writer.write(updated);
writer.close();
configOutput.close();
reloadConfig();
}
}
catch (Exception e) {
Expand All @@ -319,9 +320,9 @@ else if (javaVersion.startsWith("9") || javaVersion.startsWith("1.9") || javaVer
try {
worldScriptHelper = new BukkitWorldScriptHelper();
itemScriptHelper = new ItemScriptHelper();
InventoryScriptHelper in_helper = new InventoryScriptHelper();
EntityScriptHelper es_helper = new EntityScriptHelper();
CommandScriptHelper cs_helper = new CommandScriptHelper();
new InventoryScriptHelper();
new EntityScriptHelper();
new CommandScriptHelper();
}
catch (Exception e) {
Debug.echoError(e);
Expand Down
Expand Up @@ -120,7 +120,6 @@ else if (!scriptEntry.hasObject("script")
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {

scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("force_along")
Expand Down Expand Up @@ -194,18 +193,17 @@ public void execute(final ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
final ScriptTag script = scriptEntry.getObjectTag("script");
final ListTag definitions = scriptEntry.getObjectTag("definitions");
final double speed = scriptEntry.getElement("speed").asDouble();
final int maxTicks = ((DurationTag) scriptEntry.getObject("duration")).getTicksAsInt();
ElementTag speedElement = scriptEntry.getElement("speed");
DurationTag duration = (DurationTag) scriptEntry.getObject("duration");
ElementTag force_along = scriptEntry.getElement("force_along");
ElementTag precision = scriptEntry.getElement("precision");
ElementTag ignore_collision = scriptEntry.getElement("ignore_collision");
final boolean ignoreCollision = ignore_collision != null && ignore_collision.asBoolean();
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("origin", originEntity != null ? originEntity : originLocation) +
ArgumentHelper.debugObj("entities", entities.toString()) +
ArgumentHelper.debugObj("destination", destination) +
ArgumentHelper.debugObj("speed", speed) +
ArgumentHelper.debugObj("max ticks", maxTicks) +
destination.debug() +
speedElement.debug() +
duration.debug() +
(script != null ? script.debug() : "") +
force_along.debug() +
precision.debug() +
Expand All @@ -214,6 +212,9 @@ public void execute(final ScriptEntry scriptEntry) {
(ignore_collision != null ? ignore_collision.debug() : "") +
(definitions != null ? definitions.debug() : ""));
}
final boolean ignoreCollision = ignore_collision != null && ignore_collision.asBoolean();
final double speed = speedElement.asDouble();
final int maxTicks = duration.getTicksAsInt();
final boolean forceAlong = force_along.asBoolean();
// Keep a ListTag of entities that can be called using <entry[name].pushed_entities> later in the script queue
final ListTag entityList = new ListTag();
Expand Down
@@ -0,0 +1,51 @@
package com.denizenscript.denizen.tags.core;

import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.md_5.bungee.api.ChatColor;

import java.util.HashMap;

public class CustomColorTagBase {

public static HashMap<String, String> customColorsRaw = new HashMap<>();

public static String defaultColorRaw = ChatColor.WHITE.toString();

public static HashMap<String, String> customColors = new HashMap<>();

public static String defaultColor = null;

public CustomColorTagBase() {

// <--[tag]
// @attribute <&[<color>]>
// @returns ElementTag
// @description
// Returns a custom color value based on the common base color names defined in the Denizen config file.
// If the color name is unrecognized, returns the value of color named 'default'.
// Default color names are 'base', 'emphasis', 'warning', 'error'.
// -->
TagManager.registerTagHandler("&", attribute -> {
if (!attribute.hasContext(1)) {
return null;
}
String key = CoreUtilities.toLowerCase(attribute.getContext(1));
String result = customColors.get(key);
if (result != null) {
return new ElementTag(result);
}
String unparsed = customColorsRaw.get(key);
if (unparsed != null) {
result = TagManager.tag(unparsed, attribute.context);
customColors.put(key, result);
return new ElementTag(result);
}
if (defaultColor == null) {
defaultColor = TagManager.tag(defaultColorRaw, attribute.context);
}
return new ElementTag(defaultColor);
});
}
}
Expand Up @@ -559,6 +559,7 @@ else if (recipe instanceof CookingRecipe<?>) {
// @description
// Returns a list of all registered advancement names.
// Generally used with <@link tag PlayerTag.has_advancement>.
// See also <@link url https://minecraft.fandom.com/wiki/Advancement>.
// -->
if (attribute.startsWith("advancement_types") || attribute.startsWith("list_advancements")) {
if (attribute.matches("list_advancements")) {
Expand Down
Expand Up @@ -78,6 +78,7 @@ public static void registerMainTagHandlers() {
new TradeTagBase();
new WorldTagBase();
// Other bases
new CustomColorTagBase();
new ServerTagBase();
new TextTagBase();
new ParseTagBase();
Expand Down
Expand Up @@ -2,13 +2,15 @@

import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.scripts.commands.entity.RemoveCommand;
import com.denizenscript.denizen.tags.core.CustomColorTagBase;
import com.denizenscript.denizen.utilities.flags.PlayerFlagHandler;
import com.denizenscript.denizencore.flags.MapTagBasedFlagTracker;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.scripts.ScriptHelper;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.FutureWarning;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;

import java.nio.charset.Charset;
Expand Down Expand Up @@ -91,6 +93,16 @@ public static void refillCache() {
PlayerFlagHandler.asyncPreload = config.getBoolean("Saves.Load async on login", true);
MapTagBasedFlagTracker.skipAllCleanings = config.getBoolean("Saves.Skip flag cleaning", false);
RemoveCommand.alwaysWarnOnMassRemove = config.getBoolean("Commands.Remove.Always warn on mass delete", false);
ConfigurationSection colorSection = config.getConfigurationSection("Colors");
if (colorSection != null) {
CustomColorTagBase.customColorsRaw.clear();
CustomColorTagBase.customColors.clear();
CustomColorTagBase.defaultColor = null;
for (String key : colorSection.getKeys(false)) {
CustomColorTagBase.customColorsRaw.put(CoreUtilities.toLowerCase(key), colorSection.getString(key));
}
CustomColorTagBase.defaultColorRaw = CustomColorTagBase.customColorsRaw.getOrDefault("default", CustomColorTagBase.defaultColorRaw);
}
}

private static boolean cache_showDebug = true, cache_overrideHelp, cache_useDefaultScriptPath,
Expand Down
9 changes: 9 additions & 0 deletions plugin/src/main/resources/config.yml
Expand Up @@ -199,3 +199,12 @@ Packets:
# It also enables hiding item script IDs and most likely has no real reason to be disabled.
# Note that changing this setting requires a full server restart.
Interception: true

# This is a special map of custom colors by name, for the '&' tag base to use. 'default' is used when the name is unrecognized. You can add extra keys here.
# Tags work here and will be pre-parsed at load time. RGB color codes work here.
Colors:
base: <&2>
emphasis: <&b>
error: <red>
warning: <yellow>
default: <white>

0 comments on commit c2cb976

Please sign in to comment.