Skip to content

Commit

Permalink
entity flags property
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 8, 2021
1 parent 5a65727 commit d48efe4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 5 deletions.
Expand Up @@ -64,6 +64,7 @@ public static void registerMainProperties() {
PropertyParser.registerProperty(EntityExplosionRadius.class, EntityTag.class);
PropertyParser.registerProperty(EntityFirework.class, EntityTag.class);
PropertyParser.registerProperty(EntityFixed.class, EntityTag.class);
PropertyParser.registerProperty(EntityFlags.class, EntityTag.class);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_15)) {
PropertyParser.registerProperty(EntityFlower.class, EntityTag.class);
}
Expand Down
@@ -0,0 +1,85 @@
package com.denizenscript.denizen.objects.properties.entity;

import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.flags.DataPersistenceFlagTracker;
import com.denizenscript.denizencore.flags.MapTagFlagTracker;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;

import java.util.Collection;

public class EntityFlags implements Property {

public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag;
}

public static EntityFlags getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityFlags((EntityTag) entity);
}
}

public static final String[] handledTags = new String[] {
};

public static final String[] handledMechs = new String[] {
"flag_map"
};

private EntityFlags(EntityTag ent) {
entity = ent;
}

EntityTag entity;

@Override
public String getPropertyString() {
DataPersistenceFlagTracker tracker = (DataPersistenceFlagTracker) entity.getFlagTracker();
Collection<String> flagNames = tracker.listAllFlags();
if (flagNames.isEmpty()) {
return null;
}
MapTag flags = new MapTag();
for (String name : flagNames) {
flags.putObject(name, tracker.getRootMap(name));
}
return flags.toString();
}

@Override
public String getPropertyId() {
return "flag_map";
}

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return null;
}

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object EntityTag
// @name flag_map
// @input MapTag
// @description
// Internal setter for the EntityTag flag map.
// -->
if (mechanism.matches("flag_map") && mechanism.requireObject(MapTag.class)) {
MapTagFlagTracker flags = new MapTagFlagTracker(mechanism.valueAsType(MapTag.class));
DataPersistenceFlagTracker tracker = (DataPersistenceFlagTracker) entity.getFlagTracker();
for (String flagName : flags.map.keys()) {
tracker.setRootMap(flagName, flags.getRootMap(flagName));
}
entity.reapplyTracker(tracker);
}
}
}
Expand Up @@ -5,6 +5,7 @@
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.flags.MapTagFlagTracker;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ScriptTag;
Expand All @@ -15,6 +16,8 @@
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import com.denizenscript.denizencore.utilities.text.StringHolder;

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

public class EntityScriptContainer extends ScriptContainer {
Expand Down Expand Up @@ -44,6 +47,16 @@ public class EntityScriptContainer extends ScriptContainer {
// # | All entity scripts MUST have this key!
// entity_type: BASE_ENTITY_TYPE_HERE
//
// # If you want custom data that won't be parsed, use the 'data' root key.
// # | Some entity scripts should have this key!
// data:
// example_key: example value
//
// # You can set flags on the entity when it spawns.
// # | Some item scripts should have this key!
// flags:
// my_flag: my value
//
// # Samples of mechanisms to use (any valid EntityTag mechanisms may be listed like this):
//
// # Whether the entity has the default AI
Expand All @@ -69,6 +82,8 @@ public EntityTag getEntityFrom() {
return getEntityFrom(null, null);
}

public static HashSet<String> nonMechanismKeys = new HashSet<>(Arrays.asList("entity_type", "type", "debug", "custom", "data", "flags"));

public EntityTag getEntityFrom(PlayerTag player, NPCTag npc) {
EntityTag entity;
try {
Expand All @@ -80,9 +95,17 @@ public EntityTag getEntityFrom(PlayerTag player, NPCTag npc) {
else {
throw new Exception("Missing entity_type argument!");
}
if (contains("flags")) {
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));
}
Set<StringHolder> strings = getContents().getKeys(false);
for (StringHolder string : strings) {
if (!string.low.equals("entity_type") && !string.low.equals("type") && !string.low.equals("debug") && !string.low.equals("custom")) {
if (!nonMechanismKeys.contains(string.low)) {
ObjectTag obj = CoreUtilities.objectToTagForm(getContents().get(string.low), context, true, true);
entity.safeAdjust(new Mechanism(string.low, obj, context));
}
Expand Down
Expand Up @@ -104,7 +104,7 @@ public class ItemScriptContainer extends ScriptContainer {
// flags:
// # Each line within the flags section should be a flag name as a key, and the flag value as the value.
// # You can use lists or maps here the way you would expect them to work.
// my_flag: my_value
// my_flag: my value
//
// # You can optionally add crafting recipes for your item script.
// # Note that recipes won't show in the recipe book when you add a new item script, until you either reconnect or use the "resend_recipes" mechanism.
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.denizenscript.denizencore.flags.MapTagBasedFlagTracker;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataHolder;
Expand All @@ -28,21 +29,27 @@ public DataPersistenceFlagTracker(PersistentDataHolder holder, String keyPrefix)

public String keyPrefix = "flag_";

public static AsciiMatcher allowedKeyText = new AsciiMatcher("abcdefghijklmnopqrstuvwxyz_/.-0123456789");

public static String cleanKeyName(String input) {
return allowedKeyText.trimToMatches(CoreUtilities.toLowerCase(input));
}

@Override
public MapTag getRootMap(String key) {
return (MapTag) DataPersistenceHelper.getDenizenKey(holder, keyPrefix + CoreUtilities.toLowerCase(key));
return (MapTag) DataPersistenceHelper.getDenizenKey(holder, keyPrefix + cleanKeyName(key));
}

@Override
public void setRootMap(String key, MapTag map) {
if (map == null) {
DataPersistenceHelper.removeDenizenKey(holder, keyPrefix + CoreUtilities.toLowerCase(key));
DataPersistenceHelper.removeDenizenKey(holder, keyPrefix + cleanKeyName(key));
return;
}
if (map.map.containsKey(expirationString) || map.map.get(valueString) instanceof MapTag) {
holder.getPersistentDataContainer().set(expireNeededKey, PersistentDataType.STRING, "true");
}
DataPersistenceHelper.setDenizenKey(holder, keyPrefix + CoreUtilities.toLowerCase(key), map);
DataPersistenceHelper.setDenizenKey(holder, keyPrefix + cleanKeyName(key), map);
}

@Override
Expand Down

0 comments on commit d48efe4

Please sign in to comment.