diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java index c5691277..4d5e969b 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java @@ -82,7 +82,7 @@ public class AppleSkinProtocol implements LeavesProtocol { } case "natural_regeneration" -> { - boolean regeneration = player.serverLevel().getGameRules().getBoolean(GameRules.RULE_NATURAL_REGENERATION); + boolean regeneration = player.level().getGameRules().getBoolean(GameRules.RULE_NATURAL_REGENERATION); Boolean previousRegeneration = previousNaturalRegeneration.get(player); if (previousRegeneration == null || regeneration != previousRegeneration) { ProtocolUtils.sendBytebufPacket(player, NATURAL_REGENERATION_KEY, buf -> buf.writeBoolean(regeneration)); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/BBORProtocol.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/BBORProtocol.java index 6dccdb29..6698d367 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/BBORProtocol.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/BBORProtocol.java @@ -134,7 +134,7 @@ public class BBORProtocol implements LeavesProtocol { } private static void sendStructureList(@NotNull ServerPlayer player) { - final Registry structureRegistry = player.server.registryAccess().lookupOrThrow(Registries.STRUCTURE); + final Registry structureRegistry = MinecraftServer.getServer().registryAccess().lookupOrThrow(Registries.STRUCTURE); final Set structureIds = structureRegistry.entrySet().stream() .map(e -> e.getKey().location().toString()).collect(Collectors.toSet()); ProtocolUtils.sendBytebufPacket(player, STRUCTURE_LIST_SYNC, buf -> { diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/PcaSyncProtocol.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/PcaSyncProtocol.java index fea8d8aa..f04c1a31 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/PcaSyncProtocol.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/PcaSyncProtocol.java @@ -31,6 +31,8 @@ import org.leavesmc.leaves.protocol.core.LeavesCustomPayload; import org.leavesmc.leaves.protocol.core.LeavesProtocol; import org.leavesmc.leaves.protocol.core.ProtocolHandler; import org.leavesmc.leaves.protocol.core.ProtocolUtils; +import org.leavesmc.leaves.util.TagFactory; +import org.leavesmc.leaves.util.TagUtil; import java.util.HashMap; import java.util.HashSet; @@ -82,7 +84,7 @@ public class PcaSyncProtocol implements LeavesProtocol { @ProtocolHandler.PayloadReceiver(payload = SyncBlockEntityPayload.class) private static void syncBlockEntityHandler(ServerPlayer player, SyncBlockEntityPayload payload) { BlockPos pos = payload.pos; - ServerLevel world = player.serverLevel(); + ServerLevel world = player.level(); Bukkit.getGlobalRegionScheduler().run(MinecraftInternalPlugin.INSTANCE, (task) -> { BlockState blockState = world.getBlockState(pos); @@ -122,7 +124,7 @@ public class PcaSyncProtocol implements LeavesProtocol { private static void syncEntityHandler(ServerPlayer player, SyncEntityPayload payload) { MinecraftServer server = MinecraftServer.getServer(); int entityId = payload.entityId; - ServerLevel world = player.serverLevel(); + ServerLevel world = player.level(); Bukkit.getGlobalRegionScheduler().run(MinecraftInternalPlugin.INSTANCE, (task) -> { Entity entity = world.getEntity(entityId); @@ -188,7 +190,7 @@ public class PcaSyncProtocol implements LeavesProtocol { } public static void updateEntity(@NotNull ServerPlayer player, @NotNull Entity entity) { - CompoundTag nbt = entity.saveWithoutId(new CompoundTag()); + CompoundTag nbt = TagUtil.saveEntity(entity); ProtocolUtils.sendPayloadPacket(player, new UpdateEntityPayload(entity.level().dimension().location(), entity.getId(), nbt)); } diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/rei/REIServerProtocol.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/rei/REIServerProtocol.java index d708eeb8..1af1cdb2 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/rei/REIServerProtocol.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/rei/REIServerProtocol.java @@ -215,7 +215,7 @@ public class REIServerProtocol implements LeavesProtocol { } BiConsumer consumer = (ignored, c2sWholeBuf) -> { FriendlyByteBuf tmpBuf = new FriendlyByteBuf(Unpooled.buffer()).writeBytes(c2sWholeBuf.readByteArray()); - ItemStack itemStack = tmpBuf.readJsonWithCodec(ItemStack.OPTIONAL_CODEC); + ItemStack itemStack = tmpBuf.readLenientJsonWithCodec(ItemStack.OPTIONAL_CODEC); if (player.getInventory().add(itemStack.copy())) { RegistryFriendlyByteBuf s2cWholeBuf = ProtocolUtils.decorate(Unpooled.buffer()); s2cWholeBuf.writeJsonWithCodec(ItemStack.OPTIONAL_CODEC, itemStack.copy()); @@ -240,7 +240,7 @@ public class REIServerProtocol implements LeavesProtocol { } BiConsumer consumer = (ignored, c2sWholeBuf) -> { FriendlyByteBuf tmpBuf = new FriendlyByteBuf(Unpooled.buffer()).writeBytes(c2sWholeBuf.readByteArray()); - ItemStack itemStack = tmpBuf.readJsonWithCodec(ItemStack.OPTIONAL_CODEC); + ItemStack itemStack = tmpBuf.readLenientJsonWithCodec(ItemStack.OPTIONAL_CODEC); ItemStack stack = itemStack.copy(); AbstractContainerMenu menu = player.containerMenu; if (!menu.getCarried().isEmpty() && ItemStack.isSameItemSameComponents(menu.getCarried(), stack)) { @@ -270,7 +270,7 @@ public class REIServerProtocol implements LeavesProtocol { } BiConsumer consumer = (ignored, c2sWholeBuf) -> { FriendlyByteBuf tmpBuf = new FriendlyByteBuf(Unpooled.buffer()).writeBytes(c2sWholeBuf.readByteArray()); - ItemStack stack = tmpBuf.readJsonWithCodec(ItemStack.OPTIONAL_CODEC); + ItemStack stack = tmpBuf.readLenientJsonWithCodec(ItemStack.OPTIONAL_CODEC); int hotbarSlotId = tmpBuf.readVarInt(); if (hotbarSlotId >= 0 && hotbarSlotId < 9) { AbstractContainerMenu menu = player.containerMenu; diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxEntityDataProtocol.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxEntityDataProtocol.java index 8f5a3a4c..9faa1f27 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxEntityDataProtocol.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxEntityDataProtocol.java @@ -19,6 +19,7 @@ import org.leavesmc.leaves.protocol.core.LeavesCustomPayload; import org.leavesmc.leaves.protocol.core.LeavesProtocol; import org.leavesmc.leaves.protocol.core.ProtocolHandler; import org.leavesmc.leaves.protocol.core.ProtocolUtils; +import org.leavesmc.leaves.util.TagUtil; import java.util.HashMap; import java.util.Map; @@ -79,7 +80,7 @@ public class ServuxEntityDataProtocol implements LeavesProtocol { public static void onBlockEntityRequest(ServerPlayer player, BlockPos pos) { Bukkit.getGlobalRegionScheduler().run(MinecraftInternalPlugin.INSTANCE, (task) -> { - BlockEntity be = player.serverLevel().getBlockEntity(pos); + BlockEntity be = player.level().getBlockEntity(pos); CompoundTag nbt = be != null ? be.saveWithoutMetadata(player.registryAccess()) : new CompoundTag(); EntityDataPayload payload = new EntityDataPayload(EntityDataPayloadType.PACKET_S2C_BLOCK_NBT_RESPONSE_SIMPLE); @@ -91,8 +92,8 @@ public class ServuxEntityDataProtocol implements LeavesProtocol { public static void onEntityRequest(ServerPlayer player, int entityId) { Bukkit.getGlobalRegionScheduler().run(MinecraftInternalPlugin.INSTANCE, (task) -> { - Entity entity = player.serverLevel().getEntity(entityId); - CompoundTag nbt = entity != null ? entity.saveWithoutId(new CompoundTag()) : new CompoundTag(); + Entity entity = player.level().getEntity(entityId); + CompoundTag nbt = TagUtil.saveEntity(entity); EntityDataPayload payload = new EntityDataPayload(EntityDataPayloadType.PACKET_S2C_ENTITY_NBT_RESPONSE_SIMPLE); payload.entityId = entityId; diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxHudDataProtocol.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxHudDataProtocol.java index f0091b49..69022ef4 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxHudDataProtocol.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxHudDataProtocol.java @@ -70,7 +70,7 @@ public class ServuxHudDataProtocol implements LeavesProtocol { } public static void refreshRecipeManager(ServerPlayer player) { - Collection> recipes = player.server.getRecipeManager().getRecipes(); + Collection> recipes = MinecraftServer.getServer().getRecipeManager().getRecipes(); CompoundTag nbt = new CompoundTag(); ListTag list = new ListTag(); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxStructuresProtocol.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxStructuresProtocol.java index 99b413f1..fd08f36e 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxStructuresProtocol.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/ServuxStructuresProtocol.java @@ -142,7 +142,7 @@ public class ServuxStructuresProtocol implements LeavesProtocol { public static void initialSyncStructures(ServerPlayer player, int chunkRadius, int tickCounter) { UUID uuid = player.getUUID(); ChunkPos center = player.getLastSectionPos().chunk(); - Map references = getStructureReferences(player.serverLevel(), center, chunkRadius); + Map references = getStructureReferences(player.level(), center, chunkRadius); timeouts.remove(uuid); @@ -186,7 +186,7 @@ public class ServuxStructuresProtocol implements LeavesProtocol { } public static void sendStructures(ServerPlayer player, Map references, int tickCounter) { - ServerLevel world = player.serverLevel(); + ServerLevel world = player.level(); Map starts = getStructureStarts(world, references); if (!starts.isEmpty()) { @@ -265,7 +265,7 @@ public class ServuxStructuresProtocol implements LeavesProtocol { } if (!positionsToUpdate.isEmpty()) { - ServerLevel world = player.serverLevel(); + ServerLevel world = player.level(); ChunkPos center = player.getLastSectionPos().chunk(); Map references = new HashMap<>(); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/litematics/ServuxLitematicsProtocol.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/litematics/ServuxLitematicsProtocol.java index f7f60f48..5e82ea37 100755 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/litematics/ServuxLitematicsProtocol.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/litematics/ServuxLitematicsProtocol.java @@ -32,6 +32,7 @@ import org.leavesmc.leaves.protocol.servux.ServuxProtocol; import org.leavesmc.leaves.protocol.servux.litematics.placement.SchematicPlacement; import org.leavesmc.leaves.protocol.servux.litematics.utils.NbtUtils; import org.leavesmc.leaves.protocol.servux.litematics.utils.ReplaceBehavior; +import org.leavesmc.leaves.util.TagUtil; import java.util.HashMap; import java.util.List; @@ -131,7 +132,7 @@ public class ServuxLitematicsProtocol implements LeavesProtocol { if (!hasPermission(player)) { return; } - BlockEntity be = player.serverLevel().getBlockEntity(pos); + BlockEntity be = player.level().getBlockEntity(pos); CompoundTag tag = be != null ? be.saveWithFullMetadata(MinecraftServer.getServer().registryAccess()) : new CompoundTag(); ServuxLitematicaPayload payload = new ServuxLitematicaPayload(ServuxLitematicaPayloadType.PACKET_S2C_BLOCK_NBT_RESPONSE_SIMPLE); payload.pos = pos; @@ -143,7 +144,7 @@ public class ServuxLitematicsProtocol implements LeavesProtocol { if (!hasPermission(player)) { return; } - Entity entity = player.serverLevel().getEntity(entityId); + Entity entity = player.level().getEntity(entityId); if (entity == null) { return; } @@ -152,11 +153,11 @@ public class ServuxLitematicsProtocol implements LeavesProtocol { payload.entityId = entityId; if (entity instanceof net.minecraft.world.entity.player.Player) { ResourceLocation loc = EntityType.getKey(entity.getType()); - tag = entity.saveWithoutId(tag); + tag = TagUtil.saveEntity(entity); tag.putString("id", loc.toString()); payload.nbt = tag; encodeServerData(player, payload); - } else if (entity.saveAsPassenger(tag)) { + } else if (TagUtil.saveEntityAsPassenger(entity, tag)) { payload.nbt = tag; encodeServerData(player, payload); } @@ -167,7 +168,7 @@ public class ServuxLitematicsProtocol implements LeavesProtocol { return; } - ServerLevel world = player.serverLevel(); + ServerLevel world = player.level(); ChunkAccess chunk = world.getChunk(chunkPos.x, chunkPos.z, ChunkStatus.FULL, false); if (chunk == null) { @@ -194,14 +195,14 @@ public class ServuxLitematicsProtocol implements LeavesProtocol { } BlockEntity be = world.getBlockEntity(tePos); - CompoundTag beTag = be != null ? be.saveWithId(player.registryAccess()) : new CompoundTag(); + CompoundTag beTag = TagUtil.saveTileWithId(be); tileList.add(beTag); } for (Entity entity : entities) { CompoundTag entTag = new CompoundTag(); - if (entity.save(entTag)) { + if (TagUtil.saveEntity(entity, entTag)) { Vec3 posVec = new Vec3(entity.getX() - pos1.getX(), entity.getY() - pos1.getY(), entity.getZ() - pos1.getZ()); NbtUtils.writeEntityPositionToTag(posVec, entTag); entTag.putInt("entityId", entity.getId()); @@ -235,7 +236,7 @@ public class ServuxLitematicsProtocol implements LeavesProtocol { if (tags.getStringOr("Task", "").equals("LitematicaPaste")) { ServuxProtocol.LOGGER.debug("litematic_data: Servux Paste request from player {}", player.getName().getString()); - ServerLevel serverLevel = player.serverLevel(); + ServerLevel serverLevel = player.level(); long timeStart = System.currentTimeMillis(); SchematicPlacement placement = SchematicPlacement.createFromNbt(tags); ReplaceBehavior replaceMode = ReplaceBehavior.fromStringStatic(tags.getStringOr("ReplaceMode", ReplaceBehavior.NONE.name())); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/litematics/utils/EntityUtils.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/litematics/utils/EntityUtils.java index 5586a54d..c031382d 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/litematics/utils/EntityUtils.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/servux/litematics/utils/EntityUtils.java @@ -8,6 +8,7 @@ import net.minecraft.world.entity.EntitySpawnReason; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.level.Level; +import org.leavesmc.leaves.util.TagFactory; import javax.annotation.Nullable; import java.util.Optional; @@ -18,7 +19,7 @@ public class EntityUtils { @Nullable private static Entity createEntityFromNBTSingle(CompoundTag nbt, Level world) { try { - Optional optional = EntityType.create(nbt, world, EntitySpawnReason.LOAD); + Optional optional = EntityType.create(TagFactory.input(nbt), world, EntitySpawnReason.LOAD); if (optional.isPresent()) { Entity entity = optional.get(); diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/util/TagFactory.java b/leaves-server/src/main/java/org/leavesmc/leaves/util/TagFactory.java new file mode 100644 index 00000000..7e69073e --- /dev/null +++ b/leaves-server/src/main/java/org/leavesmc/leaves/util/TagFactory.java @@ -0,0 +1,29 @@ +package org.leavesmc.leaves.util; + +import net.minecraft.core.RegistryAccess; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.ProblemReporter; +import net.minecraft.world.level.storage.TagValueInput; +import net.minecraft.world.level.storage.TagValueOutput; + +public class TagFactory { + + private static final RegistryAccess.Frozen registryAccess = MinecraftServer.getServer().registryAccess(); + + public static TagValueOutput output() { + return TagValueOutput.createWithContext(ProblemReporter.DISCARDING, registryAccess); + } + + public static TagValueOutput output(CompoundTag tag) { + return TagValueOutput.createWrappingWithContext(ProblemReporter.DISCARDING, registryAccess, tag); + } + + public static TagValueInput input() { + return input(new CompoundTag()); + } + + public static TagValueInput input(CompoundTag tag) { + return (TagValueInput) TagValueInput.create(ProblemReporter.DISCARDING, registryAccess, tag); + } +} diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/util/TagUtil.java b/leaves-server/src/main/java/org/leavesmc/leaves/util/TagUtil.java new file mode 100644 index 00000000..e99edc6e --- /dev/null +++ b/leaves-server/src/main/java/org/leavesmc/leaves/util/TagUtil.java @@ -0,0 +1,45 @@ +package org.leavesmc.leaves.util; + +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.storage.TagValueOutput; +import org.jetbrains.annotations.Nullable; + +public class TagUtil { + + public static CompoundTag saveEntity(@Nullable Entity entity) { + if (entity == null) { + return new CompoundTag(); + } + TagValueOutput output = TagFactory.output(); + entity.save(output); + return output.buildResult(); + } + + public static boolean saveEntity(Entity entity, CompoundTag tag) { + if (entity == null) { + return false; + } + TagValueOutput output = TagFactory.output(tag); + return entity.save(output); + } + + public static CompoundTag saveTileWithId(@Nullable BlockEntity entity) { + if (entity == null) { + return new CompoundTag(); + } + TagValueOutput output = TagFactory.output(); + entity.saveWithId(output); + return output.buildResult(); + } + + public static boolean saveEntityAsPassenger(@Nullable Entity entity, CompoundTag tag) { + if (entity == null) { + return false; + } + TagValueOutput output = TagFactory.output(tag); + return entity.saveAsPassenger(output); + } + +}