use of com.denizenscript.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.
the class EntityAttributeModifiers method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
try {
MapTag input = mechanism.valueAsType(MapTag.class);
Attributable ent = getAttributable();
for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
continue;
}
for (AttributeModifier modifier : instance.getModifiers()) {
instance.removeModifier(modifier);
}
for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
instance.addModifier(modiferForMap(attr, listValue.asType(MapTag.class, mechanism.context)));
}
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
}
// -->
if (mechanism.matches("add_attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
try {
MapTag input = mechanism.valueAsType(MapTag.class);
Attributable ent = getAttributable();
for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
Attribute attr = Attribute.valueOf(subValue.getKey().str.toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
continue;
}
for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
instance.addModifier(modiferForMap(attr, listValue.asType(MapTag.class, mechanism.context)));
}
}
} catch (Throwable ex) {
Debug.echoError(ex);
}
}
// -->
if (mechanism.matches("remove_attribute_modifiers") && mechanism.requireObject(ListTag.class)) {
ArrayList<String> inputList = new ArrayList<>(mechanism.valueAsType(ListTag.class));
Attributable ent = getAttributable();
for (String toRemove : new ArrayList<>(inputList)) {
if (new ElementTag(toRemove).matchesEnum(Attribute.class)) {
inputList.remove(toRemove);
Attribute attr = Attribute.valueOf(toRemove.toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
continue;
}
for (AttributeModifier modifier : instance.getModifiers()) {
instance.removeModifier(modifier);
}
}
}
for (String toRemove : inputList) {
UUID id = UUID.fromString(toRemove);
for (Attribute attr : Attribute.values()) {
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
continue;
}
for (AttributeModifier modifer : instance.getModifiers()) {
if (modifer.getUniqueId().equals(id)) {
instance.removeModifier(modifer);
break;
}
}
}
}
}
if (mechanism.matches("attributes") && mechanism.hasValue()) {
Deprecations.legacyAttributeProperties.warn(mechanism.context);
Attributable ent = getAttributable();
ListTag list = mechanism.valueAsType(ListTag.class);
for (String str : list) {
List<String> subList = CoreUtilities.split(str, '/');
Attribute attr = Attribute.valueOf(EscapeTagBase.unEscape(subList.get(0)).toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntityType().name());
continue;
}
instance.setBaseValue(Double.parseDouble(subList.get(1)));
for (AttributeModifier modifier : instance.getModifiers()) {
instance.removeModifier(modifier);
}
for (int x = 2; x < subList.size(); x += 4) {
String slot = subList.get(x + 3).toUpperCase();
AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), EscapeTagBase.unEscape(subList.get(x)), Double.parseDouble(subList.get(x + 1)), AttributeModifier.Operation.valueOf(subList.get(x + 2).toUpperCase()), slot.equals("ANY") ? null : EquipmentSlot.valueOf(slot));
instance.addModifier(modifier);
}
}
}
}
use of com.denizenscript.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.
the class ItemRawNBT method convertObjectToNbt.
// <--[language]
// @name Raw NBT Encoding
// @group Useful Lists
// @description
// The item Raw_NBT property encodes and decodes raw NBT data.
// For the sake of inter-compatibility, a special standard format is used to preserve data types.
// This system exists in Denizen primarily for the sake of compatibility with external plugins.
// It should not be used in any scripts that don't rely on data from external plugins.
//
// NBT Tags are encoded as follows:
// CompoundTag: (a fully formed MapTag)
// ListTag: list:(NBT type-code):(a fully formed ListTag)
// ByteArrayTag: byte_array:(a pipe-separated list of numbers)
// IntArrayTag: int_array:(a pipe-separated list of numbers)
// ByteTag: byte:(#)
// ShortTag: short:(#)
// IntTag: int:(#)
// LongTag: long:(#)
// FloatTag: float:(#)
// DoubleTag: double:(#)
// StringTag: string:(text here)
// EndTag: end
//
// -->
public static Tag convertObjectToNbt(String object, TagContext context, String path) {
if (object.startsWith("map@")) {
MapTag map = MapTag.valueOf(object, context);
Map<String, Tag> result = new LinkedHashMap<>();
for (Map.Entry<StringHolder, ObjectTag> entry : map.map.entrySet()) {
try {
result.put(entry.getKey().str, convertObjectToNbt(entry.getValue().toString(), context, path + "." + entry.getKey().str));
} catch (Exception ex) {
Debug.echoError("Object NBT interpretation failed for key '" + path + "." + entry.getKey().str + "'.");
Debug.echoError(ex);
return null;
}
}
return NMSHandler.getInstance().createCompoundTag(result);
} else if (object.startsWith("list:")) {
int nextColonIndex = object.indexOf(':', "list:".length() + 1);
int typeCode = Integer.parseInt(object.substring("list:".length(), nextColonIndex));
String listValue = object.substring(nextColonIndex + 1);
List<Tag> result = new ArrayList<>();
ListTag listTag = ListTag.valueOf(listValue, context);
for (int i = 0; i < listTag.size(); i++) {
try {
result.add(convertObjectToNbt(listTag.get(i), context, path + "[" + i + "]"));
} catch (Exception ex) {
Debug.echoError("Object NBT interpretation failed for list key '" + path + "' at index " + i + ".");
Debug.echoError(ex);
return null;
}
}
return new JNBTListTag(NBTUtils.getTypeClass(typeCode), result);
} else if (object.startsWith("byte_array:")) {
ListTag numberStrings = ListTag.valueOf(object.substring("byte_array:".length()), context);
byte[] result = new byte[numberStrings.size()];
for (int i = 0; i < result.length; i++) {
result[i] = Byte.parseByte(numberStrings.get(i));
}
return new ByteArrayTag(result);
} else if (object.startsWith("int_array:")) {
ListTag numberStrings = ListTag.valueOf(object.substring("int_array:".length()), context);
int[] result = new int[numberStrings.size()];
for (int i = 0; i < result.length; i++) {
result[i] = Integer.parseInt(numberStrings.get(i));
}
return new IntArrayTag(result);
} else if (object.startsWith("byte:")) {
return new ByteTag(Byte.parseByte(object.substring("byte:".length())));
} else if (object.startsWith("short:")) {
return new ShortTag(Short.parseShort(object.substring("short:".length())));
} else if (object.startsWith("int:")) {
return new IntTag(Integer.parseInt(object.substring("int:".length())));
} else if (object.startsWith("long:")) {
return new LongTag(Long.parseLong(object.substring("long:".length())));
} else if (object.startsWith("float:")) {
return new FloatTag(Float.parseFloat(object.substring("float:".length())));
} else if (object.startsWith("double:")) {
return new DoubleTag(Double.parseDouble(object.substring("double:".length())));
} else if (object.startsWith("string:")) {
return new StringTag(object.substring("string:".length()));
} else if (object.equals("end")) {
return new EndTag();
} else {
if (context == null || context.showErrors()) {
Debug.echoError("Unknown raw NBT value: " + object);
}
return null;
}
}
use of com.denizenscript.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.
the class ItemAttributeModifiers method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
Multimap<org.bukkit.attribute.Attribute, AttributeModifier> metaMap = LinkedHashMultimap.create();
MapTag map = mechanism.valueAsType(MapTag.class);
for (Map.Entry<StringHolder, ObjectTag> mapEntry : map.map.entrySet()) {
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(mapEntry.getKey().str.toUpperCase());
for (ObjectTag listValue : CoreUtilities.objectToList(mapEntry.getValue(), mechanism.context)) {
metaMap.put(attr, EntityAttributeModifiers.modiferForMap(attr, (MapTag) listValue));
}
}
ItemMeta meta = item.getItemMeta();
meta.setAttributeModifiers(metaMap);
item.setItemMeta(meta);
}
// -->
if (mechanism.matches("add_attribute_modifiers") && mechanism.requireObject(MapTag.class)) {
ItemMeta meta = item.getItemMeta();
MapTag input = mechanism.valueAsType(MapTag.class);
for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(subValue.getKey().str.toUpperCase());
for (ObjectTag listValue : CoreUtilities.objectToList(subValue.getValue(), mechanism.context)) {
meta.addAttributeModifier(attr, EntityAttributeModifiers.modiferForMap(attr, (MapTag) listValue));
}
}
item.setItemMeta(meta);
}
// -->
if (mechanism.matches("remove_attribute_modifiers") && mechanism.requireObject(ListTag.class)) {
ItemMeta meta = item.getItemMeta();
ArrayList<String> inputList = new ArrayList<>(mechanism.valueAsType(ListTag.class));
for (String toRemove : new ArrayList<>(inputList)) {
if (new ElementTag(toRemove).matchesEnum(org.bukkit.attribute.Attribute.class)) {
inputList.remove(toRemove);
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(toRemove.toUpperCase());
meta.removeAttributeModifier(attr);
}
}
for (String toRemove : inputList) {
UUID id = UUID.fromString(toRemove);
Multimap<org.bukkit.attribute.Attribute, AttributeModifier> metaMap = meta.getAttributeModifiers();
for (org.bukkit.attribute.Attribute attribute : metaMap.keys()) {
for (AttributeModifier modifer : metaMap.get(attribute)) {
if (modifer.getUniqueId().equals(id)) {
meta.removeAttributeModifier(attribute, modifer);
break;
}
}
}
}
item.setItemMeta(meta);
}
}
use of com.denizenscript.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.
the class ClickableCommand method runClickable.
public static void runClickable(UUID id, Player player) {
Clickable clickable = clickables.get(id);
if (clickable == null) {
return;
}
if (clickable.until != 0 && System.currentTimeMillis() > clickable.until) {
clickables.remove(id);
return;
}
if (clickable.forPlayers != null && !clickable.forPlayers.contains(player.getUniqueId())) {
return;
}
if (clickable.remainingUsages > 0) {
clickable.remainingUsages--;
if (clickable.remainingUsages <= 0) {
clickables.remove(id);
}
}
Consumer<ScriptQueue> configure = (queue) -> {
if (clickable.defMap != null) {
for (Map.Entry<StringHolder, ObjectTag> val : clickable.defMap.map.entrySet()) {
queue.addDefinition(val.getKey().str, val.getValue());
}
}
};
ScriptUtilities.createAndStartQueue(clickable.script.getContainer(), clickable.path, new BukkitScriptEntryData(new PlayerTag(player), clickable.npc), null, configure, null, null, clickable.definitions, clickable.context);
}
use of com.denizenscript.denizencore.utilities.text.StringHolder in project Denizen-For-Bukkit by DenizenScript.
the class LegacySavesUpdater method updateLegacySaves.
public static void updateLegacySaves() {
Debug.log("==== UPDATING LEGACY SAVES TO NEW FLAG ENGINE ====");
File savesFile = new File(Denizen.getInstance().getDataFolder(), "saves.yml");
if (!savesFile.exists()) {
Debug.echoError("Legacy update went weird: file doesn't exist?");
return;
}
YamlConfiguration saveSection;
try {
FileInputStream fis = new FileInputStream(savesFile);
String saveData = ScriptHelper.convertStreamToString(fis, false);
fis.close();
if (saveData.trim().length() == 0) {
Debug.log("Nothing to update.");
savesFile.delete();
return;
}
saveSection = YamlConfiguration.load(saveData);
if (saveSection == null) {
Debug.echoError("Something went very wrong: legacy saves file failed to load!");
return;
}
} catch (Throwable ex) {
Debug.echoError(ex);
return;
}
if (!savesFile.renameTo(new File(Denizen.getInstance().getDataFolder(), "saves.yml.bak"))) {
Debug.echoError("Legacy saves file failed to rename!");
}
if (saveSection.contains("Global")) {
Debug.log("==== Update global data ====");
YamlConfiguration globalSection = saveSection.getConfigurationSection("Global");
if (globalSection.contains("Flags")) {
applyFlags("Server", DenizenCore.serverFlagMap, globalSection.getConfigurationSection("Flags"));
}
if (globalSection.contains("Scripts")) {
YamlConfiguration scriptsSection = globalSection.getConfigurationSection("Scripts");
for (StringHolder script : scriptsSection.getKeys(false)) {
YamlConfiguration scriptSection = scriptsSection.getConfigurationSection(script.str);
if (scriptSection.contains("Cooldown Time")) {
long time = Long.parseLong(scriptSection.getString("Cooldown Time"));
TimeTag cooldown = new TimeTag(time);
DenizenCore.serverFlagMap.setFlag("__interact_cooldown." + script.low, cooldown, cooldown);
}
}
}
}
if (saveSection.contains("Players")) {
Debug.log("==== Update player data ====");
YamlConfiguration playerSection = saveSection.getConfigurationSection("Players");
for (StringHolder plPrefix : playerSection.getKeys(false)) {
YamlConfiguration subSection = playerSection.getConfigurationSection(plPrefix.str);
for (StringHolder uuidString : subSection.getKeys(false)) {
if (uuidString.str.length() != 32) {
Debug.echoError("Cannot update data for player with non-ID entry listed: " + uuidString);
continue;
}
try {
UUID id = UUID.fromString(uuidString.str.substring(0, 8) + "-" + uuidString.str.substring(8, 12) + "-" + uuidString.str.substring(12, 16) + "-" + uuidString.str.substring(16, 20) + "-" + uuidString.str.substring(20, 32));
PlayerTag player = PlayerTag.valueOf(id.toString(), CoreUtilities.errorButNoDebugContext);
if (player == null) {
Debug.echoError("Cannot update data for player with id: " + uuidString);
continue;
}
YamlConfiguration actual = subSection.getConfigurationSection(uuidString.str);
AbstractFlagTracker tracker = player.getFlagTracker();
if (actual.contains("Flags")) {
applyFlags(player.identify(), tracker, actual.getConfigurationSection("Flags"));
}
if (actual.contains("Scripts")) {
YamlConfiguration scriptsSection = actual.getConfigurationSection("Scripts");
for (StringHolder script : scriptsSection.getKeys(false)) {
YamlConfiguration scriptSection = scriptsSection.getConfigurationSection(script.str);
if (scriptSection.contains("Current Step")) {
tracker.setFlag("__interact_step." + script, new ElementTag(scriptSection.getString("Current Step")), null);
}
if (scriptSection.contains("Cooldown Time")) {
long time = Long.parseLong(scriptSection.getString("Cooldown Time"));
TimeTag cooldown = new TimeTag(time);
tracker.setFlag("__interact_cooldown." + script, cooldown, cooldown);
}
}
}
player.reapplyTracker(tracker);
} catch (Throwable ex) {
Debug.echoError("Error updating flags for player with ID " + uuidString.str);
Debug.echoError(ex);
}
}
}
}
if (saveSection.contains("NPCs")) {
final YamlConfiguration npcsSection = saveSection.getConfigurationSection("NPCs");
new BukkitRunnable() {
@Override
public void run() {
Debug.log("==== Late update NPC data ====");
for (StringHolder npcId : npcsSection.getKeys(false)) {
YamlConfiguration actual = npcsSection.getConfigurationSection(npcId.str);
NPCTag npc = NPCTag.valueOf(npcId.str, CoreUtilities.errorButNoDebugContext);
if (npc == null) {
Debug.echoError("Cannot update data for NPC with id: " + npcId.str);
continue;
}
AbstractFlagTracker tracker = npc.getFlagTracker();
if (actual.contains("Flags")) {
applyFlags(npc.identify(), tracker, actual.getConfigurationSection("Flags"));
}
npc.reapplyTracker(tracker);
Debug.log("==== Done late-updating NPC data ====");
}
}
}.runTaskLater(Denizen.getInstance(), 3);
}
Denizen.getInstance().saveSaves(true);
Debug.log("==== Done updating legacy saves (except NPCs) ====");
}
Aggregations