From 3b00f7718b0874aa55530b8c9bdff633063c986f Mon Sep 17 00:00:00 2001 From: fullwall Date: Tue, 31 Jan 2023 22:50:25 +0800 Subject: [PATCH] Refactoring, add debug for shop trait onRemove(), add at argument for PlayerAnimation waypoint trigger --- .../net/citizensnpcs/trait/ShopTrait.java | 7 ++-- .../waypoint/triggers/AnimationTrigger.java | 14 ++++--- .../triggers/AnimationTriggerPrompt.java | 17 +++++++- .../citizensnpcs/util/PlayerAnimation.java | 39 +++++++++++++++++-- .../src/main/resources/messages_en.properties | 2 +- .../v1_17_R1/util/PlayerAnimationImpl.java | 39 ++----------------- .../v1_18_R2/util/PlayerAnimationImpl.java | 39 ++----------------- .../v1_19_R2/util/PlayerAnimationImpl.java | 33 +--------------- 8 files changed, 75 insertions(+), 115 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java index ec34a89a5..d97bee440 100644 --- a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java @@ -78,20 +78,21 @@ public class ShopTrait extends Trait { } public NPCShop getDefaultShop() { - return shops.npcShops.computeIfAbsent(npc.getUniqueId().toString(), (s) -> new NPCShop(s)); + return shops.npcShops.computeIfAbsent(npc.getUniqueId().toString(), NPCShop::new); } public NPCShop getShop(String name) { - return shops.globalShops.computeIfAbsent(name, (s) -> new NPCShop(s)); + return shops.globalShops.computeIfAbsent(name, NPCShop::new); } @Override public void onRemove() { + Messaging.debug("Removing", npc, "default shop due to onRemove"); shops.deleteShop(getDefaultShop()); } public void onRightClick(Player player) { - if (rightClickShop == null) + if (rightClickShop == null || rightClickShop.isEmpty()) return; NPCShop shop = shops.globalShops.getOrDefault(rightClickShop, getDefaultShop()); shop.display(player); diff --git a/main/src/main/java/net/citizensnpcs/trait/waypoint/triggers/AnimationTrigger.java b/main/src/main/java/net/citizensnpcs/trait/waypoint/triggers/AnimationTrigger.java index 492f2c355..7499b310e 100644 --- a/main/src/main/java/net/citizensnpcs/trait/waypoint/triggers/AnimationTrigger.java +++ b/main/src/main/java/net/citizensnpcs/trait/waypoint/triggers/AnimationTrigger.java @@ -6,6 +6,7 @@ import java.util.List; import org.bukkit.Location; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import com.google.common.base.Joiner; import com.google.common.collect.Lists; @@ -16,13 +17,13 @@ import net.citizensnpcs.util.PlayerAnimation; public class AnimationTrigger implements WaypointTrigger { @Persist(required = true) - private List animations; + private final List animations; + @Persist + private final Location at; - public AnimationTrigger() { - } - - public AnimationTrigger(Collection collection) { + public AnimationTrigger(Collection collection, Location loc) { animations = Lists.newArrayList(collection); + at = loc; } @Override @@ -34,6 +35,9 @@ public class AnimationTrigger implements WaypointTrigger { public void onWaypointReached(NPC npc, Location waypoint) { if (npc.getEntity().getType() != EntityType.PLAYER) return; + if (at != null) { + npc.teleport(at, TeleportCause.PLUGIN); + } Player player = (Player) npc.getEntity(); for (PlayerAnimation animation : animations) { animation.play(player); diff --git a/main/src/main/java/net/citizensnpcs/trait/waypoint/triggers/AnimationTriggerPrompt.java b/main/src/main/java/net/citizensnpcs/trait/waypoint/triggers/AnimationTriggerPrompt.java index 054012999..2d26cd717 100644 --- a/main/src/main/java/net/citizensnpcs/trait/waypoint/triggers/AnimationTriggerPrompt.java +++ b/main/src/main/java/net/citizensnpcs/trait/waypoint/triggers/AnimationTriggerPrompt.java @@ -2,14 +2,18 @@ package net.citizensnpcs.trait.waypoint.triggers; import java.util.List; +import org.bukkit.Location; import org.bukkit.command.CommandSender; import org.bukkit.conversations.ConversationContext; import org.bukkit.conversations.Prompt; import org.bukkit.conversations.StringPrompt; +import org.bukkit.entity.Player; import com.google.common.base.Joiner; import com.google.common.collect.Lists; +import net.citizensnpcs.api.command.CommandContext; +import net.citizensnpcs.api.command.exception.CommandException; import net.citizensnpcs.api.util.Messaging; import net.citizensnpcs.util.Messages; import net.citizensnpcs.util.PlayerAnimation; @@ -17,14 +21,25 @@ import net.citizensnpcs.util.Util; public class AnimationTriggerPrompt extends StringPrompt implements WaypointTriggerPrompt { private final List animations = Lists.newArrayList(); + private Location at; @Override public Prompt acceptInput(ConversationContext context, String input) { if (input.equalsIgnoreCase("back")) { return (Prompt) context.getSessionData("previous"); } + if (input.startsWith("at ")) { + try { + at = CommandContext.parseLocation( + context.getForWhom() instanceof Player ? ((Player) context.getForWhom()).getLocation() : null, + input.replaceFirst("at ", "")); + } catch (CommandException e) { + Messaging.send((CommandSender) context.getForWhom(), e.getMessage()); + } + return this; + } if (input.equalsIgnoreCase("finish")) { - context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, new AnimationTrigger(animations)); + context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, new AnimationTrigger(animations, at)); return (Prompt) context.getSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY); } PlayerAnimation animation = Util.matchEnum(PlayerAnimation.values(), input); diff --git a/main/src/main/java/net/citizensnpcs/util/PlayerAnimation.java b/main/src/main/java/net/citizensnpcs/util/PlayerAnimation.java index 1de601acb..3b6d74ad9 100644 --- a/main/src/main/java/net/citizensnpcs/util/PlayerAnimation.java +++ b/main/src/main/java/net/citizensnpcs/util/PlayerAnimation.java @@ -1,5 +1,6 @@ package net.citizensnpcs.util; +import org.bukkit.Bukkit; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.metadata.FixedMetadataValue; @@ -83,15 +84,45 @@ public enum PlayerAnimation { } else if (this == SLEEP) { if (player instanceof NPCHolder) { ((NPCHolder) player).getNPC().getOrAddTrait(SleepTrait.class).setSleeping(player.getLocation()); - return; + } else { + NMS.sleep(player, true); } - NMS.sleep(player, true); + return; } else if (this == STOP_SLEEPING) { if (player instanceof NPCHolder) { ((NPCHolder) player).getNPC().getOrAddTrait(SleepTrait.class).setSleeping(null); - return; + } else { + NMS.sleep(player, false); } - NMS.sleep(player, false); + return; + } else if (this == START_USE_MAINHAND_ITEM || this == START_USE_OFFHAND_ITEM) { + NMS.playAnimation(this, player, radius); + if (player.hasMetadata("citizens-using-item-remaining-ticks")) { + int remainingTicks = player.getMetadata("citizens-using-item-remaining-ticks").get(0).asInt(); + new BukkitRunnable() { + @Override + public void run() { + if (!NMS.isValid(player)) { + cancel(); + return; + } + NMS.playAnimation(PlayerAnimation.this, player, radius); + if (!player.hasMetadata("citizens-using-item-id")) { + player.setMetadata("citizens-using-item-id", + new FixedMetadataValue(CitizensAPI.getPlugin(), getTaskId())); + } + } + }.runTaskTimer(CitizensAPI.getPlugin(), Math.max(0, remainingTicks + 1), + Math.max(1, remainingTicks + 1)); + } + return; + } else if (this == STOP_USE_ITEM) { + NMS.playAnimation(this, player, radius); + if (player.hasMetadata("citizens-using-item-id")) { + Bukkit.getScheduler().cancelTask(player.getMetadata("citizens-using-item-id").get(0).asInt()); + player.removeMetadata("citizens-using-item-id", CitizensAPI.getPlugin()); + } + return; } NMS.playAnimation(this, player, radius); } diff --git a/main/src/main/resources/messages_en.properties b/main/src/main/resources/messages_en.properties index 56e10e4bd..41a1712ff 100644 --- a/main/src/main/resources/messages_en.properties +++ b/main/src/main/resources/messages_en.properties @@ -391,7 +391,7 @@ citizens.editors.waypoints.triggers.add.invalid-trigger=Couldn''t create a trigg citizens.editors.waypoints.triggers.add.prompt=Enter in a trigger name to add or type [[back]] to return to the edit prompt. Valid trigger names are {0}. citizens.editors.waypoints.triggers.animation.added=Animation [[{0}]] added. citizens.editors.waypoints.triggers.list=Current triggers are:{0} -citizens.editors.waypoints.triggers.animation.prompt=Enter in animations to perform - valid animations are {0}.
Type in [[finish]] to finish the animation trigger or [[back]] to return to the previous prompt. +citizens.editors.waypoints.triggers.animation.prompt=Enter in animations to perform - valid animations are {0}.
[[at (x:y:z:world)]] optionally set a location to perform the animation at.
Type in [[finish]] to finish the animation trigger or [[back]] to return to the previous prompt. citizens.editors.waypoints.triggers.animation.invalid-animation=Invalid animation [[{0}]]. Valid animations are {1}. citizens.editors.waypoints.triggers.chat.invalid-radius=The radius must be a number. citizens.editors.waypoints.triggers.chat.missing-radius=No radius supplied. diff --git a/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/PlayerAnimationImpl.java b/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/PlayerAnimationImpl.java index 9eb7e92d4..74c4ba18a 100644 --- a/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/PlayerAnimationImpl.java +++ b/v1_17_R1/src/main/java/net/citizensnpcs/nms/v1_17_R1/util/PlayerAnimationImpl.java @@ -2,10 +2,8 @@ package net.citizensnpcs.nms.v1_17_R1.util; import java.util.EnumMap; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.metadata.FixedMetadataValue; -import org.bukkit.scheduler.BukkitRunnable; import com.google.common.collect.Maps; @@ -39,41 +37,17 @@ public class PlayerAnimationImpl { break; case START_USE_MAINHAND_ITEM: player.startUsingItem(InteractionHand.MAIN_HAND); - new BukkitRunnable() { - @Override - public void run() { - player.startUsingItem(InteractionHand.MAIN_HAND); - sendPacketNearby( - new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), - player, radius); - if (!player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - player.getBukkitEntity().setMetadata("citizens-using-item-id", - new FixedMetadataValue(CitizensAPI.getPlugin(), getTaskId())); - } - } - }.runTaskTimer(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks() - 1, - player.getUseItemRemainingTicks() - 1); sendPacketNearby(new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), player, radius); + player.getBukkitEntity().setMetadata("citizens-using-item-remaining-ticks", + new FixedMetadataValue(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks())); break; case START_USE_OFFHAND_ITEM: player.startUsingItem(InteractionHand.OFF_HAND); - new BukkitRunnable() { - @Override - public void run() { - player.startUsingItem(InteractionHand.OFF_HAND); - sendPacketNearby( - new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), - player, radius); - if (!player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - player.getBukkitEntity().setMetadata("citizens-using-item-id", - new FixedMetadataValue(CitizensAPI.getPlugin(), getTaskId())); - } - } - }.runTaskTimer(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks() - 1, - player.getUseItemRemainingTicks() - 1); sendPacketNearby(new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), player, radius); + player.getBukkitEntity().setMetadata("citizens-using-item-remaining-ticks", + new FixedMetadataValue(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks())); break; case STOP_SNEAKING: player.setPose(Pose.STANDING); @@ -82,11 +56,6 @@ public class PlayerAnimationImpl { break; case STOP_USE_ITEM: player.stopUsingItem(); - if (player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - Bukkit.getScheduler() - .cancelTask(player.getBukkitEntity().getMetadata("citizens-using-item-id").get(0).asInt()); - player.getBukkitEntity().removeMetadata("citizens-using-item-id", CitizensAPI.getPlugin()); - } sendPacketNearby(new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), player, radius); break; diff --git a/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/PlayerAnimationImpl.java b/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/PlayerAnimationImpl.java index bbb21e2bc..a1a04d1a2 100644 --- a/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/PlayerAnimationImpl.java +++ b/v1_18_R2/src/main/java/net/citizensnpcs/nms/v1_18_R2/util/PlayerAnimationImpl.java @@ -2,10 +2,8 @@ package net.citizensnpcs.nms.v1_18_R2.util; import java.util.Map; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.metadata.FixedMetadataValue; -import org.bukkit.scheduler.BukkitRunnable; import com.google.common.collect.Maps; @@ -39,41 +37,17 @@ public class PlayerAnimationImpl { break; case START_USE_MAINHAND_ITEM: player.startUsingItem(InteractionHand.MAIN_HAND); - new BukkitRunnable() { - @Override - public void run() { - player.startUsingItem(InteractionHand.MAIN_HAND); - sendPacketNearby( - new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), - player, radius); - if (!player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - player.getBukkitEntity().setMetadata("citizens-using-item-id", - new FixedMetadataValue(CitizensAPI.getPlugin(), getTaskId())); - } - } - }.runTaskTimer(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks() - 1, - player.getUseItemRemainingTicks() - 1); sendPacketNearby(new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), player, radius); + player.getBukkitEntity().setMetadata("citizens-using-item-remaining-ticks", + new FixedMetadataValue(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks())); break; case START_USE_OFFHAND_ITEM: player.startUsingItem(InteractionHand.OFF_HAND); - new BukkitRunnable() { - @Override - public void run() { - player.startUsingItem(InteractionHand.OFF_HAND); - sendPacketNearby( - new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), - player, radius); - if (!player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - player.getBukkitEntity().setMetadata("citizens-using-item-id", - new FixedMetadataValue(CitizensAPI.getPlugin(), getTaskId())); - } - } - }.runTaskTimer(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks() - 1, - player.getUseItemRemainingTicks() - 1); sendPacketNearby(new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), player, radius); + player.getBukkitEntity().setMetadata("citizens-using-item-remaining-ticks", + new FixedMetadataValue(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks())); break; case STOP_SNEAKING: player.setPose(Pose.STANDING); @@ -82,11 +56,6 @@ public class PlayerAnimationImpl { break; case STOP_USE_ITEM: player.stopUsingItem(); - if (player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - Bukkit.getScheduler() - .cancelTask(player.getBukkitEntity().getMetadata("citizens-using-item-id").get(0).asInt()); - player.getBukkitEntity().removeMetadata("citizens-using-item-id", CitizensAPI.getPlugin()); - } sendPacketNearby(new ClientboundSetEntityDataPacket(player.getId(), player.getEntityData(), true), player, radius); break; diff --git a/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/PlayerAnimationImpl.java b/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/PlayerAnimationImpl.java index 31636dd08..38ef6682f 100644 --- a/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/PlayerAnimationImpl.java +++ b/v1_19_R2/src/main/java/net/citizensnpcs/nms/v1_19_R2/util/PlayerAnimationImpl.java @@ -2,10 +2,8 @@ package net.citizensnpcs.nms.v1_19_R2.util; import java.util.Map; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.metadata.FixedMetadataValue; -import org.bukkit.scheduler.BukkitRunnable; import com.google.common.collect.Maps; @@ -38,35 +36,13 @@ public class PlayerAnimationImpl { break; case START_USE_MAINHAND_ITEM: player.startUsingItem(InteractionHand.MAIN_HAND); - new BukkitRunnable() { - @Override - public void run() { - player.startUsingItem(InteractionHand.MAIN_HAND); - sendEntityData(radius, player); - if (!player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - player.getBukkitEntity().setMetadata("citizens-using-item-id", - new FixedMetadataValue(CitizensAPI.getPlugin(), getTaskId())); - } - } - }.runTaskTimer(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks() - 1, - player.getUseItemRemainingTicks() - 1); sendEntityData(radius, player); break; case START_USE_OFFHAND_ITEM: player.startUsingItem(InteractionHand.OFF_HAND); - new BukkitRunnable() { - @Override - public void run() { - player.startUsingItem(InteractionHand.OFF_HAND); - sendEntityData(radius, player); - if (!player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - player.getBukkitEntity().setMetadata("citizens-using-item-id", - new FixedMetadataValue(CitizensAPI.getPlugin(), getTaskId())); - } - } - }.runTaskTimer(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks() - 1, - player.getUseItemRemainingTicks() - 1); sendEntityData(radius, player); + player.getBukkitEntity().setMetadata("citizens-using-item-remaining-ticks", + new FixedMetadataValue(CitizensAPI.getPlugin(), player.getUseItemRemainingTicks())); break; case STOP_SNEAKING: player.setPose(Pose.STANDING); @@ -74,11 +50,6 @@ public class PlayerAnimationImpl { break; case STOP_USE_ITEM: player.stopUsingItem(); - if (player.getBukkitEntity().hasMetadata("citizens-using-item-id")) { - Bukkit.getScheduler() - .cancelTask(player.getBukkitEntity().getMetadata("citizens-using-item-id").get(0).asInt()); - player.getBukkitEntity().removeMetadata("citizens-using-item-id", CitizensAPI.getPlugin()); - } sendEntityData(radius, player); break; default: