Fix Goat Horn

This commit is contained in:
Navoei
2024-11-28 19:37:41 -06:00
parent 6c410c1123
commit f1a70c363d
5 changed files with 115 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import de.maxhenkel.voicechat.api.BukkitVoicechatService;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
import me.Navoei.customdiscsplugin.command.CustomDiscCommand;
import me.Navoei.customdiscsplugin.event.GoatHorn;
import me.Navoei.customdiscsplugin.event.JukeBox;
import me.Navoei.customdiscsplugin.language.Lang;
import org.bukkit.NamespacedKey;
@@ -73,6 +74,7 @@ public final class CustomDiscs extends JavaPlugin {
}
getServer().getPluginManager().registerEvents(new JukeBox(), this);
getServer().getPluginManager().registerEvents(new GoatHorn(), this);
getServer().getPluginManager().registerEvents(new HopperManager(), this);
musicDiscDistance = Objects.requireNonNull(getConfig().getInt("music-disc-distance"));
@@ -100,7 +102,7 @@ public final class CustomDiscs extends JavaPlugin {
//Start the jukebox state manager.
//This keeps the jukebox powered while custom song is playing,
//which perfectly emulated the vanilla behavior of discs.
//which perfectly emulates the vanilla behavior of discs.
JukeboxStateManager.start(jukebox);
}
}

View File

@@ -101,7 +101,6 @@ public class PlayerManager {
});
}
/*
public void playLocationalAudioHorn(VoicechatServerApi api, Path soundFilePath, Player block, Component actionbarComponent, float range) {
UUID id = UUID.nameUUIDFromBytes(block.getLocation().toString().getBytes());
@@ -156,7 +155,6 @@ public class PlayerManager {
}
});
}
*/
@Nullable
private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannel(VoicechatServerApi api, AudioChannel audioChannel, Block block, Path soundFilePath, Collection<ServerPlayer> playersInRange) {
@@ -176,7 +174,6 @@ public class PlayerManager {
}
}
/*
@Nullable
private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannelHorn(VoicechatServerApi api, AudioChannel audioChannel, Player block, Path soundFilePath, Collection<ServerPlayer> playersInRange) {
try {
@@ -194,7 +191,6 @@ public class PlayerManager {
return null;
}
}
*/
private static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
return VoicePlugin.voicechatApi.getAudioConverter().bytesToShorts(convertFormat(file, FORMAT));

View File

@@ -78,6 +78,7 @@ public class VoicePlugin implements VoicechatPlugin {
.build();
voicechatServerApi.registerVolumeCategory(goatHorns);
/*
playerHeads = voicechatServerApi.volumeCategoryBuilder()
.setId(PLAYER_HEAD_CATEGORY)
.setName("Player Heads")
@@ -85,6 +86,7 @@ public class VoicePlugin implements VoicechatPlugin {
.setIcon(getPlayerHeadsIcon())
.build();
voicechatServerApi.registerVolumeCategory(playerHeads);
*/
}
@@ -142,6 +144,7 @@ public class VoicePlugin implements VoicechatPlugin {
return null;
}
/*
private int[][] getPlayerHeadsIcon() {
try {
Enumeration<URL> resources = CustomDiscs.getInstance().getClass().getClassLoader().getResources("player_head_category.png");
@@ -168,5 +171,7 @@ public class VoicePlugin implements VoicechatPlugin {
}
return null;
}
*/
}

View File

@@ -0,0 +1,104 @@
package me.Navoei.customdiscsplugin.event;
import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.PlayerManager;
import me.Navoei.customdiscsplugin.VoicePlugin;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.TileState;
import org.bukkit.block.data.Powerable;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Objects;
public class GoatHorn implements Listener {
static CustomDiscs customDiscs = CustomDiscs.getInstance();
PlayerManager playerManager = PlayerManager.instance();
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerInteract(PlayerInteractEvent event) throws IOException {
ItemStack item = event.getItem();
Player player = event.getPlayer();
if (item == null) return;
if (item.getType() != Material.GOAT_HORN) return;
Block block = event.getClickedBlock();
if (player.hasCooldown(Material.GOAT_HORN)) return;
if (isCustomGoatHorn(event) && ((event.getAction() == Action.RIGHT_CLICK_BLOCK) || (event.getAction() == Action.RIGHT_CLICK_AIR))) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && !player.isSneaking()) {
if ((block.getBlockData() instanceof Powerable) || (block instanceof TileState) && event.useInteractedBlock().equals(Event.Result.ALLOW)) {
return;
}
}
String soundFileName = event.getItem().getItemMeta().getPersistentDataContainer().get(new NamespacedKey(customDiscs, "customhorn"), PersistentDataType.STRING);
@NotNull PersistentDataContainer persistentDataContainer = event.getItem().getItemMeta().getPersistentDataContainer();
float range;
NamespacedKey customSoundRangeKey = new NamespacedKey(customDiscs, "customsoundrange");
if(persistentDataContainer.has(customSoundRangeKey, PersistentDataType.FLOAT)) {
range = Math.min(persistentDataContainer.get(customSoundRangeKey, PersistentDataType.FLOAT), CustomDiscs.getInstance().musicDiscMaxDistance);
} else {
range = Math.min(CustomDiscs.getInstance().musicDiscDistance, CustomDiscs.getInstance().musicDiscMaxDistance);
}
int hornCooldown;
NamespacedKey hornCooldownKey = new NamespacedKey(customDiscs, "customhorncoolodwn");
if(persistentDataContainer.has(hornCooldownKey, PersistentDataType.INTEGER)) {
hornCooldown = Math.min(persistentDataContainer.get(hornCooldownKey, PersistentDataType.INTEGER), CustomDiscs.getInstance().hornMaxCooldownTicks);
} else {
hornCooldown = Math.min(Math.round(CustomDiscs.getInstance().hornCooldown * 20), CustomDiscs.getInstance().hornMaxCooldownTicks);
}
Path soundFilePath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", soundFileName);
if (soundFilePath.toFile().exists()) {
Component songNameComponent = Objects.requireNonNull(event.getItem().getItemMeta().lore()).get(0).asComponent();
String songName = PlainTextComponentSerializer.plainText().serialize(songNameComponent);
TextComponent customActionBarSongPlaying = Component.text()
.content("Now Horn Playing: " + songName)
.color(NamedTextColor.GOLD)
.build();
assert VoicePlugin.voicechatServerApi != null;
playerManager.playLocationalAudioHorn(VoicePlugin.voicechatServerApi, soundFilePath, player, customActionBarSongPlaying.asComponent(), range);
player.setCooldown(Material.GOAT_HORN, hornCooldown);
} else {
player.sendMessage(NamedTextColor.RED + "Sound file not found.");
event.setCancelled(true);
throw new FileNotFoundException("Sound file is missing!");
}
}
}
public static boolean isCustomGoatHorn(PlayerInteractEvent e) {
if (e.getItem()==null) return false;
return e.getItem().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customhorn"));
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B