Skip to content

Commit

Permalink
Add null safety/default values or variables where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Feb 17, 2024
1 parent fb5269e commit c725268
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 168 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ public enum Dust {
private final int max;
private final int min;

private final CrazyEnchantments plugin = CrazyEnchantments.getPlugin();
@NotNull
private final CrazyEnchantments plugin = CrazyEnchantments.get();

private final Methods methods = plugin.getStarter().getMethods();
@NotNull
private final Methods methods = this.plugin.getStarter().getMethods();

Dust(String name, String configName, List<String> knowNames) {
this.name = name;
this.knownNames = knowNames;
this.configName = configName;
this.max = Files.CONFIG.getFile().getInt("Settings.Dust." + configName + ".PercentRange.Max", 100);
this.min = Files.CONFIG.getFile().getInt("Settings.Dust." + configName + ".PercentRange.Min", max);

FileConfiguration config = Files.CONFIG.getFile();

this.max = config.getInt("Settings.Dust." + configName + ".PercentRange.Max", 100);
this.min = config.getInt("Settings.Dust." + configName + ".PercentRange.Min", max);
}

public static void loadDust() {
Expand All @@ -50,9 +55,9 @@ public static void loadDust() {
for (Dust dust : values()) {
String path = "Settings.Dust." + dust.getConfigName() + ".";
Dust.itemBuilderDust.put(dust, new ItemBuilder()
.setName(config.getString(path + "Name"))
.setName(config.getString(path + "Name", "Error getting name."))
.setLore(config.getStringList(path + "Lore"))
.setMaterial(config.getString(path + "Item")));
.setMaterial(config.getString(path + "Item", "GLOWSTONE_DUST")));
}
}

Expand All @@ -66,33 +71,30 @@ public static Dust getFromName(String nameString) {
}

public String getName() {
return name;
return this.name;
}

public List<String> getKnownNames() {
return knownNames;
return this.knownNames;
}

public String getConfigName() {
return configName;
return this.configName;
}

public ItemStack getDust() {
return getDust(1);
}

public ItemStack getDust(int amount) {

return getDust(methods.percentPick(max, min), amount);
return getDust(this.methods.percentPick(this.max, this.min), amount);
}

public ItemStack getDust(int percent, int amount) {
ItemStack item = itemBuilderDust.get(this)
.addLorePlaceholder("%Percent%", String.valueOf(percent))
.setAmount(amount).build();
ItemStack item = itemBuilderDust.get(this).addLorePlaceholder("%Percent%", String.valueOf(percent)).setAmount(amount).build();

// PDC Start
Gson gson = new Gson();
Gson gson = new Gson();

ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(DataKeys.dust.getNamespacedKey(), PersistentDataType.STRING, gson.toJson(new DustData(getConfigName(), this.min, this.max, percent)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public static void loadScrolls() {
for (Scrolls scroll : values()) {
String path = "Settings." + scroll.getConfigName() + ".";
itemBuilderScrolls.put(scroll, new ItemBuilder()
.setName(config.getString(path + "Name"))
.setName(config.getString(path + "Name", "Error getting name."))
.setLore(config.getStringList(path + "Item-Lore"))
.setMaterial(config.getString(path + "Item"))
.setGlow(config.getBoolean(path + "Glowing")));
.setMaterial(config.getString(path + "Item", "BOOK"))
.setGlow(config.getBoolean(path + "Glowing", false)));
}
}

Expand All @@ -58,15 +58,15 @@ public static Scrolls getFromName(String nameString) {
}

public String getName() {
return name;
return this.name;
}

public List<String> getKnownNames() {
return knownNames;
return this.knownNames;
}

public String getConfigName() {
return configName;
return this.configName;
}

private static final NamespacedKey scroll = DataKeys.scroll.getNamespacedKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,17 @@ public static void loadShopOptions() {

try {
shopOptions.put(shopOption, new Option(new ItemBuilder()
.setName(config.getString(itemPath + shopOption.getNamePath()))
.setName(config.getString(itemPath + shopOption.getNamePath(), "Error getting name."))
.setLore(config.getStringList(itemPath + shopOption.getLorePath()))
.setMaterial(config.getString(itemPath + "Item"))
.setMaterial(config.getString(itemPath + "Item", "CHEST"))
.setPlayerName(config.getString(itemPath + "Player"))
.setGlow(config.getBoolean(itemPath + "Glowing")),
.setGlow(config.getBoolean(itemPath + "Glowing", false)),
config.getInt(itemPath + "Slot", 1) - 1,
config.getBoolean(itemPath + "InGUI"),
config.getBoolean(itemPath + "InGUI", true),
config.getInt(costPath + "Cost", 100),
Currency.getCurrency(config.getString(costPath + "Currency", "Vault"))));
} catch (Exception e) {
plugin.getLogger().info("The option " + shopOption.getOptionPath() + " has failed to load.");
Log.error(e);
} catch (Exception exception) {
plugin.getLogger().log(Level.SEVERE, "The option " + shopOption.getOptionPath() + " has failed to load.", exception);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ public CEBook(CEnchantment enchantment, int level, int amount) {
this.enchantment = enchantment;
this.amount = amount;
this.level = level;
this.glowing = Files.CONFIG.getFile().getBoolean("Settings.Enchantment-Book-Glowing");
int successMax = Files.CONFIG.getFile().getInt("Settings.BlackScroll.SuccessChance.Max");
int successMin = Files.CONFIG.getFile().getInt("Settings.BlackScroll.SuccessChance.Min");
int destroyMax = Files.CONFIG.getFile().getInt("Settings.BlackScroll.DestroyChance.Max");
int destroyMin = Files.CONFIG.getFile().getInt("Settings.BlackScroll.DestroyChance.Min");
this.destroyRate = methods.percentPick(destroyMax, destroyMin);
this.successRate = methods.percentPick(successMax, successMin);

FileConfiguration config = Files.CONFIG.getFile();

this.glowing = config.getBoolean("Settings.Enchantment-Book-Glowing", true);
int successMax = config.getInt("Settings.BlackScroll.SuccessChance.Max", 100);
int successMin = config.getInt("Settings.BlackScroll.SuccessChance.Min", 15);
int destroyMax = config.getInt("Settings.BlackScroll.DestroyChance.Max", 100);
int destroyMin = config.getInt("Settings.BlackScroll.DestroyChance.Min", 15);
this.destroyRate = this.methods.percentPick(destroyMax, destroyMin);
this.successRate = this.methods.percentPick(successMax, successMin);
}

/**
Expand All @@ -91,9 +94,9 @@ public CEBook(CEnchantment enchantment, int level, int amount, Category category
this.enchantment = enchantment;
this.amount = amount;
this.level = level;
this.glowing = Files.CONFIG.getFile().getBoolean("Settings.Enchantment-Book-Glowing");
this.destroyRate = methods.percentPick(category.getMaxDestroyRate(), category.getMinDestroyRate());
this.successRate = methods.percentPick(category.getMaxSuccessRate(), category.getMinSuccessRate());
this.glowing = Files.CONFIG.getFile().getBoolean("Settings.Enchantment-Book-Glowing", true);
this.destroyRate = this.methods.percentPick(category.getMaxDestroyRate(), category.getMinDestroyRate());
this.successRate = this.methods.percentPick(category.getMaxSuccessRate(), category.getMinSuccessRate());
}

/**
Expand All @@ -107,7 +110,7 @@ public CEBook(CEnchantment enchantment, int level, int amount, int destroyRate,
this.enchantment = enchantment;
this.amount = amount;
this.level = level;
this.glowing = Files.CONFIG.getFile().getBoolean("Settings.Enchantment-Book-Glowing");
this.glowing = Files.CONFIG.getFile().getBoolean("Settings.Enchantment-Book-Glowing", true);
this.destroyRate = destroyRate;
this.successRate = successRate;
}
Expand Down Expand Up @@ -218,29 +221,28 @@ public CEBook setSuccessRate(int successRate) {
* @return Return the book as an ItemBuilder.
*/
public ItemBuilder getItemBuilder() {
String name = enchantment.getCustomName() + " " + NumberUtils.convertLevelString(level);
String name = this.enchantment.getCustomName() + " " + NumberUtils.convertLevelString(level);
List<String> lore = new ArrayList<>();

for (String bookLine : Files.CONFIG.getFile().getStringList("Settings.EnchantmentBookLore")) {
if (bookLine.contains("%Description%") || bookLine.contains("%description%")) {
for (String enchantmentLine : enchantment.getInfoDescription()) {
for (String enchantmentLine : this.enchantment.getInfoDescription()) {
lore.add(ColorUtils.color(enchantmentLine));
}
} else {
lore.add(ColorUtils.color(bookLine)
.replace("%Destroy_Rate%", String.valueOf(destroyRate)).replace("%destroy_rate%", String.valueOf(destroyRate))
.replace("%Success_Rate%", String.valueOf(successRate)).replace("%success_rate%", String.valueOf(successRate)));
.replace("%Destroy_Rate%", String.valueOf(this.destroyRate)).replace("%destroy_rate%", String.valueOf(this.destroyRate))
.replace("%Success_Rate%", String.valueOf(this.successRate)).replace("%success_rate%", String.valueOf(this.successRate)));
}
}

return enchantmentBookSettings.getNormalBook().setAmount(amount).setName(name).setLore(lore).setGlow(glowing);
return this.enchantmentBookSettings.getNormalBook().setAmount(this.amount).setName(name).setLore(lore).setGlow(this.glowing);
}

/**
* @return Return the book as an ItemStack.
*/
public ItemStack buildBook() {

ItemStack item = getItemBuilder().build();
ItemMeta meta = item.getItemMeta();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ public class EnchantmentBookSettings {
* @return True if unsafe enchantments are enabled.
*/
public boolean useUnsafeEnchantments() {
FileConfiguration config = FileManager.Files.CONFIG.getFile();
FileConfiguration config = Files.CONFIG.getFile();

return config.getBoolean("Settings.EnchantmentOptions.UnSafe-Enchantments");
}

public boolean hasEnchantment(ItemMeta meta, CEnchantment enchantment) {
PersistentDataContainer data = meta.getPersistentDataContainer();

Expand Down Expand Up @@ -67,6 +68,7 @@ public CEBook getCEBook(ItemStack book) {
break;
}
}

return new CEBook(enchantment, data.getLevel(), book.getAmount())
.setSuccessRate(data.getSuccessChance())
.setDestroyRate(data.getDestroyChance());
Expand Down Expand Up @@ -121,22 +123,22 @@ public boolean isEnchantmentBook(ItemStack book) {
* @return A list of all active enchantments.
*/
public List<CEnchantment> getRegisteredEnchantments() {
return registeredEnchantments;
return this.registeredEnchantments;
}

/**
*
* @return itemBuilder for an enchanted book.
*/
public ItemBuilder getNormalBook() {
return new ItemBuilder(enchantmentBook);
return new ItemBuilder(this.enchantmentBook);
}

/**
* @return the itemstack of the enchantment book.
*/
public ItemStack getEnchantmentBookItem() {
return new ItemBuilder(enchantmentBook).build();
return new ItemBuilder(this.enchantmentBook).build();
}

/**
Expand Down Expand Up @@ -215,42 +217,43 @@ public int getEnchantmentAmount(ItemStack item, boolean includeVanillaEnchantmen
* @return List of all the categories.
*/
public List<Category> getCategories() {
return categories;
return this.categories;
}

/**
* Loads in all config options.
*/
public void populateMaps() {
FileConfiguration config = FileManager.Files.CONFIG.getFile();
FileConfiguration config = Files.CONFIG.getFile();

for (String category : config.getConfigurationSection("Categories").getKeys(false)) {
String path = "Categories." + category;
LostBook lostBook = new LostBook(
config.getInt(path + ".LostBook.Slot"),
config.getBoolean(path + ".LostBook.InGUI"),
new ItemBuilder()
.setMaterial(config.getString(path + ".LostBook.Item"))
.setMaterial(config.getString(path + ".LostBook.Item", "BOOK"))
.setPlayerName(config.getString(path + ".LostBook.Player"))
.setName(config.getString(path + ".LostBook.Name"))
.setName(config.getString(path + ".LostBook.Name", "Error getting name."))
.setLore(config.getStringList(path + ".LostBook.Lore"))
.setGlow(config.getBoolean(path + ".LostBook.Glowing")),
.setGlow(config.getBoolean(path + ".LostBook.Glowing", true)),
config.getInt(path + ".LostBook.Cost"),
Currency.getCurrency(config.getString(path + ".LostBook.Currency")),
config.getBoolean(path + ".LostBook.FireworkToggle"),
getColors(config.getString(path + ".LostBook.FireworkColors")),
config.getBoolean(path + ".LostBook.Sound-Toggle"),
config.getString(path + ".LostBook.Sound"));
categories.add(new Category(
config.getBoolean(path + ".LostBook.FireworkToggle", false),
getColors(config.getString(path + ".LostBook.FireworkColors", "Red, White, Blue")),
config.getBoolean(path + ".LostBook.Sound-Toggle", false),
config.getString(path + ".LostBook.Sound", "BLOCK_ANVIL_PLACE"));

this.categories.add(new Category(
category,
config.getInt(path + ".Slot"),
config.getBoolean(path + ".InGUI"),
config.getBoolean(path + ".InGUI", true),
new ItemBuilder()
.setMaterial(config.getString(path + ".Item"))
.setMaterial(config.getString(path + ".Item", ColorUtils.getRandomPaneColor().getName()))
.setPlayerName(config.getString(path + ".Player"))
.setName(config.getString(path + ".Name"))
.setName(config.getString(path + ".Name", "Error getting name."))
.setLore(config.getStringList(path + ".Lore"))
.setGlow(config.getBoolean(path + ".Glowing")),
.setGlow(config.getBoolean(path + ".Glowing", false)),
config.getInt(path + ".Cost"),
Currency.getCurrency(config.getString(path + ".Currency")),
config.getInt(path + ".Rarity"),
Expand All @@ -270,14 +273,12 @@ public void populateMaps() {
* @return The category object.
*/
public Category getCategory(String name) {
for (Category category : categories) {
for (Category category : this.categories) {
if (category.getName().equalsIgnoreCase(name)) return category;
}

return null;
}



private List<Color> getColors(String string) {
List<Color> colors = new ArrayList<>();
Expand Down Expand Up @@ -313,8 +314,8 @@ public ItemStack removeEnchantment(ItemStack item, CEnchantment enchant) {

return item;
}
public ItemMeta removeEnchantment(ItemMeta meta, CEnchantment enchant) {

public ItemMeta removeEnchantment(ItemMeta meta, CEnchantment enchant) {
List<Component> lore = meta.lore();

if (lore != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ public class ProtectionCrystalSettings {
private ItemBuilder crystal;

public void loadProtectionCrystal() {
FileConfiguration config = FileManager.Files.CONFIG.getFile();
crystal = new ItemBuilder()
.setMaterial(Objects.requireNonNull(config.getString("Settings.ProtectionCrystal.Item")))
.setName(config.getString("Settings.ProtectionCrystal.Name"))
FileConfiguration config = Files.CONFIG.getFile();

this.crystal = new ItemBuilder()
.setMaterial(config.getString("Settings.ProtectionCrystal.Item", "EMERALD"))
.setName(config.getString("Settings.ProtectionCrystal.Name", "Error getting name."))
.setLore(config.getStringList("Settings.ProtectionCrystal.Lore"))
.setGlow(config.getBoolean("Settings.ProtectionCrystal.Glowing"));
.setGlow(config.getBoolean("Settings.ProtectionCrystal.Glowing", false));
}

public ItemStack getCrystals() {
Expand Down Expand Up @@ -108,9 +109,9 @@ public HashMap<UUID, List<ItemStack>> getCrystalItems() {
public boolean isProtectionSuccessful(Player player) {
if (player.hasPermission("crazyenchantments.bypass.protectioncrystal")) return true;

FileConfiguration config = FileManager.Files.CONFIG.getFile();
FileConfiguration config = Files.CONFIG.getFile();

if (config.getBoolean("Settings.ProtectionCrystal.Chance.Toggle")) return methods.randomPicker(config.getInt("Settings.ProtectionCrystal.Chance.Success-Chance", 100), 100);
if (config.getBoolean("Settings.ProtectionCrystal.Chance.Toggle", false)) return this.methods.randomPicker(config.getInt("Settings.ProtectionCrystal.Chance.Success-Chance", 100), 100);

return true;
}
Expand Down
Loading

0 comments on commit c725268

Please sign in to comment.