Skip to content

Commit

Permalink
Merge pull request #118 from HelpChat/fix/115-item-flags
Browse files Browse the repository at this point in the history
fix: item flags not working in 1.20.5+ & deprecate hide_ options
  • Loading branch information
Kqliber authored Jul 3, 2024
2 parents 2472f1b + 0b2e0cf commit 1e796fe
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.gradle
.idea
build
test-server/
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -33,6 +34,7 @@
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.banner.PatternType;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand Down Expand Up @@ -732,6 +734,8 @@ private Map<Integer, TreeMap<Integer, MenuItem>> loadMenuItems(FileConfiguration
continue;
}

checkForDeprecatedItemOptions(c.getConfigurationSection(currentPath), name);

MenuItemOptions.MenuItemOptionsBuilder builder = MenuItemOptions.builder()
.material(material)
.baseColor(Optional.ofNullable(c.getString(currentPath + "base_color"))
Expand Down Expand Up @@ -1113,13 +1117,14 @@ private RequirementList getRequirements(FileConfiguration c, String path) {
continue;
}

RequirementType type = RequirementType.getType(c.getString(rPath + ".type"));
String stringType = c.getString(rPath + ".type");
RequirementType type = RequirementType.getType(stringType);

if (type == null) {
DeluxeMenus.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Requirement type at path: " + rPath + " is not a valid requirement type!"
"Requirement type '" + stringType + "' at path '" + rPath + "' is not valid!"
);
continue;
}
Expand Down Expand Up @@ -1517,6 +1522,27 @@ public void onClick(@NotNull final MenuHolder holder) {
return handler;
}

private void checkForDeprecatedItemOptions(ConfigurationSection config, String menuName) {
final BiConsumer<String, ItemFlag> oldItemFlagOptionCheck = (option, itemFlag) -> {
if (!config.contains(option)) {
return;
}

DeluxeMenus.debug(
DebugLevel.HIGHEST,
Level.WARNING,
String.format(
"Option '%s' of item '%s' in menu '%s' is deprecated and will be removed in the future. Replace it with item_flags: [%s].",
option, config.getName(), menuName, itemFlag
)
);
};

oldItemFlagOptionCheck.accept("hide_attributes", ItemFlag.HIDE_ATTRIBUTES);
oldItemFlagOptionCheck.accept("hide_enchantments", ItemFlag.HIDE_ENCHANTS);
oldItemFlagOptionCheck.accept("hide_unbreakable", ItemFlag.HIDE_UNBREAKABLE);
}

