From 76bd849b58bee3ba75abc7740cd7ea40b6e4f756 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Wed, 7 Sep 2022 18:26:59 -0500 Subject: [PATCH 01/20] SaberFactions integration Added a SaberFactions integration --- build.gradle.kts | 19 +++++++++++---- .../pickmeup/services/ProtectionService.java | 16 +++++++------ .../hooks/protection/SaberFactionsHook.java | 23 +++++++++++++++++++ 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 src/main/java/de/eldoria/pickmeup/services/hooks/protection/SaberFactionsHook.java diff --git a/build.gradle.kts b/build.gradle.kts index af35972..226408f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,6 +8,10 @@ plugins { repositories { mavenCentral() + jcenter() + //maven(file("${projectDir}/dependencies/fanciful-0.4.0-SNAPSHOT.jar")) + maven ("https://rayzr.dev/repo/") + maven("https://jitpack.io") maven("https://eldonexus.de/repository/maven-public") maven("https://eldonexus.de/repository/maven-proxies") maven("https://raw.githubusercontent.com/FabioZumbi12/RedProtect/mvn-repo/") @@ -15,9 +19,16 @@ repositories { dependencies { implementation("de.eldoria", "eldo-util", "1.13.9") + //implementation(file("${projectDir}/dependencies/fanciful-0.4.0-SNAPSHOT.jar")) + //compileOnly("mkremins", "fanciful", "0.4.0-SNAPSHOT") + compileOnly("com.jagrosh", "jda-utilities", "2.1") + compileOnly("net.dv8tion", "JDA", "5.0.0-alpha.18") + compileOnly("me.rayzr522", "jsonmessage", "1.3.0") + compileOnly("org.spigotmc", "spigot-api", "1.16.5-R0.1-SNAPSHOT") compileOnly("com.mojang", "authlib", "1.5.25") compileOnly("org.jetbrains", "annotations", "16.0.2") + compileOnly("com.github.SaberLLC", "Saber-Factions", "2.4.0-RC") compileOnly("world.bentobox", "bentobox", "1.16.2-SNAPSHOT") compileOnly("com.github.TechFortress", "GriefPrevention", "16.17.1") compileOnly("com.github.TownyAdvanced", "Towny", "0.97.1.0") @@ -46,7 +57,7 @@ dependencies { } group = "de.eldoria" -version = "1.3.7" +version = "1.3.8" var mainPackage = "pickmeup" val shadebase = group as String? + "." + mainPackage + "." @@ -111,7 +122,7 @@ tasks { } } - register("copyToServer") { + /*register("copyToServer") { val path = project.property("targetDir") ?: ""; if (path.toString().isEmpty()) { println("targetDir is not set in gradle properties") @@ -119,7 +130,7 @@ tasks { } from(shadowJar) destinationDir = File(path.toString()) - } + }*/ build { dependsOn(shadowJar) @@ -131,7 +142,7 @@ bukkit { main = "de.eldoria.pickmeup.PickMeUp" website = "https://www.spigotmc.org/resources/88151/" apiVersion = "1.13" - softDepend = listOf("BentoBox", "RedProtect", "GriefPrevention", "PlotSquared", "Towny") + softDepend = listOf("BentoBox", "RedProtect", "GriefPrevention", "PlotSquared", "Towny", "Factions") commands { register("pickmeup") { description = "Main command of pick me up" diff --git a/src/main/java/de/eldoria/pickmeup/services/ProtectionService.java b/src/main/java/de/eldoria/pickmeup/services/ProtectionService.java index 9997909..914d98b 100644 --- a/src/main/java/de/eldoria/pickmeup/services/ProtectionService.java +++ b/src/main/java/de/eldoria/pickmeup/services/ProtectionService.java @@ -1,11 +1,6 @@ package de.eldoria.pickmeup.services; -import de.eldoria.pickmeup.services.hooks.protection.BentoBoxHook; -import de.eldoria.pickmeup.services.hooks.protection.GriefPreventionHook; -import de.eldoria.pickmeup.services.hooks.protection.IProtectionHook; -import de.eldoria.pickmeup.services.hooks.protection.PlotSquaredHook; -import de.eldoria.pickmeup.services.hooks.protection.RedProtectHook; -import de.eldoria.pickmeup.services.hooks.protection.TownyHook; +import de.eldoria.pickmeup.services.hooks.protection.*; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -35,6 +30,7 @@ private void init() { Arrays.stream(hooks()) .filter(hook -> { if (pm.isPluginEnabled(hook.pluginName())) { + plugin.getLogger().info(hook.pluginName() + " found! Enabling protection hook."); return true; } plugin.getLogger().info(hook.pluginName() + " not found. Skipping protection hook."); @@ -55,6 +51,12 @@ public boolean canInteract(Player player, Location location) { } private static IProtectionHook[] hooks() { - return new IProtectionHook[]{new BentoBoxHook(), new GriefPreventionHook(), new PlotSquaredHook(), new TownyHook(), new RedProtectHook()}; + return new IProtectionHook[]{ + new BentoBoxHook(), + new GriefPreventionHook(), + new SaberFactionsHook(), + new PlotSquaredHook(), + new TownyHook(), + new RedProtectHook()}; } } diff --git a/src/main/java/de/eldoria/pickmeup/services/hooks/protection/SaberFactionsHook.java b/src/main/java/de/eldoria/pickmeup/services/hooks/protection/SaberFactionsHook.java new file mode 100644 index 0000000..f8aedae --- /dev/null +++ b/src/main/java/de/eldoria/pickmeup/services/hooks/protection/SaberFactionsHook.java @@ -0,0 +1,23 @@ +package de.eldoria.pickmeup.services.hooks.protection; + +import com.massivecraft.factions.listeners.FactionsPlayerListener; +import com.massivecraft.factions.zcore.fperms.PermissableAction; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +public class SaberFactionsHook extends AProtectionHook { + public SaberFactionsHook() { + super("Factions"); + } + + @Override + public void init(Plugin plugin) { + } + + @Override + public boolean canInteract(Player player, Location location) { + return FactionsPlayerListener.playerCanUseItemHere(player, location, Material.BUCKET, false, PermissableAction.ITEM); + } +} From e6f94a51fcd83fc01ac062ff1bd52035e0f446a1 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Wed, 7 Sep 2022 23:45:05 -0500 Subject: [PATCH 02/20] Punching also allows to Carry mobs --- .../pickmeup/listener/CarryListener.java | 59 ++++++++++++------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 02b6d21..23393db 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -1,6 +1,5 @@ package de.eldoria.pickmeup.listener; -import de.eldoria.eldoutilities.core.EldoUtilities; import de.eldoria.eldoutilities.messages.MessageSender; import de.eldoria.eldoutilities.scheduling.DelayedActions; import de.eldoria.pickmeup.PickMeUp; @@ -9,13 +8,16 @@ import de.eldoria.pickmeup.scheduler.TrailHandler; import de.eldoria.pickmeup.services.ProtectionService; import de.eldoria.pickmeup.util.Permissions; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractAtEntityEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; import org.bukkit.plugin.Plugin; @@ -50,34 +52,47 @@ public CarryListener(Plugin plugin, Configuration config, ProtectionService prot @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onEntityInteract(PlayerInteractAtEntityEvent event) { - if (!config.worldSettings().allowInWorld(event.getPlayer().getWorld())) return; + entityCarryInteraction(event.getPlayer(), event.getRightClicked(), event); + } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onEntityInteract(EntityDamageByEntityEvent event) { + if(event.getDamager() instanceof Player player) + entityCarryInteraction(player, event.getEntity(), event); + } + + void entityCarryInteraction(Player player, Entity entity, Cancellable cancellable){ + if (!config.worldSettings().allowInWorld(player.getWorld())) return; - if (!protectionService.canInteract(event.getPlayer(), event.getRightClicked().getLocation())) return; + if (!protectionService.canInteract(player, entity.getLocation())) return; - if (blocked.contains(event.getRightClicked().getUniqueId())) return; + if (blocked.contains(entity.getUniqueId())) return; - MountState mountState = mountStates.get(event.getPlayer().getUniqueId()); + MountState mountState = mountStates.get(player.getUniqueId()); if (mountState == MountState.SNEAK_THROW) { - if (event.getPlayer().getPassengers().contains(event.getRightClicked()) - && event.getPlayer().getEquipment().getItemInMainHand().getType() == Material.AIR) { - unmountAll(event.getPlayer()); - throwBarHandler.getAndRemove(event.getPlayer()); - mountStates.remove(event.getPlayer().getUniqueId()); - blocked.add(event.getRightClicked().getUniqueId()); - delayedActions.schedule(() -> blocked.remove(event.getRightClicked().getUniqueId()), 20); - event.setCancelled(true); + if (player.getPassengers().contains(entity) + && player.getEquipment().getItemInMainHand().getType() == Material.AIR) { + + unmountAll(player); + + throwBarHandler.getAndRemove(player); + + mountStates.remove(player.getUniqueId()); + + blocked.add(entity.getUniqueId()); + + delayedActions.schedule(() -> blocked.remove(entity.getUniqueId()), 20); + + cancellable.setCancelled(true); return; } } - - Player player = event.getPlayer(); if (player.getEquipment().getItemInMainHand().getType() != Material.AIR) return; - if (!config.mobSettings().canBePickedUp(event.getPlayer(), event.getRightClicked().getType())) return; + if (!config.mobSettings().canBePickedUp(player, entity.getType())) return; if (!player.getPassengers().isEmpty()) return; if (!player.isSneaking()) return; - if (!event.getRightClicked().getPassengers().isEmpty() && !config.carrySettings().isAllowStacking()) { + if (!entity.getPassengers().isEmpty() && !config.carrySettings().isAllowStacking()) { if (!player.hasPermission(Permissions.BYPASS_NOSTACK)) { messageSender.sendLocalizedError(player, "nostack"); return; @@ -85,8 +100,8 @@ public void onEntityInteract(PlayerInteractAtEntityEvent event) { } mountStates.put(player.getUniqueId(), MountState.SNEAK_MOUNT); - player.addPassenger(event.getRightClicked()); - event.setCancelled(true); + player.addPassenger(entity); + cancellable.setCancelled(true); } @EventHandler @@ -106,12 +121,12 @@ public void onSneak(PlayerToggleSneakEvent event) { if (event.isSneaking() && mountState == MountState.WALKING) { mountStates.put(player.getUniqueId(), MountState.SNEAK_THROW); - delayedActions.schedule(() -> { + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, + () -> { if (player.isSneaking()) { throwBarHandler.register(player); } - } - , 10); + }, 10); return; } From a384476f2c99824c4b122c56bf84c7b6b9976a97 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Thu, 8 Sep 2022 00:36:52 -0500 Subject: [PATCH 03/20] More CarrySettings and Permissions maximumStacking: the maximum amount of entities that can be stacked through stacking. 0 = infinite stacking since you can disable stacking with allowStacking maximumSelfCarry: The maximum amount of entities a player can pick without stacking bypass.maxstack: infinite stacking regardless of maxStack bypass.maxselfcarry: infinitely picking up mobs regardless of maxSelfCarry --- .../eldoria/pickmeup/config/CarrySettings.java | 17 +++++++++++++---- .../pickmeup/listener/CarryListener.java | 8 +++++++- .../de/eldoria/pickmeup/util/Permissions.java | 2 ++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java index 8abebe7..41dfbea 100644 --- a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java +++ b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java @@ -11,17 +11,19 @@ @SerializableAs("pickMeUpCarrySettings") public class CarrySettings implements ConfigurationSerializable { private double throwForce = 2.0; - private boolean allowStacking; + private boolean allowStacking = true; + private int maximumStacking = 0; + private int maximumSelfCarry = 1; public CarrySettings(Map objectMap) { TypeResolvingMap map = SerializationUtil.mapOf(objectMap); throwForce = map.getValueOrDefault("throwForce", throwForce); allowStacking = map.getValueOrDefault("allowStacking", allowStacking); + maximumStacking = map.getValueOrDefault("maximumStacking", maximumStacking); + maximumSelfCarry = map.getValueOrDefault("maximumSelfCarry", maximumSelfCarry); } - public CarrySettings() { - - } + public CarrySettings() {} @Override public @NotNull Map serialize() { @@ -35,4 +37,11 @@ public double throwForce() { public boolean isAllowStacking() { return allowStacking; } + + public int getMaximumStacking() { + return maximumStacking; + } + public int getMaximumSelfCarry() { + return maximumSelfCarry; + } } diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 23393db..43ed210 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -89,7 +89,8 @@ void entityCarryInteraction(Player player, Entity entity, Cancellable cancellabl if (player.getEquipment().getItemInMainHand().getType() != Material.AIR) return; if (!config.mobSettings().canBePickedUp(player, entity.getType())) return; - if (!player.getPassengers().isEmpty()) return; + if (player.getPassengers().size() >= config.carrySettings().getMaximumSelfCarry() + && !player.hasPermission(Permissions.BYPASS_MAXSELFCARRY)) return; if (!player.isSneaking()) return; if (!entity.getPassengers().isEmpty() && !config.carrySettings().isAllowStacking()) { @@ -99,6 +100,11 @@ void entityCarryInteraction(Player player, Entity entity, Cancellable cancellabl } } + if(config.carrySettings().getMaximumStacking() != 0 && + entity.getPassengers().size() + player.getPassengers().size() + 1 >= config.carrySettings().getMaximumStacking() + && !player.hasPermission(Permissions.BYPASS_MAXSTACK)) + return; + mountStates.put(player.getUniqueId(), MountState.SNEAK_MOUNT); player.addPassenger(entity); cancellable.setCancelled(true); diff --git a/src/main/java/de/eldoria/pickmeup/util/Permissions.java b/src/main/java/de/eldoria/pickmeup/util/Permissions.java index 588c571..135d614 100644 --- a/src/main/java/de/eldoria/pickmeup/util/Permissions.java +++ b/src/main/java/de/eldoria/pickmeup/util/Permissions.java @@ -6,6 +6,8 @@ public final class Permissions { public static final String BASE = "pickmeup."; public static final String RELOAD = BASE + "reload"; public static final String BYPASS_NOSTACK = BASE + "bypass.nostack"; + public static final String BYPASS_MAXSTACK = BASE + "bypass.maxstack"; + public static final String BYPASS_MAXSELFCARRY = BASE + "bypass.maxselfcarry"; private Permissions() { } From 7a125abc7e8215edd47c955b049b673eaf708ef2 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Thu, 8 Sep 2022 01:43:03 -0500 Subject: [PATCH 04/20] Fixes throw bar appearing after dropping the entity Fixed https://github.com/eldoriarpg/PickMeUp/issues/6 Configurable throw delay --- .../pickmeup/config/CarrySettings.java | 6 +++ .../pickmeup/listener/CarryListener.java | 53 +++++++++++++++---- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java index 41dfbea..0438a5d 100644 --- a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java +++ b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java @@ -11,6 +11,7 @@ @SerializableAs("pickMeUpCarrySettings") public class CarrySettings implements ConfigurationSerializable { private double throwForce = 2.0; + private long throwDelay = 10L; private boolean allowStacking = true; private int maximumStacking = 0; private int maximumSelfCarry = 1; @@ -18,6 +19,7 @@ public class CarrySettings implements ConfigurationSerializable { public CarrySettings(Map objectMap) { TypeResolvingMap map = SerializationUtil.mapOf(objectMap); throwForce = map.getValueOrDefault("throwForce", throwForce); + throwDelay = map.getValueOrDefault("throwDelay", throwDelay); allowStacking = map.getValueOrDefault("allowStacking", allowStacking); maximumStacking = map.getValueOrDefault("maximumStacking", maximumStacking); maximumSelfCarry = map.getValueOrDefault("maximumSelfCarry", maximumSelfCarry); @@ -34,6 +36,10 @@ public double throwForce() { return throwForce; } + public long getThrowDelay() { + return throwDelay; + } + public boolean isAllowStacking() { return allowStacking; } diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 43ed210..6028ae1 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -19,8 +19,10 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; import java.util.HashMap; @@ -35,7 +37,10 @@ public class CarryListener implements Listener { private final Plugin plugin; private final ThrowBarHandler throwBarHandler; private final Set blocked = new HashSet<>(); + private final Map mountStates = new HashMap<>(); + private final Map throwTasks = new HashMap<>(); + private final TrailHandler trailHandler; private final MessageSender messageSender; private final DelayedActions delayedActions; @@ -68,18 +73,18 @@ void entityCarryInteraction(Player player, Entity entity, Cancellable cancellabl if (blocked.contains(entity.getUniqueId())) return; MountState mountState = mountStates.get(player.getUniqueId()); + //If the player is on the SNEAK_THROW state and interacts with the entity that is carrying with an empty hand if (mountState == MountState.SNEAK_THROW) { if (player.getPassengers().contains(entity) && player.getEquipment().getItemInMainHand().getType() == Material.AIR) { + //Dismount all entities unmountAll(player); - throwBarHandler.getAndRemove(player); - - mountStates.remove(player.getUniqueId()); + removePlayerData(player); + //Block the entity from being picked again, to avoid spam picking blocked.add(entity.getUniqueId()); - delayedActions.schedule(() -> blocked.remove(entity.getUniqueId()), 20); cancellable.setCancelled(true); @@ -105,6 +110,7 @@ void entityCarryInteraction(Player player, Entity entity, Cancellable cancellabl && !player.hasPermission(Permissions.BYPASS_MAXSTACK)) return; + //Put the player state as SNEAK_MOUNT since the player just mounted an entity mountStates.put(player.getUniqueId(), MountState.SNEAK_MOUNT); player.addPassenger(entity); cancellable.setCancelled(true); @@ -113,6 +119,7 @@ void entityCarryInteraction(Player player, Entity entity, Cancellable cancellabl @EventHandler public void onSneak(PlayerToggleSneakEvent event) { Player player = event.getPlayer(); + //If there is no mountState for the player, just dismount everything if (!mountStates.containsKey(player.getUniqueId())) { unmountAll(player); return; @@ -120,23 +127,29 @@ public void onSneak(PlayerToggleSneakEvent event) { MountState mountState = mountStates.get(player.getUniqueId()); + //If the player stopped sneaking and was SNEAK_MOUNT, change the state to WALKING if (!event.isSneaking() && mountState == MountState.SNEAK_MOUNT) { mountStates.put(player.getUniqueId(), MountState.WALKING); return; } + //If the player is begun sneaking with the WALKING state we start the throw delayedTask and set state to SNEAK_THROW if (event.isSneaking() && mountState == MountState.WALKING) { mountStates.put(player.getUniqueId(), MountState.SNEAK_THROW); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, - () -> { - if (player.isSneaking()) { - throwBarHandler.register(player); - } - }, 10); + throwTasks.put(player.getUniqueId(), + Bukkit.getScheduler().runTaskLater(plugin, + () -> { + //If the player is still sneaking after the delay + if (player.isSneaking()) { + throwBarHandler.register(player); + } + }, config.carrySettings().getThrowDelay())); return; } + //If the player stopped sneaking with the SNEAK_THROW state if (!event.isSneaking() && mountState == MountState.SNEAK_THROW) { + //If the player is not registered on the throwBarHandler then we simply dismount the entities if (!throwBarHandler.isRegistered(player)) { unmountAll(player); } else { @@ -153,16 +166,34 @@ public void onSneak(PlayerToggleSneakEvent event) { + " | ViewVec: " + throwVec); } } - mountStates.remove(player.getUniqueId()); + + //Remove all the leftover player data + removePlayerData(player); } } + @EventHandler + public void onPlayerDisconnect(PlayerQuitEvent event){ + //Remove the data if the player disconnects + removePlayerData(event.getPlayer()); + } + private void unmountAll(Player player) { for (Entity passenger : player.getPassengers()) { player.removePassenger(passenger); } } + private void removePlayerData(Player player){ + //If there is a pending task, cancel it + if(throwTasks.containsKey(player.getUniqueId()) + && !throwTasks.get(player.getUniqueId()).isCancelled()) + throwTasks.get(player.getUniqueId()).cancel(); + mountStates.remove(player.getUniqueId()); + throwTasks.remove(player.getUniqueId()); + throwBarHandler.getAndRemove(player); + } + private enum MountState { SNEAK_MOUNT, WALKING, SNEAK_THROW } From 307ad7d7d4a6b9a51805865da44c7eb0ec378475 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Thu, 8 Sep 2022 11:31:29 -0500 Subject: [PATCH 05/20] Fixing error on reload java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long --- src/main/java/de/eldoria/pickmeup/config/CarrySettings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java index 0438a5d..74262ce 100644 --- a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java +++ b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java @@ -11,7 +11,7 @@ @SerializableAs("pickMeUpCarrySettings") public class CarrySettings implements ConfigurationSerializable { private double throwForce = 2.0; - private long throwDelay = 10L; + private int throwDelay = 10; private boolean allowStacking = true; private int maximumStacking = 0; private int maximumSelfCarry = 1; From 40b43b72ba6bf78d2fea7b025a71a8467837a181 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Thu, 8 Sep 2022 14:16:06 -0500 Subject: [PATCH 06/20] Solving most of the suggestions --- build.gradle.kts | 18 +++++++------- .../pickmeup/config/CarrySettings.java | 8 +++---- .../pickmeup/listener/CarryListener.java | 24 ++++++++----------- .../pickmeup/services/ProtectionService.java | 9 +++++-- 4 files changed, 29 insertions(+), 30 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 226408f..6dea43c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,6 @@ plugins { repositories { mavenCentral() jcenter() - //maven(file("${projectDir}/dependencies/fanciful-0.4.0-SNAPSHOT.jar")) maven ("https://rayzr.dev/repo/") maven("https://jitpack.io") maven("https://eldonexus.de/repository/maven-public") @@ -19,16 +18,15 @@ repositories { dependencies { implementation("de.eldoria", "eldo-util", "1.13.9") - //implementation(file("${projectDir}/dependencies/fanciful-0.4.0-SNAPSHOT.jar")) - //compileOnly("mkremins", "fanciful", "0.4.0-SNAPSHOT") - compileOnly("com.jagrosh", "jda-utilities", "2.1") - compileOnly("net.dv8tion", "JDA", "5.0.0-alpha.18") - compileOnly("me.rayzr522", "jsonmessage", "1.3.0") compileOnly("org.spigotmc", "spigot-api", "1.16.5-R0.1-SNAPSHOT") compileOnly("com.mojang", "authlib", "1.5.25") compileOnly("org.jetbrains", "annotations", "16.0.2") - compileOnly("com.github.SaberLLC", "Saber-Factions", "2.4.0-RC") + compileOnly("me.rayzr522", "jsonmessage", "1.3.0") + compileOnly("com.github.SaberLLC", "Saber-Factions", "2.4.0-RC"){ + exclude("net.dv8tion.JDA") + exclude("com.jagrosh.jda-utilities") + } compileOnly("world.bentobox", "bentobox", "1.16.2-SNAPSHOT") compileOnly("com.github.TechFortress", "GriefPrevention", "16.17.1") compileOnly("com.github.TownyAdvanced", "Towny", "0.97.1.0") @@ -57,7 +55,7 @@ dependencies { } group = "de.eldoria" -version = "1.3.8" +version = "1.3.7" var mainPackage = "pickmeup" val shadebase = group as String? + "." + mainPackage + "." @@ -122,7 +120,7 @@ tasks { } } - /*register("copyToServer") { + register("copyToServer") { val path = project.property("targetDir") ?: ""; if (path.toString().isEmpty()) { println("targetDir is not set in gradle properties") @@ -130,7 +128,7 @@ tasks { } from(shadowJar) destinationDir = File(path.toString()) - }*/ + } build { dependsOn(shadowJar) diff --git a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java index 74262ce..6aaeb31 100644 --- a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java +++ b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java @@ -12,7 +12,7 @@ public class CarrySettings implements ConfigurationSerializable { private double throwForce = 2.0; private int throwDelay = 10; - private boolean allowStacking = true; + private boolean allowStacking = false; private int maximumStacking = 0; private int maximumSelfCarry = 1; @@ -36,7 +36,7 @@ public double throwForce() { return throwForce; } - public long getThrowDelay() { + public long throwDelay() { return throwDelay; } @@ -44,10 +44,10 @@ public boolean isAllowStacking() { return allowStacking; } - public int getMaximumStacking() { + public int maximumStacking() { return maximumStacking; } - public int getMaximumSelfCarry() { + public int maximumSelfCarry() { return maximumSelfCarry; } } diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 6028ae1..94cc231 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -25,11 +25,7 @@ import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.UUID; +import java.util.*; public class CarryListener implements Listener { private final Configuration config; @@ -65,7 +61,7 @@ public void onEntityInteract(EntityDamageByEntityEvent event) { entityCarryInteraction(player, event.getEntity(), event); } - void entityCarryInteraction(Player player, Entity entity, Cancellable cancellable){ + private void entityCarryInteraction(Player player, Entity entity, Cancellable cancellable){ if (!config.worldSettings().allowInWorld(player.getWorld())) return; if (!protectionService.canInteract(player, entity.getLocation())) return; @@ -94,7 +90,7 @@ void entityCarryInteraction(Player player, Entity entity, Cancellable cancellabl if (player.getEquipment().getItemInMainHand().getType() != Material.AIR) return; if (!config.mobSettings().canBePickedUp(player, entity.getType())) return; - if (player.getPassengers().size() >= config.carrySettings().getMaximumSelfCarry() + if (player.getPassengers().size() >= config.carrySettings().maximumSelfCarry() && !player.hasPermission(Permissions.BYPASS_MAXSELFCARRY)) return; if (!player.isSneaking()) return; @@ -105,8 +101,8 @@ void entityCarryInteraction(Player player, Entity entity, Cancellable cancellabl } } - if(config.carrySettings().getMaximumStacking() != 0 && - entity.getPassengers().size() + player.getPassengers().size() + 1 >= config.carrySettings().getMaximumStacking() + if(config.carrySettings().maximumStacking() != 0 && + entity.getPassengers().size() + player.getPassengers().size() + 1 >= config.carrySettings().maximumStacking() && !player.hasPermission(Permissions.BYPASS_MAXSTACK)) return; @@ -143,7 +139,7 @@ public void onSneak(PlayerToggleSneakEvent event) { if (player.isSneaking()) { throwBarHandler.register(player); } - }, config.carrySettings().getThrowDelay())); + }, config.carrySettings().throwDelay())); return; } @@ -186,11 +182,11 @@ private void unmountAll(Player player) { private void removePlayerData(Player player){ //If there is a pending task, cancel it - if(throwTasks.containsKey(player.getUniqueId()) - && !throwTasks.get(player.getUniqueId()).isCancelled()) - throwTasks.get(player.getUniqueId()).cancel(); + Optional.ofNullable(throwTasks.remove(player.getUniqueId())) + .ifPresent(task -> { + if (task.isCancelled()) task.cancel(); + }); mountStates.remove(player.getUniqueId()); - throwTasks.remove(player.getUniqueId()); throwBarHandler.getAndRemove(player); } diff --git a/src/main/java/de/eldoria/pickmeup/services/ProtectionService.java b/src/main/java/de/eldoria/pickmeup/services/ProtectionService.java index 914d98b..5a5ef92 100644 --- a/src/main/java/de/eldoria/pickmeup/services/ProtectionService.java +++ b/src/main/java/de/eldoria/pickmeup/services/ProtectionService.java @@ -1,6 +1,12 @@ package de.eldoria.pickmeup.services; -import de.eldoria.pickmeup.services.hooks.protection.*; +import de.eldoria.pickmeup.services.hooks.protection.BentoBoxHook; +import de.eldoria.pickmeup.services.hooks.protection.GriefPreventionHook; +import de.eldoria.pickmeup.services.hooks.protection.IProtectionHook; +import de.eldoria.pickmeup.services.hooks.protection.PlotSquaredHook; +import de.eldoria.pickmeup.services.hooks.protection.RedProtectHook; +import de.eldoria.pickmeup.services.hooks.protection.TownyHook; +import de.eldoria.pickmeup.services.hooks.protection.SaberFactionsHook; import org.bukkit.Location; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -30,7 +36,6 @@ private void init() { Arrays.stream(hooks()) .filter(hook -> { if (pm.isPluginEnabled(hook.pluginName())) { - plugin.getLogger().info(hook.pluginName() + " found! Enabling protection hook."); return true; } plugin.getLogger().info(hook.pluginName() + " not found. Skipping protection hook."); From 7fee899a24daa13dfc6d33c272eec14efeadf4bb Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Thu, 8 Sep 2022 17:09:00 -0500 Subject: [PATCH 07/20] Handling data removal in EntityDismountEvent This takes account for all dismount events, such as disconnect, Entity#removePassenger, the passenger dismounting for any reason, etc. --- .../pickmeup/listener/CarryListener.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 94cc231..a1cabb7 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -24,6 +24,7 @@ import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; +import org.spigotmc.event.entity.EntityDismountEvent; import java.util.*; @@ -77,8 +78,6 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca //Dismount all entities unmountAll(player); - removePlayerData(player); - //Block the entity from being picked again, to avoid spam picking blocked.add(entity.getUniqueId()); delayedActions.schedule(() -> blocked.remove(entity.getUniqueId()), 20); @@ -88,12 +87,18 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca } } + // If the player doesn't have the hand empty, leave if (player.getEquipment().getItemInMainHand().getType() != Material.AIR) return; + if (!config.mobSettings().canBePickedUp(player, entity.getType())) return; + if (player.getPassengers().size() >= config.carrySettings().maximumSelfCarry() && !player.hasPermission(Permissions.BYPASS_MAXSELFCARRY)) return; + + //If the player is not sneaking leave if (!player.isSneaking()) return; + if (!entity.getPassengers().isEmpty() && !config.carrySettings().isAllowStacking()) { if (!player.hasPermission(Permissions.BYPASS_NOSTACK)) { messageSender.sendLocalizedError(player, "nostack"); @@ -162,16 +167,13 @@ public void onSneak(PlayerToggleSneakEvent event) { + " | ViewVec: " + throwVec); } } - - //Remove all the leftover player data - removePlayerData(player); } } @EventHandler - public void onPlayerDisconnect(PlayerQuitEvent event){ - //Remove the data if the player disconnects - removePlayerData(event.getPlayer()); + public void onEntityDismount(EntityDismountEvent event){ + if(event.getDismounted() instanceof Player player) + removePlayerData(player); } private void unmountAll(Player player) { @@ -184,7 +186,7 @@ private void removePlayerData(Player player){ //If there is a pending task, cancel it Optional.ofNullable(throwTasks.remove(player.getUniqueId())) .ifPresent(task -> { - if (task.isCancelled()) task.cancel(); + if (!task.isCancelled()) task.cancel(); }); mountStates.remove(player.getUniqueId()); throwBarHandler.getAndRemove(player); From 8ae35e8355e8943c6c278b241e1695aba4e0bc61 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Sat, 10 Sep 2022 11:39:45 -0500 Subject: [PATCH 08/20] Update build.gradle.kts Changed version of Saber-Factions to 2.9.1-RC --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 6dea43c..446e9c3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -23,7 +23,7 @@ dependencies { compileOnly("com.mojang", "authlib", "1.5.25") compileOnly("org.jetbrains", "annotations", "16.0.2") compileOnly("me.rayzr522", "jsonmessage", "1.3.0") - compileOnly("com.github.SaberLLC", "Saber-Factions", "2.4.0-RC"){ + compileOnly("com.github.SaberLLC", "Saber-Factions", "2.9.1-RC"){ exclude("net.dv8tion.JDA") exclude("com.jagrosh.jda-utilities") } From eba958da54aaa09c541c09f655fad638b2f1c451 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Mon, 12 Sep 2022 01:19:47 -0500 Subject: [PATCH 09/20] Replacing getPlugin() getPlugin() returns the Towny plugin Instance. --- src/main/java/de/eldoria/pickmeup/PickMeUp.java | 10 ++++++++++ src/main/java/de/eldoria/pickmeup/commands/Reload.java | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/eldoria/pickmeup/PickMeUp.java b/src/main/java/de/eldoria/pickmeup/PickMeUp.java index 9199fb8..d1a9371 100644 --- a/src/main/java/de/eldoria/pickmeup/PickMeUp.java +++ b/src/main/java/de/eldoria/pickmeup/PickMeUp.java @@ -15,6 +15,7 @@ import de.eldoria.pickmeup.listener.CarryListener; import de.eldoria.pickmeup.services.ProtectionService; import de.eldoria.pickmeup.util.Permissions; +import org.bukkit.NamespacedKey; import org.bukkit.configuration.serialization.ConfigurationSerializable; import java.util.Arrays; @@ -24,6 +25,11 @@ public class PickMeUp extends EldoPlugin { private boolean initialized; private Configuration configuration; + private static PickMeUp instance; + + public PickMeUp(){ + instance = this; + } @Override public void onPluginEnable(boolean reload) { @@ -57,4 +63,8 @@ public void onReload() { onDisable(); onEnable(); } + public static PickMeUp instance(){ + return instance; + } + } diff --git a/src/main/java/de/eldoria/pickmeup/commands/Reload.java b/src/main/java/de/eldoria/pickmeup/commands/Reload.java index d24d64a..aadd651 100644 --- a/src/main/java/de/eldoria/pickmeup/commands/Reload.java +++ b/src/main/java/de/eldoria/pickmeup/commands/Reload.java @@ -27,7 +27,7 @@ public Reload(Plugin plugin) { @Override public void onCommand(@NotNull CommandSender sender, @NotNull String alias, @NotNull Arguments args) throws CommandException { - getPlugin().onEnable(); + PickMeUp.instance().onReload(); messageSender().sendLocalizedMessage(sender, "reload.success"); PickMeUp.logger().info("PickMeUp reloaded!"); } From 737ab97912ae5189ae9979a74fc8ca43f8f3f99a Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Mon, 12 Sep 2022 01:50:35 -0500 Subject: [PATCH 10/20] Re-ordered entityCarryInteraction Moved some stuff around to simplify the logic --- .../pickmeup/listener/CarryListener.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index a1cabb7..423cc39 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -19,7 +19,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractAtEntityEvent; -import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitTask; @@ -30,7 +29,7 @@ public class CarryListener implements Listener { private final Configuration config; - private ProtectionService protectionService; + private final ProtectionService protectionService; private final Plugin plugin; private final ThrowBarHandler throwBarHandler; private final Set blocked = new HashSet<>(); @@ -69,11 +68,12 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca if (blocked.contains(entity.getUniqueId())) return; + // If the player doesn't have the hand empty, leave + if (Objects.requireNonNull(player.getEquipment()).getItemInMainHand().getType() != Material.AIR) return; + MountState mountState = mountStates.get(player.getUniqueId()); //If the player is on the SNEAK_THROW state and interacts with the entity that is carrying with an empty hand - if (mountState == MountState.SNEAK_THROW) { - if (player.getPassengers().contains(entity) - && player.getEquipment().getItemInMainHand().getType() == Material.AIR) { + if (mountState == MountState.SNEAK_THROW && player.getPassengers().contains(entity)) { //Dismount all entities unmountAll(player); @@ -84,20 +84,17 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca cancellable.setCancelled(true); return; - } } - // If the player doesn't have the hand empty, leave - if (player.getEquipment().getItemInMainHand().getType() != Material.AIR) return; + //If the player is not sneaking leave + if (!player.isSneaking()) return; + //Check if the settings/perms allow the player to pick up this entity if (!config.mobSettings().canBePickedUp(player, entity.getType())) return; if (player.getPassengers().size() >= config.carrySettings().maximumSelfCarry() && !player.hasPermission(Permissions.BYPASS_MAXSELFCARRY)) return; - //If the player is not sneaking leave - if (!player.isSneaking()) return; - if (!entity.getPassengers().isEmpty() && !config.carrySettings().isAllowStacking()) { if (!player.hasPermission(Permissions.BYPASS_NOSTACK)) { From 154ef6bee17ac31b25a6c234c2bf8d04ba5358b8 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Mon, 12 Sep 2022 02:05:53 -0500 Subject: [PATCH 11/20] Merged mountStates and throwTasks into a handling subclass --- .../pickmeup/listener/CarryListener.java | 49 ++++++++++++------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 423cc39..56d0d3a 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -34,8 +34,7 @@ public class CarryListener implements Listener { private final ThrowBarHandler throwBarHandler; private final Set blocked = new HashSet<>(); - private final Map mountStates = new HashMap<>(); - private final Map throwTasks = new HashMap<>(); + private final Map mountHandlers = new HashMap<>(); private final TrailHandler trailHandler; private final MessageSender messageSender; @@ -71,9 +70,11 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca // If the player doesn't have the hand empty, leave if (Objects.requireNonNull(player.getEquipment()).getItemInMainHand().getType() != Material.AIR) return; - MountState mountState = mountStates.get(player.getUniqueId()); + MountHandler mountHandler = mountHandlers.get(player.getUniqueId()); //If the player is on the SNEAK_THROW state and interacts with the entity that is carrying with an empty hand - if (mountState == MountState.SNEAK_THROW && player.getPassengers().contains(entity)) { + if (mountHandler != null + && mountHandler.state == MountState.SNEAK_THROW + && player.getPassengers().contains(entity)) { //Dismount all entities unmountAll(player); @@ -109,7 +110,7 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca return; //Put the player state as SNEAK_MOUNT since the player just mounted an entity - mountStates.put(player.getUniqueId(), MountState.SNEAK_MOUNT); + mountHandlers.put(player.getUniqueId(), new MountHandler(MountState.SNEAK_MOUNT)); player.addPassenger(entity); cancellable.setCancelled(true); } @@ -118,35 +119,35 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca public void onSneak(PlayerToggleSneakEvent event) { Player player = event.getPlayer(); //If there is no mountState for the player, just dismount everything - if (!mountStates.containsKey(player.getUniqueId())) { + if (!mountHandlers.containsKey(player.getUniqueId())) { unmountAll(player); return; } - MountState mountState = mountStates.get(player.getUniqueId()); + MountHandler mountHandler = mountHandlers.get(player.getUniqueId()); //If the player stopped sneaking and was SNEAK_MOUNT, change the state to WALKING - if (!event.isSneaking() && mountState == MountState.SNEAK_MOUNT) { - mountStates.put(player.getUniqueId(), MountState.WALKING); + if (!event.isSneaking() && mountHandler.state == MountState.SNEAK_MOUNT) { + mountHandler.state = MountState.WALKING; return; } //If the player is begun sneaking with the WALKING state we start the throw delayedTask and set state to SNEAK_THROW - if (event.isSneaking() && mountState == MountState.WALKING) { - mountStates.put(player.getUniqueId(), MountState.SNEAK_THROW); - throwTasks.put(player.getUniqueId(), + if (event.isSneaking() && mountHandler.state == MountState.WALKING) { + mountHandler.state = MountState.SNEAK_THROW; + mountHandler.throwTask = Bukkit.getScheduler().runTaskLater(plugin, () -> { //If the player is still sneaking after the delay if (player.isSneaking()) { throwBarHandler.register(player); } - }, config.carrySettings().throwDelay())); + }, config.carrySettings().throwDelay()); return; } //If the player stopped sneaking with the SNEAK_THROW state - if (!event.isSneaking() && mountState == MountState.SNEAK_THROW) { + if (!event.isSneaking() && mountHandler.state == MountState.SNEAK_THROW) { //If the player is not registered on the throwBarHandler then we simply dismount the entities if (!throwBarHandler.isRegistered(player)) { unmountAll(player); @@ -181,15 +182,25 @@ private void unmountAll(Player player) { private void removePlayerData(Player player){ //If there is a pending task, cancel it - Optional.ofNullable(throwTasks.remove(player.getUniqueId())) - .ifPresent(task -> { - if (!task.isCancelled()) task.cancel(); - }); - mountStates.remove(player.getUniqueId()); + Optional.ofNullable(mountHandlers.remove(player.getUniqueId())) + .ifPresent(MountHandler::dispose); throwBarHandler.getAndRemove(player); } private enum MountState { SNEAK_MOUNT, WALKING, SNEAK_THROW } + + private class MountHandler{ + public MountState state; + public BukkitTask throwTask; + + public MountHandler(MountState state){ + this.state = state; + } + + public void dispose(){ + if (!throwTask.isCancelled()) throwTask.cancel(); + } + } } From 616df31cc62e136d5a403c70c06f556a6457dd52 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Tue, 13 Sep 2022 01:26:03 -0500 Subject: [PATCH 12/20] Increased mounting point of entities Implemented https://github.com/eldoriarpg/PickMeUp/issues/7 However this comes with a drawback. In order to achieve this, I'm adding an invisible baby armor stand as a buffer between the player and the entitiy it is holding, this comes with the drawback of obstructing the player's ability to interact/attack any other block or entity. The only ones capable of circumbenting this naturally are bedrock player's or modded clients. To the moment, the only way I've found to fix this is to teleport the armorstand on top of the player every tick instead, however, this makes the entities lag behind when running. --- .../java/de/eldoria/pickmeup/PickMeUp.java | 5 + .../pickmeup/listener/CarryListener.java | 250 ++++++++++++++++-- 2 files changed, 226 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/PickMeUp.java b/src/main/java/de/eldoria/pickmeup/PickMeUp.java index d1a9371..fd299bc 100644 --- a/src/main/java/de/eldoria/pickmeup/PickMeUp.java +++ b/src/main/java/de/eldoria/pickmeup/PickMeUp.java @@ -26,9 +26,11 @@ public class PickMeUp extends EldoPlugin { private boolean initialized; private Configuration configuration; private static PickMeUp instance; + private static NamespacedKey offsetterIdentifierKey; public PickMeUp(){ instance = this; + offsetterIdentifierKey = new NamespacedKey(this, "IsOffsetter"); } @Override @@ -66,5 +68,8 @@ public void onReload() { public static PickMeUp instance(){ return instance; } + public static NamespacedKey offsetterIdentifierKey(){ + return offsetterIdentifierKey; + } } diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 56d0d3a..9dce7c3 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -11,7 +11,9 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Sound; +import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.EventHandler; @@ -20,6 +22,8 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractAtEntityEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; @@ -71,21 +75,22 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca if (Objects.requireNonNull(player.getEquipment()).getItemInMainHand().getType() != Material.AIR) return; MountHandler mountHandler = mountHandlers.get(player.getUniqueId()); - //If the player is on the SNEAK_THROW state and interacts with the entity that is carrying with an empty hand + /*//If the player interacts with the entity that is carrying with an empty hand if (mountHandler != null && mountHandler.state == MountState.SNEAK_THROW - && player.getPassengers().contains(entity)) { + && mountHandler.getDirectPassengers().contains(entity)) { - //Dismount all entities - unmountAll(player); + //Dismount entity + if(!mountHandler.removePassenger(entity)) + return; - //Block the entity from being picked again, to avoid spam picking - blocked.add(entity.getUniqueId()); - delayedActions.schedule(() -> blocked.remove(entity.getUniqueId()), 20); + //Block the entity from being picked again, to avoid spam picking + blocked.add(entity.getUniqueId()); + delayedActions.schedule(() -> blocked.remove(entity.getUniqueId()), 20); - cancellable.setCancelled(true); - return; - } + cancellable.setCancelled(true); + return; + }*/ //If the player is not sneaking leave if (!player.isSneaking()) return; @@ -93,25 +98,37 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca //Check if the settings/perms allow the player to pick up this entity if (!config.mobSettings().canBePickedUp(player, entity.getType())) return; - if (player.getPassengers().size() >= config.carrySettings().maximumSelfCarry() - && !player.hasPermission(Permissions.BYPASS_MAXSELFCARRY)) return; + if (!player.hasPermission(Permissions.BYPASS_MAXSELFCARRY) + && (mountHandler != null + ? mountHandler.getDirectPassengers().size() + : MountHandler.getDirectPassengers(player, true).size()) + >= config.carrySettings().maximumSelfCarry()) return; - if (!entity.getPassengers().isEmpty() && !config.carrySettings().isAllowStacking()) { - if (!player.hasPermission(Permissions.BYPASS_NOSTACK)) { - messageSender.sendLocalizedError(player, "nostack"); - return; - } + if (!config.carrySettings().isAllowStacking() + &&!player.hasPermission(Permissions.BYPASS_NOSTACK) + && MountHandler.stackCount(entity, true) > 1) { + messageSender.sendLocalizedError(player, "nostack"); + return; + } - if(config.carrySettings().maximumStacking() != 0 && - entity.getPassengers().size() + player.getPassengers().size() + 1 >= config.carrySettings().maximumStacking() - && !player.hasPermission(Permissions.BYPASS_MAXSTACK)) + if(config.carrySettings().maximumStacking() != 0 + && !player.hasPermission(Permissions.BYPASS_MAXSTACK) + && MountHandler.stackCount(entity, true) + MountHandler.stackCount(player, true) - 1 > config.carrySettings().maximumStacking()) return; - //Put the player state as SNEAK_MOUNT since the player just mounted an entity - mountHandlers.put(player.getUniqueId(), new MountHandler(MountState.SNEAK_MOUNT)); - player.addPassenger(entity); + + mountHandlers.putIfAbsent(player.getUniqueId(), new MountHandler(player)); + MountHandler handler = mountHandlers.get(player.getUniqueId()); + // Set the player state as SNEAK_MOUNT since the player just mounted an entity + handler.state = MountState.SNEAK_MOUNT; + // Clear any extra data in case it was leftover + throwBarHandler.getAndRemove(player); + handler.cleanTasks(); + + handler.addPassenger(entity); + cancellable.setCancelled(true); } @@ -155,9 +172,9 @@ public void onSneak(PlayerToggleSneakEvent event) { double force = throwBarHandler.getAndRemove(player); Vector throwVec = player.getEyeLocation().getDirection().normalize().multiply(force * config.carrySettings().throwForce()); player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ENDER_DRAGON_FLAP, 1, 1); - for (Entity passenger : player.getPassengers()) { + for (Entity passenger : mountHandler.getDirectPassengers()) { delayedActions.schedule(() -> trailHandler.startTrail(passenger), 2); - player.removePassenger(passenger); + mountHandler.removePassenger(passenger); passenger.setVelocity(throwVec); plugin.getLogger().config("Throwing entity | Location:" + player.getLocation().toVector() + " | Force: " + force @@ -171,7 +188,22 @@ public void onSneak(PlayerToggleSneakEvent event) { @EventHandler public void onEntityDismount(EntityDismountEvent event){ if(event.getDismounted() instanceof Player player) - removePlayerData(player); + Optional.ofNullable(mountHandlers.get(player.getUniqueId())) + .ifPresentOrElse(handler -> { + if(handler.getDirectPassengers().isEmpty()) + removePlayerData(player); + }, + () ->{ + removePlayerData(player); + }); + + + // If an ArmorStand just dismounted any entity + if(event.getEntity() instanceof ArmorStand armorStand){ + if(MountHandler.isOffsetter(armorStand)){ // If so, remove it from existence + armorStand.remove(); + } + } } private void unmountAll(Player player) { @@ -195,12 +227,172 @@ private class MountHandler{ public MountState state; public BukkitTask throwTask; - public MountHandler(MountState state){ - this.state = state; + private final Player owner; + + private ArmorStand offsetter; + + public MountHandler(Player owner){ + this.owner = owner; + } + + public void cleanTasks(){ + if (throwTask != null && !throwTask.isCancelled()) throwTask.cancel(); } public void dispose(){ - if (!throwTask.isCancelled()) throwTask.cancel(); + cleanTasks(); + + if (offsetter == null) + return; + + for (Entity passenger : + offsetter.getPassengers()) { + offsetter.removePassenger(passenger); + } + offsetter.remove(); + offsetter = null; + } + + public boolean addPassenger(Entity passenger){ + Entity mountable = getMountable(); + + if(mountable.getUniqueId() == passenger.getUniqueId()) + return false; + + return mountable.addPassenger(passenger); + } + + public boolean removePassenger(Entity passenger){ + Entity mountable = getMountable(); + + if(mountable.getUniqueId() == passenger.getUniqueId()) + return false; + + boolean result = mountable.removePassenger(passenger); + + // If there are no more passengers, dispose + if(mountable.getPassengers().isEmpty()) + dispose(); + + return result; + } + + public List getDirectPassengers(){ + Entity mountable = getMountable(); + + return mountable.getPassengers(); + } + + public List getAllPassengers(boolean filterOffsetters){ + return getAllPassengers(owner, filterOffsetters); + } + public int stackCount(boolean filterOffsetters){ + return stackCount(owner, filterOffsetters); + } + + private void setupOffsetter() { + if (offsetter != null) + return; + + ArmorStand armorStand = (ArmorStand) owner.getWorld().spawnEntity(owner.getLocation(), EntityType.ARMOR_STAND); + + //armorStand.setInvulnerable(true); + //armorStand.setInvisible(true); + //armorStand.setMarker(true); + armorStand.setSmall(true); + armorStand.setBasePlate(false); + //armorStand.setLeftArmPose(90); + //armorStand.setRightArmPose(90); + + + // Mark it as an offsetter so it can be easily identified for removal + PersistentDataContainer dataContainer = armorStand.getPersistentDataContainer(); + dataContainer.set(PickMeUp.offsetterIdentifierKey(), PersistentDataType.BYTE, (byte) 1); + + + // Get the current passengers of the owner + List passengers = owner.getPassengers(); + + if (owner.addPassenger(armorStand)) { + offsetter = armorStand; + // Transfer any passengers from the owner to the offsetter + for (Entity passenger : passengers) + offsetter.addPassenger(passenger); + } else { + armorStand.remove(); + } + } + + private Entity getMountable(){ + setupOffsetter(); + + // If the offsetter is somehow null, default to the owner himself + if(offsetter != null) + return offsetter; + return owner; + } + + + public static boolean isOffsetter(Entity entity){ + PersistentDataContainer dataContainer = entity.getPersistentDataContainer(); + byte isOffsetter = dataContainer.getOrDefault(PickMeUp.offsetterIdentifierKey(), PersistentDataType.BYTE, (byte)0); + return isOffsetter == (byte) 1; + } + + public static List getDirectPassengers(Entity origin, boolean filterOffsetters){ + List passengers = origin.getPassengers(); + for (Entity passenger : passengers) { + if(isOffsetter(passenger)) { + passengers.addAll(passenger.getPassengers()); + if(filterOffsetters) + passengers.remove(passenger); + } + + } + + return passengers; + } + + public static List getAllPassengers(Entity origin, boolean filterOffsetters){ + List passengers = getPassengersRecursive(origin); + + if(filterOffsetters) + passengers = passengers.stream().filter(MountHandler::isOffsetter).toList(); + + return passengers; + } + + private static List getPassengersRecursive(Entity mountable){ + List passengers = mountable.getPassengers(); + for (Entity passenger : + passengers) { + passengers.addAll(getPassengersRecursive(passenger)); + } + + return passengers; + } + + public static int stackCount(Entity origin, boolean filterOffsetters){ + return stackCountRecursive(origin, filterOffsetters, 0); + } + + private static int stackCountRecursive(Entity mountable, boolean filterOffsetters, int level){ + List passengers = mountable.getPassengers(); + + int resultLevel = level; + if(!filterOffsetters || !isOffsetter(mountable)) + resultLevel += 1; + + for (Entity passenger : + passengers) { + int deeperLevel = stackCountRecursive(passenger, filterOffsetters, resultLevel); + + if(deeperLevel > resultLevel) + resultLevel = deeperLevel; + + } + + return resultLevel; } } } From 5f6a69f4d17427cde26410bdb4e2d10d5ec3a03f Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Tue, 13 Sep 2022 01:26:23 -0500 Subject: [PATCH 13/20] Hide throwbar instantly --- .../java/de/eldoria/pickmeup/scheduler/ThrowBarHandler.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/de/eldoria/pickmeup/scheduler/ThrowBarHandler.java b/src/main/java/de/eldoria/pickmeup/scheduler/ThrowBarHandler.java index 3ef5161..81aae9c 100644 --- a/src/main/java/de/eldoria/pickmeup/scheduler/ThrowBarHandler.java +++ b/src/main/java/de/eldoria/pickmeup/scheduler/ThrowBarHandler.java @@ -94,6 +94,8 @@ public void register(Player player) { public double getAndRemove(Player player) { AtomicInteger remove = currentValues.remove(player); + //Send an empty message to instantly hide the bar + sender.send(MessageChannel.ACTION_BAR, MessageType.BLANK, player, ""); if (remove != null) { return calculateForce(remove.get()); } From b37c0f9e0e6d79631e2426de981b027f6796310a Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Tue, 13 Sep 2022 01:31:20 -0500 Subject: [PATCH 14/20] maxSelfCarry has no use anymore Commenting this in the case it could be rescued later, for now it has no use because of 616df31cc62e136d5a403c70c06f556a6457dd52 Now right/left clicking while on SNEAK_THROW state, cancels the throw and switches you to SNEAK_MOUNT, which will allow you to stand back up without dropping the entity --- .../java/de/eldoria/pickmeup/config/CarrySettings.java | 8 ++++---- .../java/de/eldoria/pickmeup/listener/CarryListener.java | 4 ++-- src/main/java/de/eldoria/pickmeup/util/Permissions.java | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java index 6aaeb31..d084d1c 100644 --- a/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java +++ b/src/main/java/de/eldoria/pickmeup/config/CarrySettings.java @@ -14,7 +14,7 @@ public class CarrySettings implements ConfigurationSerializable { private int throwDelay = 10; private boolean allowStacking = false; private int maximumStacking = 0; - private int maximumSelfCarry = 1; + //private int maximumSelfCarry = 1; public CarrySettings(Map objectMap) { TypeResolvingMap map = SerializationUtil.mapOf(objectMap); @@ -22,7 +22,7 @@ public CarrySettings(Map objectMap) { throwDelay = map.getValueOrDefault("throwDelay", throwDelay); allowStacking = map.getValueOrDefault("allowStacking", allowStacking); maximumStacking = map.getValueOrDefault("maximumStacking", maximumStacking); - maximumSelfCarry = map.getValueOrDefault("maximumSelfCarry", maximumSelfCarry); + //maximumSelfCarry = map.getValueOrDefault("maximumSelfCarry", maximumSelfCarry); } public CarrySettings() {} @@ -47,7 +47,7 @@ public boolean isAllowStacking() { public int maximumStacking() { return maximumStacking; } - public int maximumSelfCarry() { + /*public int maximumSelfCarry() { return maximumSelfCarry; - } + }*/ } diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 9dce7c3..225c8d2 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -98,11 +98,11 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca //Check if the settings/perms allow the player to pick up this entity if (!config.mobSettings().canBePickedUp(player, entity.getType())) return; - if (!player.hasPermission(Permissions.BYPASS_MAXSELFCARRY) + /*if (!player.hasPermission(Permissions.BYPASS_MAXSELFCARRY) && (mountHandler != null ? mountHandler.getDirectPassengers().size() : MountHandler.getDirectPassengers(player, true).size()) - >= config.carrySettings().maximumSelfCarry()) return; + >= config.carrySettings().maximumSelfCarry()) return;*/ if (!config.carrySettings().isAllowStacking() diff --git a/src/main/java/de/eldoria/pickmeup/util/Permissions.java b/src/main/java/de/eldoria/pickmeup/util/Permissions.java index 135d614..949f682 100644 --- a/src/main/java/de/eldoria/pickmeup/util/Permissions.java +++ b/src/main/java/de/eldoria/pickmeup/util/Permissions.java @@ -7,7 +7,7 @@ public final class Permissions { public static final String RELOAD = BASE + "reload"; public static final String BYPASS_NOSTACK = BASE + "bypass.nostack"; public static final String BYPASS_MAXSTACK = BASE + "bypass.maxstack"; - public static final String BYPASS_MAXSELFCARRY = BASE + "bypass.maxselfcarry"; + //public static final String BYPASS_MAXSELFCARRY = BASE + "bypass.maxselfcarry"; private Permissions() { } From f0798d934e156df8e4ee5b79f7795b4069f2b4a3 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Tue, 13 Sep 2022 01:42:50 -0500 Subject: [PATCH 15/20] Update CarryListener.java Forgot to make the offsetter armorstand invisible and invulnerable --- src/main/java/de/eldoria/pickmeup/listener/CarryListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 225c8d2..d4a7183 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -296,8 +296,8 @@ private void setupOffsetter() { ArmorStand armorStand = (ArmorStand) owner.getWorld().spawnEntity(owner.getLocation(), EntityType.ARMOR_STAND); - //armorStand.setInvulnerable(true); - //armorStand.setInvisible(true); + armorStand.setInvulnerable(true); + armorStand.setInvisible(true); //armorStand.setMarker(true); armorStand.setSmall(true); armorStand.setBasePlate(false); From 27f96db74036a2e162ae35cffa1d233d8e36e8a9 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Tue, 13 Sep 2022 01:58:28 -0500 Subject: [PATCH 16/20] Data failsafe Adding this as afailsafe to ensure the function of the plugin --- .../java/de/eldoria/pickmeup/listener/CarryListener.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index d4a7183..841fbed 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -21,6 +21,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractAtEntityEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; @@ -206,6 +207,11 @@ public void onEntityDismount(EntityDismountEvent event){ } } + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event){ + removePlayerData(event.getPlayer()); + } + private void unmountAll(Player player) { for (Entity passenger : player.getPassengers()) { player.removePassenger(passenger); @@ -291,7 +297,7 @@ public int stackCount(boolean filterOffsetters){ } private void setupOffsetter() { - if (offsetter != null) + if (offsetter != null && !offsetter.isDead()) return; ArmorStand armorStand = (ArmorStand) owner.getWorld().spawnEntity(owner.getLocation(), EntityType.ARMOR_STAND); From 311c9b59f7366e66721f09be798e763dc5b5b177 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Tue, 13 Sep 2022 03:23:57 -0500 Subject: [PATCH 17/20] Fixed offsetter not being properly removed The offsetter armorstand wasn't being properly removed when no more entities where mounted on them --- .../de/eldoria/pickmeup/listener/CarryListener.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 841fbed..4d6a4de 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -75,7 +75,7 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca // If the player doesn't have the hand empty, leave if (Objects.requireNonNull(player.getEquipment()).getItemInMainHand().getType() != Material.AIR) return; - MountHandler mountHandler = mountHandlers.get(player.getUniqueId()); + //MountHandler mountHandler = mountHandlers.get(player.getUniqueId()); /*//If the player interacts with the entity that is carrying with an empty hand if (mountHandler != null && mountHandler.state == MountState.SNEAK_THROW @@ -191,7 +191,7 @@ public void onEntityDismount(EntityDismountEvent event){ if(event.getDismounted() instanceof Player player) Optional.ofNullable(mountHandlers.get(player.getUniqueId())) .ifPresentOrElse(handler -> { - if(handler.getDirectPassengers().isEmpty()) + if(handler.getDirectPassengers().size() - 1 <= 0) removePlayerData(player); }, () ->{ @@ -271,10 +271,10 @@ public boolean addPassenger(Entity passenger){ public boolean removePassenger(Entity passenger){ Entity mountable = getMountable(); - if(mountable.getUniqueId() == passenger.getUniqueId()) - return false; + boolean result = false; - boolean result = mountable.removePassenger(passenger); + if(mountable.getUniqueId() != passenger.getUniqueId()) + result = mountable.removePassenger(passenger); // If there are no more passengers, dispose if(mountable.getPassengers().isEmpty()) From 4991e18fa7337f5e61e64eb31aba1e21f152bf48 Mon Sep 17 00:00:00 2001 From: Gabo6480 <60194781+Gabo6480@users.noreply.github.com> Date: Tue, 13 Sep 2022 10:52:44 -0500 Subject: [PATCH 18/20] Apply some suggestions from code review Co-authored-by: Lilly Tempest <46890129+RainbowDashLabs@users.noreply.github.com> --- .../pickmeup/listener/CarryListener.java | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 4d6a4de..2dbde07 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -248,8 +248,7 @@ public void cleanTasks(){ public void dispose(){ cleanTasks(); - if (offsetter == null) - return; + if (offsetter == null) return; for (Entity passenger : offsetter.getPassengers()) { @@ -284,9 +283,7 @@ public boolean removePassenger(Entity passenger){ } public List getDirectPassengers(){ - Entity mountable = getMountable(); - - return mountable.getPassengers(); + return getMountable().getPassengers(); } public List getAllPassengers(boolean filterOffsetters){ @@ -300,7 +297,7 @@ private void setupOffsetter() { if (offsetter != null && !offsetter.isDead()) return; - ArmorStand armorStand = (ArmorStand) owner.getWorld().spawnEntity(owner.getLocation(), EntityType.ARMOR_STAND); + ArmorStand armorStand = owner.getWorld().spawn(owner.getLocation(), ArmorStand.class); armorStand.setInvulnerable(true); armorStand.setInvisible(true); @@ -312,8 +309,7 @@ private void setupOffsetter() { // Mark it as an offsetter so it can be easily identified for removal - PersistentDataContainer dataContainer = armorStand.getPersistentDataContainer(); - dataContainer.set(PickMeUp.offsetterIdentifierKey(), PersistentDataType.BYTE, (byte) 1); + DataContainerUtil.putValue(armorStand, PickMeUp.offsetterIdentifierKey(), PersistentDataType.BYTE, (byte) 1); // Get the current passengers of the owner @@ -333,16 +329,14 @@ private Entity getMountable(){ setupOffsetter(); // If the offsetter is somehow null, default to the owner himself - if(offsetter != null) - return offsetter; - return owner; + return offsetter != null ? offsetter : owner; } public static boolean isOffsetter(Entity entity){ - PersistentDataContainer dataContainer = entity.getPersistentDataContainer(); - byte isOffsetter = dataContainer.getOrDefault(PickMeUp.offsetterIdentifierKey(), PersistentDataType.BYTE, (byte)0); - return isOffsetter == (byte) 1; + return DataContainerUtil.get(entity, PickMeUp.offsetterIdentifierKey(), PersistentDataType.BYTE) + .map(b -> b == (byte) 0) + .orElse(false); } public static List getDirectPassengers(Entity origin, boolean filterOffsetters){ From 423963c3d812819158c446cd75d3243dfc919598 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Wed, 14 Sep 2022 02:26:23 -0500 Subject: [PATCH 19/20] More suggestions applied --- build.gradle.kts | 6 +- .../java/de/eldoria/pickmeup/PickMeUp.java | 13 -- .../de/eldoria/pickmeup/commands/Reload.java | 4 +- .../pickmeup/listener/CarryListener.java | 209 +++--------------- .../pickmeup/listener/MountHandler.java | 187 ++++++++++++++++ 5 files changed, 214 insertions(+), 205 deletions(-) create mode 100644 src/main/java/de/eldoria/pickmeup/listener/MountHandler.java diff --git a/build.gradle.kts b/build.gradle.kts index 446e9c3..9ce3093 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -8,8 +8,6 @@ plugins { repositories { mavenCentral() - jcenter() - maven ("https://rayzr.dev/repo/") maven("https://jitpack.io") maven("https://eldonexus.de/repository/maven-public") maven("https://eldonexus.de/repository/maven-proxies") @@ -22,10 +20,8 @@ dependencies { compileOnly("org.spigotmc", "spigot-api", "1.16.5-R0.1-SNAPSHOT") compileOnly("com.mojang", "authlib", "1.5.25") compileOnly("org.jetbrains", "annotations", "16.0.2") - compileOnly("me.rayzr522", "jsonmessage", "1.3.0") compileOnly("com.github.SaberLLC", "Saber-Factions", "2.9.1-RC"){ - exclude("net.dv8tion.JDA") - exclude("com.jagrosh.jda-utilities") + exclude("*") } compileOnly("world.bentobox", "bentobox", "1.16.2-SNAPSHOT") compileOnly("com.github.TechFortress", "GriefPrevention", "16.17.1") diff --git a/src/main/java/de/eldoria/pickmeup/PickMeUp.java b/src/main/java/de/eldoria/pickmeup/PickMeUp.java index fd299bc..cf7d42e 100644 --- a/src/main/java/de/eldoria/pickmeup/PickMeUp.java +++ b/src/main/java/de/eldoria/pickmeup/PickMeUp.java @@ -25,13 +25,6 @@ public class PickMeUp extends EldoPlugin { private boolean initialized; private Configuration configuration; - private static PickMeUp instance; - private static NamespacedKey offsetterIdentifierKey; - - public PickMeUp(){ - instance = this; - offsetterIdentifierKey = new NamespacedKey(this, "IsOffsetter"); - } @Override public void onPluginEnable(boolean reload) { @@ -65,11 +58,5 @@ public void onReload() { onDisable(); onEnable(); } - public static PickMeUp instance(){ - return instance; - } - public static NamespacedKey offsetterIdentifierKey(){ - return offsetterIdentifierKey; - } } diff --git a/src/main/java/de/eldoria/pickmeup/commands/Reload.java b/src/main/java/de/eldoria/pickmeup/commands/Reload.java index aadd651..066a459 100644 --- a/src/main/java/de/eldoria/pickmeup/commands/Reload.java +++ b/src/main/java/de/eldoria/pickmeup/commands/Reload.java @@ -16,8 +16,6 @@ import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; -import static com.palmergames.bukkit.towny.Towny.getPlugin; - public class Reload extends AdvancedCommand implements ITabExecutor { public Reload(Plugin plugin) { super(plugin, CommandMeta.builder("reload") @@ -27,7 +25,7 @@ public Reload(Plugin plugin) { @Override public void onCommand(@NotNull CommandSender sender, @NotNull String alias, @NotNull Arguments args) throws CommandException { - PickMeUp.instance().onReload(); + ((PickMeUp) plugin()).onReload(); messageSender().sendLocalizedMessage(sender, "reload.success"); PickMeUp.logger().info("PickMeUp reloaded!"); } diff --git a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java index 2dbde07..a950afc 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java +++ b/src/main/java/de/eldoria/pickmeup/listener/CarryListener.java @@ -13,31 +13,35 @@ import org.bukkit.Sound; import org.bukkit.entity.ArmorStand; import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.world.ChunkLoadEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.player.PlayerInteractAtEntityEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerToggleSneakEvent; -import org.bukkit.persistence.PersistentDataContainer; -import org.bukkit.persistence.PersistentDataType; import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitTask; import org.bukkit.util.Vector; import org.spigotmc.event.entity.EntityDismountEvent; -import java.util.*; +import java.util.Map; +import java.util.HashMap; +import java.util.Set; +import java.util.HashSet; +import java.util.UUID; +import java.util.Arrays; +import java.util.Optional; +import java.util.Objects; public class CarryListener implements Listener { private final Configuration config; private final ProtectionService protectionService; private final Plugin plugin; private final ThrowBarHandler throwBarHandler; - private final Set blocked = new HashSet<>(); + //private final Set blocked = new HashSet<>(); private final Map mountHandlers = new HashMap<>(); @@ -70,7 +74,7 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca if (!protectionService.canInteract(player, entity.getLocation())) return; - if (blocked.contains(entity.getUniqueId())) return; + //if (blocked.contains(entity.getUniqueId())) return; // If the player doesn't have the hand empty, leave if (Objects.requireNonNull(player.getEquipment()).getItemInMainHand().getType() != Material.AIR) return; @@ -123,7 +127,7 @@ private void entityCarryInteraction(Player player, Entity entity, Cancellable ca mountHandlers.putIfAbsent(player.getUniqueId(), new MountHandler(player)); MountHandler handler = mountHandlers.get(player.getUniqueId()); // Set the player state as SNEAK_MOUNT since the player just mounted an entity - handler.state = MountState.SNEAK_MOUNT; + handler.state = MountHandler.MountState.SNEAK_MOUNT; // Clear any extra data in case it was leftover throwBarHandler.getAndRemove(player); handler.cleanTasks(); @@ -145,14 +149,14 @@ public void onSneak(PlayerToggleSneakEvent event) { MountHandler mountHandler = mountHandlers.get(player.getUniqueId()); //If the player stopped sneaking and was SNEAK_MOUNT, change the state to WALKING - if (!event.isSneaking() && mountHandler.state == MountState.SNEAK_MOUNT) { - mountHandler.state = MountState.WALKING; + if (!event.isSneaking() && mountHandler.state == MountHandler.MountState.SNEAK_MOUNT) { + mountHandler.state = MountHandler.MountState.WALKING; return; } //If the player is begun sneaking with the WALKING state we start the throw delayedTask and set state to SNEAK_THROW - if (event.isSneaking() && mountHandler.state == MountState.WALKING) { - mountHandler.state = MountState.SNEAK_THROW; + if (event.isSneaking() && mountHandler.state == MountHandler.MountState.WALKING) { + mountHandler.state = MountHandler.MountState.SNEAK_THROW; mountHandler.throwTask = Bukkit.getScheduler().runTaskLater(plugin, () -> { @@ -165,7 +169,7 @@ public void onSneak(PlayerToggleSneakEvent event) { } //If the player stopped sneaking with the SNEAK_THROW state - if (!event.isSneaking() && mountHandler.state == MountState.SNEAK_THROW) { + if (!event.isSneaking() && mountHandler.state == MountHandler.MountState.SNEAK_THROW) { //If the player is not registered on the throwBarHandler then we simply dismount the entities if (!throwBarHandler.isRegistered(player)) { unmountAll(player); @@ -186,6 +190,13 @@ public void onSneak(PlayerToggleSneakEvent event) { } } + @EventHandler + public void onChunkLoad(ChunkLoadEvent event){ + Arrays.stream(event.getChunk().getEntities()) + .filter(MountHandler::isOffsetter) // Filter for offsetters + .forEach(Entity::remove); // Remove them + } + @EventHandler public void onEntityDismount(EntityDismountEvent event){ if(event.getDismounted() instanceof Player player) @@ -224,175 +235,5 @@ private void removePlayerData(Player player){ .ifPresent(MountHandler::dispose); throwBarHandler.getAndRemove(player); } - - private enum MountState { - SNEAK_MOUNT, WALKING, SNEAK_THROW - } - - private class MountHandler{ - public MountState state; - public BukkitTask throwTask; - - private final Player owner; - - private ArmorStand offsetter; - - public MountHandler(Player owner){ - this.owner = owner; - } - - public void cleanTasks(){ - if (throwTask != null && !throwTask.isCancelled()) throwTask.cancel(); - } - - public void dispose(){ - cleanTasks(); - - if (offsetter == null) return; - - for (Entity passenger : - offsetter.getPassengers()) { - offsetter.removePassenger(passenger); - } - offsetter.remove(); - offsetter = null; - } - - public boolean addPassenger(Entity passenger){ - Entity mountable = getMountable(); - - if(mountable.getUniqueId() == passenger.getUniqueId()) - return false; - - return mountable.addPassenger(passenger); - } - - public boolean removePassenger(Entity passenger){ - Entity mountable = getMountable(); - - boolean result = false; - - if(mountable.getUniqueId() != passenger.getUniqueId()) - result = mountable.removePassenger(passenger); - - // If there are no more passengers, dispose - if(mountable.getPassengers().isEmpty()) - dispose(); - - return result; - } - - public List getDirectPassengers(){ - return getMountable().getPassengers(); - } - - public List getAllPassengers(boolean filterOffsetters){ - return getAllPassengers(owner, filterOffsetters); - } - public int stackCount(boolean filterOffsetters){ - return stackCount(owner, filterOffsetters); - } - - private void setupOffsetter() { - if (offsetter != null && !offsetter.isDead()) - return; - - ArmorStand armorStand = owner.getWorld().spawn(owner.getLocation(), ArmorStand.class); - - armorStand.setInvulnerable(true); - armorStand.setInvisible(true); - //armorStand.setMarker(true); - armorStand.setSmall(true); - armorStand.setBasePlate(false); - //armorStand.setLeftArmPose(90); - //armorStand.setRightArmPose(90); - - - // Mark it as an offsetter so it can be easily identified for removal - DataContainerUtil.putValue(armorStand, PickMeUp.offsetterIdentifierKey(), PersistentDataType.BYTE, (byte) 1); - - - // Get the current passengers of the owner - List passengers = owner.getPassengers(); - - if (owner.addPassenger(armorStand)) { - offsetter = armorStand; - // Transfer any passengers from the owner to the offsetter - for (Entity passenger : passengers) - offsetter.addPassenger(passenger); - } else { - armorStand.remove(); - } - } - - private Entity getMountable(){ - setupOffsetter(); - - // If the offsetter is somehow null, default to the owner himself - return offsetter != null ? offsetter : owner; - } - - - public static boolean isOffsetter(Entity entity){ - return DataContainerUtil.get(entity, PickMeUp.offsetterIdentifierKey(), PersistentDataType.BYTE) - .map(b -> b == (byte) 0) - .orElse(false); - } - - public static List getDirectPassengers(Entity origin, boolean filterOffsetters){ - List passengers = origin.getPassengers(); - for (Entity passenger : passengers) { - if(isOffsetter(passenger)) { - passengers.addAll(passenger.getPassengers()); - if(filterOffsetters) - passengers.remove(passenger); - } - - } - - return passengers; - } - - public static List getAllPassengers(Entity origin, boolean filterOffsetters){ - List passengers = getPassengersRecursive(origin); - - if(filterOffsetters) - passengers = passengers.stream().filter(MountHandler::isOffsetter).toList(); - - return passengers; - } - - private static List getPassengersRecursive(Entity mountable){ - List passengers = mountable.getPassengers(); - for (Entity passenger : - passengers) { - passengers.addAll(getPassengersRecursive(passenger)); - } - - return passengers; - } - - public static int stackCount(Entity origin, boolean filterOffsetters){ - return stackCountRecursive(origin, filterOffsetters, 0); - } - - private static int stackCountRecursive(Entity mountable, boolean filterOffsetters, int level){ - List passengers = mountable.getPassengers(); - - int resultLevel = level; - if(!filterOffsetters || !isOffsetter(mountable)) - resultLevel += 1; - - for (Entity passenger : - passengers) { - int deeperLevel = stackCountRecursive(passenger, filterOffsetters, resultLevel); - - if(deeperLevel > resultLevel) - resultLevel = deeperLevel; - - } - - return resultLevel; - } - } } + diff --git a/src/main/java/de/eldoria/pickmeup/listener/MountHandler.java b/src/main/java/de/eldoria/pickmeup/listener/MountHandler.java new file mode 100644 index 0000000..45aaa44 --- /dev/null +++ b/src/main/java/de/eldoria/pickmeup/listener/MountHandler.java @@ -0,0 +1,187 @@ +package de.eldoria.pickmeup.listener; + +import de.eldoria.eldoutilities.utils.DataContainerUtil; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.ArmorStand; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.persistence.PersistentDataType; +import org.bukkit.scheduler.BukkitTask; + +import java.util.List; + +public class MountHandler { + @SuppressWarnings("deprecation") + private static final NamespacedKey offsetterIdentifierKey = new NamespacedKey("pickmeup", "IsOffsetter"); + + + public MountState state; + public BukkitTask throwTask; + + private final Player owner; + + private ArmorStand offsetter; + + public MountHandler(Player owner) { + this.owner = owner; + } + + public void cleanTasks() { + if (throwTask != null && !throwTask.isCancelled()) throwTask.cancel(); + } + + public void dispose() { + cleanTasks(); + + if (offsetter == null) return; + + for (Entity passenger : offsetter.getPassengers()) { + offsetter.removePassenger(passenger); + } + offsetter.remove(); + offsetter = null; + } + + public boolean addPassenger(Entity passenger) { + Entity mountable = getMountable(); + + if (mountable.getUniqueId() == passenger.getUniqueId()) + return false; + + return mountable.addPassenger(passenger); + } + + public boolean removePassenger(Entity passenger) { + Entity mountable = getMountable(); + + boolean result = false; + + if (mountable.getUniqueId() != passenger.getUniqueId()) { + result = mountable.removePassenger(passenger); + } + // If there are no more passengers, dispose + if (mountable.getPassengers().isEmpty()) dispose(); + + return result; + } + + public List getDirectPassengers() { + return getMountable().getPassengers(); + } + + public List getAllPassengers(boolean filterOffsetters) { + return getAllPassengers(owner, filterOffsetters); + } + + public int stackCount(boolean filterOffsetters) { + return stackCount(owner, filterOffsetters); + } + + private void setupOffsetter() { + if (offsetter != null && !offsetter.isDead()) + return; + + ArmorStand armorStand = owner.getWorld().spawn(owner.getLocation(), ArmorStand.class); + + armorStand.setInvulnerable(true); + armorStand.setInvisible(true); + //armorStand.setMarker(true); + armorStand.setSmall(true); + armorStand.setBasePlate(false); + //armorStand.setLeftArmPose(90); + //armorStand.setRightArmPose(90); + + + // Mark it as an offsetter so it can be easily identified for removal + DataContainerUtil.putValue(armorStand, offsetterIdentifierKey, PersistentDataType.BYTE, (byte) 1); + + + // Get the current passengers of the owner + List passengers = owner.getPassengers(); + + if (owner.addPassenger(armorStand)) { + offsetter = armorStand; + // Transfer any passengers from the owner to the offsetter + for (Entity passenger : passengers) { + offsetter.addPassenger(passenger); + } + } else { + armorStand.remove(); + } + } + + private Entity getMountable() { + setupOffsetter(); + + // If the offsetter is somehow null, default to the owner himself + return offsetter != null ? offsetter : owner; + } + + + public static boolean isOffsetter(Entity entity) { + return DataContainerUtil.get(entity, offsetterIdentifierKey, PersistentDataType.BYTE) + .map(b -> b == (byte) 0) + .orElse(false); + } + + public static List getDirectPassengers(Entity origin, boolean filterOffsetters) { + List passengers = origin.getPassengers(); + for (Entity passenger : passengers) { + if (isOffsetter(passenger)) { + passengers.addAll(passenger.getPassengers()); + if (filterOffsetters) + passengers.remove(passenger); + } + + } + + return passengers; + } + + public static List getAllPassengers(Entity origin, boolean filterOffsetters) { + List passengers = getPassengersRecursive(origin); + + if (filterOffsetters) { + passengers = passengers.stream().filter(MountHandler::isOffsetter).toList(); + } + + return passengers; + } + + private static List getPassengersRecursive(Entity mountable) { + List passengers = mountable.getPassengers(); + for (Entity passenger : passengers) { + passengers.addAll(getPassengersRecursive(passenger)); + } + + return passengers; + } + + public static int stackCount(Entity origin, boolean filterOffsetters) { + return stackCountRecursive(origin, filterOffsetters, 0); + } + + private static int stackCountRecursive(Entity mountable, boolean filterOffsetters, int level) { + List passengers = mountable.getPassengers(); + + int resultLevel = level; + if (!filterOffsetters || !isOffsetter(mountable)) { + resultLevel += 1; + } + + for (Entity passenger : passengers) { + int deeperLevel = stackCountRecursive(passenger, filterOffsetters, resultLevel); + + if (deeperLevel > resultLevel) { + resultLevel = deeperLevel; + } + + } + + return resultLevel; + } + + public enum MountState { + SNEAK_MOUNT, WALKING, SNEAK_THROW + } +} From 1e056836be9f1dd3663904c39d25f42ded0895d2 Mon Sep 17 00:00:00 2001 From: Gabo6480 Date: Wed, 14 Sep 2022 19:39:03 -0500 Subject: [PATCH 20/20] Updated NamespacedKey IsOffsetter causes an error, changed it to is-offsetter --- src/main/java/de/eldoria/pickmeup/listener/MountHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/eldoria/pickmeup/listener/MountHandler.java b/src/main/java/de/eldoria/pickmeup/listener/MountHandler.java index 45aaa44..6925fa4 100644 --- a/src/main/java/de/eldoria/pickmeup/listener/MountHandler.java +++ b/src/main/java/de/eldoria/pickmeup/listener/MountHandler.java @@ -12,7 +12,7 @@ public class MountHandler { @SuppressWarnings("deprecation") - private static final NamespacedKey offsetterIdentifierKey = new NamespacedKey("pickmeup", "IsOffsetter"); + private static final NamespacedKey offsetterIdentifierKey = new NamespacedKey("pickmeup", "is-offsetter"); public MountState state;