diff --git a/src/main/java/me/justeli/coins/config/Config.java b/src/main/java/me/justeli/coins/config/Config.java index 4f4b348..91b5e52 100644 --- a/src/main/java/me/justeli/coins/config/Config.java +++ b/src/main/java/me/justeli/coins/config/Config.java @@ -1,5 +1,6 @@ package me.justeli.coins.config; +import me.justeli.coins.util.Util; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.EntityType; @@ -38,10 +39,10 @@ public class Config @ConfigEntry (value = "check-for-updates", required = false) public static Boolean CHECK_FOR_UPDATES = true; @ConfigEntry ("language") public static String LANGUAGE = "English"; - @ConfigEntry ("coin-item") protected static String RAW_COIN_ITEM = "sunflower"; + @ConfigEntry ("coin-item") public static Material COIN_ITEM = Material.SUNFLOWER; @ConfigEntry ("pickup-message") public static String PICKUP_MESSAGE = "&2+ &a{currency}{amount}"; @ConfigEntry ("death-message") public static String DEATH_MESSAGE = "&4- &c{currency}{amount}"; - @ConfigEntry ("sound-name") protected static String RAW_SOUND_NAME = "ITEM_ARMOR_EQUIP_GOLD"; + @ConfigEntry ("sound-name") public static Sound SOUND_NAME = Sound.ITEM_ARMOR_EQUIP_GOLD; @ConfigEntry ("currency-symbol") public static String CURRENCY_SYMBOL = "$"; @ConfigEntry ("skull-texture") public static String SKULL_TEXTURE = ""; @@ -52,14 +53,14 @@ public class Config // preferred-economy-hook: 'Vault' @ConfigEntry (value = "dropped-coin-name", motivation = "This is a replacement, previous key was 'nameOfCoin', which will be unsupported in a future " + - "version.") protected static String RAW_DROPPED_COIN_NAME = "&6Coin"; + "version.") public static String DROPPED_COIN_NAME = "&6Coin"; @ConfigEntry (value = "withdrawn-coin-names.singular", motivation = "This is a replacement, previous key was 'nameOfCoin', which will be unsupported " + - "in a future version.") protected static String RAW_WITHDRAWN_COIN_NAME_SINGULAR = "&e{amount} &6Coin"; + "in a future version.") public static String WITHDRAWN_COIN_NAME_SINGULAR = "&e{amount} &6Coin"; @ConfigEntry (value = "withdrawn-coin-names.plural", motivation = "This is a replacement, previous key was 'nameOfCoin' and 'multiSuffix', which will" + - " be unsupported in a future version.") protected static String RAW_WITHDRAWN_COIN_NAME_PLURAL = "&e{amount} &6Coins"; + " be unsupported in a future version.") public static String WITHDRAWN_COIN_NAME_PLURAL = "&e{amount} &6Coins"; - @Deprecated @ConfigEntry (value = "name-of-coin", required = false) protected static String LEGACY_RAW_NAME_OF_COIN = null; - @Deprecated @ConfigEntry (value = "multi-suffix", required = false) public static String LEGACY_MULTI_SUFFIX = null; + @Deprecated @ConfigEntry (value = "name-of-coin", required = false) protected static String LEGACY_NAME_OF_COIN = null; + @Deprecated @ConfigEntry (value = "multi-suffix", required = false) protected static String LEGACY_MULTI_SUFFIX = null; @ConfigEntry ("drop-chance") public static Double DROP_CHANCE = 0.9; @ConfigEntry ("max-withdraw-amount") public static Double MAX_WITHDRAW_AMOUNT = 10000.0; @@ -87,18 +88,12 @@ public class Config @Deprecated @ConfigEntry (value = "block-multiplier", required = false) protected static Map LEGACY_RAW_BLOCK_MULTIPLIER = new HashMap<>(); - public static String DROPPED_COIN_NAME; - public static String WITHDRAWN_COIN_NAME_SINGULAR; - public static String WITHDRAWN_COIN_NAME_PLURAL; @Deprecated public static String LEGACY_WITHDRAWN_COIN_ENDING; - public static Material COIN_ITEM; - public static Sound SOUND_NAME; - public static Map BLOCK_DROPS = new HashMap<>(); public static Map MOB_MULTIPLIER = new HashMap<>(); - @Deprecated protected static final String LEGACY_PREFIX = "&e{amount} &r"; + @Deprecated protected static final String LEGACY_PREFIX = Util.color("&e{amount} &r"); private Config () {} } diff --git a/src/main/java/me/justeli/coins/config/Settings.java b/src/main/java/me/justeli/coins/config/Settings.java index cc7982c..9d1fe07 100644 --- a/src/main/java/me/justeli/coins/config/Settings.java +++ b/src/main/java/me/justeli/coins/config/Settings.java @@ -13,7 +13,6 @@ import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -import javax.annotation.Nullable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -114,7 +113,7 @@ public void parseConfig () Object defaultValue = prefixSuffix + field.get(Config.class) + prefixSuffix; warning(String.format( - "\nConfig file is missing key called '%s'. Using its default value now (%s)." + "\nConfig file is missing key `%s`. Using its default value now (%s)." + (configEntry.motivation().isEmpty()? "" : " " + configEntry.motivation()) + " Consider to add this to the config:\n----------------------------------------\n%s: %s" + "\n----------------------------------------", @@ -146,6 +145,26 @@ else if (configClass == Map.class) } configValue = configMap; } + else if (configClass == String.class || configClass == Material.class || configClass == Sound.class) + { + String value = config.getString(configKey); + if (value == null) + { + throw new NullPointerException(); + } + else if (configClass == Material.class) + { + configValue = getMaterial(value, configEntry.value()).orElse(Material.SUNFLOWER); + } + else if (configClass == Sound.class) + { + configValue = getSound(value, configEntry.value()).orElse(Sound.ITEM_ARMOR_EQUIP_GOLD); + } + else + { + configValue = Util.color(value); + } + } // can be improved in java 11 else if (configClass == Long.class || configClass == Integer.class || configClass == Float.class || configClass == Double.class) { @@ -182,7 +201,7 @@ else if (configClass == Float.class) { Object defaultValue = field.get(Config.class); warning(String.format( - "Config file has wrong value for key called '%s'. Using its default value now (%s).", + "Config file has wrong value at `%s`. Using its default value now (%s).", configEntry.value(), defaultValue )); @@ -196,53 +215,49 @@ else if (configClass == Float.class) private void parseRemainingOptions () { - Config.DROPPED_COIN_NAME = Util.color(Config.LEGACY_RAW_NAME_OF_COIN == null? Config.RAW_DROPPED_COIN_NAME : Config.LEGACY_RAW_NAME_OF_COIN); + // start compatibility with older versions - Config.WITHDRAWN_COIN_NAME_SINGULAR = Util.color(Config.LEGACY_RAW_NAME_OF_COIN == null - ? Config.RAW_WITHDRAWN_COIN_NAME_SINGULAR - : Config.LEGACY_PREFIX + Config.LEGACY_RAW_NAME_OF_COIN); - - Config.WITHDRAWN_COIN_NAME_PLURAL = Util.color(Config.LEGACY_MULTI_SUFFIX == null && Config.LEGACY_RAW_NAME_OF_COIN == null - ? Config.RAW_WITHDRAWN_COIN_NAME_PLURAL - : Config.LEGACY_PREFIX + Config.LEGACY_RAW_NAME_OF_COIN + Config.LEGACY_MULTI_SUFFIX); + if (Config.DETECT_LEGACY_COINS) + { + if (Config.LEGACY_WITHDRAWN_COIN_ENDING != null) + { + Config.DROPPED_COIN_NAME = Config.LEGACY_NAME_OF_COIN; + } - Config.LEGACY_WITHDRAWN_COIN_ENDING = Config.LEGACY_MULTI_SUFFIX == null && Config.LEGACY_RAW_NAME_OF_COIN == null - ? null - : Util.color(Config.LEGACY_RAW_NAME_OF_COIN + Config.LEGACY_MULTI_SUFFIX); + if (Config.LEGACY_NAME_OF_COIN != null) + { + Config.WITHDRAWN_COIN_NAME_SINGULAR = Config.LEGACY_PREFIX + Config.LEGACY_NAME_OF_COIN; + } - Config.COIN_ITEM = coinItem(); - Config.SOUND_NAME = soundName(); + if (Config.LEGACY_MULTI_SUFFIX != null && Config.LEGACY_NAME_OF_COIN != null) + { + Config.WITHDRAWN_COIN_NAME_PLURAL = Config.LEGACY_PREFIX + Config.LEGACY_NAME_OF_COIN + Config.LEGACY_MULTI_SUFFIX; + Config.LEGACY_WITHDRAWN_COIN_ENDING = Config.LEGACY_NAME_OF_COIN + Config.LEGACY_MULTI_SUFFIX; + } - if (Config.DETECT_LEGACY_COINS) - { Config.ALLOW_NAME_CHANGE = false; } Config.RAW_BLOCK_DROPS.putAll(Config.LEGACY_RAW_BLOCK_MULTIPLIER); + // end compatibility with older versions + Config.BLOCK_DROPS.clear(); Config.RAW_BLOCK_DROPS.forEach((k, v) -> { - Material material = getMaterial(k, "block-drops"); - if (material != null) - { - Config.BLOCK_DROPS.put(material, v); - } + Optional material = getMaterial(k, "block-drops"); + material.ifPresent(value -> Config.BLOCK_DROPS.put(value, v)); }); Config.MOB_MULTIPLIER.clear(); Config.RAW_MOB_MULTIPLIER.forEach((k, v) -> { - EntityType entityType = getEntityType(k, "mob-multiplier"); - if (entityType != null) - { - Config.MOB_MULTIPLIER.put(entityType, v); - } + Optional entityType = getEntityType(k, "mob-multiplier"); + entityType.ifPresent(type -> Config.MOB_MULTIPLIER.put(type, v)); }); } - @Nullable - private Material getMaterial (String name, String configKey) + private Optional getMaterial (String name, String configKey) { Material material = Material.matchMaterial(name.replace(" ", "_").toUpperCase(Locale.ROOT).replace("COIN", "SUNFLOWER")); @@ -251,46 +266,39 @@ private Material getMaterial (String name, String configKey) warning("The material '" + name + "' in the config at `" + configKey + "` does not exist. Please use a " + "material from: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html"); - return null; + return Optional.empty(); } - return material; + return Optional.of(material); } - @Nullable - private EntityType getEntityType (String name, String configKey) + private Optional getEntityType (String name, String configKey) { try { - return EntityType.valueOf(name.replace(" ", "_").toUpperCase(Locale.ROOT)); + return Optional.of(EntityType.valueOf(name.replace(" ", "_").toUpperCase(Locale.ROOT))); } catch (IllegalArgumentException exception) { warning("The mob name '" + name + "' in the config at `" + configKey + "` does not exist. Please use a " + "name from: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html"); - return null; + return Optional.empty(); } } - private Material coinItem () - { - Material coin = getMaterial(Config.RAW_COIN_ITEM, "coin-item"); - return coin == null? Material.SUNFLOWER : coin; - } - - private Sound soundName () + private Optional getSound (String name, String configKey) { try { - return Sound.valueOf(Config.RAW_SOUND_NAME.toUpperCase().replace(" ", "_")); + return Optional.of(Sound.valueOf(name.toUpperCase().replace(" ", "_"))); } catch (IllegalArgumentException exception) { - warning("The sound '" + Config.RAW_SOUND_NAME + "' in the config at `sound-name` does not exist. Please use a " + + warning("The sound '" + name + "' in the config at `" + configKey + "` does not exist. Please use a " + "sound from: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html"); - return Sound.ITEM_ARMOR_EQUIP_GOLD; + return Optional.empty(); } } diff --git a/src/main/java/me/justeli/coins/handler/DropHandler.java b/src/main/java/me/justeli/coins/handler/DropHandler.java index 730450c..f17c07b 100644 --- a/src/main/java/me/justeli/coins/handler/DropHandler.java +++ b/src/main/java/me/justeli/coins/handler/DropHandler.java @@ -6,6 +6,7 @@ import me.justeli.coins.util.Permission; import me.justeli.coins.util.SubTitle; import me.justeli.coins.util.Util; +import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -215,22 +216,22 @@ public void onBlockBreak (BlockBreakEvent event) if (Config.MINE_PERCENTAGE == 0) return; - if (blockDropsSameItem(event)) + if (event.getPlayer().getGameMode() != GameMode.SURVIVAL || blockDropsSameItem(event)) return; int multiplier = Config.BLOCK_DROPS.computeIfAbsent(event.getBlock().getType(), empty -> 0); if (multiplier == 0) return; - if (RANDOM.nextDouble() <= Config.MINE_PERCENTAGE) - { - this.coins.sync(1, () -> drop( - multiplier, - event.getPlayer(), - event.getBlock().getLocation().clone().add(0.5, 0.5, 0.5), - Enchantment.LOOT_BONUS_BLOCKS - )); - } + if (RANDOM.nextDouble() > Config.MINE_PERCENTAGE) + return; + + this.coins.sync(1, () -> drop( + multiplier, + event.getPlayer(), + event.getBlock().getLocation().clone().add(0.5, 0.5, 0.5), + Enchantment.LOOT_BONUS_BLOCKS + )); } private boolean blockDropsSameItem (BlockBreakEvent event) diff --git a/src/main/java/me/justeli/coins/util/Util.java b/src/main/java/me/justeli/coins/util/Util.java index a7d1c7d..ba36a9d 100644 --- a/src/main/java/me/justeli/coins/util/Util.java +++ b/src/main/java/me/justeli/coins/util/Util.java @@ -4,7 +4,6 @@ import me.justeli.coins.config.Config; import net.md_5.bungee.api.ChatColor; import org.bukkit.Bukkit; -import org.bukkit.Sound; import org.bukkit.World; import org.bukkit.entity.Boss; import org.bukkit.entity.Entity; @@ -21,6 +20,7 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.projectiles.ProjectileSource; +import org.jetbrains.annotations.NotNull; import java.math.BigDecimal; import java.math.RoundingMode; @@ -42,7 +42,7 @@ public final class Util private static final HashMap PLAYER_MULTIPLIER = new HashMap<>(); private static final SplittableRandom RANDOM = new SplittableRandom(); - public static String color (String msg) + public static String color (@NotNull String msg) { return ChatColor.translateAlternateColorCodes('&', parseRGB(msg)); } @@ -59,7 +59,7 @@ public static String formatCurrency (String text) return text.replaceAll("(\\{currency}|\\{\\$})", Matcher.quoteReplacement(Config.CURRENCY_SYMBOL)); } - private static String parseRGB (String msg) + private static String parseRGB (@NotNull String msg) { if (PaperLib.getMinecraftVersion() >= 16) { @@ -142,11 +142,7 @@ public static void playCoinPickupSound (Player player) float volume = Config.SOUND_VOLUME; float pitch = Config.SOUND_PITCH; - Sound sound = Config.SOUND_NAME; - if (sound == null) - return; - - player.playSound(player.getEyeLocation(), sound, volume == 0? 0.3f : volume, pitch == 0? 0.3f : pitch); + player.playSound(player.getEyeLocation(), Config.SOUND_NAME, volume == 0? 0.3f : volume, pitch == 0? 0.3f : pitch); } public static boolean isDisabledHere (World world) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9113ff8..67fcc9a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -218,7 +218,7 @@ block-drops: 'deepslate redstone ore': 1 # What's the chance per mined block? Set to 0 to disable dropping coins for blocks. -mine-percentage: 0.30 +mine-percentage: 0.20 # # # # # # # Withdraw #