Beta 20220414-SNAPSHOT

Changelog:
 - Added the ability to fully mature crops when using bone meal
 - Added the ability to cheat death (Grants 1 half heart and between 2.5 and 5 points of absorption)
 - Added #info, #warn, and #err to MiniComponent, and utilized these across the board.
 - Switched the #color method to use org.bukkit.ChatColor instead of TextColor for accessibility.
 - Moved loading configurations and listener registration to separate methods.
This commit is contained in:
Paldiu 2022-04-14 14:58:14 -05:00
parent 899768819e
commit 8e70e1de73
7 changed files with 143 additions and 42 deletions

View File

@ -1,17 +1,13 @@
package io.github.simplex.lib; package io.github.simplex.lib;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
public enum Messages { public enum Messages {
NOT_FROM_CONSOLE(MiniComponent.of("This command may only be used in game.").send()), NOT_FROM_CONSOLE(MiniComponent.err("This command may only be used in game.")),
NO_PERMISSION(MiniComponent NO_PERMISSION(MiniComponent.err("You do not have permission to use this command.")),
.of("You do not have permission to use this command.") NO_PLAYER(MiniComponent.warn("That player cannot be found.")),
.color(TextColor.color(255, 0, 0)) OUT_OF_BOUNDS(MiniComponent.err("Number must be between -1024.0 and 1024.0"));
.decorate(TextDecoration.ITALIC)
.send());
private final Component message; private final Component message;

View File

@ -3,6 +3,7 @@ package io.github.simplex.lib;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.ChatColor;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
public class MiniComponent { public class MiniComponent {
@ -15,6 +16,21 @@ public class MiniComponent {
return new MiniComponent(content); return new MiniComponent(content);
} }
@Contract("_ -> new")
public static Component info(String content) {
return new MiniComponent(content).color(ChatColor.GREEN).send();
}
@Contract("_ -> new")
public static Component warn(String content) {
return new MiniComponent(content).color(ChatColor.YELLOW).decorate(TextDecoration.ITALIC).send();
}
@Contract("_ -> new")
public static Component err(String content) {
return new MiniComponent(content).color(ChatColor.RED).decorate(TextDecoration.BOLD).send();
}
public MiniComponent(String content) { public MiniComponent(String content) {
this.content = content; this.content = content;
} }
@ -24,8 +40,8 @@ public class MiniComponent {
return this; return this;
} }
public MiniComponent color(TextColor color) { public MiniComponent color(ChatColor color) {
this.color = color; this.color = TextColor.color(color.asBungee().getColor().getRGB());
return this; return this;
} }

View File

@ -28,27 +28,9 @@ public final class FeelingLucky extends JavaPlugin {
getLogger().info("Initializing the PlayerHandler..."); getLogger().info("Initializing the PlayerHandler...");
handler = new PlayerHandler(this); handler = new PlayerHandler(this);
getLogger().info("Initialization complete! Attempting to register the Listeners..."); getLogger().info("Initialization complete! Attempting to register the Listeners...");
new PlayerListener(this); registerListeners();
new BlockDrops(this);
new ItemDrops(this);
new TakeDamage(this);
new RestoreHunger(this);
new EnchantmentBoost(this);
new ExpBoost(this);
getLogger().info("Registration complete! Attempting to load all player configuration files..."); getLogger().info("Registration complete! Attempting to load all player configuration files...");
loadConfigurations();
File[] files = getDataFolder().listFiles();
if (files != null) {
Arrays.stream(files).forEach(file -> {
UUID uuid = UUID.fromString(file.getName().split("\\.")[0]);
configMap.put(uuid, PlayerConfig.loadFrom(this, file));
});
configMap.forEach((u, pc) -> pc.load());
getLogger().info("Successfully loaded all configurations!");
} else {
getLogger().info("There are no player configurations to load.");
}
Bukkit.getLogger().info("Attempting to load the Luck command..."); Bukkit.getLogger().info("Attempting to load the Luck command...");
cmd = new LuckCMD(this); cmd = new LuckCMD(this);
Bukkit.getLogger().info("Successfully loaded the Luck command!"); Bukkit.getLogger().info("Successfully loaded the Luck command!");
@ -61,4 +43,30 @@ public final class FeelingLucky extends JavaPlugin {
Bukkit.getLogger().info("Saving all player configurations..."); Bukkit.getLogger().info("Saving all player configurations...");
configMap.values().forEach(PlayerConfig::save); configMap.values().forEach(PlayerConfig::save);
} }
private void loadConfigurations() {
File[] files = getDataFolder().listFiles();
if (files != null) {
Arrays.stream(files).forEach(file -> {
UUID uuid = UUID.fromString(file.getName().split("\\.")[0]);
configMap.put(uuid, PlayerConfig.loadFrom(this, file));
});
configMap.forEach((u, pc) -> pc.load());
getLogger().info("Successfully loaded all configurations!");
} else {
getLogger().info("There are no player configurations to load.");
}
}
private void registerListeners() {
new PlayerListener(this);
new BlockDrops(this);
new ItemDrops(this);
new TakeDamage(this);
new RestoreHunger(this);
new EnchantmentBoost(this);
new ExpBoost(this);
new CheatDeath(this);
new BonemealFullCrop(this);
}
} }

View File

@ -1,8 +1,8 @@
package io.github.simplex.luck.listener; package io.github.simplex.luck.listener;
import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.util.SneakyWorker;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.SneakyWorker;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;

View File

