Skip to content

Commit

Permalink
Support for item flags on dropped items
Browse files Browse the repository at this point in the history
Adds support for adding item flags https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/inventory/ItemFlag.html to item drops. Formatting for flags is very specific, please see usage below:

```yaml
    # Example without any flags, showing enchantment and name/lore specifications
    drop: diamond_sword@!unbreaking#2!sharpness#3!~name?;lore?;no flags

    # Example with hide enchant and hide attributes flags, showing usage with enchantments and custom name/lore
    drop: diamond_sword@hide_enchants;HIDE_ATTRIBUTES;unbreaking#2!sharpness#3!~name?;lore?;testing attribute in lore;hide_enchants```

Important notes: Item flags **MUST** be defined **before** all item *enchantments, custom name, and lore* specifications, and must be separated with a `;` which is also now **required before** the enchantment/name string begins.
  • Loading branch information
CoolLord22 committed Jun 24, 2024
1 parent e00ad3a commit 96aab5f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/com/gmail/zariust/otherdrops/drop/ItemDrop.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

Expand All @@ -47,6 +48,7 @@ public class ItemDrop extends DropType {
private final IntRange quantity;
private int rolledQuantity;
private final List<CMEnchantment> enchantments;
private final List<ItemFlag> itemFlags;

public ItemDrop(Material mat) {
this(mat, 100.0);
Expand Down Expand Up @@ -85,21 +87,22 @@ public ItemDrop(IntRange amount, Material mat, double percent, List<CMEnchantmen
}

public ItemDrop(IntRange amount, Material mat, int data, double percent, List<CMEnchantment> enchantment, String loreName) {
this(amount, mat, new ItemData(data), percent, enchantment, loreName, null);
this(amount, mat, new ItemData(data), percent, enchantment, loreName, null, null);
}

public ItemDrop(ItemStack stack, double percent) {
this(new IntRange(stack == null ? 1 : stack.getAmount()), stack == null ? null : stack.getType(), stack == null ? null : new ItemData(stack), percent, null, "", null);
this(new IntRange(stack == null ? 1 : stack.getAmount()), stack == null ? null : stack.getType(), stack == null ? null : new ItemData(stack), percent, null, "", null, null);
}

public ItemDrop(IntRange amount, Material mat, Data data, double percent, List<CMEnchantment> enchPass, String loreName, List<String> loreList) { // Rome
public ItemDrop(IntRange amount, Material mat, Data data, double percent, List<CMEnchantment> enchPass, String loreName, List<String> loreList, List<ItemFlag> itemFlags) { // Rome
super(DropCategory.ITEM, percent);
quantity = amount;
material = mat;
durability = data;
this.enchantments = enchPass;
this.displayName = ODVariables.preParse(loreName);
this.lore = ODVariables.preParse(loreList);
this.itemFlags = itemFlags;
}

/**
Expand All @@ -116,6 +119,13 @@ public ItemStack getItem(Target source, DropFlags flags) {
rolledQuantity = quantity.getRandomIn(OtherDrops.rng);
ItemStack stack = new ItemStack(material, rolledQuantity, data);
stack = CommonEnchantments.applyEnchantments(stack, enchantments);
if(itemFlags != null && !itemFlags.isEmpty()) {
ItemMeta meta = stack.getItemMeta();
for(ItemFlag flag : itemFlags) {
meta.addItemFlags(flag);
}
stack.setItemMeta(meta);
}
setItemMeta(stack, source, flags);
return stack;
}
Expand Down Expand Up @@ -270,7 +280,7 @@ public static DropType parse(String drop, String defaultData, IntRange amount, d
if (data == null)
return null; // Data should only be null if invalid for this type, so don't continue

return new ItemDrop(amount, mat, data, chance, item.enchantments, item.displayname, item.lore);
return new ItemDrop(amount, mat, data, chance, item.enchantments, item.displayname, item.lore, item.itemFlags);
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions src/com/gmail/zariust/otherdrops/things/ODItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.gmail.zariust.common.CMEnchantment;
import com.gmail.zariust.common.CommonEnchantments;
import com.gmail.zariust.common.CommonMaterial;
import com.gmail.zariust.common.Verbosity;
import com.gmail.zariust.otherdrops.Log;
import com.gmail.zariust.otherdrops.data.Data;
import com.gmail.zariust.otherdrops.data.ItemData;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemFlag;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -19,6 +21,7 @@ public class ODItem {
private String dataString;
public String enchantmentString;
public List<CMEnchantment> enchantments = new ArrayList<CMEnchantment>();
public List<ItemFlag> itemFlags = new ArrayList<>();
public String displayname;
public final List<String> lore = new ArrayList<String>();
private Material material;
Expand Down Expand Up @@ -73,6 +76,8 @@ public static ODItem parseItem(String drop, String defaultData) {
// displayname found, treat next as lore
value = ChatColor.translateAlternateColorCodes('&', value);
item.lore.add(value);
} else if (getItemFlag(value) != null) {
item.itemFlags.add(getItemFlag(value));
} else {
// first check for enchantment
List<CMEnchantment> ench = CommonEnchantments
Expand Down Expand Up @@ -178,4 +183,13 @@ public static ODItem parseItem(String blockName) {
return parseItem(blockName, "");
}

private static ItemFlag getItemFlag(String flagName) {
for (ItemFlag value : ItemFlag.values()) {
if(value.toString().replaceAll("_", "").equalsIgnoreCase(flagName.replaceAll("_", "")))
return value;
}
Log.logInfo("ODItem Parsing: ItemFlag not found: " + flagName, Verbosity.HIGH);
return null;
}

}

1 comment on commit 96aab5f

@CoolLord22
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolves #45

Please sign in to comment.