Fix some NPE caused by null itemstacks

This commit is contained in:
Len 2024-08-04 20:59:45 +02:00
parent 633bc97abb
commit 7d9cb418af
3 changed files with 6 additions and 4 deletions

View File

@ -55,7 +55,7 @@ public class ListShopsGui extends AbstractGui {
for (int i = startIndex; i < shops.size(); i++) { for (int i = startIndex; i < shops.size(); i++) {
PlayerShop shop = shops.get(i); PlayerShop shop = shops.get(i);
ItemStack item = shop.getItemStack(); ItemStack item = shop.getItemStack();
if (!shop.isInitialized()) if (!shop.isInitialized() || item == null)
item = GuiIcon.EMPTY_SHOP.getItemStack(); item = GuiIcon.EMPTY_SHOP.getItemStack();
if (!addItem(item)) { if (!addItem(item)) {

View File

@ -153,7 +153,7 @@ public class TransactionListener extends EventListener {
TagResolver placeholders = TagResolver.resolver(action, TagResolver placeholders = TagResolver.resolver(action,
Placeholder.parsed("amount", "" + playerShop.getAmount()), Placeholder.parsed("amount", "" + playerShop.getAmount()),
Placeholder.component("item", ShopUtil.itemNameComponent(playerShop.getItemStack())), Placeholder.component("item", ShopUtil.itemNameComponent(playerShop.getItemStack())),
Placeholder.parsed("material", Util.capitalize(playerShop.getItemStack().getType().name())), Placeholder.component("material", ShopUtil.trimmedItemNameComponent(playerShop.getItemStack())),
Placeholder.parsed("price", "" + ShopUtil.round(playerShop.getPrice())) Placeholder.parsed("price", "" + ShopUtil.round(playerShop.getPrice()))
); );

View File

@ -313,7 +313,7 @@ public class PlayerShop {
} }
private TransactionError executeGambleTransaction(Player player) { private TransactionError executeGambleTransaction(Player player) {
ItemStack itemStack = getRandomItem(); ItemStack itemStack = getItemStack();
if (itemStack == null) if (itemStack == null)
return TransactionError.INSUFFICIENT_FUNDS_SHOP; return TransactionError.INSUFFICIENT_FUNDS_SHOP;
@ -419,9 +419,11 @@ public class PlayerShop {
transactions.add(transaction); transactions.add(transaction);
} }
private ItemStack getRandomItem() { public ItemStack getItemStack() {
if (!isInitialized()) if (!isInitialized())
return null; return null;
if (this.getType() != ShopType.GAMBLE)
return itemStack;
if (this.getInventory().isEmpty()) if (this.getInventory().isEmpty())
return null; return null;
ArrayList<ItemStack> contents = new ArrayList<>(); ArrayList<ItemStack> contents = new ArrayList<>();