From 0ad60075dac1bd9f746ba7875b40fb540f319541 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Wed, 15 Jun 2022 09:35:35 -0500 Subject: [PATCH] FeelingLucky v1.3.0 This update will entail providing spigot support. This is currently incomplete. --- build.gradle | 10 ++++++ .../io/github/simplex/lib/MiniComponent.java | 8 ++--- .../io/github/simplex/luck/FeelingLucky.java | 31 ++++++++++++++-- .../luck/listener/AbstractListener.java | 1 - .../luck/listener/BonemealFullCrop.java | 3 +- .../simplex/luck/listener/CheatDeath.java | 6 ++-- .../simplex/luck/listener/GiveDamage.java | 1 - .../simplex/luck/listener/HideCheck.java | 11 +++--- .../github/simplex/luck/listener/OreVein.java | 2 +- .../simplex/luck/listener/PlayerListener.java | 3 +- .../player/CancellablePlayerDeathEvent.java | 36 +++++++++++++++++++ 11 files changed, 91 insertions(+), 21 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/player/CancellablePlayerDeathEvent.java diff --git a/build.gradle b/build.gradle index ac5ec39..c1db0fb 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java' + id 'com.github.johnrengelman.shadow' version '7.1.0' } group = 'io.github.simplex' @@ -9,10 +10,19 @@ repositories { mavenCentral() maven { url = uri("https://s01.oss.sonatype.org/content/groups/public/") } maven { url = uri("https://papermc.io/repo/repository/maven-public/")} + maven { url = uri("https://hub.spigotmc.org/nexus/content/repositories/snapshots/")} } dependencies { + compileOnly("org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT") compileOnly("io.papermc.paper:paper-api:1.19-R0.1-SNAPSHOT") + shadow("org.jetbrains:annotations:16.0.2") + shadow("net.kyori:adventure-api:4.11.0") + shadow("io.papermc:paperlib:1.0.7") +} + +shadowJar { + relocate("io.papermc.lib", "io.github.simplex.paperlib") } def targetJavaVersion = 17 diff --git a/src/main/java/io/github/simplex/lib/MiniComponent.java b/src/main/java/io/github/simplex/lib/MiniComponent.java index d5db9d6..fbafac0 100644 --- a/src/main/java/io/github/simplex/lib/MiniComponent.java +++ b/src/main/java/io/github/simplex/lib/MiniComponent.java @@ -21,17 +21,17 @@ public class MiniComponent { } @Contract("_ -> new") - public static Component info(String content) { + public static String info(String content) { return new MiniComponent(content).color(ChatColor.GREEN).send(); } @Contract("_ -> new") - public static Component warn(String content) { + public static String warn(String content) { return new MiniComponent(content).color(ChatColor.YELLOW).decorate(TextDecoration.ITALIC).send(); } @Contract("_ -> new") - public static Component err(String content) { + public static String err(String content) { return new MiniComponent(content).color(ChatColor.RED).decorate(TextDecoration.BOLD).send(); } @@ -45,7 +45,7 @@ public class MiniComponent { return this; } - public Component send() { + public String send() { if (color == null) { if (decoration == null) return Component.empty().content(content); diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index 720642d..306681b 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -7,11 +7,14 @@ import io.github.simplex.luck.util.LuckCMD; import io.github.simplex.luck.util.RegenerateConfigCMD; import io.github.simplex.luck.util.SpecialFootItem; import io.github.simplex.metrics.Metrics; +import io.papermc.lib.PaperLib; import org.bukkit.command.CommandMap; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; +import java.lang.reflect.Field; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -24,11 +27,17 @@ public final class FeelingLucky extends JavaPlugin { private PlayerHandler handler; private Config config; + private CommandMap commandMap = null; public Map getConfigMap() { return configMap; } + @Override + public void onLoad() { + if (!PaperLib.isPaper()) PaperLib.suggestPaper(this); + } + @Override public void onEnable() { getLogger().info("Initializing metrics..."); @@ -41,7 +50,9 @@ 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! Loading commands..."); + getLogger().info("Main Config loaded successfully! Attempting to open the CommandMap..."); + initCommandMap(); + getLogger().info("Command Map successfully initialized! Attempting to register the commands..."); new LuckCMD(this); new RegenerateConfigCMD(this); getLogger().info("Successfully loaded all commands!"); @@ -58,6 +69,16 @@ public final class FeelingLucky extends JavaPlugin { getLogger().info("Complete! Goodbye! :)"); } + private void initCommandMap() { + try { + Field f = getServer().getClass().getDeclaredField("commandMap"); + f.setAccessible(true); + commandMap = (CommandMap) f.get(getServer()); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + } + private void loadPlayerConfigurations() { if (!playerDirectory.exists()) { getLogger().info("No directory exists. Creating..."); @@ -84,7 +105,6 @@ public final class FeelingLucky extends JavaPlugin { new BonemealFullCrop(this); new CheatDeath(this); new EnchantmentBoost(this); - new ExpBoost(this); new GiveDamage(this); new HideCheck(this); new IllOmen(this); @@ -97,6 +117,10 @@ public final class FeelingLucky extends JavaPlugin { new TakeDamage(this); new UnbreakableTool(this); new VillagerInventory(this); + + if (PaperLib.isPaper()) { + new ExpBoost(this); + } } public PlayerHandler getHandler() { @@ -113,7 +137,8 @@ public final class FeelingLucky extends JavaPlugin { return specialFootItem; } + @Nullable public CommandMap getCommandMap() { - return getServer().getCommandMap(); + return commandMap; } } diff --git a/src/main/java/io/github/simplex/luck/listener/AbstractListener.java b/src/main/java/io/github/simplex/luck/listener/AbstractListener.java index 9af3afd..66eb410 100644 --- a/src/main/java/io/github/simplex/luck/listener/AbstractListener.java +++ b/src/main/java/io/github/simplex/luck/listener/AbstractListener.java @@ -1,6 +1,5 @@ package io.github.simplex.luck.listener; -import io.github.simplex.luck.Config; import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.PlayerHandler; import org.bukkit.event.Listener; diff --git a/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java b/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java index 24e4f65..41ec226 100644 --- a/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java +++ b/src/main/java/io/github/simplex/luck/listener/BonemealFullCrop.java @@ -1,7 +1,6 @@ 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; @@ -35,7 +34,7 @@ public final class BonemealFullCrop extends AbstractListener { BlockData data = block.getBlockData(); - if (action.isRightClick() + if ((action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) && handItem.isSimilar(bonemeal) && (data instanceof Ageable crop) && luck.quickRNG(luck.getValue()) diff --git a/src/main/java/io/github/simplex/luck/listener/CheatDeath.java b/src/main/java/io/github/simplex/luck/listener/CheatDeath.java index 29aee16..3e6c76a 100644 --- a/src/main/java/io/github/simplex/luck/listener/CheatDeath.java +++ b/src/main/java/io/github/simplex/luck/listener/CheatDeath.java @@ -2,8 +2,10 @@ package io.github.simplex.luck.listener; import io.github.simplex.lib.MiniComponent; import io.github.simplex.luck.FeelingLucky; +import io.github.simplex.luck.player.CancellablePlayerDeathEvent; import io.github.simplex.luck.player.Luck; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.PlayerDeathEvent; @@ -14,8 +16,8 @@ public final class CheatDeath extends AbstractListener { } @EventHandler - public void cheatDeath(PlayerDeathEvent event) { - Player player = event.getPlayer(); + public void cheatDeath(CancellablePlayerDeathEvent event) { + Player player = event.getEntity(); Luck luck = getHandler().getLuckContainer(player); double absorption = Math.round(Luck.RNG().nextDouble(5.0, 10.0)); if (luck.quickRNG(luck.getValue()) && doesQualify("cheat_death", luck.getValue())) { diff --git a/src/main/java/io/github/simplex/luck/listener/GiveDamage.java b/src/main/java/io/github/simplex/luck/listener/GiveDamage.java index 46be18a..d29f8f4 100644 --- a/src/main/java/io/github/simplex/luck/listener/GiveDamage.java +++ b/src/main/java/io/github/simplex/luck/listener/GiveDamage.java @@ -1,6 +1,5 @@ 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.LivingEntity; diff --git a/src/main/java/io/github/simplex/luck/listener/HideCheck.java b/src/main/java/io/github/simplex/luck/listener/HideCheck.java index fbbe34e..f92dbe1 100644 --- a/src/main/java/io/github/simplex/luck/listener/HideCheck.java +++ b/src/main/java/io/github/simplex/luck/listener/HideCheck.java @@ -5,6 +5,7 @@ import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Mob; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.entity.EntityTargetLivingEntityEvent; @@ -15,7 +16,7 @@ import org.bukkit.event.player.PlayerToggleSneakEvent; import java.util.*; public class HideCheck extends AbstractListener { - public Map> entityMapList = new HashMap<>(); + public Map> entityMapList = new HashMap<>(); public HideCheck(FeelingLucky plugin) { super(plugin); @@ -30,8 +31,8 @@ public class HideCheck extends AbstractListener { @EventHandler public void checkTargeting(EntityTargetLivingEntityEvent event) { if (event.getTarget() instanceof Player player) { - if (event.getEntity() instanceof LivingEntity entity) { - List buffer = entityMapList.get(player).isEmpty() ? + if (event.getEntity() instanceof Mob entity) { + List buffer = entityMapList.get(player).isEmpty() ? new ArrayList<>() : entityMapList.get(player); buffer.add(entity); entityMapList.replace(player, buffer); @@ -46,9 +47,7 @@ public class HideCheck extends AbstractListener { Luck luck = plugin.getHandler().getLuckContainer(player); if (luck.quickRNG(luck.getValue()) && doesQualify("hide_check", luck.getValue())) { - entityMapList.get(player).forEach(e -> { - e.getTrackedPlayers().remove(player); - }); + entityMapList.get(player).forEach(e -> e.getTarget().remove()); player.sendMessage(MiniComponent.info("Your luck has hidden you from sight.")); } } diff --git a/src/main/java/io/github/simplex/luck/listener/OreVein.java b/src/main/java/io/github/simplex/luck/listener/OreVein.java index 5c31544..ad916a4 100644 --- a/src/main/java/io/github/simplex/luck/listener/OreVein.java +++ b/src/main/java/io/github/simplex/luck/listener/OreVein.java @@ -25,7 +25,7 @@ public class OreVein extends AbstractListener { public void playerMine(BlockBreakEvent event) { Player player = event.getPlayer(); Luck luck = plugin.getHandler().getLuckContainer(player); - if (luck.quickRNG(luck.getValue()) && doesQualify("ore_vein", luck.getValue()) && event.getBlock().isValidTool(player.getInventory().getItemInMainHand())) { + if (luck.quickRNG(luck.getValue()) && doesQualify("ore_vein", luck.getValue()) && event.getBlock().isPreferredTool(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.")); } diff --git a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java index c1ad067..37652aa 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -42,7 +42,8 @@ public final class PlayerListener extends AbstractListener { return; } - if (action.isRightClick() && player.getInventory().getItemInMainHand().getType().equals(foot.getType())) { + if ((action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) + && player.getInventory().getItemInMainHand().getType().equals(foot.getType())) { if (foot.getItemMeta().equals(special.meta()) || foot.equals(special.get())) { luck.setMultiplier(luck.multiplier() + 0.1); player.sendMessage(MiniComponent.info("Your luck multiplier has increased by 0.1!")); diff --git a/src/main/java/io/github/simplex/luck/player/CancellablePlayerDeathEvent.java b/src/main/java/io/github/simplex/luck/player/CancellablePlayerDeathEvent.java new file mode 100644 index 0000000..a493610 --- /dev/null +++ b/src/main/java/io/github/simplex/luck/player/CancellablePlayerDeathEvent.java @@ -0,0 +1,36 @@ +package io.github.simplex.luck.player; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class CancellablePlayerDeathEvent extends PlayerDeathEvent implements Cancellable { + private boolean cancelled = false; + + public CancellablePlayerDeathEvent(@NotNull Player player, @NotNull List drops, int droppedExp, @Nullable String deathMessage) { + super(player, drops, droppedExp, deathMessage); + } + + public CancellablePlayerDeathEvent(@NotNull Player player, @NotNull List drops, int droppedExp, int newExp, @Nullable String deathMessage) { + super(player, drops, droppedExp, newExp, deathMessage); + } + + public CancellablePlayerDeathEvent(@NotNull Player player, @NotNull List drops, int droppedExp, int newExp, int newTotalExp, int newLevel, @Nullable String deathMessage) { + super(player, drops, droppedExp, newExp, newTotalExp, newLevel, deathMessage); + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + cancelled = cancel; + } +}