mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-25 09:59:20 +00:00
不稳定声音
This commit is contained in:
@@ -7,11 +7,13 @@ import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MRegistries
|
||||
import net.momirealms.craftengine.bukkit.util.ComponentUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.KeyUtils;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.sound.AbstractSoundManager;
|
||||
import net.momirealms.craftengine.core.sound.JukeboxSong;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -26,6 +28,37 @@ public class BukkitSoundManager extends AbstractSoundManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerSounds(Collection<Key> sounds) {
|
||||
if (sounds.isEmpty()) return;
|
||||
Object registry = MBuiltInRegistries.SOUND_EVENT;
|
||||
try {
|
||||
CoreReflections.field$MappedRegistry$frozen.set(registry, false);
|
||||
for (Key soundEventId : sounds) {
|
||||
Object resourceLocation = KeyUtils.toResourceLocation(soundEventId);
|
||||
// 检查之前有没有注册过了
|
||||
Object soundEvent = FastNMS.INSTANCE.method$Registry$getValue(registry, resourceLocation);
|
||||
// 只有没注册才注册,否则会报错
|
||||
if (soundEvent == null) {
|
||||
soundEvent = VersionHelper.isOrAbove1_21_2() ?
|
||||
CoreReflections.constructor$SoundEvent.newInstance(resourceLocation, Optional.of(0)) :
|
||||
CoreReflections.constructor$SoundEvent.newInstance(resourceLocation, 0, false);
|
||||
Object holder = CoreReflections.method$Registry$registerForHolder.invoke(null, registry, resourceLocation, soundEvent);
|
||||
CoreReflections.method$Holder$Reference$bindValue.invoke(holder, soundEvent);
|
||||
CoreReflections.field$Holder$Reference$tags.set(holder, Set.of());
|
||||
int id = FastNMS.INSTANCE.method$Registry$getId(registry, soundEvent);
|
||||
super.customSoundsInRegistry.put(id, soundEventId);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
this.plugin.logger().warn("Failed to register jukebox songs.", e);
|
||||
} finally {
|
||||
try {
|
||||
CoreReflections.field$MappedRegistry$frozen.set(registry, true);
|
||||
} catch (ReflectiveOperationException ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void registerSongs(Map<Key, JukeboxSong> songs) {
|
||||
if (songs.isEmpty()) return;
|
||||
@@ -40,13 +73,12 @@ public class BukkitSoundManager extends AbstractSoundManager {
|
||||
Object soundId = KeyUtils.toResourceLocation(jukeboxSong.sound());
|
||||
// 检查之前有没有注册过了
|
||||
Object song = FastNMS.INSTANCE.method$Registry$getValue(registry, resourceLocation);
|
||||
|
||||
Object soundEvent = VersionHelper.isOrAbove1_21_2() ?
|
||||
CoreReflections.constructor$SoundEvent.newInstance(soundId, Optional.of(jukeboxSong.range())) :
|
||||
CoreReflections.constructor$SoundEvent.newInstance(soundId, jukeboxSong.range(), false);
|
||||
Object soundHolder = CoreReflections.method$Holder$direct.invoke(null, soundEvent);
|
||||
// 只有没注册才注册,否则会报错
|
||||
if (song == null) {
|
||||
Object soundEvent = VersionHelper.isOrAbove1_21_2() ?
|
||||
CoreReflections.constructor$SoundEvent.newInstance(soundId, Optional.of(jukeboxSong.range())) :
|
||||
CoreReflections.constructor$SoundEvent.newInstance(soundId, jukeboxSong.range(), false);
|
||||
Object soundHolder = CoreReflections.method$Holder$direct.invoke(null, soundEvent);
|
||||
song = CoreReflections.constructor$JukeboxSong.newInstance(soundHolder, ComponentUtils.adventureToMinecraft(jukeboxSong.description()), jukeboxSong.lengthInSeconds(), jukeboxSong.comparatorOutput());
|
||||
Object holder = CoreReflections.method$Registry$registerForHolder.invoke(null, registry, resourceLocation, song);
|
||||
CoreReflections.method$Holder$Reference$bindValue.invoke(holder, song);
|
||||
|
||||
@@ -19,6 +19,7 @@ public abstract class AbstractSoundManager implements SoundManager {
|
||||
protected final Map<Key, JukeboxSong> songs = new HashMap<>();
|
||||
protected final SoundParser soundParser;
|
||||
protected final SongParser songParser;
|
||||
protected final Map<Integer, Key> customSoundsInRegistry = new HashMap<>();
|
||||
|
||||
public AbstractSoundManager(CraftEngine plugin) {
|
||||
this.plugin = plugin;
|
||||
@@ -46,6 +47,8 @@ public abstract class AbstractSoundManager implements SoundManager {
|
||||
@Override
|
||||
public void runDelayedSyncTasks() {
|
||||
if (!VersionHelper.isOrAbove1_21()) return;
|
||||
// 问题是会踢客户端
|
||||
// this.registerSounds(this.byId.keySet());
|
||||
this.registerSongs(this.songs);
|
||||
}
|
||||
|
||||
@@ -60,6 +63,8 @@ public abstract class AbstractSoundManager implements SoundManager {
|
||||
|
||||
protected abstract void registerSongs(Map<Key, JukeboxSong> songs);
|
||||
|
||||
protected abstract void registerSounds(Collection<Key> sounds);
|
||||
|
||||
public class SongParser implements ConfigParser {
|
||||
public static final String[] CONFIG_SECTION_NAME = new String[] {"jukebox_songs", "jukebox_song", "jukebox-songs", "jukebox-song"};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user