mirror of
https://github.com/LeavesMC/Leaves.git
synced 2025-12-29 20:09:23 +00:00
Roughly fix protocols
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -134,7 +134,7 @@ public class BBORProtocol implements LeavesProtocol {
|
||||
}
|
||||
|
||||
private static void sendStructureList(@NotNull ServerPlayer player) {
|
||||
final Registry<Structure> structureRegistry = player.server.registryAccess().lookupOrThrow(Registries.STRUCTURE);
|
||||
final Registry<Structure> structureRegistry = MinecraftServer.getServer().registryAccess().lookupOrThrow(Registries.STRUCTURE);
|
||||
final Set<String> structureIds = structureRegistry.entrySet().stream()
|
||||
.map(e -> e.getKey().location().toString()).collect(Collectors.toSet());
|
||||
ProtocolUtils.sendBytebufPacket(player, STRUCTURE_LIST_SYNC, buf -> {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -215,7 +215,7 @@ public class REIServerProtocol implements LeavesProtocol {
|
||||
}
|
||||
BiConsumer<ResourceLocation, RegistryFriendlyByteBuf> 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<ResourceLocation, RegistryFriendlyByteBuf> 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<ResourceLocation, RegistryFriendlyByteBuf> 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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -70,7 +70,7 @@ public class ServuxHudDataProtocol implements LeavesProtocol {
|
||||
}
|
||||
|
||||
public static void refreshRecipeManager(ServerPlayer player) {
|
||||
Collection<RecipeHolder<?>> recipes = player.server.getRecipeManager().getRecipes();
|
||||
Collection<RecipeHolder<?>> recipes = MinecraftServer.getServer().getRecipeManager().getRecipes();
|
||||
CompoundTag nbt = new CompoundTag();
|
||||
ListTag list = new ListTag();
|
||||
|
||||
|
||||
@@ -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<Structure, LongSet> references = getStructureReferences(player.serverLevel(), center, chunkRadius);
|
||||
Map<Structure, LongSet> 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<Structure, LongSet> references, int tickCounter) {
|
||||
ServerLevel world = player.serverLevel();
|
||||
ServerLevel world = player.level();
|
||||
Map<ChunkPos, StructureStart> 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<Structure, LongSet> references = new HashMap<>();
|
||||
|
||||
|
||||
@@ -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()));
|
||||
|
||||
@@ -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<Entity> optional = EntityType.create(nbt, world, EntitySpawnReason.LOAD);
|
||||
Optional<Entity> optional = EntityType.create(TagFactory.input(nbt), world, EntitySpawnReason.LOAD);
|
||||
|
||||
if (optional.isPresent()) {
|
||||
Entity entity = optional.get();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user