Beta 20220416-SNAPSHOT

Changelog:
 - Changed the way classes interact with the PlayerHandler by using a getter instead of the actual field.
 - Added #isTool(Material) to ItemBuilder to check if an item is, in fact, a type of tool.
 - Created the UnbreakableTool listener which will grant the unbreakable status to a tool if a user is lucky enough.
  NOTE: This is a beta feature, and while it may remain included the way it works will most likely change.
This commit is contained in:
Paldiu 2022-04-16 17:27:54 -05:00
parent 077ed05d74
commit b424a83062
15 changed files with 91 additions and 36 deletions

View File

@ -26,6 +26,19 @@ public final class ItemBuilder {
return new ItemBuilder(material); return new ItemBuilder(material);
} }
@Contract(pure = true)
public static boolean isTool(Material m) {
String name = m.getKey().getKey();
return m.equals(Material.SHEARS)
|| name.endsWith("bow")
|| name.endsWith("pickaxe")
|| name.endsWith("axe")
|| name.endsWith("sword")
|| name.endsWith("shovel")
|| name.endsWith("hoe");
}
public ItemBuilder setName(String name) { public ItemBuilder setName(String name) {
meta.displayName(MiniComponent.of(name).send()); meta.displayName(MiniComponent.of(name).send());
return this; return this;

View File

@ -11,6 +11,10 @@ public class MiniComponent {
private TextDecoration decoration = null; private TextDecoration decoration = null;
private TextColor color = null; private TextColor color = null;
public MiniComponent(String content) {
this.content = content;
}
@Contract("_ -> new") @Contract("_ -> new")
public static MiniComponent of(String content) { public static MiniComponent of(String content) {
return new MiniComponent(content); return new MiniComponent(content);
@ -31,10 +35,6 @@ public class MiniComponent {
return new MiniComponent(content).color(ChatColor.RED).decorate(TextDecoration.BOLD).send(); return new MiniComponent(content).color(ChatColor.RED).decorate(TextDecoration.BOLD).send();
} }
public MiniComponent(String content) {
this.content = content;
}
public MiniComponent decorate(TextDecoration decoration) { public MiniComponent decorate(TextDecoration decoration) {
this.decoration = decoration; this.decoration = decoration;
return this; return this;

View File

@ -16,8 +16,7 @@ import java.util.UUID;
public final class FeelingLucky extends JavaPlugin { public final class FeelingLucky extends JavaPlugin {
private final Map<UUID, PlayerConfig> configMap = new HashMap<>(); private final Map<UUID, PlayerConfig> configMap = new HashMap<>();
public LuckCMD cmd; private PlayerHandler handler;
public PlayerHandler handler;
public Map<UUID, PlayerConfig> getConfigMap() { public Map<UUID, PlayerConfig> getConfigMap() {
return configMap; return configMap;
@ -32,7 +31,7 @@ public final class FeelingLucky extends JavaPlugin {
getLogger().info("Registration complete! Attempting to load all player configuration files..."); getLogger().info("Registration complete! Attempting to load all player configuration files...");
loadConfigurations(); loadConfigurations();
Bukkit.getLogger().info("Attempting to load the Luck command..."); Bukkit.getLogger().info("Attempting to load the Luck command...");
cmd = new LuckCMD(this); new LuckCMD(this);
Bukkit.getLogger().info("Successfully loaded the Luck command!"); Bukkit.getLogger().info("Successfully loaded the Luck command!");
Bukkit.getLogger().info("Successfully initialized!"); Bukkit.getLogger().info("Successfully initialized!");
@ -68,5 +67,10 @@ public final class FeelingLucky extends JavaPlugin {
new ExpBoost(this); new ExpBoost(this);
new CheatDeath(this); new CheatDeath(this);
new BonemealFullCrop(this); new BonemealFullCrop(this);
new UnbreakableTool(this);
}
public PlayerHandler getHandler() {
return handler;
} }
} }

View File

@ -19,7 +19,7 @@ public record BlockDrops(FeelingLucky plugin) implements Listener {
@EventHandler @EventHandler
public void extraBlockDrops(BlockDropItemEvent event) { public void extraBlockDrops(BlockDropItemEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
List<Item> items = event.getItems(); List<Item> items = event.getItems();
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getPercentage())) {
items.forEach(SneakyWorker::move); items.forEach(SneakyWorker::move);

View File

@ -6,7 +6,6 @@ import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.Ageable; import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -26,7 +25,7 @@ public record BonemealFullCrop(FeelingLucky plugin) implements Listener {
Player player = event.getPlayer(); Player player = event.getPlayer();
Action action = event.getAction(); Action action = event.getAction();
ItemStack bonemeal = ItemBuilder.of(Material.BONE_MEAL).build(); ItemStack bonemeal = ItemBuilder.of(Material.BONE_MEAL).build();
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
ItemStack handItem = event.getItem(); ItemStack handItem = event.getItem();
if (handItem == null) return; if (handItem == null) return;

View File

@ -17,7 +17,7 @@ public record CheatDeath(FeelingLucky plugin) implements Listener {
@EventHandler @EventHandler
public void cheatDeath(PlayerDeathEvent event) { public void cheatDeath(PlayerDeathEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0)); double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0));
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getPercentage())) {
event.setCancelled(true); event.setCancelled(true);

View File

@ -22,7 +22,7 @@ public record EnchantmentBoost(FeelingLucky plugin) implements Listener {
Map<Enchantment, Integer> enchMap = event.getEnchantsToAdd(); Map<Enchantment, Integer> enchMap = event.getEnchantsToAdd();
List<Enchantment> enchList = enchMap.keySet().stream().toList(); List<Enchantment> enchList = enchMap.keySet().stream().toList();
Player player = event.getEnchanter(); Player player = event.getEnchanter();
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getPercentage())) {
Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size())); Enchantment particular = enchList.get(Luck.RNG().nextInt(enchList.size()));
int rng = Luck.RNG().nextInt(1, 5); int rng = Luck.RNG().nextInt(1, 5);

View File

@ -21,7 +21,7 @@ public record ExpBoost(FeelingLucky plugin) implements Listener {
int math = (5 * n ^ 2) / (2 * n + 4); int math = (5 * n ^ 2) / (2 * n + 4);
int rounded = Math.round(math); int rounded = Math.round(math);
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage())) { if (luck.quickRNG(luck.getPercentage())) {
orb.setExperience(rounded); orb.setExperience(rounded);
} }

View File

@ -2,7 +2,6 @@ package io.github.simplex.luck.listener;
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 org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -59,7 +58,7 @@ public class ItemDrops implements Listener {
if (entityPlayerMap.get(entity.getUniqueId()) == null) return; if (entityPlayerMap.get(entity.getUniqueId()) == null) return;
Player player = entityPlayerMap.get(entity.getUniqueId()); Player player = entityPlayerMap.get(entity.getUniqueId());
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
Item item = event.getItemDrop(); Item item = event.getItemDrop();
ItemStack stack = item.getItemStack(); ItemStack stack = item.getItemStack();
int amount = stack.getAmount(); int amount = stack.getAmount();

View File

@ -29,7 +29,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
ItemStack foot = new ItemStack(Material.RABBIT_FOOT); ItemStack foot = new ItemStack(Material.RABBIT_FOOT);
SpecialFootItem special = new SpecialFootItem(); SpecialFootItem special = new SpecialFootItem();
Player player = event.getPlayer(); Player player = event.getPlayer();
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) { if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) {
if (foot.getItemMeta().equals(special.meta()) || foot.equals(special.get())) { if (foot.getItemMeta().equals(special.meta()) || foot.equals(special.get())) {
luck.setMultiplier(luck.multiplier() + 1); luck.setMultiplier(luck.multiplier() + 1);
@ -37,7 +37,7 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
double rng = Luck.RNG().nextDouble(2.0, 5.0); double rng = Luck.RNG().nextDouble(2.0, 5.0);
player.getInventory().remove(player.getInventory().getItemInMainHand()); player.getInventory().remove(player.getInventory().getItemInMainHand());
luck.addTo(rng); luck.addTo(rng);
plugin.handler.updatePlayer(player, luck); plugin.getHandler().updatePlayer(player, luck);
player.sendMessage(Component.empty().content("Your luck has been increased by " + rng + " points.")); player.sendMessage(Component.empty().content("Your luck has been increased by " + rng + " points."));
} }
} }
@ -56,11 +56,11 @@ public record PlayerListener(FeelingLucky plugin) implements Listener {
return; return;
} }
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
if (cause.equals(EntityDamageEvent.DamageCause.MAGIC) || cause.equals(EntityDamageEvent.DamageCause.POISON)) { if (cause.equals(EntityDamageEvent.DamageCause.MAGIC) || cause.equals(EntityDamageEvent.DamageCause.POISON)) {
if (luck.quickRNG(33.0)) { if (luck.quickRNG(33.0)) {
luck.takeFrom(5.0); luck.takeFrom(5.0);
plugin.handler.updatePlayer(player, luck); plugin.getHandler().updatePlayer(player, luck);
} }
} }
} }

View File

@ -2,8 +2,8 @@ package io.github.simplex.luck.listener;
import io.github.simplex.lib.PotionEffectBuilder; import io.github.simplex.lib.PotionEffectBuilder;
import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.util.ListBox;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.ListBox;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerItemConsumeEvent;
@ -19,7 +19,7 @@ public record RestoreHunger(FeelingLucky plugin) implements Listener {
@EventHandler @EventHandler
public void restoreHunger(PlayerItemConsumeEvent event) { public void restoreHunger(PlayerItemConsumeEvent event) {
ItemStack item = event.getItem(); ItemStack item = event.getItem();
Luck luck = plugin.handler.getLuckContainer(event.getPlayer()); Luck luck = plugin.getHandler().getLuckContainer(event.getPlayer());
PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create(); PotionEffect effect = PotionEffectBuilder.newEffect().type(PotionEffectType.SATURATION).amplifier(2).duration(10).particles(false).create();
if (luck.notDefault()) { if (luck.notDefault()) {
double percentage = luck.getPercentage(); double percentage = luck.getPercentage();

View File

@ -2,8 +2,8 @@ package io.github.simplex.luck.listener;
import io.github.simplex.lib.PotionEffectBuilder; import io.github.simplex.lib.PotionEffectBuilder;
import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.FeelingLucky;
import io.github.simplex.luck.util.ListBox;
import io.github.simplex.luck.player.Luck; import io.github.simplex.luck.player.Luck;
import io.github.simplex.luck.util.ListBox;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -23,7 +23,7 @@ public record TakeDamage(FeelingLucky plugin) implements Listener {
return; return;
} }
Player player = (Player) event.getEntity(); Player player = (Player) event.getEntity();
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
if (ListBox.acceptedCauses.contains(event.getCause())) { if (ListBox.acceptedCauses.contains(event.getCause())) {
if (luck.notDefault()) { if (luck.notDefault()) {
double percentage = luck.getPercentage(); double percentage = luck.getPercentage();

View File

@ -0,0 +1,40 @@
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.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public record UnbreakableTool(FeelingLucky plugin) implements Listener {
public UnbreakableTool {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void unbreakableTool(CraftItemEvent event) {
CraftingInventory inventory = event.getInventory();
ItemStack stack = inventory.getResult();
if (stack == null) return;
ItemMeta meta = stack.getItemMeta();
if (ItemBuilder.isTool(stack.getType())) {
if (event.getWhoClicked() instanceof Player player) {
Luck luck = plugin.getHandler().getLuckContainer(player);
if (luck.quickRNG(luck.getPercentage())) {
meta.setUnbreakable(true);
stack.setItemMeta(meta);
inventory.setResult(stack);
player.sendMessage(MiniComponent.info("By the grace of Luck you have crafted an unbreakable tool!"));
}
}
}
}
}

View File

@ -47,7 +47,7 @@ public class PlayerConfig {
if (tempUsername == null) { if (tempUsername == null) {
config.set("username", player.getName()); config.set("username", player.getName());
config.set("luck", plugin.handler.getLuckContainer(player).getDefaultValue()); config.set("luck", plugin.getHandler().getLuckContainer(player).getDefaultValue());
config.set("multiplier", "1.0"); config.set("multiplier", "1.0");
save(); save();
} }

View File

@ -5,11 +5,11 @@ 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;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.*; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -46,27 +46,27 @@ public class LuckCMD extends Command implements TabCompleter {
return true; return true;
} }
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId()); PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
switch (args[0]) { switch (args[0]) {
case "set" -> { case "set" -> {
luck.setValue(amount); luck.setValue(amount);
plugin.handler.updatePlayer(player, luck); plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue()); config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("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.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue()); config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("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.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue()); config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
@ -88,7 +88,7 @@ public class LuckCMD extends Command implements TabCompleter {
return true; return true;
} }
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
sender.sendMessage(MiniComponent.info("Luck stat for " + args[1] + ": " + luck.getValue())); sender.sendMessage(MiniComponent.info("Luck stat for " + args[1] + ": " + luck.getValue()));
return true; return true;
} }
@ -101,10 +101,10 @@ public class LuckCMD extends Command implements TabCompleter {
return true; return true;
} }
Luck luck = plugin.handler.getLuckContainer(player); Luck luck = plugin.getHandler().getLuckContainer(player);
PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId()); PlayerConfig config = plugin.getConfigMap().get(player.getUniqueId());
luck.reset(); luck.reset();
plugin.handler.updatePlayer(player, luck); plugin.getHandler().updatePlayer(player, luck);
config.setLuck(luck.getValue()); config.setLuck(luck.getValue());
sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat.")); sender.sendMessage(MiniComponent.info("Successfully reset " + args[1] + "'s Luck stat."));
return true; return true;
@ -124,7 +124,7 @@ public class LuckCMD extends Command implements TabCompleter {
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.getHandler().getLuckContainer(player);
player.sendMessage(MiniComponent.info("Your Luck: " + luck.getPercentage())); player.sendMessage(MiniComponent.info("Your Luck: " + luck.getPercentage()));
return true; return true;
} }