Skip to content

Commit

Permalink
rename item.flags property to item.hides, adapt format to be cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 17, 2020
1 parent 560ab4d commit 4ebe6e3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
Expand Up @@ -114,7 +114,7 @@ public static void registermainProperties() {
}
PropertyParser.registerProperty(ItemEnchantments.class, ItemTag.class);
PropertyParser.registerProperty(ItemFirework.class, ItemTag.class);
PropertyParser.registerProperty(ItemFlags.class, ItemTag.class);
PropertyParser.registerProperty(ItemHidden.class, ItemTag.class);
PropertyParser.registerProperty(ItemInventory.class, ItemTag.class);
PropertyParser.registerProperty(ItemKnowledgeBookRecipes.class, ItemTag.class);
PropertyParser.registerProperty(ItemLock.class, ItemTag.class);
Expand Down
Expand Up @@ -6,39 +6,41 @@
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class ItemFlags implements Property {
public class ItemHidden implements Property {

public static boolean describes(ObjectTag item) {
// All items can have flags
return item instanceof ItemTag && ((ItemTag) item).getItemStack().getType() != Material.AIR;
}

public static ItemFlags getFrom(ObjectTag _item) {
public static ItemHidden getFrom(ObjectTag _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemFlags((ItemTag) _item);
return new ItemHidden((ItemTag) _item);
}
}

public static final String[] handledTags = new String[] {
"flags"
"flags", "hides"
};

public static final String[] handledMechs = new String[] {
"flags"
"flags", "hides"
};

private ItemFlags(ItemTag _item) {
private ItemHidden(ItemTag _item) {
item = _item;
}

@Deprecated
public ListTag flags() {
ListTag output = new ListTag();
ItemStack itemStack = item.getItemStack();
Expand All @@ -50,6 +52,17 @@ public ListTag flags() {
return output;
}

public ListTag hides() {
ListTag output = new ListTag();
ItemStack itemStack = item.getItemStack();
if (itemStack.hasItemMeta()) {
for (ItemFlag flag : itemStack.getItemMeta().getItemFlags()) {
output.add(flag.name().substring("HIDE_".length()));
}
}
return output;
}

ItemTag item;

@Override
Expand All @@ -60,28 +73,31 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
}

// <--[tag]
// @attribute <ItemTag.flags>
// @attribute <ItemTag.hides>
// @returns ListTag
// @mechanism ItemTag.flags
// @mechanism ItemTag.hides
// @group properties
// @description
// Returns a list of flags set on this item.
// Valid flags include: HIDE_ATTRIBUTES, HIDE_DESTROYS, HIDE_ENCHANTS, HIDE_PLACED_ON, HIDE_POTION_EFFECTS, and HIDE_UNBREAKABLE
// NOTE: 'HIDE_POTION_EFFECTS' also hides banner patterns.
// Returns a list of item data types to be hidden from view on this item.
// Valid hide types include: ATTRIBUTES, DESTROYS, ENCHANTS, PLACED_ON, POTION_EFFECTS, and UNBREAKABLE
// NOTE: 'POTION_EFFECTS' also hides banner patterns.
// -->
if (attribute.startsWith("hides")) {
return hides().getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("flags")) {
return flags()
.getObjectAttribute(attribute.fulfill(1));
Deprecations.itemFlagsProperty.warn(attribute.context);
return flags().getObjectAttribute(attribute.fulfill(1));
}

return null;
}

@Override
public String getPropertyString() {
ListTag flags = flags();
if (flags.size() > 0) {
return flags().identify();
ListTag hidden = hides();
if (hidden.size() > 0) {
return hidden.identify();
}
else {
return null;
Expand All @@ -90,27 +106,33 @@ public String getPropertyString() {

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

@Override
public void adjust(Mechanism mechanism) {

// <--[mechanism]
// @object ItemTag
// @name flags
// @name hides
// @input ListTag
// @description
// Sets the item's meta flag set.
// Use "HIDE_ALL" to automatically apply all flags.
// Sets the item's list of data types to hide.
// Use "ALL" to automatically hide all hideable item data.
// @tags
// <ItemTag.flags>
// <ItemTag.hides>
// -->
if (mechanism.matches("flags")) {
if (mechanism.matches("flags") || mechanism.matches("hides")) {
if (mechanism.matches("flags")) {
Deprecations.itemFlagsProperty.warn(mechanism.context);
}
ItemMeta meta = item.getItemStack().getItemMeta();
meta.removeItemFlags(ItemFlag.values());
ListTag new_flags = mechanism.valueAsType(ListTag.class);
for (String str : new_flags) {
ListTag new_hides = mechanism.valueAsType(ListTag.class);
for (String str : new_hides) {
if (!str.startsWith("HIDE_")) {
str = "HIDE_" + str;
}
if (str.equalsIgnoreCase("HIDE_ALL")) {
meta.addItemFlags(ItemFlag.values());
}
Expand Down

0 comments on commit 4ebe6e3

Please sign in to comment.