use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class EntityAttributeBaseValues method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("attribute_base_values") && mechanism.requireObject(MapTag.class)) {
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;
}
ElementTag value = subValue.getValue().asElement();
if (!value.isDouble()) {
mechanism.echoError("Invalid input '" + value + "': must be a decimal number.");
continue;
}
instance.setBaseValue(value.asDouble());
}
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemPotion method parseEffect.
public static PotionEffect parseEffect(MapTag effectMap, TagContext context) {
PotionEffectType type;
DurationTag duration = new DurationTag(0);
int amplifier = 0;
boolean ambient = true;
boolean particles = true;
boolean icon = false;
if (effectMap.getObject("type") != null) {
String typeString = effectMap.getObject("type").toString();
type = PotionEffectType.getByName(typeString);
if (type == null) {
if (context.showErrors()) {
Debug.echoError("Invalid potion effect type '" + typeString + "': effect type is required.");
}
return null;
}
} else {
if (context.showErrors()) {
Debug.echoError("Invalid potion effect type: effect type is required.");
}
return null;
}
if (effectMap.getObject("amplifier") != null) {
ElementTag amplifierElement = effectMap.getObject("amplifier").asElement();
if (amplifierElement.isInt()) {
amplifier = amplifierElement.asInt();
} else if (context.showErrors()) {
Debug.echoError("Invalid amplifier '" + amplifierElement + "': must be an integer.");
}
}
if (effectMap.getObject("duration") != null) {
ObjectTag durationObj = effectMap.getObject("duration");
if (durationObj.canBeType(DurationTag.class)) {
duration = durationObj.asType(DurationTag.class, context);
} else if (context.showErrors()) {
Debug.echoError("Invalid duration '" + durationObj + "': must be a valid DurationTag");
}
}
if (effectMap.getObject("ambient") != null) {
ElementTag ambientElement = effectMap.getObject("ambient").asElement();
if (ambientElement.isBoolean()) {
ambient = ambientElement.asBoolean();
} else if (context.showErrors()) {
Debug.echoError("Invalid ambient state '" + ambientElement + "': must be a boolean.");
}
}
if (effectMap.getObject("particles") != null) {
ElementTag particlesElement = effectMap.getObject("particles").asElement();
if (particlesElement.isBoolean()) {
particles = particlesElement.asBoolean();
} else if (context.showErrors()) {
Debug.echoError("Invalid particles state '" + particlesElement + "': must be a boolean.");
}
}
if (effectMap.getObject("icon") != null) {
ElementTag iconElement = effectMap.getObject("icon").asElement();
if (iconElement.isBoolean()) {
icon = iconElement.asBoolean();
} else if (context.showErrors()) {
Debug.echoError("Invalid icon state '" + iconElement + "': must be a boolean.");
}
}
return new PotionEffect(type, duration.getTicksAsInt(), amplifier, ambient, particles, icon);
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemPotion method adjust.
@Override
public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("potion_effects")) {
List<ObjectTag> data = new ArrayList<>(CoreUtilities.objectToList(mechanism.value, mechanism.context));
ObjectTag firstObj = data.remove(0);
PotionMeta meta = getMeta();
PotionType type;
boolean upgraded = false;
boolean extended = false;
ColorTag color = null;
if (firstObj.canBeType(MapTag.class)) {
MapTag baseEffect = firstObj.asType(MapTag.class, mechanism.context);
if (baseEffect.getObject("type") != null) {
ElementTag typeElement = baseEffect.getObject("type").asElement();
if (!typeElement.matchesEnum(PotionType.class)) {
mechanism.echoError("Invalid base potion type '" + typeElement + "': type is required");
return;
}
type = PotionType.valueOf(typeElement.asString().toUpperCase());
} else {
mechanism.echoError("No base potion type specified: type is required");
return;
}
if (baseEffect.getObject("upgraded") != null) {
ElementTag upgradedElement = baseEffect.getObject("upgraded").asElement();
if (upgradedElement.isBoolean()) {
upgraded = upgradedElement.asBoolean();
} else {
mechanism.echoError("Invalid upgraded state '" + upgradedElement + "': must be a boolean");
}
}
if (baseEffect.getObject("extended") != null) {
ElementTag extendedElement = baseEffect.getObject("extended").asElement();
if (extendedElement.isBoolean()) {
extended = extendedElement.asBoolean();
} else {
mechanism.echoError("Invalid extended state '" + extendedElement + "': must be a boolean");
}
}
if (baseEffect.getObject("color") != null) {
ObjectTag colorObj = baseEffect.getObject("color");
if (colorObj.canBeType(ColorTag.class)) {
color = colorObj.asType(ColorTag.class, mechanism.context);
} else {
mechanism.echoError("Invalid color '" + colorObj + "': must be a valid ColorTag");
}
}
} else {
String[] d1 = firstObj.toString().split(",");
try {
type = PotionType.valueOf(d1[0].toUpperCase());
} catch (IllegalArgumentException ex) {
mechanism.echoError("Invalid base potion type '" + d1[0] + "': type is required");
return;
}
upgraded = CoreUtilities.equalsIgnoreCase(d1[1], "true");
extended = CoreUtilities.equalsIgnoreCase(d1[2], "true");
if (d1.length > 3) {
ColorTag temp = ColorTag.valueOf(d1[3].replace("&comma", ","), mechanism.context);
if (temp == null) {
mechanism.echoError("Invalid color '" + d1[3] + "': must be a valid ColorTag");
} else {
color = temp;
}
}
}
if (upgraded && !type.isUpgradeable()) {
mechanism.echoError("Cannot upgrade potion of type '" + type.name() + "'");
upgraded = false;
}
if (extended && !type.isExtendable()) {
mechanism.echoError("Cannot extend potion of type '" + type.name() + "'");
extended = false;
}
if (upgraded && extended) {
mechanism.echoError("Cannot both upgrade and extend a potion");
extended = false;
}
if (color != null) {
meta.setColor(color.getColor());
}
meta.setBasePotionData(new PotionData(type, extended, upgraded));
meta.clearCustomEffects();
for (ObjectTag effectObj : data) {
PotionEffect effect;
if (effectObj.canBeType(MapTag.class)) {
effect = parseEffect(effectObj.asType(MapTag.class, mechanism.context), mechanism.context);
} else {
effect = parseEffect(effectObj.toString(), mechanism.context);
}
if (effect != null) {
meta.addCustomEffect(effect, false);
} else {
mechanism.echoError("Invalid potion effect '" + effectObj + "'");
}
}
item.setItemMeta(meta);
}
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemRawNBT method setFullNBT.
public void setFullNBT(ItemTag item, MapTag input, TagContext context, boolean retainOld) {
CompoundTag compoundTag = retainOld ? NMSHandler.getItemHelper().getNbtData(item.getItemStack()) : null;
Map<String, Tag> result = compoundTag == null ? new LinkedHashMap<>() : new LinkedHashMap<>(compoundTag.getValue());
for (Map.Entry<StringHolder, ObjectTag> entry : input.map.entrySet()) {
try {
Tag tag = convertObjectToNbt(entry.getValue().toString(), context, "(item).");
if (tag != null) {
result.put(entry.getKey().str, tag);
}
} catch (Exception ex) {
Debug.echoError("Raw_Nbt input failed for root key '" + entry.getKey().str + "'.");
Debug.echoError(ex);
return;
}
}
compoundTag = NMSHandler.getInstance().createCompoundTag(result);
item.setItemStack(NMSHandler.getItemHelper().setNbtData(item.getItemStack(), compoundTag));
}
use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.
the class ItemRawNBT method jnbtTagToObject.
public static ObjectTag jnbtTagToObject(Tag tag) {
if (tag instanceof CompoundTag) {
MapTag result = new MapTag();
for (Map.Entry<String, Tag> entry : ((CompoundTag) tag).getValue().entrySet()) {
result.putObject(entry.getKey(), jnbtTagToObject(entry.getValue()));
}
return result;
} else if (tag instanceof JNBTListTag) {
ListTag result = new ListTag();
for (Tag entry : ((JNBTListTag) tag).getValue()) {
result.addObject(jnbtTagToObject(entry));
}
return new ElementTag("list:" + NBTUtils.getTypeCode(((JNBTListTag) tag).getType()) + ":" + result.identify());
} else if (tag instanceof ByteArrayTag) {
byte[] data = ((ByteArrayTag) tag).getValue();
StringBuilder output = new StringBuilder(data.length * 4);
for (int i = 0; i < data.length; i++) {
output.append(data[i]).append("|");
}
return new ElementTag("byte_array:" + output.toString());
} else if (tag instanceof IntArrayTag) {
int[] data = ((IntArrayTag) tag).getValue();
StringBuilder output = new StringBuilder(data.length * 4);
for (int i = 0; i < data.length; i++) {
output.append(data[i]).append("|");
}
return new ElementTag("int_array:" + output.toString());
} else if (tag instanceof ByteTag) {
return new ElementTag("byte:" + ((ByteTag) tag).getValue());
} else if (tag instanceof ShortTag) {
return new ElementTag("short:" + ((ShortTag) tag).getValue());
} else if (tag instanceof IntTag) {
return new ElementTag("int:" + ((IntTag) tag).getValue());
} else if (tag instanceof LongTag) {
return new ElementTag("long:" + ((LongTag) tag).getValue());
} else if (tag instanceof FloatTag) {
return new ElementTag("float:" + ((FloatTag) tag).getValue());
} else if (tag instanceof DoubleTag) {
return new ElementTag("double:" + ((DoubleTag) tag).getValue());
} else if (tag instanceof StringTag) {
return new ElementTag("string:" + ((StringTag) tag).getValue());
} else if (tag instanceof EndTag) {
return new ElementTag("end");
} else {
return new ElementTag("unknown:" + tag.getValue());
}
}
Aggregations