Skip to content

Commit

Permalink
Pequenas melhorias
Browse files Browse the repository at this point in the history
  • Loading branch information
SrBedrock committed Mar 9, 2024
1 parent dddb123 commit 21395ad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public List<String> getPrizeMessage() {
* @return the winning prize.
*/
public Prize pickPrize(Player player) {
List<Prize> prizes = new ArrayList<>();
List<Prize> prizeList = new ArrayList<>();
List<Prize> usablePrizes = new ArrayList<>();

// ================= Blacklist Check ================= //
Expand All @@ -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;
Expand All @@ -279,7 +279,7 @@ private void chanceCheck(List<Prize> prizes, List<Prize> 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);
Expand Down Expand Up @@ -322,28 +322,32 @@ public void setPreviewItems(List<ItemStack> itemStacks) {
* @return the winning prize based on the crate's tiers.
*/
public Prize pickPrize(Player player, Tier tier) {
List<Prize> prizes = new ArrayList<>();
List<Prize> prizeList = new ArrayList<>();
List<Prize> 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()));
}

/**
Expand Down Expand Up @@ -527,7 +531,9 @@ public List<Prize> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +62,6 @@ public Prize(ConfigurationSection section, List<Tier> 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);
Expand Down Expand Up @@ -97,7 +97,7 @@ public Prize(String prizeName, String prizeNumber, ConfigurationSection section)

this.section = section;
}

/**
* @return the name of the prize.
*/
Expand Down Expand Up @@ -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<Tier> getTiers() {
return this.tiers;
}

/**
* @return the messages sent to the player.
*/
public List<String> getMessages() {
return this.messages;
}

/**
* @return the commands that are run when the player wins.
*/
public List<String> getCommands() {
return this.commands;
}

/**
* @return the Editor ItemStacks that are given to the player that wins.
*/
public List<ItemStack> getItems() {
return this.items;
}

/**
* @return the ItemBuilders for all the custom items made from the Items: option.
*/
public List<ItemBuilder> 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.
*/
Expand Down Expand Up @@ -320,12 +320,11 @@ private ItemBuilder display() {

return builder;
} catch (Exception exception) {
List<String> 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<String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 21395ad

Please sign in to comment.