Skip to content

Commit

Permalink
config parsing optimization, reduce default mine-%
Browse files Browse the repository at this point in the history
  • Loading branch information
justEli committed Apr 15, 2022
1 parent ea80af1 commit e0a8cff
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 79 deletions.
23 changes: 9 additions & 14 deletions src/main/java/me/justeli/coins/config/Config.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 = "";

Expand All @@ -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;
Expand Down Expand Up @@ -87,18 +88,12 @@ public class Config
@Deprecated @ConfigEntry (value = "block-multiplier", required = false)
protected static Map<String, Integer> 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<Material, Integer> BLOCK_DROPS = new HashMap<>();
public static Map<EntityType, Integer> 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 () {}
}
100 changes: 54 additions & 46 deletions src/main/java/me/justeli/coins/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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----------------------------------------",
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
));
Expand All @@ -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> 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> entityType = getEntityType(k, "mob-multiplier");
entityType.ifPresent(type -> Config.MOB_MULTIPLIER.put(type, v));
});
}

@Nullable
private Material getMaterial (String name, String configKey)
private Optional<Material> getMaterial (String name, String configKey)
{
Material material = Material.matchMaterial(name.replace(" ", "_").toUpperCase(Locale.ROOT).replace("COIN", "SUNFLOWER"));

Expand All @@ -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<EntityType> 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<Sound> 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();
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/main/java/me/justeli/coins/handler/DropHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/me/justeli/coins/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -42,7 +42,7 @@ public final class Util
private static final HashMap<UUID, Double> 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));
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down

0 comments on commit e0a8cff

Please sign in to comment.