@ -0,0 +1,50 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.ItemBuilder;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
public record BonemealFullCrop(FeelingLucky plugin) implements Listener {
public BonemealFullCrop {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void bonemealFullCrop(PlayerInteractEvent event) {
Player player = event.getPlayer();
Action action = event.getAction();
ItemStack bonemeal = ItemBuilder.of(Material.BONE_MEAL).build();
Luck luck = plugin.handler.getLuckContainer(player);
ItemStack handItem = event.getItem();
if (handItem == null) return;
Block block = event.getClickedBlock();
if (block == null) return;
BlockData data = block.getBlockData();
if (action.isRightClick()
&& handItem.isSimilar(bonemeal)
&& (data instanceof Ageable crop)
&& luck.quickRNG(luck.getPercentage())) {
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

@ -0,0 +1,30 @@
package io.github.simplex.luck.listener;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
public record CheatDeath(FeelingLucky plugin) implements Listener {
public CheatDeath(FeelingLucky plugin) {
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void cheatDeath(PlayerDeathEvent event) {
Player player = event.getPlayer();
Luck luck = plugin.handler.getLuckContainer(player);
double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0));
if (luck.quickRNG(luck.getPercentage())) {
event.setCancelled(true);
player.setHealth(1.0);
player.setAbsorptionAmount(absorption);
player.sendMessage(MiniComponent.of("You got lucky and cheated death!").send());
}
}
}

View File

@ -1,6 +1,7 @@
package io.github.simplex.luck.util; package io.github.simplex.luck.util;
import io.github.simplex.lib.Messages; import io.github.simplex.lib.Messages;
import io.github.simplex.lib.MiniComponent;
import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.player.PlayerConfig; import io.github.simplex.luck.player.PlayerConfig;
@ -36,12 +37,12 @@ public class LuckCMD extends Command implements TabCompleter {
double amount = Double.parseDouble(args[2]); double amount = Double.parseDouble(args[2]);
if (player == null) { if (player == null) {
sender.sendMessage(Component.empty().content("That player cannot be found.")); sender.sendMessage(Messages.NO_PLAYER.get());
return true; return true;
} }
if (amount > 1024.0 || amount < -1024.0) { if (amount > 1024.0 || amount < -1024.0) {
sender.sendMessage(Component.empty().content("Number must be between -1024.0 and 1024.0")); sender.sendMessage(Messages.OUT_OF_BOUNDS.get());
return true; return true;
} }
@ -53,21 +54,21 @@ public class LuckCMD extends Command implements TabCompleter {
luck.setValue(amount); luck.setValue(amount);
plugin.handler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.getValue()); config.setLuck(luck.getValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
} }
case "give" -> { case "give" -> {
luck.addTo(amount); luck.addTo(amount);
plugin.handler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.getValue()); config.setLuck(luck.getValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
} }
case "take" -> { case "take" -> {
luck.takeFrom(amount); luck.takeFrom(amount);
plugin.handler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.getValue()); config.setLuck(luck.getValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
} }
} }
@ -83,12 +84,12 @@ public class LuckCMD extends Command implements TabCompleter {
Player player = Bukkit.getPlayer(args[1]); Player player = Bukkit.getPlayer(args[1]);
if (player == null) { if (player == null) {
sender.sendMessage("That player cannot be found."); sender.sendMessage(Messages.NO_PLAYER.get());
return true; return true;
} }
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
sender.sendMessage(Component.empty().content("Luck stat for " + args[1] + ": " + luck.getValue())); sender.sendMessage(MiniComponent.info("Luck stat for " + args[1] + ": " + luck.getValue()));
return true; return true;
} }
@ -96,7 +97,7 @@ public class LuckCMD extends Command implements TabCompleter {
Player player = Bukkit.getPlayer(args[1]); Player player = Bukkit.getPlayer(args[1]);
if (player == null) { if (player == null) {
sender.sendMessage(Component.empty().content("That player cannot be found.")); sender.sendMessage(Messages.NO_PLAYER.get());
return true; return true;
} }
@ -105,7 +106,7 @@ public class LuckCMD extends Command implements TabCompleter {
luck.reset(); luck.reset();
plugin.handler.updatePlayer(player, luck); plugin.handler.updatePlayer(player, luck);
config.setLuck(luck.getValue()); config.setLuck(luck.getValue());
sender.sendMessage(Component.empty().content("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
} }
} else { } else {
@ -117,14 +118,14 @@ public class LuckCMD extends Command implements TabCompleter {
if (args.length == 1) { if (args.length == 1) {
if (args[0].equalsIgnoreCase("reload") && sender.hasPermission("luck.admin")) { if (args[0].equalsIgnoreCase("reload") && sender.hasPermission("luck.admin")) {
plugin.getConfigMap().values().forEach(PlayerConfig::reload); plugin.getConfigMap().values().forEach(PlayerConfig::reload);
sender.sendMessage(MiniComponent.info("Player configurations reloaded."));
return true; return true;
} }
if ((sender instanceof Player player) && player.hasPermission("luck.default")) { if ((sender instanceof Player player) && player.hasPermission("luck.default")) {
if (args[0].equalsIgnoreCase("info")) { if (args[0].equalsIgnoreCase("info")) {
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.handler.getLuckContainer(player);
TextComponent c = Component.text("Your Luck: " + luck.getPercentage()); player.sendMessage(MiniComponent.info("Your Luck: " + luck.getPercentage()));
player.sendMessage(c.color(TextColor.color(0, 255, 0)));
return true; return true;
} }
} else if (sender instanceof ConsoleCommandSender) { } else if (sender instanceof ConsoleCommandSender) {