Skip to content

Commit

Permalink
container key type check
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 4, 2021
1 parent 4a00de7 commit 8745a42
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 162 deletions.
Expand Up @@ -41,7 +41,7 @@ public String getName() {

public void specialHackRunEvent() {
for (WorldScriptContainer container : ScriptEvent.worldContainers) {
if (container.contains("events.on server prestart")) {
if (container.containsScriptSection("events.on server prestart")) {
ScriptPath path = new ScriptPath(container, "server prestart", "on server prestart");
clone().run(path);
}
Expand Down
Expand Up @@ -36,7 +36,7 @@ public String doAction(String actionName, NPCTag npc, PlayerTag player, Assignme
return determination;
}

if (!assignment.contains("actions.on " + actionName)) {
if (!assignment.containsScriptSection("actions.on " + actionName)) {
return determination;
}

Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.bukkit.projectiles.ProjectileSource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

Expand Down Expand Up @@ -172,7 +173,7 @@ public void describe(CommandSender sender, int page) throws CommandException {
boolean entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Interact Scripts:");
paginator.addLine("<e>Key: <a>Priority <b>Name");
if (assignmentScript.contains("INTERACT SCRIPTS")) {
if (assignmentScript.contains("INTERACT SCRIPTS", List.class)) {
entriesPresent = true;
for (String scriptEntry : assignmentScript.getStringList("INTERACT SCRIPTS")) {
paginator.addLine("<b>" + scriptEntry);
Expand All @@ -188,25 +189,11 @@ public void describe(CommandSender sender, int page) throws CommandException {
}
return;
}
// Scheduled Activities
entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Scheduled Scripts:");
paginator.addLine("<e>Key: <a>Time <b>Name");
if (assignmentScript.contains("SCHEDULED ACTIVITIES")) {
entriesPresent = true;
for (String scriptEntry : assignmentScript.getStringList("SCHEDULED ACTIVITIES")) {
paginator.addLine("<a>" + scriptEntry.split(" ")[0] + "<b> " + scriptEntry.split(" ", 2)[1]);
}
}
if (!entriesPresent) {
paginator.addLine("<c>No scheduled scripts activities.");
}
paginator.addLine("");
// Actions
entriesPresent = false;
paginator.addLine(ChatColor.GRAY + "Actions:");
paginator.addLine("<e>Key: <a>Action name <b>Script Size");
if (assignmentScript.contains("ACTIONS")) {
if (assignmentScript.contains("ACTIONS", Map.class)) {
entriesPresent = true;
}
if (entriesPresent) {
Expand Down
Expand Up @@ -145,7 +145,7 @@ public Map<String, String> rebuildAssignmentConstants() {
}

try {
if (ScriptRegistry.getScriptContainer(assignment).contains("DEFAULT CONSTANTS")) {
if (ScriptRegistry.getScriptContainer(assignment).contains("DEFAULT CONSTANTS", Map.class)) {
for (StringHolder constant : ScriptRegistry.getScriptContainer(assignment)
.getConfigurationSection("DEFAULT CONSTANTS").getKeys(false)) {
assignmentConstants.put(CoreUtilities.toLowerCase(constant.str),
Expand Down
Expand Up @@ -30,7 +30,7 @@ public class ColorTag implements ObjectTag {
//
// Note that a ColorTag is NOT a base dye color (used by wool, etc). That is handled by a separate naming system.
//
// Construction a ColorTag also accepts 'random' to pick a random RGB color, or hex code like '#FF00FF'.
// Constructing a ColorTag also accepts 'random' to pick a random RGB color, or hex code like '#FF00FF'.
//
// A list of accepted color names can be found at
// <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Color.html>
Expand Down
Expand Up @@ -2041,7 +2041,7 @@ else if (meta.hasDisplayName() && CoreUtilities.toLowerCase(meta.getDisplayName(
// @returns ObjectTag
// @description
// If one slot is specified, returns the ItemTag in the specified slot.
// If more than what slot is specified, returns a ListTag(ItemTag) of the item in each given slot.
// If more than one slot is specified, returns a ListTag(ItemTag) of the item in each given slot.
// -->
tagProcessor.registerTag(ObjectTag.class, "slot", (attribute, object) -> {
if (!attribute.hasParam()) {
Expand Down
Expand Up @@ -361,7 +361,7 @@ public String getScriptName() {
}

public void setItemScript(ItemScriptContainer script) {
if (script.contains("NO_ID") && Boolean.valueOf(script.getString("NO_ID"))) {
if (script.contains("NO_ID", String.class) && Boolean.valueOf(script.getString("NO_ID"))) {
return;
}
setItemStack(NMSHandler.getItemHelper().addNbtData(getItemStack(), "DenizenItemScript", new StringTag(CoreUtilities.toLowerCase(script.getName()))));
Expand Down
Expand Up @@ -241,11 +241,13 @@ public void adjust(Mechanism mechanism) {
// @input MapTag
// @description
// Sets the item's enchantments as a map of EnchantmentTags or enchantment names to level.
// For example: - inventory adjust slot:hand enchantments:sharpness=1
// @tags
// <ItemTag.enchantment_map>
// -->
if (mechanism.matches("enchantments")) {
if (mechanism.getValue().asString().startsWith("map@")) {
String val = mechanism.getValue().asString();
if (val.startsWith("map@") || val.startsWith("[") || (val.contains("=") && !val.contains(","))) {
MapTag map = mechanism.valueAsType(MapTag.class);
for (Map.Entry<StringHolder, ObjectTag> enchantments : map.map.entrySet()) {
Enchantment ench = EnchantmentTag.valueOf(enchantments.getKey().low, mechanism.context).enchantment;
Expand Down
Expand Up @@ -4,6 +4,8 @@
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.utilities.YamlConfiguration;

import java.util.List;

public class AssignmentScriptContainer extends ScriptContainer {

// <--[language]
Expand Down Expand Up @@ -53,7 +55,7 @@ public class AssignmentScriptContainer extends ScriptContainer {

public AssignmentScriptContainer(YamlConfiguration configurationSection, String scriptContainerName) {
super(configurationSection, scriptContainerName);
if (contains("interact scripts") && getStringList("interact scripts").size() > 1) {
if (contains("interact scripts", List.class) && getStringList("interact scripts").size() > 1) {
Debug.echoError("Assignment script '" + getName() + "' invalid: assignment scripts should only have ONE interact script in modern Denizen, not multiple!");
}
}
Expand Down
Expand Up @@ -67,22 +67,22 @@ public ItemTag writeBookTo(ItemTag book, TagContext context) {
}
// Get current ItemMeta from the book
BookMeta bookInfo = (BookMeta) book.getItemMeta();
if (contains("title")) {
if (contains("title", String.class)) {
String title = getString("title");
title = TagManager.tag(title, context);
bookInfo.setTitle(title);
}
if (contains("signed")) {
if (contains("signed", String.class)) {
if (getString("signed").equalsIgnoreCase("false")) {
book.getItemStack().setType(Material.WRITABLE_BOOK);
}
}
if (contains("author")) {
if (contains("author", String.class)) {
String author = getString("author");
author = TagManager.tag(author, context);
bookInfo.setAuthor(author);
}
if (contains("text")) {
if (contains("text", List.class)) {
List<String> pages = getStringList("text");
for (String page : pages) {
page = TagManager.tag(page, context);
Expand Down
Expand Up @@ -141,10 +141,10 @@ public CommandScriptContainer(YamlConfiguration configurationSection, String scr
super(configurationSection, scriptContainerName);
CommandScriptHelper.init();
CommandScriptHelper.commandScripts.put(getName(), this);
if (contains("tab complete")) {
if (containsScriptSection("tab complete")) {
hasProcStyleTabComplete = true;
}
if (contains("tab completions")) {
if (contains("tab completions", Map.class)) {
tabCompletionTaggables = new HashMap<>();
YamlConfiguration section = getConfigurationSection("tab completions");
for (StringHolder key : section.getKeys(false)) {
Expand Down Expand Up @@ -270,7 +270,7 @@ public List<String> runTabCompleteProcedure(PlayerTag player, NPCTag npc, Map<St
}

public boolean hasAllowedHelpProcedure() {
return contains("allowed help");
return containsScriptSection("allowed help");
}

public boolean hasTabCompleteProcedure() {
Expand Down
Expand Up @@ -19,6 +19,7 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class EntityScriptContainer extends ScriptContainer {
Expand Down Expand Up @@ -93,22 +94,22 @@ public EntityTag getEntityFrom(PlayerTag player, NPCTag npc) {
EntityTag entity;
try {
TagContext context = new BukkitTagContext(player, npc, new ScriptTag(this));
if (contains("entity_type")) {
if (contains("entity_type", String.class)) {
String entityType = TagManager.tag((getString("entity_type", "")), context);
entity = EntityTag.valueOf(entityType, context);
}
else {
throw new Exception("Missing entity_type argument!");
}
if (contains("flags")) {
if (contains("flags", Map.class)) {
YamlConfiguration flagSection = getConfigurationSection("flags");
MapTagFlagTracker tracker = new MapTagFlagTracker();
for (StringHolder key : flagSection.getKeys(false)) {
tracker.setFlag(key.str, CoreUtilities.objectToTagForm(flagSection.get(key.str), context, true, true), null);
}
entity.safeAdjust(new Mechanism("flag_map", tracker.map, context));
}
if (contains("mechanisms")) {
if (contains("mechanisms", Map.class)) {
YamlConfiguration mechSection = getConfigurationSection("mechanisms");
Set<StringHolder> strings = mechSection.getKeys(false);
for (StringHolder string : strings) {
Expand Down
Expand Up @@ -86,15 +86,12 @@ public InteractScriptContainer(YamlConfiguration configurationSection, String sc
// Find steps/default step in the script
Set<StringHolder> keys;
keys = getConfigurationSection("steps").getKeys(false);

if (contains("requirements")) {
if (contains("requirements", Object.class)) {
Debug.echoError("Interact script '" + getName() + "' is outdated: 'requirements' do not exist in modern Denizen!");
}

if (keys.isEmpty()) {
throw new ExceptionInInitializerError("Could not find any STEPS in " + getName() + "! Is the type on this script correct?");
}

for (StringHolder step1 : keys) {
String step = step1.str;
if (step.contains("*")) {
Expand All @@ -104,7 +101,6 @@ public InteractScriptContainer(YamlConfiguration configurationSection, String sc
set("steps." + step + "*", null);
defaultStep = step;
}

if (step.equalsIgnoreCase("1")) {
defaultStep = step;
}
Expand All @@ -113,17 +109,14 @@ public InteractScriptContainer(YamlConfiguration configurationSection, String sc
}
steps.add(step);
}

}
catch (Exception e) {
Debug.echoError(e);
}

// Make default step the only step if there is only one step
if (defaultStep == null && steps.size() == 1) {
defaultStep = steps.get(0);
}

if (defaultStep == null) {
throw new ExceptionInInitializerError("Must specify a default step in '" + getName() + "'!");
}
Expand All @@ -132,10 +125,6 @@ public InteractScriptContainer(YamlConfiguration configurationSection, String sc
private String defaultStep = null;
private List<String> steps = new ArrayList<>();

public List<String> getStepNames() {
return steps;
}

/**
* <p>Gets the name of the default step for this interact script container. Default step
* is specified by a '*' character on the end of the step name.</p>
Expand Down Expand Up @@ -170,45 +159,7 @@ public String getDefaultStepName() {
*/
public boolean containsTriggerInStep(String step, Class<? extends AbstractTrigger> trigger) {
String triggerName = Denizen.getInstance().triggerRegistry.get(trigger).getName();
return contains("steps." + step + "." + triggerName + " trigger");
}

/**
* <p>Gets a list of ScriptEntries from a Trigger's script key. In order to get the correct
* entries, pass along the id of the exact script for the Trigger, which should be identified
* by using getPossibleTriggersFor(). If no id is specified by passing null, the
* base Script key will be used for the Trigger. If no script matches the trigger or
* trigger/id combination, an empty list is returned.</p>
* <p/>
* <b>Example:</b>
* <tt>
* Example Interact Script: <br/>
* Type: interact <br/>
* Steps: <br/>
* Current Step: <--- checked with Player object <br/>
* Click Trigger: <--- obtained with the Trigger class <br/>
* <br/>
* id: <--- id of the specific trigger script/script options <br/>
* Script: <br/>
* - ... <--- entries obtained if id matches <br/>
* - ... <br/>
* <br/>
* Script: <--- script that is referenced if NO id is specified <br/>
* - ... <--- entries returned <br/>
* - ... <br/>
* </tt>
* <p/>
* <p>Note: This is handled internally with the parse() method in AbstractTrigger, so for
* basic triggers, you probably don't need to even call this.</p>
*
* @param trigger the class of the Trigger to use
* @param player the Player involved
* @param npc the NPC involved
* @param id the id of the Trigger Script, optional
* @return a list of ScriptEntries from the script or an empty list if no script was found
*/
public List<ScriptEntry> getEntriesFor(Class<? extends AbstractTrigger> trigger, PlayerTag player, NPCTag npc, String id) {
return getEntriesFor(trigger, player, npc, id, false);
return contains("steps." + step + "." + triggerName + " trigger", String.class);
}

public List<ScriptEntry> getEntriesFor(Class<? extends AbstractTrigger> trigger, PlayerTag player, NPCTag npc, String id, boolean quiet) {
Expand All @@ -217,7 +168,7 @@ public List<ScriptEntry> getEntriesFor(Class<? extends AbstractTrigger> trigger,
// Check for entries
String key = "steps." + InteractScriptHelper.getCurrentStep(player, getName()) + "."
+ triggerName + " trigger." + (id == null ? "script" : id + ".script");
if (contains(key)) {
if (containsScriptSection(key)) {
// Entries exist, so get them and return the list of ScriptEntries
return getEntries(new BukkitScriptEntryData(player, npc), key);
// No entries, so just return an empty list to avoid NPEs
Expand Down Expand Up @@ -261,15 +212,14 @@ public List<ScriptEntry> getEntriesFor(Class<? extends AbstractTrigger> trigger,
* @param player The Denizen Player object for the player who triggered it
* @return A map of options in the trigger's script, excluding a plain 'script'
*/

public Map<String, String> getIdMapFor(Class<? extends AbstractTrigger> trigger, PlayerTag player) {
// Get the trigger name
String triggerName = Denizen.getInstance().triggerRegistry.get(trigger).getName();
// Get the step
String step = InteractScriptHelper.getCurrentStep(player, getName());
// Check for entries
String keyBase = "steps." + step + "." + triggerName + " trigger";
if (contains(keyBase)) {
if (contains(keyBase, Map.class)) {
// Trigger exists in Player's current step, get ids.
Map<String, String> idMap = new LinkedHashMap<>();
// Iterate through IDs to build the idMap
Expand All @@ -291,21 +241,4 @@ public Map<String, String> getIdMapFor(Class<? extends AbstractTrigger> trigger,
return Collections.emptyMap();
}
}

public String getTriggerOptionFor(Class<? extends AbstractTrigger> trigger, PlayerTag player, String id, String option) {
// Get the trigger name
String triggerName = Denizen.getInstance().triggerRegistry.get(trigger).getName();
// Get the step
String step = InteractScriptHelper.getCurrentStep(player, getName());
return getString("steps." + step + "." + triggerName + " trigger"
+ (id == null ? "" : "." + id) + "." + option);
}

public boolean hasTriggerOptionFor(Class<? extends AbstractTrigger> trigger, PlayerTag player, String id, String option) {
// Get the trigger name
String triggerName = Denizen.getInstance().triggerRegistry.get(trigger).getName();
// Get the step
String step = InteractScriptHelper.getCurrentStep(player, getName());
return contains("steps." + step + "." + triggerName + " trigger" + (id == null ? "" : "." + id) + "." + option);
}
}
Expand Up @@ -23,7 +23,7 @@ public static InteractScriptContainer getInteractScript(NPCTag npc) {
if (assignmentScript == null) {
return null;
}
if (!assignmentScript.contains("interact scripts")) {
if (!assignmentScript.contains("interact scripts", List.class)) {
return null;
}
List<String> assignedScripts = assignmentScript.getStringList("interact scripts");
Expand Down

0 comments on commit 8745a42

Please sign in to comment.