From ce7982c8cf4d9d36e692f7d3816ae02470b17493 Mon Sep 17 00:00:00 2001 From: nik2143 <44278678+nik2143@users.noreply.github.com> Date: Sat, 17 Apr 2021 03:04:32 +0200 Subject: [PATCH] Changed Spigot Api version to prevent a dupe exploit in versions higher than 1.9, Added a way to apply effects only if better than the effect that has the player --- pom.xml | 6 +-- .../nik2143/customgapple/Configuration.java | 19 +++++++ .../nik2143/customgapple/CustomGapple.java | 13 ++++- .../commands/CustomGappleCommand.java | 14 ++--- .../listeners/EatGappleListener.java | 51 ++++++++++++++++--- src/main/resources/config.yml | 4 +- src/main/resources/plugin.yml | 1 + 7 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 src/main/java/io/github/nik2143/customgapple/Configuration.java diff --git a/pom.xml b/pom.xml index 6422fef..306a9ad 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.nik2143 CustomGapple - 1.2.1 + 1.2.2 jar CustomGapple @@ -90,13 +90,13 @@ org.spigotmc spigot-api - 1.8.8-R0.1-SNAPSHOT + 1.16.5-R0.1-SNAPSHOT provided de.tr7zw item-nbt-api - 2.5.0 + 2.7.1 diff --git a/src/main/java/io/github/nik2143/customgapple/Configuration.java b/src/main/java/io/github/nik2143/customgapple/Configuration.java new file mode 100644 index 0000000..20ba8f0 --- /dev/null +++ b/src/main/java/io/github/nik2143/customgapple/Configuration.java @@ -0,0 +1,19 @@ +package io.github.nik2143.customgapple; + +public class Configuration { + + public int defaultAmount; + public int defaultDuration; + public boolean applyonlybetter; + + public Configuration(){ + loadConfig(); + } + + public void loadConfig(){ + defaultAmount = CustomGapple.getCustomGapple().getConfig().getInt("Default-Amount"); + defaultDuration = CustomGapple.getCustomGapple().getConfig().getInt("Default-Duration"); + applyonlybetter = CustomGapple.getCustomGapple().getConfig().getBoolean("apply-only-better",false); + } + +} diff --git a/src/main/java/io/github/nik2143/customgapple/CustomGapple.java b/src/main/java/io/github/nik2143/customgapple/CustomGapple.java index 29dbe7b..a6758c5 100644 --- a/src/main/java/io/github/nik2143/customgapple/CustomGapple.java +++ b/src/main/java/io/github/nik2143/customgapple/CustomGapple.java @@ -7,11 +7,20 @@ public final class CustomGapple extends JavaPlugin { + private Configuration configuration; + private static CustomGapple customGapple; + @Override public void onEnable() { saveDefaultConfig(); - this.getCommand("customgapple").setExecutor(new CustomGappleCommand(this)); - this.getCommand("customgapple").setTabCompleter(new CustomGappleCommand(this)); + customGapple = this; + configuration = new Configuration(); + getCommand("customgapple").setExecutor(new CustomGappleCommand()); + getCommand("customgapple").setTabCompleter(new CustomGappleCommand()); Bukkit.getPluginManager().registerEvents(new EatGappleListener(),this); } + + public Configuration getConfiguration() { return configuration; } + + public static CustomGapple getCustomGapple() { return customGapple; } } diff --git a/src/main/java/io/github/nik2143/customgapple/commands/CustomGappleCommand.java b/src/main/java/io/github/nik2143/customgapple/commands/CustomGappleCommand.java index 9ee0bd6..fd09e37 100644 --- a/src/main/java/io/github/nik2143/customgapple/commands/CustomGappleCommand.java +++ b/src/main/java/io/github/nik2143/customgapple/commands/CustomGappleCommand.java @@ -21,16 +21,10 @@ public class CustomGappleCommand implements CommandExecutor, TabCompleter { - private final CustomGapple plugin; - - public CustomGappleCommand (CustomGapple plugin){ - this.plugin = plugin; - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (command.getName().equalsIgnoreCase("customgapple")) { - Player player = null; + Player player; if (!(sender instanceof Player)) { if (args.length == 0 || args.length == 1 || args.length == 2){ sender.sendMessage("Wrong sintax. Use /customgapple [Levels] [Durations] [Amount] [Enchanted (true/false)]"); @@ -40,7 +34,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St player = Bukkit.getPlayerExact(args[0]); ArrayList argslist = new ArrayList<>(Arrays.asList(args)); argslist.remove(0); - args = argslist.toArray(new String[argslist.size()]); + args = argslist.toArray(new String[0]); } else { sender.sendMessage("Player "+ args[0] +" isn't online"); return true; @@ -102,7 +96,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } }else { for (int i = 0; i < effectsnumber; i++){ - durations.add(plugin.getConfig().getInt("Default-Duration")); + durations.add(CustomGapple.getCustomGapple().getConfiguration().defaultDuration); } } if (args.length >= effectsnumber * 3 + 3){ @@ -115,7 +109,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, Integer.parseInt(args[effectsnumber * 3 + 1 ]),enchantedGapple)); } else { - player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, plugin.getConfig().getInt("Default-Amount"),enchantedGapple)); + player.getInventory().addItem(CreateGapple(effectsnumber, effects, levels, durations, CustomGapple.getCustomGapple().getConfiguration().defaultAmount,enchantedGapple)); } return true; } diff --git a/src/main/java/io/github/nik2143/customgapple/listeners/EatGappleListener.java b/src/main/java/io/github/nik2143/customgapple/listeners/EatGappleListener.java index ea62703..b778258 100644 --- a/src/main/java/io/github/nik2143/customgapple/listeners/EatGappleListener.java +++ b/src/main/java/io/github/nik2143/customgapple/listeners/EatGappleListener.java @@ -1,8 +1,10 @@ package io.github.nik2143.customgapple.listeners; import de.tr7zw.changeme.nbtapi.NBTItem; +import io.github.nik2143.customgapple.CustomGapple; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerItemConsumeEvent; @@ -10,24 +12,61 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + public class EatGappleListener implements Listener { @EventHandler private void GappleEatEvent(PlayerItemConsumeEvent e){ if (e.getItem().getType().equals(Material.GOLDEN_APPLE)){ ItemStack item = e.getItem(); + ItemStack originalItem = e.getItem(); NBTItem nbti = new NBTItem(item); if (nbti.getCompound("GappleEffects") != null){ e.setCancelled(true); - for(String key : nbti.getCompound("GappleEffects").getKeys()){ - e.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.getByName(nbti.getCompound("GappleEffects").getCompound(key).getString("Effect")), - nbti.getCompound("GappleEffects").getCompound(key).getInteger("Duration"), - nbti.getCompound("GappleEffects").getCompound(key).getInteger("Level")),true); - } + applyEffects(nbti,e.getPlayer()); item.setAmount(item.getAmount() - 1); e.getPlayer().setFoodLevel(e.getPlayer().getFoodLevel()+4); e.getPlayer().setSaturation((float) (e.getPlayer().getSaturation()+1.2)); - e.getPlayer().getInventory().setItemInHand(item); + + if(item.getAmount() <= 0) item = null; + + try { + ItemStack mainHand = e.getPlayer().getInventory().getItemInMainHand(); + ItemStack offHand = e.getPlayer().getInventory().getItemInOffHand(); + + if(mainHand.equals(originalItem)) e.getPlayer().getInventory().setItemInMainHand(item); + else if(offHand.equals(originalItem)) e.getPlayer().getInventory().setItemInOffHand(item); + else if(mainHand.getType() == Material.GOLDEN_APPLE) + e.getPlayer().getInventory().setItemInMainHand(item); + } catch (NoSuchMethodError error){ + e.getPlayer().getInventory().setItemInHand(item); + } + } + } + } + + private void applyEffects(NBTItem nbti, Player p){ + Collection activeEffects = p.getActivePotionEffects(); + for(String key : nbti.getCompound("GappleEffects").getKeys()){ + boolean haseffect = false; + PotionEffect pe = new PotionEffect(Objects.requireNonNull(PotionEffectType.getByName(nbti.getCompound("GappleEffects").getCompound(key).getString("Effect"))), + nbti.getCompound("GappleEffects").getCompound(key).getInteger("Duration"), + nbti.getCompound("GappleEffects").getCompound(key).getInteger("Level")); + if (CustomGapple.getCustomGapple().getConfiguration().applyonlybetter){ + for (PotionEffect effect : activeEffects){ + if (effect.getType().equals(pe.getType())){ + haseffect = true; + if (effect.getAmplifier()