Refactoring, add debug for shop trait onRemove(), add at argument for PlayerAnimation waypoint trigger

This commit is contained in:
fullwall 2023-01-31 22:50:25 +08:00
parent c258ad1b9a
commit 3b00f7718b
8 changed files with 75 additions and 115 deletions

View File

@ -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);

View File

@ -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<PlayerAnimation> animations;
private final List<PlayerAnimation> animations;
@Persist
private final Location at;
public AnimationTrigger() {
}
public AnimationTrigger(Collection<PlayerAnimation> collection) {
public AnimationTrigger(Collection<PlayerAnimation> 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);

View File

@ -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<PlayerAnimation> 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);

View File

@ -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);
}

View File

@ -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}.<br>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}.<br>[[at (x:y:z:world)]] optionally set a location to perform the animation at.<br>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.

View File

@ -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;

View File

@ -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;

View File

@ -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: