diff --git a/build.gradle b/build.gradle index c8a968c..254a0d1 100644 --- a/build.gradle +++ b/build.gradle @@ -49,6 +49,7 @@ dependencies { //shadow "dev.jorel:commandapi-paper-shade-mojang-mapped:${command_api_version}" compileOnly group: "com.comphenix.protocol", name: "ProtocolLib", version: "5.3.0" + //compileOnly "com.github.retrooper:packetevents-spigot:2.9.4" } repositories { diff --git a/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java b/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java index b28dea0..0157a8c 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java +++ b/src/main/java/me/Navoei/customdiscsplugin/CustomDiscs.java @@ -1,11 +1,5 @@ package me.Navoei.customdiscsplugin; -import me.Navoei.customdiscsplugin.command.CustomDiscCommand; -import me.Navoei.customdiscsplugin.event.JukeBox; -import me.Navoei.customdiscsplugin.event.HeadPlay; -import me.Navoei.customdiscsplugin.event.HornPlay; -import me.Navoei.customdiscsplugin.language.Lang; - import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolManager; @@ -13,6 +7,11 @@ import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; +import me.Navoei.customdiscsplugin.command.CustomDiscCommand; +import me.Navoei.customdiscsplugin.event.JukeBox; +import me.Navoei.customdiscsplugin.event.HeadPlay; +import me.Navoei.customdiscsplugin.event.HornPlay; +import me.Navoei.customdiscsplugin.language.Lang; import de.maxhenkel.voicechat.api.BukkitVoicechatService; @@ -126,9 +125,7 @@ public final class CustomDiscs extends JavaPlugin { hornMaxCooldown = 1; } - ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); - protocolManager.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL, PacketType.Play.Server.WORLD_EVENT) { @Override public void onPacketSending(PacketEvent event) { @@ -137,13 +134,13 @@ public final class CustomDiscs extends JavaPlugin { if (packet.getIntegers().read(0).toString().equals("1010")) { if (!isMusicDiscEnable()) { return; } Jukebox jukebox = (Jukebox) packet.getBlockPositionModifier().read(0).toLocation(event.getPlayer().getWorld()).getBlock().getState(); - + if (!jukebox.getRecord().hasItemMeta()) return; - + if (jukebox.getRecord().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(this.plugin, "customdisc"), PersistentDataType.STRING)) { event.setCancelled(true); } - + //Start the jukebox state manager. //This keeps the jukebox powered while custom song is playing, //which perfectly emulates the vanilla behavior of discs. diff --git a/src/main/java/me/Navoei/customdiscsplugin/JukeboxStateManager.java b/src/main/java/me/Navoei/customdiscsplugin/JukeboxStateManager.java index 7439a61..626a957 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/JukeboxStateManager.java +++ b/src/main/java/me/Navoei/customdiscsplugin/JukeboxStateManager.java @@ -3,14 +3,13 @@ package me.Navoei.customdiscsplugin; import org.bukkit.Location; import org.bukkit.block.Jukebox; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; public class JukeboxStateManager { static CustomDiscs plugin = CustomDiscs.getInstance(); static PlayerManager playerManager = PlayerManager.instance(); - static List jukeboxLocations = new ArrayList<>(); + static HashSet jukeboxLocations = new HashSet<>(); public static void start(Jukebox jukebox) { if (jukeboxLocations.contains(jukebox.getLocation()) || !playerManager.isAudioPlayerPlaying(jukebox.getLocation())) return; diff --git a/src/main/java/me/Navoei/customdiscsplugin/TypeChecker.java b/src/main/java/me/Navoei/customdiscsplugin/TypeChecker.java index eb1b650..dce9524 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/TypeChecker.java +++ b/src/main/java/me/Navoei/customdiscsplugin/TypeChecker.java @@ -1,5 +1,7 @@ package me.Navoei.customdiscsplugin; +import com.destroystokyo.paper.MaterialTags; +import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerInteractEvent; @@ -14,7 +16,7 @@ public class TypeChecker { // MUSIC DISCS public static boolean isMusicDisc(ItemStack item) { - return item.getType().toString().contains("MUSIC_DISC"); + return MaterialTags.MUSIC_DISCS.isTagged(item.getType()); } /*public static boolean isMusicDiscPlayer(Player p) { @@ -24,7 +26,7 @@ public class TypeChecker { public static boolean isCustomMusicDisc(ItemStack itemStack) { if (itemStack == null) return false; if (itemStack.getItemMeta() == null) return false; - return itemStack.getType().toString().contains("MUSIC_DISC") && itemStack.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); + return MaterialTags.MUSIC_DISCS.isTagged(itemStack.getType()) && itemStack.getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customdisc"), PersistentDataType.STRING); } /*public static boolean isCustomMusicDiscPlayer(Player p) { @@ -38,16 +40,16 @@ public class TypeChecker { }*/ public static boolean isGoatHornPlayer(Player p) { - return p.getInventory().getItemInMainHand().getType().toString().contains("GOAT_HORN"); + return p.getInventory().getItemInMainHand().getType().equals(Material.GOAT_HORN); } public static boolean isCustomGoatHorn(PlayerInteractEvent e) { if (e.getItem()==null) return false; - return e.getItem().getType().toString().contains("GOAT_HORN") && e.getItem().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customhorn"), PersistentDataType.STRING); + return e.getItem().getType().equals(Material.GOAT_HORN) && e.getItem().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customhorn"), PersistentDataType.STRING); } public static boolean isCustomGoatHornPlayer(Player p) { - return p.getInventory().getItemInMainHand().getType().toString().contains("GOAT_HORN") && p.getInventory().getItemInMainHand().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customhorn"), PersistentDataType.STRING); + return p.getInventory().getItemInMainHand().getType().equals(Material.GOAT_HORN) && p.getInventory().getItemInMainHand().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customhorn"), PersistentDataType.STRING); } // PLAYER HEADS @@ -57,7 +59,7 @@ public class TypeChecker { }*/ public static boolean isHeadPlayer(Player p) { - return p.getInventory().getItemInMainHand().getType().toString().contains("PLAYER_HEAD"); + return p.getInventory().getItemInMainHand().getType().equals(Material.PLAYER_HEAD); } /*public static boolean isCustomHead(ItemStack itemStack) { @@ -67,7 +69,7 @@ public class TypeChecker { }*/ public static boolean isCustomHeadPlayer(Player p) { - return p.getInventory().getItemInMainHand().getType().toString().contains("PLAYER_HEAD") && p.getInventory().getItemInMainHand().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customhead"), PersistentDataType.STRING); + return p.getInventory().getItemInMainHand().getType().equals(Material.PLAYER_HEAD) && p.getInventory().getItemInMainHand().getItemMeta().getPersistentDataContainer().has(new NamespacedKey(customDiscs, "customhead"), PersistentDataType.STRING); } } \ No newline at end of file diff --git a/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/CreateSubCommand.java b/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/CreateSubCommand.java index f1a15de..dca4c3d 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/CreateSubCommand.java +++ b/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/CreateSubCommand.java @@ -79,15 +79,13 @@ public class CreateSubCommand extends CommandAPICommand { if (!resultIsMusicDisc && !resultIsHorn && !resultIsHead) { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NOT_HOLDING_CORRECT_ITEM.toString())); - return 1; + return 0; } - - String filename = Objects.requireNonNull(arguments.getByClass("filename", String.class)); if (filename.contains("../")) { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FILENAME.toString())); - return 1; + return 0; } File getDirectory = new File(this.plugin.getDataFolder(), "musicdata"); @@ -95,11 +93,11 @@ public class CreateSubCommand extends CommandAPICommand { if (songFile.exists()) { if (!getFileExtension(filename).equals("wav") && !getFileExtension(filename).equals("mp3") && !getFileExtension(filename).equals("flac")) { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FORMAT.toString())); - return 1; + return 0; } } else { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.FILE_NOT_FOUND.toString())); - return 1; + return 0; } String song_name = Objects.requireNonNull(arguments.getByClass("song_name", String.class)); @@ -138,7 +136,8 @@ public class CreateSubCommand extends CommandAPICommand { PersistentDataContainer data = meta.getPersistentDataContainer(); data.set(new NamespacedKey(this.plugin, "customhorn"), PersistentDataType.STRING, filename); player.getInventory().getItemInMainHand().setItemMeta(meta); - } else if (resultIsHead) { + } else { + //Must be a player head. if (!CustomDiscs.isCustomHeadEnable()) { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CUSTOM_HEAD_DISABLED.toString())); return 1; } final Component customLoreHead = Component.text().decoration(TextDecoration.ITALIC, false).content(song_name).color(NamedTextColor.GRAY).build(); String serialized = GsonComponentSerializer.gson().serialize(customLoreHead); @@ -154,10 +153,7 @@ public class CreateSubCommand extends CommandAPICommand { data.set(new NamespacedKey(this.plugin, "customhead"), PersistentDataType.STRING, filename); data.set(new NamespacedKey(this.plugin, "headlore"), PersistentDataType.STRING, serialized); player.getInventory().getItemInMainHand().setItemMeta(meta); - } else { - return 1; } - player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_FILENAME.toString().replace("%filename%", filename))); player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_CUSTOM_NAME.toString().replace("%custom_name%", song_name))); return 1; diff --git a/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/SetHornCooldownSubCommand.java b/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/SetHornCooldownSubCommand.java index 535189c..061ecdb 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/SetHornCooldownSubCommand.java +++ b/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/SetHornCooldownSubCommand.java @@ -20,6 +20,7 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; import java.util.Objects; +import java.util.Optional; public class SetHornCooldownSubCommand extends CommandAPICommand { private final CustomDiscs plugin; @@ -41,28 +42,28 @@ public class SetHornCooldownSubCommand extends CommandAPICommand { private int onCommandPlayer(Player player, CommandArguments arguments) { if (!TypeChecker.isCustomGoatHornPlayer(player)) { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NOT_HOLDING_MODIFIED_GOATHORN.toString())); - return 1; + return 0; } if (TypeChecker.isCustomGoatHornPlayer(player)) { if (!CustomDiscs.isMusicDiscEnable()) { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CUSTOM_HORN_DISABLED.toString())); return 1; } } - int goatcooldown = Objects.requireNonNull(arguments.getByClass("goatcooldown", Integer.class)); + int goatCooldown = Optional.ofNullable(arguments.getByClass("goatcooldown", Integer.class)).orElse(0); - if (goatcooldown <= 0 || goatcooldown > this.plugin.hornMaxCooldown) { + if (goatCooldown <= 0 || goatCooldown > this.plugin.hornMaxCooldown) { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_COOLDOWN.toString().replace("%cooldown_value%", Integer.toString(this.plugin.hornMaxCooldown)))); - return 1; + return 0; } ItemStack disc = new ItemStack(player.getInventory().getItemInMainHand()); ItemMeta meta = disc.getItemMeta(); PersistentDataContainer data = meta.getPersistentDataContainer(); - data.set(new NamespacedKey(this.plugin, "customhorncoolodwn"), PersistentDataType.INTEGER, Math.min(goatcooldown, CustomDiscs.getInstance().hornMaxCooldown)); + data.set(new NamespacedKey(this.plugin, "goat_horn_cooldown"), PersistentDataType.INTEGER, Math.min(goatCooldown, CustomDiscs.getInstance().hornMaxCooldown)); player.getInventory().getItemInMainHand().setItemMeta(meta); - player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_CUSTOM_GOAT_COOLDOWN.toString().replace("%custom_goat_cooldown%", Integer.toString(goatcooldown)))); + player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_CUSTOM_GOAT_COOLDOWN.toString().replace("%custom_goat_cooldown%", Integer.toString(goatCooldown)))); return 1; } diff --git a/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/SetRangeSubCommand.java b/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/SetRangeSubCommand.java index 8e72900..879266e 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/SetRangeSubCommand.java +++ b/src/main/java/me/Navoei/customdiscsplugin/command/SubCommands/SetRangeSubCommand.java @@ -20,6 +20,7 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; import java.util.Objects; +import java.util.Optional; public class SetRangeSubCommand extends CommandAPICommand { private final CustomDiscs plugin; @@ -50,9 +51,9 @@ public class SetRangeSubCommand extends CommandAPICommand { return 1; } - Float range = Objects.requireNonNull(arguments.getByClass("range", Float.class)); + float range = Optional.ofNullable(arguments.getByClass("range", Float.class)).orElse(0f); - Float configMusicDiscMaxDistance; + float configMusicDiscMaxDistance; if (resultIsCustomHorn) { if (!CustomDiscs.isCustomHornEnable()) { player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CUSTOM_HORN_DISABLED.toString())); return 1; } configMusicDiscMaxDistance = this.plugin.customHornMaxDistance; diff --git a/src/main/java/me/Navoei/customdiscsplugin/event/HornPlay.java b/src/main/java/me/Navoei/customdiscsplugin/event/HornPlay.java index 2d9e501..31af20e 100644 --- a/src/main/java/me/Navoei/customdiscsplugin/event/HornPlay.java +++ b/src/main/java/me/Navoei/customdiscsplugin/event/HornPlay.java @@ -108,9 +108,9 @@ public class HornPlay implements Listener{ } int hornCooldown; - NamespacedKey hornCooldownKey = new NamespacedKey(customDiscs, "customhorncoolodwn"); + NamespacedKey hornCooldownKey = new NamespacedKey(customDiscs, "goat_horn_cooldown"); if(persistentDataContainer.has(hornCooldownKey, PersistentDataType.INTEGER)) { - hornCooldown = Math.min(persistentDataContainer.get(hornCooldownKey, PersistentDataType.INTEGER), CustomDiscs.getInstance().hornMaxCooldown); + hornCooldown = Math.min(Optional.ofNullable(persistentDataContainer.get(hornCooldownKey, PersistentDataType.INTEGER)).orElse(0), CustomDiscs.getInstance().hornMaxCooldown); } else { hornCooldown = Math.min(CustomDiscs.getInstance().hornCooldown, CustomDiscs.getInstance().hornMaxCooldown); } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0d4ee83..053e895 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,7 +4,7 @@ main: me.Navoei.customdiscsplugin.CustomDiscs api-version: '${bukkit_api_version}' prefix: CustomDiscs authors: [ "Navoei", "Athar42", "alfw" ] -description: A plugin which uses the Simple Voice Chat API to add custom music discs, goat horns and player's head. +description: A plugin which uses the Simple Voice Chat API to add custom music discs, goat horns and player heads. depend: [ "voicechat", "ProtocolLib" ] dependencies: server: