Use ResourceLocation.tryParse for SoundEvent

This commit is contained in:
fullwall 2024-07-27 21:03:38 +08:00
parent c9696cc6c1
commit f27fe08c9c
13 changed files with 112 additions and 82 deletions

View File

@ -1777,8 +1777,12 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEffect getSoundEffect(NPC npc, SoundEffect snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: SoundEffect.a.get(new MinecraftKey(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd
: SoundEffect.a.get(data.contains(":") ? new MinecraftKey(data.split(":")[0], data.split(":")[1])
: new MinecraftKey(data));
}
public static void initNetworkManager(NetworkManager network) {

View File

@ -1836,8 +1836,12 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEffect getSoundEffect(NPC npc, SoundEffect snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: SoundEffect.a.get(new MinecraftKey(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd
: SoundEffect.a.get(data.contains(":") ? new MinecraftKey(data.split(":")[0], data.split(":")[1])
: new MinecraftKey(data));
}
public static void initNetworkManager(NetworkManager network) {

View File

@ -1844,8 +1844,12 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEffect getSoundEffect(NPC npc, SoundEffect snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: SoundEffect.a.get(new MinecraftKey(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd
: SoundEffect.a.get(data.contains(":") ? new MinecraftKey(data.split(":")[0], data.split(":")[1])
: new MinecraftKey(data));
}
public static void initNetworkManager(NetworkManager network) {

View File

@ -1934,8 +1934,11 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEffect getSoundEffect(NPC npc, SoundEffect snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: IRegistry.SOUND_EVENT.get(new MinecraftKey(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd
: IRegistry.SOUND_EVENT.get(data.contains(":") ? MinecraftKey.a(data) : new MinecraftKey(data));
}
public static void initNetworkManager(NetworkManager network) {

View File

@ -2015,8 +2015,11 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEffect getSoundEffect(NPC npc, SoundEffect snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: IRegistry.SOUND_EVENT.get(new MinecraftKey(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd
: IRegistry.SOUND_EVENT.get(data.contains(":") ? MinecraftKey.a(data) : new MinecraftKey(data));
}
public static void initNetworkManager(NetworkManager network) {

View File

@ -2059,8 +2059,11 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEffect getSoundEffect(NPC npc, SoundEffect snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: IRegistry.SOUND_EVENT.get(new MinecraftKey(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd
: IRegistry.SOUND_EVENT.get(data.contains(":") ? MinecraftKey.a(data) : new MinecraftKey(data));
}
public static void initNetworkManager(NetworkManager network) {

View File

@ -2052,8 +2052,11 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEffect getSoundEffect(NPC npc, SoundEffect snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: IRegistry.SOUND_EVENT.get(new MinecraftKey(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd
: IRegistry.SOUND_EVENT.get(data.contains(":") ? MinecraftKey.a(data) : new MinecraftKey(data));
}
public static void initNetworkManager(NetworkManager network) {

View File

@ -372,8 +372,8 @@ public class NMSImpl implements NMSBridge {
return;
try {
CHUNKMAP_UPDATE_PLAYER_STATUS.invoke(
((ServerChunkCache) ((ServerLevel) handle.level).getChunkSource()).chunkMap, handle, !remove);
CHUNKMAP_UPDATE_PLAYER_STATUS.invoke(((ServerChunkCache) handle.level.getChunkSource()).chunkMap, handle,
!remove);
} catch (Throwable e) {
e.printStackTrace();
}
@ -2059,9 +2059,10 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEvent getSoundEffect(NPC npc, SoundEvent snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: Registry.SOUND_EVENT
.get(new ResourceLocation(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd : Registry.SOUND_EVENT.get(ResourceLocation.tryParse(data));
}
public static void initNetworkManager(Connection network) {

View File

@ -2068,9 +2068,10 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEvent getSoundEffect(NPC npc, SoundEvent snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: Registry.SOUND_EVENT
.get(new ResourceLocation(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd : Registry.SOUND_EVENT.get(ResourceLocation.tryParse(data));
}
public static void initNetworkManager(Connection network) {

View File

@ -934,22 +934,6 @@ public class NMSImpl implements NMSBridge {
return handle.valid && handle.isAlive();
}
@Override
public void positionInteractionText(org.bukkit.entity.Player player, org.bukkit.entity.Entity entity,
org.bukkit.entity.Entity mount, double offset) {
offset += getRidingHeightOffset(entity, mount) + 0.5;
sendPacket(player,
new ClientboundBundlePacket(List.of(
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_WIDTH, 0f).value(),
new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, (float) offset).value(),
new SynchedEntityData.DataItem<>(DATA_POSE, Pose.CROAKING).value(),
new SynchedEntityData.DataItem<>(DATA_NAME_VISIBLE, true).value())),
new ClientboundSetPassengersPacket(getHandle(mount)),
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, 999999f).value())))));
}
@Override
public void load(CommandManager manager) {
registerTraitWithCommand(manager, EnderDragonTrait.class);
@ -1314,6 +1298,22 @@ public class NMSImpl implements NMSBridge {
};
}
@Override
public void positionInteractionText(org.bukkit.entity.Player player, org.bukkit.entity.Entity entity,
org.bukkit.entity.Entity mount, double offset) {
offset += getRidingHeightOffset(entity, mount) + 0.5;
sendPacket(player,
new ClientboundBundlePacket(List.of(
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_WIDTH, 0f).value(),
new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, (float) offset).value(),
new SynchedEntityData.DataItem<>(DATA_POSE, Pose.CROAKING).value(),
new SynchedEntityData.DataItem<>(DATA_NAME_VISIBLE, true).value())),
new ClientboundSetPassengersPacket(getHandle(mount)),
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, 999999f).value())))));
}
@Override
public void registerEntityClass(Class<?> clazz) {
if (ENTITY_REGISTRY == null)
@ -2295,9 +2295,10 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEvent getSoundEffect(NPC npc, SoundEvent snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: BuiltInRegistries.SOUND_EVENT
.get(new ResourceLocation(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd : BuiltInRegistries.SOUND_EVENT.get(ResourceLocation.tryParse(data));
}
public static void initNetworkManager(Connection network) {

View File

@ -911,22 +911,6 @@ public class NMSImpl implements NMSBridge {
return handle.valid && handle.isAlive();
}
@Override
public void positionInteractionText(org.bukkit.entity.Player player, org.bukkit.entity.Entity entity,
org.bukkit.entity.Entity mount, double offset) {
offset += getRidingHeightOffset(entity, mount);
sendPacket(player,
new ClientboundBundlePacket(List.of(
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_WIDTH, 0f).value(),
new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, (float) offset).value(),
new SynchedEntityData.DataItem<>(DATA_POSE, Pose.CROAKING).value(),
new SynchedEntityData.DataItem<>(DATA_NAME_VISIBLE, true).value())),
new ClientboundSetPassengersPacket(getHandle(mount)),
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, 999999f).value())))));
}
@Override
public void load(CommandManager manager) {
registerTraitWithCommand(manager, EnderDragonTrait.class);
@ -1301,6 +1285,22 @@ public class NMSImpl implements NMSBridge {
};
}
@Override
public void positionInteractionText(org.bukkit.entity.Player player, org.bukkit.entity.Entity entity,
org.bukkit.entity.Entity mount, double offset) {
offset += getRidingHeightOffset(entity, mount);
sendPacket(player,
new ClientboundBundlePacket(List.of(
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_WIDTH, 0f).value(),
new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, (float) offset).value(),
new SynchedEntityData.DataItem<>(DATA_POSE, Pose.CROAKING).value(),
new SynchedEntityData.DataItem<>(DATA_NAME_VISIBLE, true).value())),
new ClientboundSetPassengersPacket(getHandle(mount)),
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, 999999f).value())))));
}
@Override
public void registerEntityClass(Class<?> clazz) {
if (ENTITY_REGISTRY == null)
@ -2297,9 +2297,10 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEvent getSoundEffect(NPC npc, SoundEvent snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: BuiltInRegistries.SOUND_EVENT
.get(new ResourceLocation(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd : BuiltInRegistries.SOUND_EVENT.get(ResourceLocation.tryParse(data));
}
public static boolean isLeashed(NPC npc, Supplier<Boolean> isLeashed, Mob entity) {

View File

@ -58,7 +58,7 @@ import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
public class EntityHumanNPC extends ServerPlayer implements NPCHolder, SkinnableEntity, ForwardingMobAI {
private MobAI ai;
private final MobAI ai;
private int jumpTicks = 0;
private final CitizensNPC npc;
private boolean setBukkitEntity;
@ -73,12 +73,13 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
ai = new BasicMobAI(this);
skinTracker = new SkinPacketTracker(this);
try {
GAMEMODE_SETTING.invoke(gameMode, GameType.SURVIVAL, null);
GAMEMODE_SETTER.invoke(gameMode, GameType.SURVIVAL, null);
} catch (Throwable e) {
e.printStackTrace();
}
initialise(minecraftServer, ci);
} else {
ai = null;
skinTracker = null;
}
}
@ -474,6 +475,6 @@ public class EntityHumanNPC extends ServerPlayer implements NPCHolder, Skinnable
}
private static final float EPSILON = 0.003F;
private static final MethodHandle GAMEMODE_SETTING = NMS.getFirstMethodHandle(ServerPlayerGameMode.class, true,
private static final MethodHandle GAMEMODE_SETTER = NMS.getFirstMethodHandle(ServerPlayerGameMode.class, true,
GameType.class, GameType.class);
}

View File

@ -890,22 +890,6 @@ public class NMSImpl implements NMSBridge {
return handle.valid && handle.isAlive();
}
@Override
public void positionInteractionText(org.bukkit.entity.Player player, org.bukkit.entity.Entity entity,
org.bukkit.entity.Entity mount, double offset) {
offset += getRidingHeightOffset(entity, mount);
sendPacket(player,
new ClientboundBundlePacket(List.of(
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_WIDTH, 0f).value(),
new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, (float) offset).value(),
new SynchedEntityData.DataItem<>(DATA_POSE, Pose.CROAKING).value(),
new SynchedEntityData.DataItem<>(DATA_NAME_VISIBLE, true).value())),
new ClientboundSetPassengersPacket(getHandle(mount)),
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, 999999f).value())))));
}
@Override
public void load(CommandManager manager) {
registerTraitWithCommand(manager, EnderDragonTrait.class);
@ -1279,6 +1263,22 @@ public class NMSImpl implements NMSBridge {
};
}
@Override
public void positionInteractionText(org.bukkit.entity.Player player, org.bukkit.entity.Entity entity,
org.bukkit.entity.Entity mount, double offset) {
offset += getRidingHeightOffset(entity, mount);
sendPacket(player,
new ClientboundBundlePacket(List.of(
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_WIDTH, 0f).value(),
new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, (float) offset).value(),
new SynchedEntityData.DataItem<>(DATA_POSE, Pose.CROAKING).value(),
new SynchedEntityData.DataItem<>(DATA_NAME_VISIBLE, true).value())),
new ClientboundSetPassengersPacket(getHandle(mount)),
new ClientboundSetEntityDataPacket(entity.getEntityId(),
List.of(new SynchedEntityData.DataItem<>(INTERACTION_HEIGHT, 999999f).value())))));
}
@Override
public void registerEntityClass(Class<?> clazz) {
if (ENTITY_REGISTRY == null)
@ -2283,9 +2283,10 @@ public class NMSImpl implements NMSBridge {
}
public static SoundEvent getSoundEffect(NPC npc, SoundEvent snd, NPC.Metadata meta) {
return npc == null || !npc.data().has(meta) ? snd
: BuiltInRegistries.SOUND_EVENT.get(
ResourceLocation.withDefaultNamespace(npc.data().get(meta, snd == null ? "" : snd.toString())));
if (npc == null)
return snd;
String data = npc.data().get(meta);
return data == null ? snd : BuiltInRegistries.SOUND_EVENT.get(ResourceLocation.tryParse(data));
}
public static boolean isLeashed(NPC npc, Supplier<Boolean> isLeashed, Mob entity) {