Initial commit

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

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 ]