9
0
mirror of https://github.com/HibiscusMC/HMCCosmetics.git synced 2025-12-19 15:09:19 +00:00

Added sounds to click items (Configs must use new format!)

This commit is contained in:
HeroBrineGoat
2022-01-31 22:28:18 -05:00
parent d2592809c7
commit 4b64ee1b3e
8 changed files with 202 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
<option name="PREFERRED_PROJECT_CODE_STYLE" value="GoogleStyle" />
</state>
</component>

3
.idea/gradle.xml generated
View File

@@ -4,8 +4,11 @@
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="delegatedBuild" value="true" />
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="openjdk-16" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

View File

@@ -33,7 +33,7 @@ dependencies {
implementation("net.kyori:adventure-api:4.9.3")
implementation("net.kyori:adventure-text-minimessage:4.10.0-SNAPSHOT")
implementation("net.kyori:adventure-platform-bukkit:4.0.1")
implementation("dev.triumphteam:triumph-gui:3.0.3")
implementation("dev.triumphteam:triumph-gui:3.1.1")
implementation("me.mattstudios.utils:matt-framework:1.4.6")
implementation("org.spongepowered:configurate-yaml:4.1.2")
implementation("org.bstats:bstats-bukkit:2.2.1")

View File

@@ -0,0 +1,88 @@
package io.github.fisher2911.hmccosmetics.config;
import com.comphenix.protocol.wrappers.EnumWrappers;
import dev.triumphteam.gui.components.GuiAction;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
import java.util.Arrays;
public class ActionSerializer implements TypeSerializer<GuiAction<InventoryClickEvent>> {
public static final ActionSerializer INSTANCE = new ActionSerializer();
private static final HMCCosmetics plugin;
private static final String OPEN_MENU = "open-menu";
private static final String SOUND = "sound";
private static final String SOUND_NAME = "name";
private static final String SOUND_VOLUME = "volume";
private static final String SOUND_PITCH = "pitch";
private static final String SOUND_CATEGORY = "category";
static {
plugin = HMCCosmetics.getPlugin(HMCCosmetics.class);
}
private ActionSerializer() {
}
private ConfigurationNode nonVirtualNode(final ConfigurationNode source, final Object... path)
throws SerializationException {
if (!source.hasChild(path)) {
throw new SerializationException(
"Required field " + Arrays.toString(path) + " was not present in node");
}
return source.node(path);
}
@Override
public GuiAction<InventoryClickEvent> deserialize(final Type type, final ConfigurationNode source) {
final ConfigurationNode openMenuNode = source.node(OPEN_MENU);
final ConfigurationNode soundNode = source.node(SOUND);
final ConfigurationNode soundNameNode = soundNode.node(SOUND_NAME);
final ConfigurationNode volumeNode = soundNode.node(SOUND_VOLUME);
final ConfigurationNode pitchNode = soundNode.node(SOUND_PITCH);
final ConfigurationNode categoryNode = soundNode.node(SOUND_CATEGORY);
final String openMenu = openMenuNode.getString();
final SoundData soundData;
final String soundName = soundNameNode.getString();
final String category = categoryNode.getString();
final int volume = volumeNode.getInt();
final int pitch = pitchNode.getInt();
if (soundName == null || category == null) {
soundData = null;
} else {
soundData = new SoundData(
soundName,
EnumWrappers.SoundCategory.valueOf(category),
volume,
pitch
);
}
return event -> {
if (!(event.getWhoClicked() instanceof final Player player)) return;
if (soundData != null) {
soundData.play(player);
}
if (openMenu != null) plugin.getCosmeticsMenu().openMenu(openMenu, event.getWhoClicked());
};
}
@Override
public void serialize(final Type type, @Nullable final GuiAction<InventoryClickEvent> obj, final ConfigurationNode node) throws SerializationException {
}
}

View File

@@ -1,5 +1,6 @@
package io.github.fisher2911.hmccosmetics.config;
import dev.triumphteam.gui.components.GuiAction;
import dev.triumphteam.gui.guis.GuiItem;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import io.github.fisher2911.hmccosmetics.gui.ArmorItem;
@@ -25,6 +26,7 @@ import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.Registry;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -54,7 +56,7 @@ public class ItemSerializer implements TypeSerializer<GuiItem> {
private static final String BLUE = "blue";
private static final String PERMISSION = "permission";
private static final String TYPE = "type";
private static final String OPEN_MENU = "open-menu";
private static final String ACTION = "action";
private static final String ID = "id";
private static final String DYEABLE = "dyeable";
@@ -96,7 +98,7 @@ public class ItemSerializer implements TypeSerializer<GuiItem> {
final ConfigurationNode blueNode = colorNode.node(BLUE);
final ConfigurationNode permissionNode = source.node(PERMISSION);
final ConfigurationNode typeNode = source.node(TYPE);
final ConfigurationNode openMenuNode = source.node(OPEN_MENU);
final ConfigurationNode actionNode = source.node(ACTION);
final ConfigurationNode idNode = source.node(ID);
final ConfigurationNode dyeableNode = source.node(DYEABLE);
@@ -212,8 +214,7 @@ public class ItemSerializer implements TypeSerializer<GuiItem> {
itemFlags(itemFlags).
build();
final String openMenu = openMenuNode.getString(
Utils.replaceIfNull(OPEN_MENU, ""));
final GuiAction<InventoryClickEvent> action = ActionSerializer.INSTANCE.deserialize(GuiAction.class, actionNode);
Keys.setKey(itemStack);
@@ -228,7 +229,7 @@ public class ItemSerializer implements TypeSerializer<GuiItem> {
return new ArmorItem(
itemStack,
event -> plugin.getCosmeticsMenu().openMenu(openMenu, event.getWhoClicked()),
action,
Utils.replaceIfNull(idNode.getString(), ""),
lockedLore,
permission,
@@ -238,9 +239,9 @@ public class ItemSerializer implements TypeSerializer<GuiItem> {
);
} catch (final IllegalArgumentException exception) {
return dev.triumphteam.gui.builder.item.ItemBuilder.from(
itemStack).
asGuiItem(event -> plugin.getCosmeticsMenu().openMenu(openMenu, event.getWhoClicked()));
final GuiItem guiItem = dev.triumphteam.gui.builder.item.ItemBuilder.from(itemStack).asGuiItem();
guiItem.setAction(action);
return guiItem;
}
}

View File

@@ -0,0 +1,61 @@
package io.github.fisher2911.hmccosmetics.config;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.MinecraftKey;
import io.github.fisher2911.hmccosmetics.packet.PacketManager;
import org.bukkit.entity.Player;
public class SoundData {
private final String name;
private final EnumWrappers.SoundCategory soundCategory;
private final float volume;
private final float pitch;
public SoundData(final String name, final EnumWrappers.SoundCategory soundCategory, final float volume, final float pitch) {
this.name = name;
this.soundCategory = soundCategory;
this.volume = volume;
this.pitch = pitch;
}
public String getName() {
return name;
}
public float getVolume() {
return volume;
}
public float getPitch() {
return pitch;
}
public EnumWrappers.SoundCategory getSoundCategory() {
return soundCategory;
}
public void play(final Player player) {
final PacketContainer soundPacket = PacketManager.getSoundPacket(
player,
player.getLocation(),
this.getKey(this.name),
this.volume,
this.pitch,
this.soundCategory
);
PacketManager.sendPacket(player, soundPacket);
}
private MinecraftKey getKey(final String string) {
if (!string.contains(":")) {
return new MinecraftKey(string);
}
final String[] parts = string.split(":");
return new MinecraftKey(parts[0], parts[1]);
}
}

View File

@@ -1,7 +1,9 @@
package io.github.fisher2911.hmccosmetics.gui;
import dev.triumphteam.gui.components.GuiAction;
import dev.triumphteam.gui.guis.GuiItem;
import io.github.fisher2911.hmccosmetics.HMCCosmetics;
import io.github.fisher2911.hmccosmetics.config.ActionSerializer;
import io.github.fisher2911.hmccosmetics.config.DyeGuiSerializer;
import io.github.fisher2911.hmccosmetics.config.GuiSerializer;
import io.github.fisher2911.hmccosmetics.config.ItemSerializer;

View File

@@ -5,6 +5,7 @@ import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.MinecraftKey;
import com.comphenix.protocol.wrappers.Pair;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
@@ -82,6 +83,42 @@ public class PacketManager {
return destroyPacket;
}
public static PacketContainer getSoundPacket(
final Player player,
final Location location,
final MinecraftKey name,
final float volume,
final float pitch,
final EnumWrappers.SoundCategory soundCategory
) {
// final Location location = player.getLocation();
final var manager = ProtocolLibrary.getProtocolManager();
final var packet = manager.createPacket(PacketType.Play.Server.CUSTOM_SOUND_EFFECT);
packet.getMinecraftKeys()
.write(
0,
name
);
packet.getSoundCategories()
.write(0, EnumWrappers.SoundCategory.valueOf(soundCategory.name()));
packet.getIntegers()
.write(0, location.getBlockX() * 8)
.write(
1, location.getBlockY() * 8
)
.write(2, location.getBlockZ() * 8);
packet.getFloat()
.write(0, volume)
.write(1, pitch);
return packet;
}
public static void sendPacket(final Player to, final PacketContainer... packets) {
final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
try {