Skip to content

Commit

Permalink
Add changes for moving away from getItemMeta/hasItemMeta (#775)
Browse files Browse the repository at this point in the history
* add initial changes for moving away from getItemMeta/hasItemMeta

* import persistentdatatype

---------

Signed-off-by: Ryder Belserion <[email protected]>
  • Loading branch information
ryderbelserion authored Aug 23, 2024
1 parent dbf6faf commit 5a18a8c
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 110 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ Crate:
- Fixed an issue where CMI likely wouldn't be detected
### Changes:
- Removed all getItemMeta/hasItemMeta calls for checking PersistentDataContainer
- We now check ItemStack#PersistentDataContainerView which no longer relies on ItemStack#getItemMeta
- TLDR: stonks
- Right click now opens the crate menu as well.
- Checked location strings instead of object ids
- Simplified multiple location getters, don't need to get the same location 3 times if we aren't changing it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ public void run(InventoryClickEvent event) {

if (item == null || item.getType() == Material.AIR) return;

if (!item.hasItemMeta()) return;

final Crate crate = this.crateManager.getCrateFromName(ItemUtils.getKey(item.getItemMeta().getPersistentDataContainer()));
final Crate crate = this.crateManager.getCrateFromName(ItemUtils.getKey(item.getPersistentDataContainer()));

if (crate == null) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.objects.Tier;
import com.badbones69.crazycrates.tasks.InventoryManager;
import io.papermc.paper.persistence.PersistentDataContainerView;
import net.kyori.adventure.sound.Sound;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -119,15 +118,11 @@ public void run(InventoryClickEvent event) {

if (item == null || item.getType() == Material.AIR) return;

if (!item.hasItemMeta()) return;

final Crate crate = this.inventoryManager.getCratePreview(player);

if (crate == null) return;

final ItemMeta itemMeta = item.getItemMeta();

final PersistentDataContainer container = itemMeta.getPersistentDataContainer();
final PersistentDataContainerView container = item.getPersistentDataContainer();

if (container.has(PersistentKeys.main_menu_button.getNamespacedKey()) && this.config.getProperty(ConfigKeys.enable_crate_menu)) { // Clicked the menu button.
if (this.inventoryManager.inCratePreview(player)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
import com.badbones69.crazycrates.api.objects.Tier;
import com.badbones69.crazycrates.config.ConfigManager;
import com.badbones69.crazycrates.config.impl.ConfigKeys;
import io.papermc.paper.persistence.PersistentDataContainerView;
import net.kyori.adventure.sound.Sound;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
Expand Down Expand Up @@ -51,15 +50,11 @@ public void run(InventoryClickEvent event) {

if (item == null || item.getType() == Material.AIR) return;

if (!item.hasItemMeta()) return;

final Crate crate = this.inventoryManager.getCratePreview(player);

if (crate == null) return;

final ItemMeta itemMeta = item.getItemMeta();

final PersistentDataContainer container = itemMeta.getPersistentDataContainer();
final PersistentDataContainerView container = item.getPersistentDataContainer();

if (this.config.getProperty(ConfigKeys.enable_crate_menu) && container.has(PersistentKeys.main_menu_button.getNamespacedKey())) {
if (this.inventoryManager.inCratePreview(player)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.bukkit.Particle;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.Nullable;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -571,11 +570,7 @@ public final CrateType getCrateType() {
}

public final @Nullable Prize getPrize(@NotNull final ItemStack item) {
ItemMeta itemMeta = item.getItemMeta();

PersistentDataContainer container = itemMeta.getPersistentDataContainer();

return getPrize(container.get(PersistentKeys.crate_prize.getNamespacedKey(), PersistentDataType.STRING));
return getPrize(item.getPersistentDataContainer().get(PersistentKeys.crate_prize.getNamespacedKey(), PersistentDataType.STRING));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.badbones69.crazycrates.api.builders.ItemBuilder;
import com.ryderbelserion.vital.paper.util.DyeUtil;
import com.ryderbelserion.vital.paper.util.ItemUtil;
import io.papermc.paper.persistence.PersistentDataContainerView;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.block.banner.PatternType;
Expand Down Expand Up @@ -167,7 +168,7 @@ public static boolean isSimilar(@NotNull final ItemStack itemStack, @NotNull fin
* @param container the {@link PersistentDataContainer}
* @return the {@link String}
*/
public static String getKey(@NotNull final PersistentDataContainer container) {
public static String getKey(@NotNull final PersistentDataContainerView container) {
return container.get(PersistentKeys.crate_key.getNamespacedKey(), PersistentDataType.STRING);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.ryderbelserion.vital.paper.api.enums.Support;
import com.ryderbelserion.vital.paper.util.AdvUtil;
import com.ryderbelserion.vital.paper.util.ItemUtil;
import io.papermc.paper.persistence.PersistentDataContainerView;
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.sound.Sound;
import com.badbones69.crazycrates.config.ConfigManager;
Expand All @@ -25,8 +26,6 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.jetbrains.annotations.NotNull;
import org.bukkit.configuration.file.YamlConfiguration;
import us.crazycrew.crazycrates.api.enums.types.CrateType;
Expand Down Expand Up @@ -166,9 +165,6 @@ public void onMysteryBoxClick(InventoryClickEvent event) {
// Check if null or air.
if (itemStack == null || itemStack.getType() == Material.AIR) return;

// If no item meta, return.
if (!itemStack.hasItemMeta()) return;

// Get crate manager.
final CosmicCrateManager cosmicCrateManager = (CosmicCrateManager) crate.getManager();

Expand All @@ -178,11 +174,8 @@ public void onMysteryBoxClick(InventoryClickEvent event) {
// Get picked slot.
final int pickedSlot = slot+1;

// Get clicked item's item meta.
final ItemMeta itemMeta = itemStack.getItemMeta();

// Get the pdc container.
final PersistentDataContainer container = itemMeta.getPersistentDataContainer();
final PersistentDataContainerView container = itemStack.getPersistentDataContainer();

// Check if it has the mystery crate key otherwise check picked key.
if (container.has(PersistentKeys.cosmic_mystery_crate.getNamespacedKey())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import com.badbones69.crazycrates.api.events.PlayerPrizeEvent;
import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.objects.Prize;
import io.papermc.paper.persistence.PersistentDataContainerView;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import us.crazycrew.crazycrates.api.enums.types.CrateType;
Expand All @@ -41,13 +41,13 @@ public void onCrateUse(PlayerInteractEvent event) {

if (item.getType() == Material.AIR) return;

if (!item.hasItemMeta()) return;
final PersistentDataContainerView container = item.getPersistentDataContainer();

final ItemMeta itemMeta = item.getItemMeta();
final NamespacedKey key = PersistentKeys.crate_key.getNamespacedKey();

final PersistentDataContainer container = itemMeta.getPersistentDataContainer();
if (!container.has(key)) return;

final Crate crate = this.crateManager.getCrateFromName(container.get(PersistentKeys.crate_key.getNamespacedKey(), PersistentDataType.STRING));
final Crate crate = this.crateManager.getCrateFromName(container.get(key, PersistentDataType.STRING));

if (crate == null) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

public class QuadCrateListener implements Listener {

Expand Down Expand Up @@ -85,10 +85,7 @@ public void onChestClick(PlayerInteractEvent event) {
final ItemMeta itemMeta = display.getItemMeta();

// Access the pdc and set "crazycrates-item"
final PersistentKeys key = PersistentKeys.crate_prize;

//noinspection unchecked
itemMeta.getPersistentDataContainer().set(key.getNamespacedKey(), key.getType(), "1");
itemMeta.getPersistentDataContainer().set(PersistentKeys.crate_prize.getNamespacedKey(), PersistentDataType.STRING, "1");

// Set the item meta.
display.setItemMeta(itemMeta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import com.ryderbelserion.vital.common.utils.FileUtil;
import com.badbones69.crazycrates.api.builders.ItemBuilder;
import com.ryderbelserion.vital.paper.api.enums.Support;
import io.papermc.paper.persistence.PersistentDataContainerView;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.NamespacedKey;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -1099,13 +1099,7 @@ public final boolean isKey(@NotNull final ItemStack item) {
* @return a crate if is a key from a crate otherwise null if it is not.
*/
public @Nullable final Crate getCrateFromKey(@NotNull final ItemStack item) {
if (!item.hasItemMeta()) return null;

ItemMeta itemMeta = item.getItemMeta();

if (itemMeta == null) return null;

return getCrateFromName(ItemUtils.getKey(itemMeta.getPersistentDataContainer()));
return getCrateFromName(ItemUtils.getKey(item.getPersistentDataContainer()));
}

/**
Expand Down Expand Up @@ -1182,19 +1176,11 @@ public final boolean isKey(@NotNull final ItemStack item) {
*/
public final boolean isDisplayReward(@NotNull final Entity entity) {
if (entity instanceof Item item) {
ItemStack itemStack = item.getItemStack();
final ItemStack itemStack = item.getItemStack();

if (itemStack.getType() == Material.AIR) return false;

if (!itemStack.hasItemMeta()) return false;

ItemMeta itemMeta = itemStack.getItemMeta();

PersistentKeys prize = PersistentKeys.crate_prize;

PersistentDataContainer container = itemMeta.getPersistentDataContainer();

return container.has(prize.getNamespacedKey());
return itemStack.getPersistentDataContainer().has(PersistentKeys.crate_prize.getNamespacedKey());
}

return false;
Expand All @@ -1208,19 +1194,11 @@ public final boolean isDisplayReward(@NotNull final Entity entity) {
* @return true if it belongs to that Crate and false if it does not.
*/
public final boolean isKeyFromCrate(@Nullable final ItemStack item, @Nullable final Crate crate) {
if (item == null) return false;

if (crate == null) return false;

if (item == null || crate == null) return false;
if (crate.getCrateType() == CrateType.menu) return false;

if (item.getType() == Material.AIR) return false;

if (!item.hasItemMeta()) return false;

final ItemMeta itemMeta = item.getItemMeta();

final PersistentDataContainer container = itemMeta.getPersistentDataContainer();
final PersistentDataContainerView container = item.getPersistentDataContainer();

if (!container.has(PersistentKeys.crate_key.getNamespacedKey())) return false;

Expand Down Expand Up @@ -1288,7 +1266,7 @@ private ItemBuilder getKey(@NotNull final FileConfiguration file) {
final boolean glowing = file.getBoolean("Crate.PhysicalKey.Glowing", true);
final boolean hideFlags = file.getBoolean("Crate.PhysicalKey.HideItemFlags", false);

final ItemBuilder itemBuilder = file.contains("Crate.PhysicalKey.Data") ? new ItemBuilder().fromBase64(file.getString("Crate.PhysicalKey.Data")) : new ItemBuilder().withType(file.getString("Crate.PhysicalKey.Item", "tripwire_hook"));
final ItemBuilder itemBuilder = file.contains("Crate.PhysicalKey.Data") ? new ItemBuilder().fromBase64(file.getString("Crate.PhysicalKey.Data", "")) : new ItemBuilder().withType(file.getString("Crate.PhysicalKey.Item", "tripwire_hook"));

return itemBuilder.setDisplayName(name).setDisplayLore(lore).setGlowing(glowing).setHidingItemFlags(hideFlags).setCustomModelData(customModelData);
}
Expand Down Expand Up @@ -1426,22 +1404,28 @@ public void endQuickCrate(@NotNull final Player player, @NotNull final Location
}
}

/**
* Purge all rewards!
*/
public void purgeRewards() {
if (!this.allRewards.isEmpty()) this.allRewards.stream().filter(Objects::nonNull).forEach(Entity::remove);
}

public Tier getTier(final Crate crate, final ItemStack item) {
if (!item.hasItemMeta()) return null;

ItemMeta itemMeta = item.getItemMeta();
/**
* Gets the {@link Tier}
*
* @param crate {@link Crate}
* @param item {@link ItemStack}
* @return {@link Tier}
*/
public final Tier getTier(final Crate crate, final ItemStack item) {
final PersistentDataContainerView container = item.getPersistentDataContainer();

PersistentDataContainer container = itemMeta.getPersistentDataContainer();
final NamespacedKey key = PersistentKeys.crate_tier.getNamespacedKey();

if (container.has(PersistentKeys.crate_tier.getNamespacedKey())) {
return crate.getTier(container.get(PersistentKeys.crate_tier.getNamespacedKey(), PersistentDataType.STRING));
}
if (!container.has(key)) return null;

return null;
return crate.getTier(container.get(key, PersistentDataType.STRING));
}

private final Map<UUID, ArrayList<Integer>> slots = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.objects.Tier;
import com.badbones69.crazycrates.api.builders.ItemBuilder;
import io.papermc.paper.persistence.PersistentDataContainerView;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import com.badbones69.crazycrates.api.enums.PersistentKeys;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -92,20 +91,13 @@ public final int getTotalPrizes() {
* @return the tier
*/
public final Tier getTier(@NotNull final ItemStack itemStack, @NotNull final Crate crate) {
if (itemStack.hasItemMeta()) {
final ItemMeta itemMeta = itemStack.getItemMeta();
final PersistentDataContainerView container = itemStack.getPersistentDataContainer();

final PersistentDataContainer container = itemMeta.getPersistentDataContainer();

if (container.has(PersistentKeys.crate_tier.getNamespacedKey())) {
return crate.getTier(container.get(PersistentKeys.crate_tier.getNamespacedKey(), PersistentDataType.STRING));
}

// In case there is no tier.
return PrizeManager.getTier(crate);
if (container.has(PersistentKeys.crate_tier.getNamespacedKey())) {
return crate.getTier(container.get(PersistentKeys.crate_tier.getNamespacedKey(), PersistentDataType.STRING));
}

// In case there is no item meta.
// In case there is no tier.
return PrizeManager.getTier(crate);
}

Expand Down
Loading

0 comments on commit 5a18a8c

Please sign in to comment.