Adds particle effects to NPCs' crafting stations
All checks were successful
KnarCraft/BlacksmithVisuals/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-08-05 15:11:40 +02:00
parent df4512864f
commit 050491d7ba
2 changed files with 82 additions and 6 deletions

41
pom.xml
View File

@ -30,13 +30,40 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<relocations>
<relocation>
<pattern>net.knarcraft.knarlib</pattern>
<shadedPattern>net.knarcraft.blacksmithvisuals.lib.knarlib</shadedPattern>
</relocation>
<relocation>
<pattern>org.jetbrains.annotations</pattern>
<shadedPattern>net.knarcraft.blacksmithvisuals.lib.annotations</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>net.knarcraft:knarlib</artifact>
<includes>
<include>net/knarcraft/knarlib/**</include>
</includes>
</filter>
<filter>
<artifact>org.jetbrains:annotations</artifact>
<includes>
<include>org/jetbrains/annotations/**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
@ -100,5 +127,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId>
<version>1.2.7</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -22,8 +22,12 @@ import net.knarcraft.blacksmithvisuals.manager.ConfigurationManager;
import net.knarcraft.blacksmithvisuals.property.NPCPosition;
import net.knarcraft.blacksmithvisuals.property.SoundIdentifier;
import net.knarcraft.blacksmithvisuals.util.SoundHelper;
import net.knarcraft.knarlib.particle.ParticleConfig;
import net.knarcraft.knarlib.particle.ParticleMode;
import net.knarcraft.knarlib.util.ParticleHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -107,7 +111,8 @@ public class BlacksmithListener implements Listener {
BukkitScheduler scheduler = Bukkit.getScheduler();
NPC npc = event.getNpc();
long delay = moveToWorkingPosition(npc, NPCPosition.getFromMaterial(event.getCraftingStation()));
NPCPosition npcPosition = NPCPosition.getFromMaterial(event.getCraftingStation());
long delay = moveToWorkingPosition(npc, npcPosition);
if (npc.hasTrait(LookClose.class)) {
LookClose trait = npc.getTraitNullable(LookClose.class);
this.oldLookRanges.put(npc.getUniqueId(), trait.getRange());
@ -116,7 +121,7 @@ public class BlacksmithListener implements Listener {
long finishTime = event.getActionDurationTicks() - (2 * delay);
scheduler.scheduleSyncDelayedTask(instance, () -> startWorkAnimation(npc.getEntity(),
this.configurationManager.getSoundData(soundIdentifier), animationData, finishTime - 20), delay);
this.configurationManager.getSoundData(soundIdentifier), animationData, finishTime - 20, npcPosition), delay);
scheduler.scheduleSyncDelayedTask(instance, () -> moveBack(event.getNpc()), finishTime);
}
@ -207,14 +212,16 @@ public class BlacksmithListener implements Listener {
* @param soundData <p>The sound data for the sound to play</p>
* @param animationData <p>The animation data for the animation to play</p>
* @param durationTicks <p>The duration of the work animation</p>
* @param npcPosition <p>The npc position to animate</p>
*/
private void startWorkAnimation(@NotNull Entity entity, @NotNull SoundData soundData,
@NotNull AnimationData animationData, long durationTicks) {
@NotNull AnimationData animationData, long durationTicks,
@Nullable NPCPosition npcPosition) {
BlacksmithVisuals instance = BlacksmithVisuals.getInstance();
BukkitScheduler scheduler = Bukkit.getScheduler();
int playWorkSound = scheduler.scheduleSyncRepeatingTask(instance,
() -> animateNPC(entity, soundData, animationData), 20, animationData.animationDelay());
() -> animateNPC(entity, soundData, animationData, npcPosition), 20, animationData.animationDelay());
scheduler.scheduleSyncDelayedTask(instance, () -> scheduler.cancelTask(playWorkSound), durationTicks);
}
@ -237,8 +244,10 @@ public class BlacksmithListener implements Listener {
* @param entity <p>The entity to animate and play the sound at</p>
* @param soundData <p>The sound to be played</p>
* @param animationData <p>The animation to be played</p>
* @param npcPosition <p>The npc position to animate</p>
*/
private void animateNPC(@NotNull Entity entity, @NotNull SoundData soundData, @NotNull AnimationData animationData) {
private void animateNPC(@NotNull Entity entity, @NotNull SoundData soundData, @NotNull AnimationData animationData,
@Nullable NPCPosition npcPosition) {
if (random.nextInt(100) >= animationData.animationChance()) {
return;
}
@ -256,6 +265,34 @@ public class BlacksmithListener implements Listener {
} else {
animateOffhand(entity);
}
if (npcPosition == null) {
npcPosition = NPCPosition.WORKING_REPAIRABLE;
}
for (int i = 0; i < random.nextInt(5) + 1; i++) {
spawnSparkParticle(entity, npcPosition);
}
}
/**
* Spawns a spark particle at the given NPC's work station
*
* @param entity <p>The entity whose working station should produce a spark</p>
* @param npcPosition <p>The npc potion to spawn a particle for</p>
*/
private void spawnSparkParticle(@NotNull Entity entity, @NotNull NPCPosition npcPosition) {
double randomX = this.random.nextDouble(-1, 1);
double randomY = this.random.nextDouble(0.1, 1);
double randomZ = this.random.nextDouble(-1, 1);
Particle particle;
if (npcPosition == NPCPosition.WORKING_CRAFTING) {
particle = Particle.ELECTRIC_SPARK;
} else {
particle = Particle.FLAME;
}
Location spawnLocation = entity.getLocation().clone().getBlock().getRelative(entity.getFacing()).getLocation().add(0.5, 0, 0.5);
ParticleConfig particleConfig = new ParticleConfig(ParticleMode.SINGLE, particle, 0, 0, 1.1, randomX, randomY, randomZ, 0.1);
ParticleHelper.spawnParticle(entity.getWorld(), spawnLocation, particleConfig, 1);
}
/**