|
|
|
@ -65,43 +65,36 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onDefaultSound(@NotNull NPCSoundEvent event) {
|
|
|
|
|
BlacksmithVisuals.debug("Detected onDefaultSound event");
|
|
|
|
|
event.setCancelled(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onReforgeStart(@NotNull BlacksmithReforgeStartEvent event) {
|
|
|
|
|
BlacksmithVisuals.debug("Detected onReforgeStart event");
|
|
|
|
|
runWorkingAnimation(event, SoundIdentifier.REFORGING_WORKING, this.configurationManager.getBlacksmithAnimationData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onSalvageStart(@NotNull ScrapperSalvageStartEvent event) {
|
|
|
|
|
BlacksmithVisuals.debug("Detected onSalvageStart event");
|
|
|
|
|
runWorkingAnimation(event, SoundIdentifier.SALVAGING_WORKING, this.configurationManager.getScrapperAnimationData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onReforgeSuccess(@NotNull BlacksmithReforgeSucceedEvent event) {
|
|
|
|
|
BlacksmithVisuals.debug("Detected onReforgeSuccess event");
|
|
|
|
|
SoundHelper.playSound(event.getEntity(), this.configurationManager.getSoundData(SoundIdentifier.REFORGING_SUCCESS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onSalvageSuccess(@NotNull ScrapperSalvageSucceedEvent event) {
|
|
|
|
|
BlacksmithVisuals.debug("Detected onSalvageSuccess event");
|
|
|
|
|
SoundHelper.playSound(event.getEntity(), this.configurationManager.getSoundData(SoundIdentifier.SALVAGING_SUCCESS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onReforgeFail(@NotNull BlacksmithReforgeFailEvent event) {
|
|
|
|
|
BlacksmithVisuals.debug("Detected onReforgeFail event");
|
|
|
|
|
SoundHelper.playSound(event.getEntity(), this.configurationManager.getSoundData(SoundIdentifier.REFORGING_FAILURE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
|
public void onSalvageFail(@NotNull ScrapperSalvageFailEvent event) {
|
|
|
|
|
BlacksmithVisuals.debug("Detected onSalvageFail event");
|
|
|
|
|
SoundHelper.playSound(event.getEntity(), this.configurationManager.getSoundData(SoundIdentifier.SALVAGING_FAILURE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -114,25 +107,19 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
*/
|
|
|
|
|
private void runWorkingAnimation(@NotNull ActionStartEvent event, @NotNull SoundIdentifier soundIdentifier,
|
|
|
|
|
@NotNull AnimationData animationData) {
|
|
|
|
|
BlacksmithVisuals.debug("Preparing to run working animation");
|
|
|
|
|
BlacksmithVisuals instance = BlacksmithVisuals.getInstance();
|
|
|
|
|
BukkitScheduler scheduler = Bukkit.getScheduler();
|
|
|
|
|
NPC npc = event.getNpc();
|
|
|
|
|
|
|
|
|
|
NPCPosition npcPosition = NPCPosition.getFromMaterial(event.getCraftingStation());
|
|
|
|
|
BlacksmithVisuals.debug("Found NPC position: " + npcPosition);
|
|
|
|
|
long delay = moveToWorkingPosition(npc, event.getEntity(), npcPosition);
|
|
|
|
|
BlacksmithVisuals.debug("Calculated delay to: " + delay);
|
|
|
|
|
if (npc.hasTrait(LookClose.class)) {
|
|
|
|
|
BlacksmithVisuals.debug("Found LookClose trait");
|
|
|
|
|
LookClose trait = npc.getTraitNullable(LookClose.class);
|
|
|
|
|
this.oldLookRanges.put(npc.getUniqueId(), trait.getRange());
|
|
|
|
|
trait.setRange(0);
|
|
|
|
|
BlacksmithVisuals.debug("Edited LookClose range");
|
|
|
|
|
}
|
|
|
|
|
long finishTime = event.getActionDurationTicks() - (2 * delay);
|
|
|
|
|
|
|
|
|
|
BlacksmithVisuals.debug("Scheduling work animation and back movement");
|
|
|
|
|
scheduler.scheduleSyncDelayedTask(instance, () -> startWorkAnimation(event.getEntity(),
|
|
|
|
|
this.configurationManager.getSoundData(soundIdentifier), animationData, finishTime - 20, npcPosition), delay);
|
|
|
|
|
scheduler.scheduleSyncDelayedTask(instance, () -> moveBack(event.getNpc(), event.getEntity()), finishTime);
|
|
|
|
@ -146,13 +133,11 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
*/
|
|
|
|
|
private void moveBack(@NotNull NPC npc, @NotNull Entity entity) {
|
|
|
|
|
if (!npc.isSpawned()) {
|
|
|
|
|
BlacksmithVisuals.debug("NPC isn't spawned, and thus cannot be moved back");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NPCData npcData = BlacksmithVisuals.getInstance().getNpcDataManager().getData(npc.getUniqueId());
|
|
|
|
|
if (npcData == null) {
|
|
|
|
|
BlacksmithVisuals.debug("Unable to get NPC data for NPC");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -169,10 +154,11 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
if (oldRange != null) {
|
|
|
|
|
trait.setRange(oldRange);
|
|
|
|
|
}
|
|
|
|
|
BlacksmithVisuals.debug("Restored LookCLose range");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
moveNPC(npc, entity, targetLocation);
|
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(), () ->
|
|
|
|
|
entity.teleport(targetLocation), getWalkTime(entity.getLocation(), targetLocation));
|
|
|
|
|
npc.getNavigator().setTarget(targetLocation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -185,13 +171,11 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
*/
|
|
|
|
|
private long moveToWorkingPosition(@NotNull NPC npc, @NotNull Entity entity, @Nullable NPCPosition npcPosition) {
|
|
|
|
|
if (!npc.isSpawned() || npcPosition == null) {
|
|
|
|
|
BlacksmithVisuals.debug("The NPC isn't spawned, or its working position doesn't exist");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NPCData npcData = BlacksmithVisuals.getInstance().getNpcDataManager().getData(npc.getUniqueId());
|
|
|
|
|
if (npcData == null) {
|
|
|
|
|
BlacksmithVisuals.debug("NPC sata could not be retrieved");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -200,7 +184,6 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
targetLocation = npcData.positions().get(NPCPosition.WORKING_REPAIRABLE);
|
|
|
|
|
}
|
|
|
|
|
if (targetLocation == null) {
|
|
|
|
|
BlacksmithVisuals.debug("Working location not set for WORKING_REPAIRABLE");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -209,30 +192,18 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
npc.getName() + " is unreachable!");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
Location finalTargetLocation = targetLocation;
|
|
|
|
|
|
|
|
|
|
return moveNPC(npc, entity, targetLocation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Moves an NPC to a different location
|
|
|
|
|
*
|
|
|
|
|
* @param npc <p>The NPC to be moved</p>
|
|
|
|
|
* @param entity <p>The NPC's entity</p>
|
|
|
|
|
* @param finalTargetLocation <p>The location the NPC should be moved to</p>
|
|
|
|
|
* @return <p>The assumed time it will take the NPC to get to the position</p>
|
|
|
|
|
*/
|
|
|
|
|
private long moveNPC(@NotNull NPC npc, @NotNull Entity entity, @NotNull Location finalTargetLocation) {
|
|
|
|
|
// Move NPC using Citizens path-finding
|
|
|
|
|
BlacksmithVisuals.debug("Preparing rotation and walk to working location");
|
|
|
|
|
Location current = entity.getLocation().clone();
|
|
|
|
|
npc.teleport(current.setDirection(finalTargetLocation.toVector().subtract(current.toVector())), PlayerTeleportEvent.TeleportCause.PLUGIN);
|
|
|
|
|
npc.teleport(current.setDirection(targetLocation.toVector().subtract(current.toVector())), PlayerTeleportEvent.TeleportCause.PLUGIN);
|
|
|
|
|
|
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(), () -> npc.getNavigator().setTarget(finalTargetLocation), 2);
|
|
|
|
|
|
|
|
|
|
// Teleport the NPC tp get it in the exact final location
|
|
|
|
|
long walkTime = getWalkTime(entity.getLocation(), finalTargetLocation);
|
|
|
|
|
BlacksmithVisuals.debug("Queuing teleportation to working position");
|
|
|
|
|
long walkTime = getWalkTime(entity.getLocation(), targetLocation);
|
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(), () ->
|
|
|
|
|
entity.teleport(finalTargetLocation, PlayerTeleportEvent.TeleportCause.PLUGIN), walkTime);
|
|
|
|
|
entity.teleport(finalTargetLocation), walkTime);
|
|
|
|
|
return walkTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -248,15 +219,12 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
private void startWorkAnimation(@NotNull Entity entity, @NotNull SoundData soundData,
|
|
|
|
|
@NotNull AnimationData animationData, long durationTicks,
|
|
|
|
|
@Nullable NPCPosition npcPosition) {
|
|
|
|
|
BlacksmithVisuals.debug("Starting work animation");
|
|
|
|
|
BlacksmithVisuals instance = BlacksmithVisuals.getInstance();
|
|
|
|
|
BukkitScheduler scheduler = Bukkit.getScheduler();
|
|
|
|
|
|
|
|
|
|
BlacksmithVisuals.debug("Scheduling animation");
|
|
|
|
|
int playWorkSound = scheduler.scheduleSyncRepeatingTask(instance,
|
|
|
|
|
() -> animateNPC(entity, soundData, animationData, npcPosition), 20, animationData.animationDelay());
|
|
|
|
|
scheduler.scheduleSyncDelayedTask(instance, () -> scheduler.cancelTask(playWorkSound), durationTicks);
|
|
|
|
|
BlacksmithVisuals.debug("Stopped working animation");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -282,26 +250,21 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
*/
|
|
|
|
|
private void animateNPC(@NotNull Entity entity, @NotNull SoundData soundData, @NotNull AnimationData animationData,
|
|
|
|
|
@Nullable NPCPosition npcPosition) {
|
|
|
|
|
// Don't play disabled animations
|
|
|
|
|
if (!animationData.animateOffHand()) {
|
|
|
|
|
BlacksmithVisuals.debug("Offhand animation is disabled");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (random.nextInt(100) >= animationData.animationChance()) {
|
|
|
|
|
BlacksmithVisuals.debug("Animation skipped because of RNG");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BlacksmithVisuals.debug("Playing work sound");
|
|
|
|
|
SoundHelper.playSound(entity, soundData);
|
|
|
|
|
|
|
|
|
|
// Don't play disabled animations
|
|
|
|
|
if (!animationData.animateOffHand()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (soundData.offsetTicks() < 0) {
|
|
|
|
|
BlacksmithVisuals.debug("Delaying off-hand animation");
|
|
|
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(BlacksmithVisuals.getInstance(),
|
|
|
|
|
() -> animateOffhand(entity), -soundData.offsetTicks());
|
|
|
|
|
} else {
|
|
|
|
|
BlacksmithVisuals.debug("Animating off-hand");
|
|
|
|
|
animateOffhand(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -309,7 +272,6 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
npcPosition = NPCPosition.WORKING_REPAIRABLE;
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0; i < random.nextInt(5) + 1; i++) {
|
|
|
|
|
BlacksmithVisuals.debug("Spawning particle");
|
|
|
|
|
spawnSparkParticle(entity, npcPosition);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -321,7 +283,6 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
* @param npcPosition <p>The npc potion to spawn a particle for</p>
|
|
|
|
|
*/
|
|
|
|
|
private void spawnSparkParticle(@NotNull Entity entity, @NotNull NPCPosition npcPosition) {
|
|
|
|
|
BlacksmithVisuals.debug("Preparing particle spawning");
|
|
|
|
|
double randomX = this.random.nextDouble(-1, 1);
|
|
|
|
|
double randomY = this.random.nextDouble(0.1, 1);
|
|
|
|
|
double randomZ = this.random.nextDouble(-1, 1);
|
|
|
|
@ -332,10 +293,8 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
particle = Particle.FLAME;
|
|
|
|
|
}
|
|
|
|
|
Location spawnLocation = entity.getLocation().clone().getBlock().getRelative(entity.getFacing()).getLocation().add(0.5, 0, 0.5);
|
|
|
|
|
BlacksmithVisuals.debug("Calculated particle location to " + spawnLocation);
|
|
|
|
|
ParticleConfig particleConfig = new ParticleConfig(ParticleMode.SINGLE, particle, 0, 0, 1.1, randomX, randomY, randomZ, 0.1);
|
|
|
|
|
ParticleHelper.spawnParticle(entity.getWorld(), spawnLocation, particleConfig, 1);
|
|
|
|
|
BlacksmithVisuals.debug("Spawned particle");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -344,7 +303,6 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
* @param entity <p>The entity to animate</p>
|
|
|
|
|
*/
|
|
|
|
|
private void animateOffhand(@NotNull Entity entity) {
|
|
|
|
|
BlacksmithVisuals.debug("Preparing off-hand animation");
|
|
|
|
|
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
|
|
|
|
|
PacketContainer packet = manager.createPacket(PacketType.Play.Server.ANIMATION);
|
|
|
|
|
packet.getIntegers().write(0, entity.getEntityId());
|
|
|
|
@ -352,11 +310,9 @@ public class BlacksmithListener implements Listener {
|
|
|
|
|
|
|
|
|
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
|
|
|
|
if (player.getWorld().equals(entity.getWorld())) {
|
|
|
|
|
BlacksmithVisuals.debug("Sending animation packet to " + player);
|
|
|
|
|
manager.sendServerPacket(player, packet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
BlacksmithVisuals.debug("Finished sending animation packets");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|