164 lines
5.3 KiB
Java
164 lines
5.3 KiB
Java
package net.paladrill.paladrillmod;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
|
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
|
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
|
import net.minecraftforge.fml.common.network.ByteBufUtils;
|
|
|
|
import net.minecraft.world.storage.WorldSavedData;
|
|
import net.minecraft.world.World;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
public class PaladrillmodModVariables {
|
|
public static class MapVariables extends WorldSavedData {
|
|
public static final String DATA_NAME = "paladrillmod_mapvars";
|
|
public boolean CoffreNord = false;
|
|
public double UnclaimFindersFetcher = 0;
|
|
public boolean HideUnclaimfinder = false;
|
|
public double chancePalaCrusher = 0;
|
|
public boolean isGreenUnclaimFinderInHand = false;
|
|
public boolean isOrangeUnclaimFinderInHand = false;
|
|
public MapVariables() {
|
|
super(DATA_NAME);
|
|
}
|
|
|
|
public MapVariables(String s) {
|
|
super(s);
|
|
}
|
|
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound nbt) {
|
|
CoffreNord = nbt.getBoolean("CoffreNord");
|
|
UnclaimFindersFetcher = nbt.getDouble("UnclaimFindersFetcher");
|
|
HideUnclaimfinder = nbt.getBoolean("HideUnclaimfinder");
|
|
chancePalaCrusher = nbt.getDouble("chancePalaCrusher");
|
|
isGreenUnclaimFinderInHand = nbt.getBoolean("isGreenUnclaimFinderInHand");
|
|
isOrangeUnclaimFinderInHand = nbt.getBoolean("isOrangeUnclaimFinderInHand");
|
|
}
|
|
|
|
@Override
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
|
|
nbt.setBoolean("CoffreNord", CoffreNord);
|
|
nbt.setDouble("UnclaimFindersFetcher", UnclaimFindersFetcher);
|
|
nbt.setBoolean("HideUnclaimfinder", HideUnclaimfinder);
|
|
nbt.setDouble("chancePalaCrusher", chancePalaCrusher);
|
|
nbt.setBoolean("isGreenUnclaimFinderInHand", isGreenUnclaimFinderInHand);
|
|
nbt.setBoolean("isOrangeUnclaimFinderInHand", isOrangeUnclaimFinderInHand);
|
|
return nbt;
|
|
}
|
|
|
|
public void syncData(World world) {
|
|
this.markDirty();
|
|
if (world.isRemote) {
|
|
PaladrillmodMod.PACKET_HANDLER.sendToServer(new WorldSavedDataSyncMessage(0, this));
|
|
} else {
|
|
PaladrillmodMod.PACKET_HANDLER.sendToAll(new WorldSavedDataSyncMessage(0, this));
|
|
}
|
|
}
|
|
|
|
public static MapVariables get(World world) {
|
|
MapVariables instance = (MapVariables) world.getMapStorage().getOrLoadData(MapVariables.class, DATA_NAME);
|
|
if (instance == null) {
|
|
instance = new MapVariables();
|
|
world.getMapStorage().setData(DATA_NAME, instance);
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
public static class WorldVariables extends WorldSavedData {
|
|
public static final String DATA_NAME = "paladrillmod_worldvars";
|
|
public WorldVariables() {
|
|
super(DATA_NAME);
|
|
}
|
|
|
|
public WorldVariables(String s) {
|
|
super(s);
|
|
}
|
|
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound nbt) {
|
|
}
|
|
|
|
@Override
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
|
|
return nbt;
|
|
}
|
|
|
|
public void syncData(World world) {
|
|
this.markDirty();
|
|
if (world.isRemote) {
|
|
PaladrillmodMod.PACKET_HANDLER.sendToServer(new WorldSavedDataSyncMessage(1, this));
|
|
} else {
|
|
PaladrillmodMod.PACKET_HANDLER.sendToDimension(new WorldSavedDataSyncMessage(1, this), world.provider.getDimension());
|
|
}
|
|
}
|
|
|
|
public static WorldVariables get(World world) {
|
|
WorldVariables instance = (WorldVariables) world.getPerWorldStorage().getOrLoadData(WorldVariables.class, DATA_NAME);
|
|
if (instance == null) {
|
|
instance = new WorldVariables();
|
|
world.getPerWorldStorage().setData(DATA_NAME, instance);
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
public static class WorldSavedDataSyncMessageHandler implements IMessageHandler<WorldSavedDataSyncMessage, IMessage> {
|
|
@Override
|
|
public IMessage onMessage(WorldSavedDataSyncMessage message, MessageContext context) {
|
|
if (context.side == Side.SERVER)
|
|
context.getServerHandler().player.getServerWorld()
|
|
.addScheduledTask(() -> syncData(message, context, context.getServerHandler().player.world));
|
|
else
|
|
Minecraft.getMinecraft().addScheduledTask(() -> syncData(message, context, Minecraft.getMinecraft().player.world));
|
|
return null;
|
|
}
|
|
|
|
private void syncData(WorldSavedDataSyncMessage message, MessageContext context, World world) {
|
|
if (context.side == Side.SERVER) {
|
|
message.data.markDirty();
|
|
if (message.type == 0)
|
|
PaladrillmodMod.PACKET_HANDLER.sendToAll(message);
|
|
else
|
|
PaladrillmodMod.PACKET_HANDLER.sendToDimension(message, world.provider.getDimension());
|
|
}
|
|
if (message.type == 0) {
|
|
world.getMapStorage().setData(MapVariables.DATA_NAME, message.data);
|
|
} else {
|
|
world.getPerWorldStorage().setData(WorldVariables.DATA_NAME, message.data);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class WorldSavedDataSyncMessage implements IMessage {
|
|
public int type;
|
|
public WorldSavedData data;
|
|
public WorldSavedDataSyncMessage() {
|
|
}
|
|
|
|
public WorldSavedDataSyncMessage(int type, WorldSavedData data) {
|
|
this.type = type;
|
|
this.data = data;
|
|
}
|
|
|
|
@Override
|
|
public void toBytes(io.netty.buffer.ByteBuf buf) {
|
|
buf.writeInt(this.type);
|
|
ByteBufUtils.writeTag(buf, this.data.writeToNBT(new NBTTagCompound()));
|
|
}
|
|
|
|
@Override
|
|
public void fromBytes(io.netty.buffer.ByteBuf buf) {
|
|
this.type = buf.readInt();
|
|
if (this.type == 0)
|
|
this.data = new MapVariables();
|
|
else
|
|
this.data = new WorldVariables();
|
|
this.data.readFromNBT(ByteBufUtils.readTag(buf));
|
|
}
|
|
}
|
|
}
|