Update Release 1.2.0

Added a command to regenerate the configuration file.

This command can only be used from console.
This commit is contained in:
Paldiu 2022-06-14 01:51:45 -05:00
parent c3d781f5b6
commit 0c82515f43
24 changed files with 170 additions and 80 deletions

View File

@ -3,7 +3,7 @@ plugins {
}
group = 'io.github.simplex'
version = '1.2.0-RC01'
version = '1.2.0'
repositories {
mavenCentral()

View File

@ -3,56 +3,59 @@ package io.github.simplex.luck;
import io.github.simplex.luck.listener.AbstractListener;
import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("ResultOfMethodCallIgnored")
public class Config extends YamlConfiguration {
private final FeelingLucky plugin;
private final List<String> configEntries = new ArrayList<>() {{
add("high_rarity_chance");
add("medium_rarity_chance");
add("low_rarity_chance");
add("block_drops");
add("bonemeal");
add("cheat_death");
add("enchanting");
add("experience");
add("item_drops");
add("random_effect");
add("restore_hunger");
add("take_damage");
add("unbreakable");
private final Map<String, Object> configEntries = new HashMap<>() {{
put("high_rarity_chance", 512.0);
put("medium_rarity_chance", 128.0);
put("low_rarity_chance", 64.0);
put("block_drops", "LOW");
put("bonemeal", "MED");
put("cheat_death", "MED");
put("enchanting", "HIGH");
put("experience", "HIGH");
put("give_damage", "LOW");
put("hide_check", "MED");
put("item_drops", "LOW");
put("jump_boost", "MED");
put("ore_vein", "HIGH");
put("random_effect", "HIGH");
put("restore_hunger", "NONE");
put("take_damage", "MED");
put("unbreakable", "HIGH");
}};
private File configFile;
public Config(FeelingLucky plugin) {
this.plugin = plugin;
File dataFolder = plugin.getDataFolder();
if (!dataFolder.exists()) dataFolder.mkdirs();
if (dataFolder.mkdirs()) {
plugin.getLogger().info("Created new data folder. Writing new configuration file...");
plugin.saveResource("config.yml", true);
}
File configFile = new File(dataFolder, "config.yml");
if (!configFile.exists()) {
SneakyWorker.sneakyTry(configFile::createNewFile);
plugin.getLogger().info("No configuration file exists. Creating a new one...");
plugin.saveResource("config.yml", true);
}
this.configFile = configFile;
load();
if (validateIntegrity()) {
File newFile = new File(plugin.getDataFolder(), "config.yml");
SneakyWorker.sneakyTry(() -> {
Files.delete(Path.of(this.configFile.getPath()));
newFile.createNewFile();
plugin.saveResource("config.yml", true);
});
this.configFile = newFile;
if (validateIntegrity(this.configFile)) {
load();
} else {
configEntries.forEach(super::set);
plugin.getLogger().warning("Your configuration file is missing keys. " +
"\nPlease use /rgc in the console to regenerate the config file. " +
"\nAlternatively, delete the config.yml and restart your server. " +
"\nIt is safe to ignore this, as default values will be used." +
"\nHowever, it is highly recommended to regenerate the configuration.");
}
}
@ -69,16 +72,22 @@ public class Config extends YamlConfiguration {
load();
}
public boolean validateIntegrity() {
for (String key : getKeys(false)) {
if (!configEntries.contains(key)) {
plugin.getLogger().severe("The contents of your configuration file is corrupted! Regenerating a new configuration file...");
return true;
}
}
public boolean validateIntegrity(@NotNull File fromDisk) {
YamlConfiguration disk = YamlConfiguration.loadConfiguration(fromDisk);
if (disk.getKeys(true).size() <= 0) {
return false;
}
boolean result = true;
for (String key : configEntries.keySet()) {
if (!disk.getKeys(false).contains(key)) {
if (result) result = false;
}
}
return result;
}
public AbstractListener.Rarity getRarity(String name) {
return AbstractListener.Rarity.valueOf(getString(name));
}

View File

@ -4,14 +4,14 @@ import io.github.simplex.luck.listener.*;
import io.github.simplex.luck.player.PlayerConfig;
import io.github.simplex.luck.player.PlayerHandler;
import io.github.simplex.luck.util.LuckCMD;
import io.github.simplex.luck.util.SneakyWorker;
import io.github.simplex.luck.util.RegenerateConfigCMD;
import io.github.simplex.luck.util.SpecialFootItem;
import io.github.simplex.metrics.Metrics;
import org.bukkit.command.CommandMap;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@ -41,9 +41,10 @@ public final class FeelingLucky extends JavaPlugin {
loadPlayerConfigurations();
getLogger().info("Attempting to load the main configuration...");
config = new Config(this);
getLogger().info("Main Config loaded successfully! Attempting to load the Luck command...");
getLogger().info("Main Config loaded successfully! Loading commands...");
new LuckCMD(this);
getLogger().info("Successfully loaded the Luck command!");
new RegenerateConfigCMD(this);
getLogger().info("Successfully loaded all commands!");
getLogger().info("Successfully initialized!");
}
@ -79,18 +80,23 @@ public final class FeelingLucky extends JavaPlugin {
}
private void registerListeners() {
try {
Class<?>[] listeners = SneakyWorker.getClasses(AbstractListener.class.getPackage().getName());
Arrays.stream(listeners).forEach(l -> {
if (AbstractListener.class.isAssignableFrom(l)) {
if (l.equals(AbstractListener.class)) return;
SneakyWorker.sneakyTry(() -> l.getDeclaredConstructor(FeelingLucky.class).newInstance(this));
}
});
} catch (IOException | ClassNotFoundException ex) {
getLogger().severe(ex.getMessage());
}
new BlockDrops(this);
new BonemealFullCrop(this);
new CheatDeath(this);
new EnchantmentBoost(this);
new ExpBoost(this);
new GiveDamage(this);
new HideCheck(this);
new IllOmen(this);
new ItemDrops(this);
new JumpBoost(this);
new OreVein(this);
new PlayerListener(this);
new RandomEffect(this);
new RestoreHunger(this);
new TakeDamage(this);
new UnbreakableTool(this);
new VillagerInventory(this);
}
public PlayerHandler getHandler() {
@ -106,4 +112,8 @@ public final class FeelingLucky extends JavaPlugin {
public SpecialFootItem getFoot() {
return specialFootItem;
}
public CommandMap getCommandMap() {
return getServer().getCommandMap();
}
}

View File

@ -7,23 +7,24 @@ import org.bukkit.event.Listener;
public abstract class AbstractListener implements Listener {
protected final FeelingLucky plugin;
protected final Config config;
public AbstractListener(FeelingLucky plugin) {
this.plugin = plugin;
this.config = plugin.getConfig();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
protected PlayerHandler getHandler() {
return plugin.getHandler();
}
public void register(AbstractListener listener) {
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
}
public boolean doesQualify(String name, double luck) {
return switch (config.getRarity(name)) {
case HIGH -> luck > config.getChance("high_rarity_chance");
case MED -> luck > config.getChance("medium_rarity_chance");
case LOW -> luck > config.getChance("low_rarity_chance");
return switch (plugin.getConfig().getRarity(name)) {
case HIGH -> luck > plugin.getConfig().getChance("high_rarity_chance");
case MED -> luck > plugin.getConfig().getChance("medium_rarity_chance");
case LOW -> luck > plugin.getConfig().getChance("low_rarity_chance");
case NONE -> true;
};
}

View File

@ -13,6 +13,7 @@ import java.util.List;
public final class BlockDrops extends AbstractListener {
public BlockDrops(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler
@ -21,7 +22,6 @@ public final class BlockDrops extends AbstractListener {
Luck luck = getHandler().getLuckContainer(player);
List<Item> items = event.getItems();
if (luck.quickRNG(luck.getValue()) && doesQualify("block_drops", luck.getValue())) {
event.getItems().clear();
event.getItems().addAll(items.stream().map(SneakyWorker::move).toList());
}
}

View File

@ -17,6 +17,7 @@ import org.bukkit.inventory.ItemStack;
public final class BonemealFullCrop extends AbstractListener {
public BonemealFullCrop(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler
@ -42,7 +43,6 @@ public final class BonemealFullCrop extends AbstractListener {
crop.setAge(crop.getMaximumAge());
data.merge(crop);
block.setBlockData(data);
player.sendMessage(MiniComponent.info("You got lucky and your crops grew to maturity."));
}
}
}

View File

@ -10,6 +10,7 @@ import org.bukkit.event.entity.PlayerDeathEvent;
public final class CheatDeath extends AbstractListener {
public CheatDeath(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler

View File

@ -13,6 +13,7 @@ import java.util.Map;
public final class EnchantmentBoost extends AbstractListener {
public EnchantmentBoost(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler

View File

@ -10,6 +10,7 @@ import org.bukkit.event.EventHandler;
public final class ExpBoost extends AbstractListener {
public ExpBoost(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler

View File

@ -11,6 +11,7 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent;
public class GiveDamage extends AbstractListener {
public GiveDamage(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler
@ -19,9 +20,8 @@ public class GiveDamage extends AbstractListener {
&& (e.getEntity() instanceof LivingEntity)) {
double nextDmg = e.getDamage() + Luck.RNG().nextDouble(1.0, 5.0);
Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getValue())) {
if (luck.quickRNG(luck.getValue()) && doesQualify("give_damage", luck.getValue())) {
e.setDamage(nextDmg);
player.sendMessage(MiniComponent.info("Your luck has increased your damage output!"));
}
}
}

View File

@ -19,6 +19,7 @@ public class HideCheck extends AbstractListener {
public HideCheck(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler
@ -41,10 +42,10 @@ public class HideCheck extends AbstractListener {
@EventHandler
public void checkForSneak(PlayerToggleSneakEvent event) {
Player player = event.getPlayer();
if (!player.isSneaking()) return;
if (player.isSneaking()) return;
Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getValue()) && !luck.isMarked(player)) {
if (luck.quickRNG(luck.getValue()) && doesQualify("hide_check", luck.getValue())) {
entityMapList.get(player).forEach(e -> {
e.getTrackedPlayers().remove(player);
});

View File

@ -14,6 +14,7 @@ import org.bukkit.potion.PotionEffectType;
public class IllOmen extends AbstractListener {
public IllOmen(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler

View File

@ -22,6 +22,7 @@ public class ItemDrops extends AbstractListener {
public ItemDrops(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler

View File

@ -11,16 +11,17 @@ import org.bukkit.util.Vector;
public class JumpBoost extends AbstractListener {
public JumpBoost(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler
public void detectJumping(PlayerJumpEvent event) {
Player player = event.getPlayer(); // Player is never null; they're in game and jumping.
Luck luck = plugin.getHandler().getLuckContainer(player);
Vector velocity = player.getVelocity().clone();
if (luck.quickRNG(luck.getValue()) && !luck.isMarked(player)) {
player.setVelocity(new Vector(0, 2, 0));
player.sendMessage(MiniComponent.info("Your luck has boosted your jump height!"));
if (luck.quickRNG(luck.getValue()) && doesQualify("jump_boost", luck.getValue())) {
player.setVelocity(new Vector(velocity.getX(), velocity.getY() + 3, velocity.getZ()));
}
}
}

View File

@ -18,13 +18,14 @@ public class OreVein extends AbstractListener {
public OreVein(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler
public void playerMine(BlockBreakEvent event) {
Player player = event.getPlayer();
Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getValue()) && event.getBlock().isValidTool(player.getInventory().getItemInMainHand())) {
if (luck.quickRNG(luck.getValue()) && doesQualify("ore_vein", luck.getValue()) && event.getBlock().isValidTool(player.getInventory().getItemInMainHand())) {
getOresInArea(event.getBlock()).forEach(Block::breakNaturally);
player.sendMessage(MiniComponent.info("Your luck has let you mine all the blocks with one swing."));
}

View File

@ -25,6 +25,7 @@ public final class PlayerListener extends AbstractListener {
public PlayerListener(FeelingLucky plugin) {
super(plugin);
this.timer = new CooldownTimer();
register(this);
}
@EventHandler
@ -47,6 +48,7 @@ public final class PlayerListener extends AbstractListener {
player.sendMessage(MiniComponent.info("Your luck multiplier has increased by 0.1!"));
}
double rng = Luck.RNG().nextDouble(2.0, 5.0);
rng = Math.round(rng);
player.getInventory().remove(player.getInventory().getItemInMainHand());
luck.addTo(rng);
plugin.getHandler().updatePlayer(player, luck);

View File

@ -15,6 +15,7 @@ import java.util.List;
public class RandomEffect extends AbstractListener {
public RandomEffect(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler

View File

@ -13,6 +13,7 @@ import org.bukkit.potion.PotionEffectType;
public class RestoreHunger extends AbstractListener {
public RestoreHunger(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler

View File

@ -1,5 +1,6 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.lib.PotionEffectBuilder;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
@ -13,6 +14,7 @@ import org.bukkit.event.entity.EntityDamageEvent;
public class TakeDamage extends AbstractListener {
public TakeDamage(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler
@ -36,7 +38,7 @@ public class TakeDamage extends AbstractListener {
if (luck.quickRNG(percentage)) {
event.setCancelled(true);
player.damage(event.getDamage() * 2);
player.sendMessage(Component.empty().content("You were unlucky and took double damage."));
player.sendMessage(MiniComponent.warn("You were unlucky and took double damage!"));
}
return;
}
@ -44,7 +46,6 @@ public class TakeDamage extends AbstractListener {
if (luck.quickRNG(percentage) && doesQualify("take_damage", percentage)) {
event.setCancelled(true);
player.damage(event.getDamage() / 2);
player.sendMessage(Component.empty().content("You got lucky and took less damage."));
}
}
}
@ -70,7 +71,7 @@ public class TakeDamage extends AbstractListener {
event.setCancelled(true);
player.getActivePotionEffects().removeIf(p -> ListBox.potionEffects.contains(p.getType()));
player.setFireTicks(0);
player.sendMessage(Component.empty().content("You got lucky and your afflictions were cured."));
player.sendMessage(MiniComponent.info("You got lucky and your afflictions were cured."));
}
}
}

View File

@ -14,6 +14,7 @@ import org.bukkit.inventory.meta.ItemMeta;
public class UnbreakableTool extends AbstractListener {
public UnbreakableTool(FeelingLucky plugin) {
super(plugin);
register(this);
}
@EventHandler

View File

@ -31,6 +31,8 @@ public class VillagerInventory extends AbstractListener {
recipe.setPriceMultiplier(1.25F);
recipe.setVillagerExperience(25);
recipe.setSpecialPrice(4);
register(this);
}
@EventHandler

View File

@ -22,12 +22,16 @@ public class LuckCMD extends Command implements TabCompleter, PluginIdentifiable
super("luck", "FeelingLucky main command.", "/<command> <info | set | reset | give | take> [player] [amount]", List.of());
this.plugin = plugin;
setPermission("luck.default");
register(plugin.getServer().getCommandMap());
plugin.getCommandMap().register("luck", "FeelingLucky", this);
plugin.getLogger().info("Successfully registered command: Luck");
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
if (args.length < 1 || args.length > 3) return false;
if (args.length < 1 || args.length > 3) {
sender.sendMessage(this.getUsage());
return false;
}
if (args.length == 3) {
if ((sender instanceof ConsoleCommandSender) || sender.hasPermission("luck.admin")) {
@ -65,21 +69,21 @@ public class LuckCMD extends Command implements TabCompleter, PluginIdentifiable
luck.setValue(amount);
plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
sender.sendMessage(MiniComponent.info("Successfully set " + args[1] + "'s Luck stat to " + amount + "."));
return true;
}
case "give" -> {
luck.addTo(amount);
plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
sender.sendMessage(MiniComponent.info("Successfully gave " + args[1] + " " + amount + " points of luck!"));
return true;
}
case "take" -> {
luck.takeFrom(amount);
plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
sender.sendMessage(MiniComponent.info("Successfully took " + amount + " points of luck from " + args[1]));
return true;
}
}

View File

@ -0,0 +1,46 @@
package io.github.simplex.luck.util;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import org.bukkit.command.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class RegenerateConfigCMD extends Command implements TabCompleter, PluginIdentifiableCommand {
private final FeelingLucky plugin;
public RegenerateConfigCMD(FeelingLucky plugin) {
super("rgc", "Regenerate this plugin's config file.", "/<command>", List.of());
this.plugin = plugin;
setPermission("luck.rgc");
plugin.getCommandMap().register("rgc", "FeelingLucky", this);
plugin.getLogger().info("Successfully registered command: RGC.");
}
@Override
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
if (!(sender instanceof ConsoleCommandSender)) {
sender.sendMessage(MiniComponent.err("This command can only be used through console access."));
return true;
}
plugin.saveResource("config.yml", true);
plugin.getConfig().load();
plugin.getLogger().info("Configuration regenerated.");
return true;
}
@Override
public @NotNull FeelingLucky getPlugin() {
return plugin;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
return new ArrayList<>();
}
}

View File

@ -19,7 +19,11 @@ bonemeal: MED
cheat_death: MED
enchanting: HIGH
experience: HIGH
give_damage: LOW
hide_check: MED
item_drops: LOW
jump_boost: MED
ore_vein: HIGH
random_effect: HIGH
restore_hunger: NONE
take_damage: MED