Initial commit

This commit is contained in:
Kristian Knarvik 2024-07-29 12:27:14 +02:00
commit 966eed910a
6 changed files with 289 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target/
/BlacksmithVisuals.iml
/.idea/

4
README.md Normal file
View File

@ -0,0 +1,4 @@
# Blacksmith Visuals
This plugin adds additional visual and audial details to blacksmiths while they are working, so it's easier to see if
they are working or not. It is recommended to put a mace or other hammer-like item in the NPC's off-hand for full effect.

104
pom.xml Normal file
View File

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.knarcraft</groupId>
<artifactId>BlacksmithVisuals</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>BlacksmithVisuals</name>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
<repository>
<id>knarcraft-repo</id>
<url>https://git.knarcraft.net/api/packages/EpicKnarvik97/maven</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib</artifactId>
<version>5.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>blacksmith</artifactId>
<version>1.1.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.citizensnpcs</groupId>
<artifactId>citizens-main</artifactId>
<version>2.0.33-SNAPSHOT</version>
<type>jar</type>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,148 @@
package net.knarcraft.blacksmithvisuals;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.event.ActionStartEvent;
import net.knarcraft.blacksmith.event.BlacksmithReforgeFailEvent;
import net.knarcraft.blacksmith.event.BlacksmithReforgeStartEvent;
import net.knarcraft.blacksmith.event.BlacksmithReforgeSucceedEvent;
import net.knarcraft.blacksmith.event.NPCSoundEvent;
import net.knarcraft.blacksmith.event.ScrapperSalvageFailEvent;
import net.knarcraft.blacksmith.event.ScrapperSalvageStartEvent;
import net.knarcraft.blacksmith.event.ScrapperSalvageSucceedEvent;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Random;
/**
* A listener for blacksmith-related events
*/
public class BlacksmithListener implements Listener {
private static final Random random = new Random();
@EventHandler
public void onReforgeStart(@NotNull BlacksmithReforgeStartEvent event) {
onActionStart(event);
}
@EventHandler
public void onSalvageStart(@NotNull ScrapperSalvageStartEvent event) {
onActionStart(event);
}
@EventHandler
public void onDefaultSound(@NotNull NPCSoundEvent event) {
event.setCancelled(true);
}
@EventHandler
public void onReforgeSuccess(@NotNull BlacksmithReforgeSucceedEvent event) {
playSuccessSound(event.getNpc().getEntity());
}
@EventHandler
public void onSalvageSuccess(@NotNull ScrapperSalvageSucceedEvent event) {
playSuccessSound(event.getNpc().getEntity());
}
@EventHandler
public void onReforgeFail(@NotNull BlacksmithReforgeFailEvent event) {
playFailSound(event.getNpc().getEntity());
}
@EventHandler
public void onSalvageFail(@NotNull ScrapperSalvageFailEvent event) {
playFailSound(event.getNpc().getEntity());
}
/**
* A method performing actions required when a blacksmith or scrapper action starts
*
* @param event <p>The event that's starting</p>
*/
private void onActionStart(@NotNull ActionStartEvent event) {
BukkitScheduler scheduler = Bukkit.getScheduler();
int playWorkSound = scheduler.scheduleSyncRepeatingTask(BlacksmithPlugin.getInstance(),
() -> displayWorkAnimation(event), 20, 5);
scheduler.scheduleSyncDelayedTask(BlacksmithPlugin.getInstance(), () -> scheduler.cancelTask(playWorkSound),
event.getActionDurationTicks());
}
/**
* Displays the animation of a working NPC
*
* @param event <p>The action start event to display for</p>
*/
private void displayWorkAnimation(@NotNull ActionStartEvent event) {
if (random.nextInt(100) >= 20) {
return;
}
List<Entity> nearby = event.getNpc().getEntity().getNearbyEntities(10, 10, 5);
nearby.removeIf((entity) -> !(entity instanceof Player));
this.playWorkSound(event.getNpc().getEntity());
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
PacketContainer packet = manager.createPacket(PacketType.Play.Server.ANIMATION);
packet.getIntegers().write(0, event.getNpc().getEntity().getEntityId());
packet.getIntegers().write(1, 3);
manager.sendServerPacket(event.getPlayer(), packet);
}
/**
* Plays the fail action sound
*
* @param entity <p>The entity to play the sound at</p>
*/
private void playFailSound(@NotNull Entity entity) {
playSound(entity, Sound.ENTITY_VILLAGER_NO);
}
/**
* Plays the succeed action sound
*
* @param entity <p>The entity to play the sound at</p>
*/
private void playSuccessSound(@NotNull Entity entity) {
playSound(entity, Sound.BLOCK_ANVIL_USE);
}
/**
* Plays a npc sound using a cancellable event
*
* @param entity <p>The entity that should play the sound</p>
*/
private void playWorkSound(@NotNull Entity entity) {
playSound(entity, Sound.ITEM_ARMOR_EQUIP_NETHERITE);
}
/**
* Plays a npc sound using a cancellable event
*
* @param entity <p>The entity that should play the sound</p>
*/
private void playSound(@NotNull Entity entity, Sound sound) {
World world = entity.getLocation().getWorld();
if (world == null) {
return;
}
world.playSound(entity, sound, SoundCategory.AMBIENT, 0.5f, 1.0f);
}
}

View File

@ -0,0 +1,25 @@
package net.knarcraft.blacksmithvisuals;
import org.bukkit.plugin.java.JavaPlugin;
/**
* The blacksmith visual main class
*/
@SuppressWarnings("unused")
public final class BlacksmithVisuals extends JavaPlugin {
@Override
public void onEnable() {
// Plugin startup logic
getServer().getPluginManager().registerEvents(new BlacksmithListener(), this);
//TODO: Allow customization of sounds, volumes and pitches
// Allow setting an idle position and a working position, and move the NPC to the right position at the right time.
// Use the distance between the two locations to decide how much sooner before the success/fail sounds are played the NPC should start walking
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
}

View File

@ -0,0 +1,5 @@
name: BlacksmithVisuals
version: '${project.version}'
main: net.knarcraft.blacksmithvisuals.BlacksmithVisuals
api-version: 1.20
depend: [ ProtocolLib ]