9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-30 04:19:27 +00:00

refactor sounds

This commit is contained in:
XiaoMoMi
2025-04-04 04:06:44 +08:00
parent 4e945f2af2
commit 74d5320251
13 changed files with 146 additions and 200 deletions

View File

@@ -11,7 +11,6 @@ import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.loot.AbstractVanillaLootManager;
import net.momirealms.craftengine.core.loot.LootTable;
import net.momirealms.craftengine.core.loot.VanillaLoot;
import net.momirealms.craftengine.core.loot.VanillaLootManager;
import net.momirealms.craftengine.core.loot.parameter.LootParameters;
import net.momirealms.craftengine.core.pack.LoadingSequence;
import net.momirealms.craftengine.core.pack.Pack;
@@ -19,7 +18,6 @@ import net.momirealms.craftengine.core.plugin.config.ConfigSectionParser;
import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.PreConditions;
import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.craftengine.core.util.context.ContextHolder;
import net.momirealms.craftengine.core.world.Vec3d;
@@ -34,7 +32,10 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import java.nio.file.Path;
import java.util.*;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
// note: block listeners are in BlockEventListener to reduce performance cost
public class BukkitVanillaLootManager extends AbstractVanillaLootManager implements Listener {

View File

@@ -1,58 +0,0 @@
package net.momirealms.craftengine.bukkit.sound;
import net.momirealms.craftengine.bukkit.util.ComponentUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.sound.song.AbstractJukeboxSongManager;
import net.momirealms.craftengine.core.sound.song.JukeboxSong;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.VersionHelper;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public class BukkitJukeboxSongManager extends AbstractJukeboxSongManager {
public BukkitJukeboxSongManager(CraftEngine plugin) {
super(plugin);
}
@Override
protected void registerSongs(Map<Key, JukeboxSong> songs) {
if (songs.isEmpty()) return;
try {
unfreezeRegistry();
for (Map.Entry<Key, JukeboxSong> entry : songs.entrySet()) {
Key id = entry.getKey();
JukeboxSong jukeboxSong = entry.getValue();
Object resourceLocation = Reflections.method$ResourceLocation$fromNamespaceAndPath.invoke(null, id.namespace(), id.value());
Object soundId = Reflections.method$ResourceLocation$fromNamespaceAndPath.invoke(null, jukeboxSong.sound().namespace(), jukeboxSong.sound().value());
Object song = Reflections.method$Registry$get.invoke(Reflections.instance$InternalRegistries$JUKEBOX_SONG, resourceLocation);
Object soundEvent = VersionHelper.isVersionNewerThan1_21_2() ?
Reflections.constructor$SoundEvent.newInstance(soundId, Optional.of(jukeboxSong.range())) :
Reflections.constructor$SoundEvent.newInstance(soundId, jukeboxSong.range(), false);
Object soundHolder = Reflections.method$Holder$direct.invoke(null, soundEvent);
if (song == null) {
song = Reflections.constructor$JukeboxSong.newInstance(soundHolder, ComponentUtils.adventureToMinecraft(jukeboxSong.description()), jukeboxSong.lengthInSeconds(), jukeboxSong.comparatorOutput());
Object holder = Reflections.method$Registry$registerForHolder.invoke(null, Reflections.instance$InternalRegistries$JUKEBOX_SONG, resourceLocation, song);
Reflections.method$Holder$Reference$bindValue.invoke(holder, song);
Reflections.field$Holder$Reference$tags.set(holder, Set.of());
}
}
freezeRegistry();
} catch (Exception e) {
plugin.logger().warn("Failed to register jukebox songs.", e);
}
}
private void unfreezeRegistry() throws IllegalAccessException {
Reflections.field$MappedRegistry$frozen.set(Reflections.instance$InternalRegistries$JUKEBOX_SONG, false);
}
private void freezeRegistry() throws IllegalAccessException {
Reflections.field$MappedRegistry$frozen.set(Reflections.instance$InternalRegistries$JUKEBOX_SONG, true);
}
}

View File

@@ -1,8 +1,16 @@
package net.momirealms.craftengine.bukkit.sound;
import net.momirealms.craftengine.bukkit.util.ComponentUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.sound.AbstractSoundManager;
import net.momirealms.craftengine.core.sound.song.JukeboxSongManager;
import net.momirealms.craftengine.core.sound.JukeboxSong;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.VersionHelper;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public class BukkitSoundManager extends AbstractSoundManager {
@@ -11,7 +19,40 @@ public class BukkitSoundManager extends AbstractSoundManager {
}
@Override
protected JukeboxSongManager createJukeboxSongManager() {
return new BukkitJukeboxSongManager(super.plugin);
protected void registerSongs(Map<Key, JukeboxSong> songs) {
if (songs.isEmpty()) return;
try {
unfreezeRegistry();
for (Map.Entry<Key, JukeboxSong> entry : songs.entrySet()) {
Key id = entry.getKey();
JukeboxSong jukeboxSong = entry.getValue();
Object resourceLocation = Reflections.method$ResourceLocation$fromNamespaceAndPath.invoke(null, id.namespace(), id.value());
Object soundId = Reflections.method$ResourceLocation$fromNamespaceAndPath.invoke(null, jukeboxSong.sound().namespace(), jukeboxSong.sound().value());
Object song = Reflections.method$Registry$get.invoke(Reflections.instance$InternalRegistries$JUKEBOX_SONG, resourceLocation);
Object soundEvent = VersionHelper.isVersionNewerThan1_21_2() ?
Reflections.constructor$SoundEvent.newInstance(soundId, Optional.of(jukeboxSong.range())) :
Reflections.constructor$SoundEvent.newInstance(soundId, jukeboxSong.range(), false);
Object soundHolder = Reflections.method$Holder$direct.invoke(null, soundEvent);
if (song == null) {
song = Reflections.constructor$JukeboxSong.newInstance(soundHolder, ComponentUtils.adventureToMinecraft(jukeboxSong.description()), jukeboxSong.lengthInSeconds(), jukeboxSong.comparatorOutput());
Object holder = Reflections.method$Registry$registerForHolder.invoke(null, Reflections.instance$InternalRegistries$JUKEBOX_SONG, resourceLocation, song);
Reflections.method$Holder$Reference$bindValue.invoke(holder, song);
Reflections.field$Holder$Reference$tags.set(holder, Set.of());
}
}
freezeRegistry();
} catch (Exception e) {
plugin.logger().warn("Failed to register jukebox songs.", e);
}
}
private void unfreezeRegistry() throws IllegalAccessException {
Reflections.field$MappedRegistry$frozen.set(Reflections.instance$InternalRegistries$JUKEBOX_SONG, false);
}
private void freezeRegistry() throws IllegalAccessException {
Reflections.field$MappedRegistry$frozen.set(Reflections.instance$InternalRegistries$JUKEBOX_SONG, true);
}
}