Tweak /npc sound command to allow arbitrary sounds (as long as they are registered with NMS)

This commit is contained in:
fullwall 2023-06-19 20:55:16 +08:00
parent ad023c3bd5
commit 6d5f881b91
15 changed files with 42 additions and 36 deletions

View File

@ -19,6 +19,7 @@ import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
@ -2884,8 +2885,8 @@ public class NPCCommands {
max = 1,
permission = "citizens.npc.sound")
@Requirements(selected = true, ownership = true, livingEntity = true)
public void sound(CommandContext args, CommandSender sender, NPC npc, @Flag("death") String death,
@Flag("ambient") String ambient, @Flag("hurt") String hurt) throws CommandException {
public void sound(CommandContext args, CommandSender sender, NPC npc, @Flag("death") Sound death,
@Flag("ambient") Sound ambient, @Flag("hurt") Sound hurt) throws CommandException {
String ambientSound = npc.data().get(NPC.Metadata.AMBIENT_SOUND);
String deathSound = npc.data().get(NPC.Metadata.DEATH_SOUND);
String hurtSound = npc.data().get(NPC.Metadata.HURT_SOUND);
@ -2906,13 +2907,21 @@ public class NPCCommands {
npc.data().setPersistent(NPC.Metadata.SILENT, false);
} else {
if (death != null) {
deathSound = death.equals("d") ? null : NMS.getSound(death);
deathSound = NMS.getSoundPath(death);
} else if (args.hasValueFlag("death")) {
deathSound = args.getFlag("death").equals("d") ? null : args.getFlag("death");
}
if (ambient != null) {
ambientSound = ambient.equals("d") ? null : NMS.getSound(ambient);
ambientSound = NMS.getSoundPath(ambient);
} else if (args.hasValueFlag("ambient")) {
ambientSound = args.getFlag("ambient").equals("d") ? null : args.getFlag("ambient");
}
if (hurt != null) {
hurtSound = hurt.equals("d") ? null : NMS.getSound(hurt);
hurtSound = NMS.getSoundPath(hurt);
} else if (args.hasValueFlag("hurt")) {
hurtSound = args.getFlag("hurt").equals("d") ? null : args.getFlag("hurt");
}
}
if (deathSound == null) {

View File

@ -14,6 +14,7 @@ import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.block.Block;
@ -526,8 +527,8 @@ public class NMS {
return null;
}
public static String getSound(String flag) throws CommandException {
return BRIDGE.getSound(flag);
public static String getSoundPath(Sound flag) throws CommandException {
return BRIDGE.getSoundPath(flag);
}
public static Entity getSource(BlockCommandSender sender) {

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.BlockCommandSender;
@ -95,7 +96,7 @@ public interface NMSBridge {
public GameProfile getProfile(SkullMeta meta);
public String getSound(String flag) throws CommandException;
public String getSoundPath(Sound flag) throws CommandException;
public Entity getSource(BlockCommandSender sender);

View File

@ -572,9 +572,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
String ret = CraftSound.getSound(Sound.valueOf(flag.toUpperCase()));
String ret = CraftSound.getSound(flag);
if (ret == null)
throw new CommandException(Messages.INVALID_SOUND);
return ret;

View File

@ -592,9 +592,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
String ret = CraftSound.getSound(Sound.valueOf(flag.toUpperCase()));
String ret = CraftSound.getSound(flag);
if (ret == null)
throw new CommandException(Messages.INVALID_SOUND);
return ret;

View File

@ -592,9 +592,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
String ret = CraftSound.getSound(Sound.valueOf(flag.toUpperCase()));
String ret = CraftSound.getSound(flag);
if (ret == null)
throw new CommandException(Messages.INVALID_SOUND);
return ret;

View File

@ -620,9 +620,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
String ret = CraftSound.getSound(Sound.valueOf(flag.toUpperCase()));
String ret = CraftSound.getSound(flag);
if (ret == null)
throw new CommandException(Messages.INVALID_SOUND);
return ret;

View File

@ -655,9 +655,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
String ret = CraftSound.getSound(Sound.valueOf(flag.toUpperCase()));
String ret = CraftSound.getSound(flag);
if (ret == null)
throw new CommandException(Messages.INVALID_SOUND);
return ret;

View File

@ -670,9 +670,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
String ret = CraftSound.getSound(Sound.valueOf(flag.toUpperCase()));
String ret = CraftSound.getSound(flag);
if (ret == null)
throw new CommandException(Messages.INVALID_SOUND);
return ret;

View File

@ -686,9 +686,8 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound sound) throws CommandException {
try {
Sound sound = Sound.valueOf(flag.toUpperCase());
if (CRAFTSOUND_GETSOUND != null) {
String ret = (String) CRAFTSOUND_GETSOUND.invoke(sound);
if (ret == null)

View File

@ -695,10 +695,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
Sound sound = Sound.valueOf(flag.toUpperCase());
SoundEvent effect = CraftSound.getSoundEffect(sound);
SoundEvent effect = CraftSound.getSoundEffect(flag);
if (effect == null)
throw new CommandException(Messages.INVALID_SOUND);
return effect.getLocation().getPath();

View File

@ -702,10 +702,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
Sound sound = Sound.valueOf(flag.toUpperCase());
SoundEvent effect = CraftSound.getSoundEffect(sound);
SoundEvent effect = CraftSound.getSoundEffect(flag);
if (effect == null)
throw new CommandException(Messages.INVALID_SOUND);
return effect.getLocation().getPath();

View File

@ -735,10 +735,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
Sound sound = Sound.valueOf(flag.toUpperCase());
SoundEvent effect = CraftSound.getSoundEffect(sound);
SoundEvent effect = CraftSound.getSoundEffect(flag);
if (effect == null)
throw new CommandException(Messages.INVALID_SOUND);
return effect.getLocation().getPath();

View File

@ -734,10 +734,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
Sound sound = Sound.valueOf(flag.toUpperCase());
SoundEvent effect = CraftSound.getSoundEffect(sound);
SoundEvent effect = CraftSound.getSoundEffect(flag);
if (effect == null)
throw new CommandException(Messages.INVALID_SOUND);
return effect.getLocation().getPath();

View File

@ -520,9 +520,9 @@ public class NMSImpl implements NMSBridge {
}
@Override
public String getSound(String flag) throws CommandException {
public String getSoundPath(Sound flag) throws CommandException {
try {
String ret = CraftSound.getSound(Sound.valueOf(flag.toUpperCase()));
String ret = CraftSound.getSound(flag);
if (ret == null)
throw new CommandException(Messages.INVALID_SOUND);
return ret;