From 21395ad478ae850d3385522f952cf13127afd77f Mon Sep 17 00:00:00 2001 From: ThiagoROX <51332006+SrBedrock@users.noreply.github.com> Date: Sat, 9 Mar 2024 02:46:03 -0300 Subject: [PATCH] Pequenas melhorias --- .../crazycrates/api/PrizeManager.java | 4 +- .../crazycrates/api/objects/Crate.java | 30 ++++++++------ .../crazycrates/api/objects/Prize.java | 39 +++++++++---------- .../api/objects/other/ItemBuilder.java | 9 +++-- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/PrizeManager.java b/paper/src/main/java/com/badbones69/crazycrates/api/PrizeManager.java index c07a31fd0..6359bce76 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/api/PrizeManager.java +++ b/paper/src/main/java/com/badbones69/crazycrates/api/PrizeManager.java @@ -18,6 +18,7 @@ import java.util.HashMap; import java.util.concurrent.ThreadLocalRandom; +import java.util.logging.Level; import static com.badbones69.crazycrates.api.utils.MiscUtils.RANDOM; import static java.util.regex.Matcher.quoteReplacement; @@ -36,8 +37,7 @@ public class PrizeManager { */ public static void givePrize(Player player, Prize prize, Crate crate) { if (prize == null) { - if (plugin.isLogging()) - plugin.getLogger().warning("No prize was found when giving " + player.getName() + " a prize."); + plugin.debug(() -> "No prize was found when giving " + player.getName() + " a prize.", Level.WARNING); return; } diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/objects/Crate.java b/paper/src/main/java/com/badbones69/crazycrates/api/objects/Crate.java index 0b9b55397..71213eb51 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/api/objects/Crate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/api/objects/Crate.java @@ -239,7 +239,7 @@ public List getPrizeMessage() { * @return the winning prize. */ public Prize pickPrize(Player player) { - List prizes = new ArrayList<>(); + List prizeList = new ArrayList<>(); List usablePrizes = new ArrayList<>(); // ================= Blacklist Check ================= // @@ -256,10 +256,10 @@ public Prize pickPrize(Player player) { } // ================= Chance Check ================= // - chanceCheck(prizes, usablePrizes); + chanceCheck(prizeList, usablePrizes); try { - return prizes.get(MiscUtils.useOtherRandom() ? ThreadLocalRandom.current().nextInt(prizes.size()) : RANDOM.nextInt(prizes.size())); + return prizeList.get(MiscUtils.useOtherRandom() ? ThreadLocalRandom.current().nextInt(prizeList.size()) : RANDOM.nextInt(prizeList.size())); } catch (IllegalArgumentException exception) { this.plugin.getLogger().log(Level.WARNING, "Failed to find prize from the " + name + " crate for player " + player.getName() + ".", exception); return null; @@ -279,7 +279,7 @@ private void chanceCheck(List prizes, List usablePrizes) { int chance = prize.getChance(); int num; - for (int counter = 1; counter <= 1; counter++) { + for (int counter = 1; counter == 1; counter++) { num = MiscUtils.useOtherRandom() ? 1 + ThreadLocalRandom.current().nextInt(max) : 1 + RANDOM.nextInt(max); if (num <= chance) prizes.add(prize); @@ -322,28 +322,32 @@ public void setPreviewItems(List itemStacks) { * @return the winning prize based on the crate's tiers. */ public Prize pickPrize(Player player, Tier tier) { - List prizes = new ArrayList<>(); + List prizeList = new ArrayList<>(); List usablePrizes = new ArrayList<>(); // ================= Blacklist Check ================= // if (player.isOp()) { for (Prize prize : getPrizes()) { - if (prize.getTiers().contains(tier)) usablePrizes.add(prize); + if (prize.getTiers().contains(tier)) { + usablePrizes.add(prize); + } } } else { for (Prize prize : getPrizes()) { - if (prize.hasPermission(player)) { - if (prize.hasAlternativePrize()) continue; + if (prize.hasPermission(player) && prize.hasAlternativePrize()) { + continue; } - if (prize.getTiers().contains(tier)) usablePrizes.add(prize); + if (prize.getTiers().contains(tier)) { + usablePrizes.add(prize); + } } } // ================= Chance Check ================= // - chanceCheck(prizes, usablePrizes); + chanceCheck(prizeList, usablePrizes); - return prizes.get(MiscUtils.useOtherRandom() ? ThreadLocalRandom.current().nextInt(prizes.size()) : RANDOM.nextInt(prizes.size())); + return prizeList.get(MiscUtils.useOtherRandom() ? ThreadLocalRandom.current().nextInt(prizeList.size()) : RANDOM.nextInt(prizeList.size())); } /** @@ -527,7 +531,9 @@ public List getPrizes() { */ public Prize getPrize(String name) { for (Prize prize : this.prizes) { - if (prize.getPrizeName().equalsIgnoreCase(name)) return prize; + if (prize.getPrizeName().equalsIgnoreCase(name)) { + return prize; + } } return null; diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/objects/Prize.java b/paper/src/main/java/com/badbones69/crazycrates/api/objects/Prize.java index cbf7423db..0ab0ce451 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/api/objects/Prize.java +++ b/paper/src/main/java/com/badbones69/crazycrates/api/objects/Prize.java @@ -3,6 +3,7 @@ import com.badbones69.crazycrates.api.enums.PersistentKeys; import com.badbones69.crazycrates.api.objects.other.ItemBuilder; import com.badbones69.crazycrates.api.utils.MiscUtils; +import com.google.common.base.MoreObjects; import org.apache.commons.lang.WordUtils; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -61,7 +62,6 @@ public Prize(ConfigurationSection section, List tierPrizes, String crateNa this.tiers = tierPrizes; this.alternativePrize = alternativePrize; - this.prizeName = section.getString("DisplayName", WordUtils.capitalizeFully(section.getString("DisplayItem", "STONE").replaceAll("_", " "))); this.maxRange = section.getInt("MaxRange", 100); this.chance = section.getInt("Chance", 100); @@ -97,7 +97,7 @@ public Prize(String prizeName, String prizeNumber, ConfigurationSection section) this.section = section; } - + /** * @return the name of the prize. */ @@ -150,84 +150,84 @@ public ItemStack getDisplayItem(Player player) { public ItemBuilder getDisplayItemBuilder() { return this.displayItem; } - + /** * @return the list of tiers the prize is in. */ public List getTiers() { return this.tiers; } - + /** * @return the messages sent to the player. */ public List getMessages() { return this.messages; } - + /** * @return the commands that are run when the player wins. */ public List getCommands() { return this.commands; } - + /** * @return the Editor ItemStacks that are given to the player that wins. */ public List getItems() { return this.items; } - + /** * @return the ItemBuilders for all the custom items made from the Items: option. */ public List getItemBuilders() { return this.builders; } - + /** * @return the name of the crate the prize is in. */ public String getCrateName() { return this.crateName; } - + /** * @return the chance the prize has of being picked. */ public int getChance() { return this.chance; } - + /** * @return the max range of the prize. */ public int getMaxRange() { return this.maxRange; } - + /** * @return true if a firework explosion is played and false if not. */ public boolean useFireworks() { return this.firework; } - + /** * @return the alternative prize the player wins if they have a blacklist permission. */ public Prize getAlternativePrize() { return this.alternativePrize; } - + /** * @return true if the prize doesn't have an alternative prize and false if it does. */ public boolean hasAlternativePrize() { return this.alternativePrize == null; } - + /** * @return true if they prize has blacklist permissions and false if not. */ @@ -320,12 +320,11 @@ private ItemBuilder display() { return builder; } catch (Exception exception) { - List list = new ArrayList<>() {{ - add("&cThere was an error with one of your prizes!"); - add("&cThe reward in question is labeled: &e" + section.getName() + " &cin crate: &e" + crateName); - add("&cName of the reward is " + section.getString("DisplayName")); - add("&cIf you are confused, Stop by our discord for support!"); - }}; + List list = List.of( + "&cThere was an error with one of your prizes!", + "&cThe reward in question is labeled: &e" + this.section.getName() + " &cin crate: &e" + this.crateName, + "&cName of the reward is " + this.section.getString("DisplayName"), + "&cIf you are confused, Stop by our discord for support!"); return new ItemBuilder().setMaterial(Material.RED_TERRACOTTA).setName("&c&lERROR").setLore(list); } diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/objects/other/ItemBuilder.java b/paper/src/main/java/com/badbones69/crazycrates/api/objects/other/ItemBuilder.java index 9c7c4c067..5f867f0bd 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/api/objects/other/ItemBuilder.java +++ b/paper/src/main/java/com/badbones69/crazycrates/api/objects/other/ItemBuilder.java @@ -5,6 +5,7 @@ import com.badbones69.crazycrates.api.utils.MsgUtils; import com.badbones69.crazycrates.support.PluginSupport; import com.badbones69.crazycrates.support.SkullCreator; +import com.destroystokyo.paper.MaterialTags; import com.ryderbelserion.cluster.utils.DyeUtils; import de.tr7zw.changeme.nbtapi.NBTItem; import emanondev.itemedit.ItemEdit; @@ -423,9 +424,7 @@ public ItemStack build() { if (this.nbtItem != null) this.itemStack = this.nbtItem.getItem(); ItemStack item = this.itemStack; - setCustomItem(); - if (item.getType() != Material.AIR) { if (this.isHead) { // Has to go 1st due to it removing all data when finished. if (this.isHash) { // Sauce: https://github.com/deanveloper/SkullCreator @@ -573,7 +572,9 @@ private void configureOraxenItem(String id) { private void configureItemEditItem(String id) { var item = ItemEdit.get().getServerStorage().getItem(id); - updateItemStack(item); + if (item != null) { + updateItemStack(item); + } } private void updateItemStack(ItemStack item) { @@ -1159,7 +1160,7 @@ private static ItemBuilder set(ItemStack item, ItemBuilder itemBuilder) { itemMeta.setUnbreakable(itemMeta.isUnbreakable()); - if (itemMeta instanceof Damageable) itemBuilder.setDamage(((Damageable) itemMeta).getDamage()); + if (itemMeta instanceof Damageable damageable) itemBuilder.setDamage(damageable.getDamage()); } return itemBuilder;