public void debug(String... messages) {
DeluxeMenus.debug(DebugLevel.LOWEST, Level.INFO, messages);
}
Expand Down
34 changes: 14 additions & 20 deletions src/main/java/com/extendedclip/deluxemenus/menu/MenuItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) {
// This checks if a lore should be kept from the hooked item, and then if a lore exists on the item
// ItemMeta.getLore is nullable. In that case, we just create a new ArrayList so we don't add stuff to a null list.
List<String> itemLore = Objects.requireNonNullElse(itemMeta.getLore(), new ArrayList<>());
// Ensures backwards compadibility with how hooked items are currently handled
// Ensures backwards compatibility with how hooked items are currently handled
LoreAppendMode mode = this.options.loreAppendMode().orElse(LoreAppendMode.OVERRIDE);
if (!this.options.hasLore() && this.options.loreAppendMode().isEmpty()) mode = LoreAppendMode.IGNORE;
switch (mode) {
Expand All @@ -252,28 +252,10 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) {

itemMeta.setLore(lore);

if (!this.options.itemFlags().isEmpty()) {
for (final ItemFlag flag : this.options.itemFlags()) {
itemMeta.addItemFlags(flag);
}
}

if (this.options.hideAttributes()) {
itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
}

if (this.options.hideEnchants()) {
itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}

if (this.options.unbreakable()) {
itemMeta.setUnbreakable(true);
}

if (this.options.hideUnbreakable()) {
itemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
}

if (VersionHelper.HAS_ARMOR_TRIMS && ItemUtils.hasArmorMeta(itemStack)) {
final Optional<String> trimMaterialName = this.options.trimMaterial();
final Optional<String> trimPatternName = this.options.trimPattern();
Expand Down Expand Up @@ -369,7 +351,7 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) {
}

if (!(itemMeta instanceof EnchantmentStorageMeta) && !this.options.enchantments().isEmpty()) {
itemStack.addUnsafeEnchantments(this.options.enchantments());
this.options.enchantments().forEach((enchantment, level) -> itemMeta.addEnchant(enchantment, level, true));
}

if (this.options.lightLevel().isPresent() && itemMeta instanceof BlockDataMeta) {
Expand Down Expand Up @@ -407,6 +389,18 @@ public ItemStack getItemStack(@NotNull final MenuHolder holder) {
}
}

if (!this.options.itemFlags().isEmpty()) {
for (final ItemFlag flag : this.options.itemFlags()) {
itemMeta.addItemFlags(flag);

if (flag == ItemFlag.HIDE_ATTRIBUTES && VersionHelper.HAS_DATA_COMPONENTS) {
itemMeta.setAttributeModifiers(null);
}
}
}

itemStack.setItemMeta(itemMeta);

if (NbtProvider.isAvailable()) {
if (this.options.nbtString().isPresent()) {
final String tag = holder.setPlaceholdersAndArguments(this.options.nbtString().get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class MenuItemOptions {

Expand All @@ -38,12 +41,9 @@ public class MenuItemOptions {
private final Map<Enchantment, Integer> enchantments;
private final List<PotionEffect> potionEffects;
private final List<Pattern> bannerMeta;
private final List<ItemFlag> itemFlags;
private final Set<ItemFlag> itemFlags = new HashSet<>();

private final boolean unbreakable;
private final boolean hideAttributes;
private final boolean hideEnchants;
private final boolean hideUnbreakable;

private final boolean displayNameHasPlaceholders;
private final boolean loreHasPlaceholders;
Expand Down Expand Up @@ -94,11 +94,8 @@ private MenuItemOptions(final @NotNull MenuItemOptionsBuilder builder) {
this.enchantments = builder.enchantments;
this.potionEffects = builder.potionEffects;
this.bannerMeta = builder.bannerMeta;
this.itemFlags = builder.itemFlags;
this.itemFlags.addAll(builder.itemFlags);
this.unbreakable = builder.unbreakable;
this.hideAttributes = builder.hideAttributes;
this.hideEnchants = builder.hideEnchants;
this.hideUnbreakable = builder.hideUnbreakable;
this.displayNameHasPlaceholders = builder.displayNameHasPlaceholders;
this.loreHasPlaceholders = builder.loreHasPlaceholders;
this.nbtString = builder.nbtString;
Expand Down Expand Up @@ -199,26 +196,14 @@ public void headType(final @Nullable HeadType headType) {
return bannerMeta;
}

public @NotNull List<ItemFlag> itemFlags() {
public @NotNull Set<ItemFlag> itemFlags() {
return itemFlags;
}

public boolean unbreakable() {
return unbreakable;
}

public boolean hideAttributes() {
return hideAttributes;
}

public boolean hideEnchants() {
return hideEnchants;
}

public boolean hideUnbreakable() {
return hideUnbreakable;
}

public boolean displayNameHasPlaceholders() {
return displayNameHasPlaceholders;
}
Expand Down Expand Up @@ -338,9 +323,6 @@ public boolean updatePlaceholders() {
.bannerMeta(this.bannerMeta)
.itemFlags(this.itemFlags)
.unbreakable(this.unbreakable)
.hideAttributes(this.hideAttributes)
.hideEnchants(this.hideEnchants)
.hideUnbreakable(this.hideUnbreakable)
.nbtString(this.nbtString)
.nbtInt(this.nbtInt)
.nbtStrings(this.nbtStrings)
Expand Down Expand Up @@ -384,12 +366,9 @@ public static class MenuItemOptionsBuilder {
private Map<Enchantment, Integer> enchantments = Collections.emptyMap();
private List<PotionEffect> potionEffects = Collections.emptyList();
private List<Pattern> bannerMeta = Collections.emptyList();
private List<ItemFlag> itemFlags = Collections.emptyList();
private final Set<ItemFlag> itemFlags = new HashSet<>();

private boolean unbreakable;
private boolean hideAttributes;
private boolean hideEnchants;
private boolean hideUnbreakable;

private boolean displayNameHasPlaceholders;
private boolean loreHasPlaceholders;
Expand Down Expand Up @@ -512,8 +491,8 @@ public MenuItemOptionsBuilder bannerMeta(final @NotNull List<Pattern> bannerMeta
return this;
}

public MenuItemOptionsBuilder itemFlags(final @NotNull List<ItemFlag> itemFlags) {
this.itemFlags = itemFlags;
public MenuItemOptionsBuilder itemFlags(final @NotNull Collection<ItemFlag> itemFlags) {
this.itemFlags.addAll(itemFlags);
return this;
}

Expand All @@ -522,18 +501,36 @@ public MenuItemOptionsBuilder unbreakable(final boolean unbreakable) {
return this;
}

/**
* @deprecated Use {@link #itemFlags(Collection)} with {@link ItemFlag#HIDE_ATTRIBUTES}
*/
@Deprecated()
public MenuItemOptionsBuilder hideAttributes(final boolean hideAttributes) {
this.hideAttributes = hideAttributes;
if (hideAttributes) {
this.itemFlags.add(ItemFlag.HIDE_ATTRIBUTES);
}
return this;
}

/**
* @deprecated Use {@link #itemFlags(Collection)} with {@link ItemFlag#HIDE_ENCHANTS}
*/
@Deprecated
public MenuItemOptionsBuilder hideEnchants(final boolean hideEnchants) {
this.hideEnchants = hideEnchants;
if (hideEnchants) {
this.itemFlags.add(ItemFlag.HIDE_ENCHANTS);
}
return this;
}

/**
* @deprecated Use {@link #itemFlags(Collection)} with {@link ItemFlag#HIDE_UNBREAKABLE}
*/
@Deprecated
public MenuItemOptionsBuilder hideUnbreakable(final boolean hideUnbreakable) {
this.hideUnbreakable = hideUnbreakable;
if (hideUnbreakable) {
this.itemFlags.add(ItemFlag.HIDE_UNBREAKABLE);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public final class VersionHelper {
private static final String PACKAGE_NAME = Bukkit.getServer().getClass().getPackage().getName();
public static final String NMS_VERSION = PACKAGE_NAME.substring(PACKAGE_NAME.lastIndexOf('.') + 1);

// Data components
private static final int V1_20_5 = 1_20_5;
// ArmorTrims
private static final int V1_19_4 = 1194;
// PlayerProfile API
Expand All @@ -42,6 +44,10 @@ public final class VersionHelper {

private static final boolean IS_PAPER = checkPaper();

/**
* Checks if the current version includes the <a href="https://minecraft.wiki/w/Data_component_format">Data Components</a>
*/
public static final boolean HAS_DATA_COMPONENTS = CURRENT_VERSION >= V1_20_5;

/**
* Checks if the current version includes the ArmorTrims API
Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/advanced_menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ items:
slot: 11
priority: 1
update: true
hide_attributes: true
item_flags:
- HIDE_ATTRIBUTES
display_name: '&bExample Kit'
lore:
- ''
Expand All @@ -46,7 +47,8 @@ items:
slot: 11
priority: 2
update: true
hide_attributes: true
item_flags:
- HIDE_ATTRIBUTES
display_name: '&cExample Kit Unavailable'
lore:
- '&7This kit is on cooldown!'
Expand All @@ -62,7 +64,8 @@ items:
slot: 11
priority: 3
update: true
hide_attributes: true
item_flags:
- HIDE_ATTRIBUTES
display_name: '&7Example Kit'
lore:
- '&7You do not have permission for this kit!'
Expand Down

0 comments on commit 1e796fe

Please sign in to comment.