移除WorldVariables
This commit is contained in:
parent
a8d71ad3f3
commit
9a88b01de5
4 changed files with 0 additions and 125 deletions
|
@ -1,54 +0,0 @@
|
|||
package com.atsuishio.superbwarfare.capability.player;
|
||||
|
||||
import com.atsuishio.superbwarfare.ModUtils;
|
||||
import com.atsuishio.superbwarfare.network.message.SavedDataSyncMessage;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.LevelAccessor;
|
||||
import net.minecraft.world.level.saveddata.SavedData;
|
||||
import net.neoforged.neoforge.network.PacketDistributor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
public class ModVariables {
|
||||
|
||||
// 这玩意有用吗?
|
||||
|
||||
public static class WorldVariables extends SavedData {
|
||||
public static final String DATA_NAME = ModUtils.MODID + "_world_variables";
|
||||
|
||||
public static WorldVariables load(CompoundTag tag, HolderLookup.Provider provider) {
|
||||
WorldVariables data = new WorldVariables();
|
||||
data.read(tag);
|
||||
return data;
|
||||
}
|
||||
|
||||
public void read(CompoundTag nbt) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CompoundTag save(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider registries) {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public void syncData(LevelAccessor world) {
|
||||
this.setDirty();
|
||||
if (world instanceof Level level && !level.isClientSide()) {
|
||||
PacketDistributor.sendToAllPlayers(new SavedDataSyncMessage(1, this, null));
|
||||
}
|
||||
}
|
||||
|
||||
public static WorldVariables clientSide = new WorldVariables();
|
||||
|
||||
public static WorldVariables get(LevelAccessor world) {
|
||||
if (world instanceof ServerLevel level)
|
||||
return level.getDataStorage().computeIfAbsent(new Factory<>(
|
||||
WorldVariables::new, WorldVariables::load, null
|
||||
), DATA_NAME);
|
||||
return clientSide;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -3,13 +3,11 @@ package com.atsuishio.superbwarfare.capability.player;
|
|||
import com.atsuishio.superbwarfare.ModUtils;
|
||||
import com.atsuishio.superbwarfare.capability.ModCapabilities;
|
||||
import com.atsuishio.superbwarfare.network.message.PlayerVariablesSyncMessage;
|
||||
import com.atsuishio.superbwarfare.network.message.SavedDataSyncMessage;
|
||||
import com.atsuishio.superbwarfare.tools.AmmoType;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.level.saveddata.SavedData;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.fml.common.EventBusSubscriber;
|
||||
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||
|
@ -146,22 +144,4 @@ public class PlayerVariable {
|
|||
var cap = player.getCapability(ModCapabilities.PLAYER_VARIABLE, null);
|
||||
if (cap != null) cap.syncPlayerVariables(player);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
|
||||
if (event.getEntity().level().isClientSide()) return;
|
||||
SavedData worldData = ModVariables.WorldVariables.get(event.getEntity().level());
|
||||
if (worldData != null) {
|
||||
PacketDistributor.sendToPlayer((ServerPlayer) event.getEntity(), new SavedDataSyncMessage(1, worldData, null));
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onPlayerChangedDimension(PlayerEvent.PlayerChangedDimensionEvent event) {
|
||||
if (event.getEntity().level().isClientSide()) return;
|
||||
SavedData worldData = ModVariables.WorldVariables.get(event.getEntity().level());
|
||||
if (worldData != null) {
|
||||
PacketDistributor.sendToPlayer((ServerPlayer) event.getEntity(), new SavedDataSyncMessage(1, worldData, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package com.atsuishio.superbwarfare.network;
|
||||
|
||||
import com.atsuishio.superbwarfare.network.message.PlayerVariablesSyncMessage;
|
||||
import com.atsuishio.superbwarfare.network.message.SavedDataSyncMessage;
|
||||
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
|
||||
import net.neoforged.neoforge.network.registration.PayloadRegistrar;
|
||||
|
||||
|
@ -14,11 +13,5 @@ public class NetworkRegistry {
|
|||
PlayerVariablesSyncMessage.STREAM_CODEC,
|
||||
PlayerVariablesSyncMessage::handler
|
||||
);
|
||||
|
||||
registrar.playToClient(
|
||||
SavedDataSyncMessage.TYPE,
|
||||
SavedDataSyncMessage.STREAM_CODEC,
|
||||
SavedDataSyncMessage::handler
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
package com.atsuishio.superbwarfare.network.message;
|
||||
|
||||
import com.atsuishio.superbwarfare.ModUtils;
|
||||
import com.atsuishio.superbwarfare.capability.player.ModVariables;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.codec.ByteBufCodecs;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.world.level.saveddata.SavedData;
|
||||
import net.neoforged.neoforge.network.handling.IPayloadContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public record SavedDataSyncMessage(
|
||||
int messageType,
|
||||
SavedData data,
|
||||
HolderLookup.Provider registries
|
||||
) implements CustomPacketPayload {
|
||||
public static final CustomPacketPayload.Type<SavedDataSyncMessage> TYPE = new CustomPacketPayload.Type<>(ModUtils.loc("saved_data_sync"));
|
||||
|
||||
public static final StreamCodec<ByteBuf, SavedDataSyncMessage> STREAM_CODEC = StreamCodec.composite(
|
||||
ByteBufCodecs.INT,
|
||||
SavedDataSyncMessage::messageType,
|
||||
ByteBufCodecs.COMPOUND_TAG,
|
||||
SavedDataSyncMessage::getNBT,
|
||||
(type, nbt) -> new SavedDataSyncMessage(type, null, null)
|
||||
);
|
||||
|
||||
public CompoundTag getNBT() {
|
||||
return this.data.save(new CompoundTag(), registries);
|
||||
}
|
||||
|
||||
public static void handler(final SavedDataSyncMessage message, final IPayloadContext context) {
|
||||
if (message.data != null && message.messageType != 0) {
|
||||
ModVariables.WorldVariables.clientSide = (ModVariables.WorldVariables) message.data;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Type<? extends CustomPacketPayload> type() {
|
||||
return TYPE;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue