2023-03-30 17:19:42 +02:00
|
|
|
/*
|
2023-04-18 21:08:38 +02:00
|
|
|
* Copyright (c) 2023 FalsePattern
|
|
|
|
|
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
|
|
|
|
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/
|
|
|
|
|
* or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
|
2023-03-30 17:19:42 +02:00
|
|
|
*/
|
|
|
|
|
|
2023-03-09 21:44:17 +01:00
|
|
|
package com.falsepattern.chunk.api;
|
|
|
|
|
|
2023-03-30 17:48:26 +02:00
|
|
|
import com.falsepattern.lib.StableAPI;
|
2023-03-30 17:17:11 +02:00
|
|
|
import org.jetbrains.annotations.Contract;
|
2023-11-15 19:39:43 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
2023-03-28 23:58:39 +02:00
|
|
|
|
2023-03-09 21:44:17 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2023-03-30 17:17:11 +02:00
|
|
|
import net.minecraft.world.chunk.Chunk;
|
|
|
|
|
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
|
2023-03-28 23:58:39 +02:00
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
2023-03-09 21:44:17 +01:00
|
|
|
|
|
|
|
|
/**
|
2023-03-30 17:17:11 +02:00
|
|
|
* Singleton instances that can manage custom in chunks. This class only manages the registration of
|
|
|
|
|
* managers. For the actual networking and data storage, see the internal interfaces of this interface.
|
|
|
|
|
* Note: This interface does nothing by itself, you also need to implement one or more of the internal interfaces.
|
2023-03-30 17:48:26 +02:00
|
|
|
*
|
|
|
|
|
* @since 0.1.0
|
|
|
|
|
* @see ChunkDataManager.PacketDataManager
|
|
|
|
|
* @see ChunkDataManager.ChunkNBTDataManager
|
|
|
|
|
* @see ChunkDataManager.SectionNBTDataManager
|
|
|
|
|
* @see ChunkDataRegistry
|
|
|
|
|
* @author FalsePattern
|
|
|
|
|
* @version 0.1.0
|
2023-03-09 21:44:17 +01:00
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI(since = "0.1.0")
|
2023-03-30 17:17:11 +02:00
|
|
|
public interface ChunkDataManager {
|
2023-03-28 23:58:39 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return The domain of this manager. Usually the modid of the mod that owns this manager.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
@Contract(pure = true)
|
2023-03-28 23:58:39 +02:00
|
|
|
String domain();
|
|
|
|
|
|
2023-03-09 21:44:17 +01:00
|
|
|
/**
|
2023-03-28 23:58:39 +02:00
|
|
|
* @return The id of this manager. Usually the name of the manager. Unique per domain.
|
2023-03-09 21:44:17 +01:00
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
@Contract(pure = true)
|
2023-03-09 21:44:17 +01:00
|
|
|
String id();
|
2023-03-28 23:58:39 +02:00
|
|
|
|
2023-03-09 21:44:17 +01:00
|
|
|
/**
|
2023-03-30 17:17:11 +02:00
|
|
|
* Implement this interface if you want to synchronize your data with the client.
|
2023-03-30 17:48:26 +02:00
|
|
|
*
|
|
|
|
|
* @since 0.1.0
|
|
|
|
|
* @author FalsePattern
|
|
|
|
|
* @version 0.1.0
|
2023-03-09 21:44:17 +01:00
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI(since = "0.1.0")
|
2023-03-30 17:17:11 +02:00
|
|
|
interface PacketDataManager extends ChunkDataManager {
|
|
|
|
|
/**
|
|
|
|
|
* @return The maximum amount of bytes your data can take up in a packet.
|
2023-03-30 17:40:30 +02:00
|
|
|
*
|
|
|
|
|
* @implNote This is used to determine the size of the packet compression/decompression buffer.
|
2023-07-16 13:58:46 +02:00
|
|
|
* Only called ONCE, during registration!
|
2023-03-30 17:17:11 +02:00
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
@Contract(pure = true)
|
|
|
|
|
int maxPacketSize();
|
2023-03-28 23:58:39 +02:00
|
|
|
|
2023-03-30 17:17:11 +02:00
|
|
|
/**
|
|
|
|
|
* Serializes your data into a packet.
|
|
|
|
|
*
|
|
|
|
|
* @param chunk The chunk to serialize.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
void writeToBuffer(Chunk chunk, int ebsMask, boolean forceUpdate, ByteBuffer data);
|
2023-03-28 23:58:39 +02:00
|
|
|
|
2023-03-30 17:17:11 +02:00
|
|
|
/**
|
|
|
|
|
* Deserializes your data from a packet.
|
|
|
|
|
* Mutates this object.
|
|
|
|
|
*
|
|
|
|
|
* @param chunk The chunk to deserialize.
|
|
|
|
|
* @param buffer The packet buffer to read from.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
void readFromBuffer(Chunk chunk, int ebsMask, boolean forceUpdate, ByteBuffer buffer);
|
|
|
|
|
}
|
2023-03-28 23:58:39 +02:00
|
|
|
|
2023-11-15 19:39:43 +01:00
|
|
|
/**
|
|
|
|
|
* The common superinterface for ChunkNBTDataManager and SectionNBTDataManager.
|
|
|
|
|
* Contains version information and messages for users attempting to upgrade/remove versions.
|
|
|
|
|
*/
|
|
|
|
|
@StableAPI(since = "0.4.0")
|
|
|
|
|
interface NBTDataManager extends ChunkDataManager {
|
|
|
|
|
/**
|
|
|
|
|
* @return The current version of the data manager
|
|
|
|
|
*/
|
|
|
|
|
@StableAPI.Expose
|
|
|
|
|
@Contract(pure = true)
|
|
|
|
|
@NotNull
|
|
|
|
|
String version();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return The message to show to users when a world is opened with this mod for the first time.
|
|
|
|
|
* Return null to show no message, and treat the manager as fully compatible with vanilla.
|
|
|
|
|
*/
|
|
|
|
|
@StableAPI.Expose
|
|
|
|
|
@Contract(pure = true)
|
|
|
|
|
@Nullable
|
|
|
|
|
String newInstallDescription();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return The message to show to users when this mod is removed and they try to load the world (stored in the
|
|
|
|
|
* world's NBT during save)
|
|
|
|
|
*/
|
|
|
|
|
@StableAPI.Expose
|
|
|
|
|
@Contract(pure = true)
|
|
|
|
|
@NotNull
|
|
|
|
|
String uninstallMessage();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param priorVersion The version of the manager this world was saved with.
|
|
|
|
|
* @return A warning message to show to the user when upgrading.\
|
|
|
|
|
* If null, the manager is treated as fully compatible with the old version, and no warning is shown.
|
|
|
|
|
*/
|
|
|
|
|
@StableAPI.Expose
|
|
|
|
|
@Contract(pure = true)
|
|
|
|
|
@Nullable
|
|
|
|
|
String versionChangeMessage(String priorVersion);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 21:44:17 +01:00
|
|
|
/**
|
2023-03-30 17:17:11 +02:00
|
|
|
* Implement this interface if you want to save your data to disk. This is called once per chunk.
|
2023-03-30 17:48:26 +02:00
|
|
|
*
|
|
|
|
|
* @since 0.1.0
|
|
|
|
|
* @author FalsePattern
|
|
|
|
|
* @version 0.1.0
|
2023-03-09 21:44:17 +01:00
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI(since = "0.1.0")
|
2023-11-15 19:39:43 +01:00
|
|
|
interface ChunkNBTDataManager extends NBTDataManager {
|
2023-03-30 17:17:11 +02:00
|
|
|
/**
|
|
|
|
|
* If false, the given nbt compound will be a freshly created object that gets inserted into the actual
|
|
|
|
|
* level NBT tag under the `domain:id` name.
|
|
|
|
|
* <p>
|
|
|
|
|
* If true, the nbt tag passed in into the write/read methods will be the raw Level NBT tag without filtering.
|
|
|
|
|
*
|
|
|
|
|
* @implNote This is used internally for reimplementing the vanilla logic. Only change this if you know what you're doing.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
@Contract(pure = true)
|
|
|
|
|
default boolean chunkPrivilegedAccess() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Serializes your data into an NBT tag. This is used when saving the chunk to disk.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
void writeChunkToNBT(Chunk chunk, NBTTagCompound nbt);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deserializes your data from an NBT tag. This is used when loading the chunk from disk.
|
|
|
|
|
* The NBT *may* be null if the chunk was saved before this manager was registered
|
|
|
|
|
* (e.g., loading save before the mod was added), and the manager is not {@link #chunkPrivilegedAccess() privileged}.
|
|
|
|
|
* In this case, you should initialize the data to a sane default.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
void readChunkFromNBT(Chunk chunk, NBTTagCompound nbt);
|
|
|
|
|
}
|
2023-03-28 23:58:39 +02:00
|
|
|
|
2023-03-09 21:44:17 +01:00
|
|
|
/**
|
2023-03-30 17:17:11 +02:00
|
|
|
* Implement this interface if you want to save your ExtendedBlockStorage data to disk. This is called once per
|
|
|
|
|
* ExtendedBlockStorage per chunk. (16 times per chunk)
|
2023-03-30 17:48:26 +02:00
|
|
|
*
|
|
|
|
|
* @since 0.1.0
|
|
|
|
|
* @author FalsePattern
|
|
|
|
|
* @version 0.1.0
|
2023-03-09 21:44:17 +01:00
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI(since = "0.1.0")
|
2023-11-15 19:39:43 +01:00
|
|
|
interface SectionNBTDataManager extends NBTDataManager {
|
2023-03-30 17:17:11 +02:00
|
|
|
/**
|
|
|
|
|
* If false, the given nbt compound will be a freshly created object that gets inserted into the actual
|
|
|
|
|
* segment NBT tag under the `domain:id` name.
|
|
|
|
|
* <p>
|
|
|
|
|
* If true, the nbt tag passed in into the write/read methods will be the raw segment NBT tag without filtering.
|
|
|
|
|
*
|
|
|
|
|
* @implNote This is used internally for reimplementing the vanilla logic. Only change this if you know what you're doing.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
@Contract(pure = true)
|
|
|
|
|
default boolean sectionPrivilegedAccess() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-03-30 17:40:30 +02:00
|
|
|
|
2023-03-30 17:17:11 +02:00
|
|
|
/**
|
|
|
|
|
* Serializes your data into an NBT tag. This is used when saving the chunk to disk.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
void writeSectionToNBT(Chunk chunk, ExtendedBlockStorage ebs, NBTTagCompound section);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deserializes your data from an NBT tag. This is used when loading the chunk from disk.
|
|
|
|
|
* The NBT *may* be null if the chunk was saved before this manager was registered
|
|
|
|
|
* (e.g., loading save before the mod was added), and the manager is not {@link #sectionPrivilegedAccess() privileged}.
|
|
|
|
|
* In this case, you should initialize the data to a sane default.
|
|
|
|
|
*/
|
2023-03-30 17:48:26 +02:00
|
|
|
@StableAPI.Expose
|
2023-03-30 17:17:11 +02:00
|
|
|
void readSectionFromNBT(Chunk chunk, ExtendedBlockStorage ebs, NBTTagCompound section);
|
|
|
|
|
}
|
2023-03-09 21:44:17 +01:00
|
|
|
}
|