Skip to content

Commit

Permalink
apply max stack size to player inventory when doing addItem
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Jul 12, 2024
1 parent c9242ba commit 2025ad4
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void givePrize(@NotNull final Player player, @Nullable Prize prize
if (!prize.getItemBuilders().isEmpty()) {
for (final ItemBuilder item : prize.getItemBuilders()) {
if (!MiscUtils.isInventoryFull(player)) {
player.getInventory().addItem(item.setPlayer(player).getStack());
MiscUtils.addItem(player, item.setPlayer(player).getStack());
} else {
player.getWorld().dropItemNaturally(player.getLocation(), item.setPlayer(player).getStack());
}
Expand All @@ -52,7 +52,7 @@ public static void givePrize(@NotNull final Player player, @Nullable Prize prize
// Only give them the display item as a reward if prize commands are empty.
if (prize.getCommands().isEmpty()) {
if (!MiscUtils.isInventoryFull(player)) {
player.getInventory().addItem(prize.getPrizeItem().setPlayer(player).getStack());
MiscUtils.addItem(player, prize.getPrizeItem().setPlayer(player).getStack());
} else {
player.getWorld().dropItemNaturally(player.getLocation(), prize.getPrizeItem().setPlayer(player).getStack());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.badbones69.crazycrates.api.enums.Messages;
import com.badbones69.crazycrates.api.enums.Permissions;
import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.utils.MiscUtils;
import com.ryderbelserion.vital.paper.builders.items.ItemBuilder;
import com.ryderbelserion.vital.paper.util.AdvUtil;
import org.bukkit.Material;
Expand Down Expand Up @@ -93,9 +94,7 @@ public void run(InventoryClickEvent event) {

switch (clickType) {
case LEFT -> {
ItemStack key = crate.getKey(player);

player.getInventory().addItem(key);
MiscUtils.addItem(player, crate.getKey(player));

//todo() make this configurable?
player.playSound(player.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_CHIME, 1f, 1f);
Expand All @@ -106,9 +105,7 @@ public void run(InventoryClickEvent event) {
}

case SHIFT_LEFT -> {
ItemStack key = crate.getKey(8, player);

player.getInventory().addItem(key);
MiscUtils.addItem(player, crate.getKey(8, player));

//todo() make this configurable?
player.playSound(player.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_CHIME, 1f, 1f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ public static ItemBuilder getRandomPaneColor() {
return new ItemBuilder(panes.get(ThreadLocalRandom.current().nextInt(panes.size())));
}

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

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

/**
* Decides when the crate should start to slow down.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void addKey(@NotNull final CommandSender sender, @Nullable Player player
if (event.isCancelled()) return;

if (crate.getCrateType() == CrateType.crate_on_the_go) {
player.getInventory().addItem(crate.getKey(amount, player));
MiscUtils.addItem(player, crate.getKey(amount, player));
} else {
this.userManager.addKeys(player.getUniqueId(), crate.getName(), type, amount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void all(CommandSender sender, @Suggestion("keys") String type, @Suggesti
player.sendRichMessage(Messages.obtaining_keys.getMessage(player, placeholders));

if (crate.getCrateType() == CrateType.crate_on_the_go) {
player.getInventory().addItem(crate.getKey(amount, player));
MiscUtils.addItem(player, crate.getKey(amount, player));

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void addKeys(@NotNull final UUID uuid, @NotNull final String crateName, @
switch (keyType) {
case physical_key -> {
if (!MiscUtils.isInventoryFull(player)) {
player.getInventory().addItem(crate.getKey(amount, player));
MiscUtils.addItem(player, crate.getKey(amount, player));

return;
}
Expand Down Expand Up @@ -475,7 +475,7 @@ public void loadOfflinePlayersKeys(@NotNull final Player player, @NotNull final
// If the crate type is on the go.
if (crate.getCrateType() == CrateType.crate_on_the_go) {
// If the inventory not full, add to inventory.
player.getInventory().addItem(crate.getKey(amount, player));
MiscUtils.addItem(player, crate.getKey(amount, player));
} else {
// Otherwise add virtual keys.
addVirtualKeys(uuid, crate.getName(), amount);
Expand Down Expand Up @@ -507,7 +507,7 @@ public void loadOfflinePlayersKeys(@NotNull final Player player, @NotNull final
}

// If the inventory not full, add to inventory.
player.getInventory().addItem(crate.getKey(keysGiven, player));
MiscUtils.addItem(player, crate.getKey(keysGiven, player));

// If keys given is greater or equal than, remove data.
if (keysGiven >= amount) this.configuration.set("Offline-Players." + uuid + ".Physical." + crate.getName(), null);
Expand Down

0 comments on commit 2025ad4

Please sign in to comment.