Skip to content

Commit

Permalink
Re-work adding items to inventories
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Jan 31, 2025
1 parent 88625e4 commit 01455db
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 59 deletions.
38 changes: 25 additions & 13 deletions paper/src/main/java/com/badbones69/crazyvouchers/Methods.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import com.badbones69.crazyvouchers.config.types.ConfigKeys;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -27,26 +30,35 @@ public class Methods {

private @NotNull static final CrazyVouchers plugin = CrazyVouchers.get();
private @NotNull static final SettingsManager config = ConfigManager.getConfig();

public static void removeItem(final ItemStack item, final Player player) {
if (item.getAmount() <= 1) {
player.getInventory().removeItem(item);
} else if (item.getAmount() > 1) {
item.setAmount(item.getAmount() - 1);
}
}

public static String getPrefix(final String message) {
return MsgUtils.color(config.getProperty(ConfigKeys.command_prefix) + message);
}

public static void addItem(final Player player, final ItemStack... items) {
final Inventory inventory = player.getInventory();
final PlayerInventory inventory = player.getInventory();

inventory.setMaxStackSize(64);
inventory.addItem(items);

final List<ItemStack> itemStacks = Arrays.asList(items);

itemStacks.forEach(item -> {
if (isInventoryFull(inventory)) {
player.getWorld().dropItem(player.getLocation(), item);
} else {
inventory.setItem(inventory.firstEmpty(), item);
}
});
}

public static boolean isInt(final String value) {
try {
Integer.parseInt(value);
Expand All @@ -56,7 +68,7 @@ public static boolean isInt(final String value) {

return true;
}

public static boolean isInt(final CommandSender sender, final String value) {
try {
Integer.parseInt(value);
Expand All @@ -80,7 +92,7 @@ public static String replacePlaceholders(final Map<String, String> placeholders,

if (isCommand) return message; else return MsgUtils.color(message);
}

public static boolean isOnline(final CommandSender sender, final String name) {
for (Player player : plugin.getServer().getOnlinePlayers()) {
if (player.getName().equalsIgnoreCase(name)) return true;
Expand All @@ -90,7 +102,7 @@ public static boolean isOnline(final CommandSender sender, final String name) {

return false;
}

public static boolean hasPermission(final Player player, final String perm) {
if (!player.hasPermission("crazyvouchers." + perm) || !player.hasPermission("voucher." + perm)) {
Messages.no_permission.sendMessage(player);
Expand All @@ -100,7 +112,7 @@ public static boolean hasPermission(final Player player, final String perm) {

return true;
}

public static boolean hasPermission(final CommandSender sender, final String perm) {
if (sender instanceof Player player) {
if (!player.hasPermission("crazyvouchers." + perm) || !player.hasPermission("voucher." + perm)) {
Expand All @@ -114,11 +126,11 @@ public static boolean hasPermission(final CommandSender sender, final String per
return true;
}
}
public static boolean isInventoryFull(final Player player) {
return player.getInventory().firstEmpty() == -1;

public static boolean isInventoryFull(final PlayerInventory inventory) {
return inventory.firstEmpty() == -1;
}

public static void firework(final Location location, final List<Color> list) {
if (location.getWorld() == null) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import com.ryderbelserion.vital.paper.util.DyeUtil;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
Expand Down Expand Up @@ -284,21 +284,19 @@ public ItemStack buildItem() {
public ItemStack buildItem(int amount) {
final ItemStack item = this.itemBuilder.setAmount(amount).setGlow(this.glowing).build();

setUniqueId(item);

item.editMeta(itemMeta -> {
final PersistentDataContainer container = itemMeta.getPersistentDataContainer();

if (this.config.getProperty(ConfigKeys.dupe_protection)) {
container.set(PersistentKeys.dupe_protection.getNamespacedKey(), PersistentDataType.STRING, UUID.randomUUID().toString());
}

container.set(PersistentKeys.voucher_item.getNamespacedKey(), PersistentDataType.STRING, getName());
});

return item;
}

public List<ItemStack> buildItems(String argument, int amount) {
List<ItemStack> itemStacks = new ArrayList<>();
final List<ItemStack> itemStacks = new ArrayList<>();

if (this.config.getProperty(ConfigKeys.dupe_protection)) {
while (itemStacks.size() < amount) {
Expand All @@ -311,24 +309,36 @@ public List<ItemStack> buildItems(String argument, int amount) {
return itemStacks;
}

public ItemStack buildItem(String argument, int amount) {
ItemStack item = this.itemBuilder.setAmount(amount).addLorePlaceholder("{arg}", argument).addNamePlaceholder("{arg}", argument).setGlow(this.glowing).build();
public ItemStack buildItem(final String argument, final int amount) {
final ItemStack item = this.itemBuilder.setAmount(amount).addLorePlaceholder("{arg}", argument).addNamePlaceholder("{arg}", argument).setGlow(this.glowing).build(true);

setUniqueId(item);

item.editMeta(itemMeta -> {
final PersistentDataContainer container = itemMeta.getPersistentDataContainer();

if (this.config.getProperty(ConfigKeys.dupe_protection)) {
container.set(PersistentKeys.dupe_protection.getNamespacedKey(), PersistentDataType.STRING, UUID.randomUUID().toString());
}

container.set(PersistentKeys.voucher_item.getNamespacedKey(), PersistentDataType.STRING, getName());

if (!argument.isEmpty()) container.set(PersistentKeys.voucher_arg.getNamespacedKey(), PersistentDataType.STRING, argument);
});

return item;
}


private void setUniqueId(final ItemStack item) {
final String uuid = UUID.randomUUID().toString();

if (this.config.getProperty(ConfigKeys.dupe_protection)) {
item.editMeta(itemMeta -> {
final PersistentDataContainer container = itemMeta.getPersistentDataContainer();

if (this.config.getProperty(ConfigKeys.dupe_protection)) {
container.set(PersistentKeys.dupe_protection.getNamespacedKey(), PersistentDataType.STRING, uuid);
}
});
}
}

public String getVoucherUsedMessage() {
return this.usedMessage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,16 @@ private boolean isArmor() {
return name.endsWith("_HELMET") || name.endsWith("_CHESTPLATE") || name.endsWith("_LEGGINGS") || name.endsWith("_BOOTS") || name.equals(Material.TURTLE_HELMET.name());
}

public ItemStack build() {
return build(false);
}

/**
* Builder the item from all the information that was given to the builder.
*
* @return the result of all the info that was given to the builder as an ItemStack.
*/
public ItemStack build() {
public ItemStack build(boolean buildNewItemStack) {
ItemStack item = this.itemStack;

if (Support.head_database.isEnabled()) {
Expand Down Expand Up @@ -479,6 +483,10 @@ public ItemStack build() {
this.itemStack.setData(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true);
}

if (buildNewItemStack) {
return item.clone();
}

return item;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
}
}

for (ItemBuilder itemBuilder : voucherCode.getItems()) {
if (!Methods.isInventoryFull(player)) {
Methods.addItem(player, itemBuilder.build());
} else {
player.getWorld().dropItem(player.getLocation(), itemBuilder.build());
}
for (final ItemBuilder itemBuilder : voucherCode.getItems()) {
Methods.addItem(player, itemBuilder.build());
}

if (voucherCode.useSounds()) {
for (Sound sound : voucherCode.getSounds()) {
for (final Sound sound : voucherCode.getSounds()) {
player.playSound(player.getLocation(), sound, SoundCategory.PLAYERS, voucherCode.getVolume(), voucherCode.getPitch());
}
}
Expand Down Expand Up @@ -393,17 +389,10 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
itemStacks.addAll(voucher.buildItems("", amount));
}

if (Methods.isInventoryFull(player)) {
itemStacks.forEach(itemStack -> player.getWorld().dropItem(player.getLocation(), itemStack));
} else {
itemStacks.forEach(itemStack -> {
Methods.addItem(player, itemStack);

player.updateInventory();
});
}
itemStacks.forEach(itemStack -> Methods.addItem(player, itemStack));

Map<String, String> placeholders = new HashMap<>();

placeholders.put("{player}", player.getName());
placeholders.put("{voucher}", voucher.getName());

Expand Down Expand Up @@ -455,16 +444,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
itemStacks.addAll(voucher.buildItems("", amount));
}

for (Player player : this.plugin.getServer().getOnlinePlayers()) {
if (Methods.isInventoryFull(player)) {
itemStacks.forEach(itemStack -> player.getWorld().dropItem(player.getLocation(), itemStack));
} else {
itemStacks.forEach(itemStack -> {
Methods.addItem(player, itemStack);

player.updateInventory();
});
}
for (final Player player : this.plugin.getServer().getOnlinePlayers()) {
itemStacks.forEach(itemStack -> Methods.addItem(player, itemStack));
}

Map<String, String> placeholders = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,8 @@ private void voucherClick(Player player, ItemStack item, Voucher voucher, String
}
}

for (ItemBuilder itemStack : voucher.getItems()) {
if (!Methods.isInventoryFull(player)) {
Methods.addItem(player, itemStack.build());
} else {
player.getWorld().dropItem(player.getLocation(), itemStack.build());
}
for (final ItemBuilder itemStack : voucher.getItems()) {
Methods.addItem(player, itemStack.build());
}

if (voucher.playSounds()) {
Expand Down

0 comments on commit 01455db

Please sign in to comment.