mirror of
https://github.com/LegacyModdingMC/ChunkAPI.git
synced 2026-11-17 02:45:03 +00:00
trying random stuff, nothing serious yet
This commit is contained in:
19 files changed
+327
-23
No files matched your search
@@ -0,0 +1,19 @@
|
||||
# ChunkAPI
|
||||
A multipurpose chunk hooking library that makes adding arbitrary extra data to chunks much easier.
|
||||
|
||||
## How
|
||||
Read the code.
|
||||
TL;DR: Replacing the vanilla chunk netcode and save logic with a modular system, and reimplementing the stock vanilla
|
||||
logic on top of this. You can then add your own stuff.
|
||||
|
||||
## Compatibility
|
||||
This will definitely break things, but the mods that were developed on top of this would've broken stuff anyways, and
|
||||
having the most breaking changes centralized in a single hook mod makes writing compat much simpler.
|
||||
|
||||
## API
|
||||
You can use the api of this mod to add your own chunk data. Minimum requirements you need:
|
||||
1. A unique ID string for your data (collision avoidance)
|
||||
2. An object that can be stored in a Map<String, Object> inside each chunk (note: the vanilla replacements bypass this!)
|
||||
3. Packet data size/serialization/deserialization (for client/server communication)
|
||||
4. NBT serialization/serialization (for savegames)
|
||||
|
||||
+4
-4
@@ -4,7 +4,7 @@
|
||||
modName = ChunkAPI
|
||||
|
||||
# This is a case-sensitive string to identify your mod. Convention is to use lower case.
|
||||
modId = fplib_chunkapi
|
||||
modId = fp_chunkapi
|
||||
|
||||
# The "root package" of your mod. All of your mod classes *should* be placed under this package for simplicity.
|
||||
modGroup = com.falsepattern.chunk
|
||||
@@ -145,13 +145,13 @@ usesMixins = true
|
||||
# if a dependency requires mixins BUT this mod doesn't.
|
||||
hasMixinDeps = false
|
||||
# Specify the location of your implementation of IMixinConfigPlugin. Leave it empty otherwise.
|
||||
mixinPlugin = mixin.plugin.MixinPlugin
|
||||
mixinPlugin = internal.mixin.plugin.MixinPlugin
|
||||
# Whether you want the plugin to be configured as a PREINIT mixin
|
||||
mixinPluginPreInit = false
|
||||
# The minimum SpongePowered Mixins version required by the plugin. Internal default is 0.8.5 (GasStation)
|
||||
mixinPluginMinimumVersion =
|
||||
# Specify the package that contains all of your Mixins. You may only place Mixins in this package or the build will fail!
|
||||
mixinsPackage = mixin.mixins
|
||||
mixinsPackage = internal.mixin.mixins
|
||||
# Specify any custom mixin .json configs here. If you have multiple, comma-separate them. If you don't want to load
|
||||
# any jsons manually, leave this empty. You can use custom configs even if you already have a plugin added.
|
||||
# note: mixins.<modid>.json is used by the mixinPlugin's autogenerated config. Additionally, the refmap from the mixinsPackage
|
||||
@@ -201,7 +201,7 @@ usesShadowedDependencies = false
|
||||
# path will be removed, and all classes that refer to said classes will be modified so that the "stubpackage" will map
|
||||
# to the root package instead. This is useful for referring to compile-time inaccessible classes, such as classes in the
|
||||
# default package
|
||||
remapStubs = false
|
||||
remapStubs = true
|
||||
|
||||
# Set this to true if you want to publish a proprietary mod without visible source code.
|
||||
#noPublishedSources =
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.falsepattern.chunk;
|
||||
|
||||
import cpw.mods.fml.common.Mod;
|
||||
|
||||
@Mod(modid = Tags.MODID,
|
||||
version = Tags.VERSION,
|
||||
name = Tags.MODNAME,
|
||||
acceptedMinecraftVersions = "[1.7.10]",
|
||||
dependencies = "required-after:falsepatternlib@[0.10.15,);")
|
||||
public class ChunkAPI {
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.falsepattern.chunk.api;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
/**
|
||||
* Singleton instances that can manage custom in chunks.
|
||||
* Note: All the methods should be thread-safe and stateless unless otherwise specified.
|
||||
*/
|
||||
public interface ChunkDataManager {
|
||||
/**
|
||||
* The unique identifier of your data. The common convention is <code>modid:dataid</code>.
|
||||
* @return The unique identifier of your data. Should be unique across all mods.
|
||||
* @apiNote Ideally, this should be an interned string for maximum performance. (see {@link String#intern()}).
|
||||
* @implNote This is used to identify your data in packets and NBT tags.
|
||||
*/
|
||||
String id();
|
||||
/**
|
||||
* Initializes your data in a chunk. This is called when the chunk is first created.
|
||||
* @param chunk The chunk to initialize.
|
||||
*/
|
||||
void init(ModdedChunk chunk);
|
||||
/**
|
||||
* The amount of bytes your data will take up in a packet.
|
||||
* @param chunk The chunk that the data is in.
|
||||
* @return The amount of bytes your data will take up in a packet. -1 if the data is not present.
|
||||
*/
|
||||
int getPacketSize(ModdedChunk chunk);
|
||||
/**
|
||||
* Serializes your data into a packet. Not called if {@link #getPacketSize(ModdedChunk)} returns -1.
|
||||
* @param chunk The chunk that the data is in.
|
||||
* @param buffer The packet buffer to write to.
|
||||
*/
|
||||
void writeToPacket(ModdedChunk chunk, PacketBuffer buffer);
|
||||
/**
|
||||
* Deserializes your data from a packet. Not called if {@link #getPacketSize(ModdedChunk)} returned -1 on the other end.
|
||||
* @param chunk The chunk that the data is in.
|
||||
* @param buffer The packet buffer to read from.
|
||||
* @return The deserialized data.
|
||||
*/
|
||||
void readFromPacket(ModdedChunk chunk, PacketBuffer buffer);
|
||||
/**
|
||||
* Serializes your data into an NBT tag. This is used when saving the chunk to disk.
|
||||
* @param chunk The chunk that the data is in.
|
||||
* @param tag The tag to write to.
|
||||
*/
|
||||
void writeToNBT(ModdedChunk chunk, NBTTagCompound tag);
|
||||
/**
|
||||
* Deserializes your data from an NBT tag. This is used when loading the chunk from disk.
|
||||
* @param chunk The chunk that the data gets loaded into.
|
||||
* @param tag The tag to read from.
|
||||
*/
|
||||
void readFromNBT(ModdedChunk chunk, NBTTagCompound tag);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.falsepattern.chunk.api;
|
||||
|
||||
import com.falsepattern.chunk.internal.ChunkDataRegistryImpl;
|
||||
import com.falsepattern.chunk.api.exception.ManagerRegistrationException;
|
||||
|
||||
/**
|
||||
* This class is used to register ChunkDataManagers.
|
||||
*/
|
||||
public class ChunkDataRegistry {
|
||||
public static void registerDataManager(ChunkDataManager manager) throws ManagerRegistrationException {
|
||||
ChunkDataRegistryImpl.registerDataManager(manager);
|
||||
}
|
||||
|
||||
public static void disableDataManager(String id) {
|
||||
ChunkDataRegistryImpl.disableDataManager(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.falsepattern.chunk.api;
|
||||
|
||||
/**
|
||||
* This interface is injected into {@link net.minecraft.world.chunk.Chunk} by ChunkAPI. You can cast any Chunk instance
|
||||
* to this to access the API methods.
|
||||
*/
|
||||
public interface ModdedChunk {
|
||||
/**
|
||||
* Gets the data stored in this chunk by the specified manager.
|
||||
* @param manager The manager to get the data from.
|
||||
* @return The data stored in this chunk by the specified manager.
|
||||
* @param <T> The type of the data.
|
||||
*/
|
||||
<T> T getData(ChunkDataManager manager);
|
||||
|
||||
/**
|
||||
* Sets the data stored in this chunk by the specified manager.
|
||||
* @param manager The manager to set the data for.
|
||||
* @param data The data to store in this chunk.
|
||||
* @param <T> The type of the data.
|
||||
*/
|
||||
<T> void setData(ChunkDataManager manager, T data);
|
||||
|
||||
/**
|
||||
* Checks if this chunk has data stored by the specified manager.
|
||||
* @param manager The manager to check for.
|
||||
* @return True if this chunk has data stored by the specified manager.
|
||||
*/
|
||||
boolean hasData(ChunkDataManager manager);
|
||||
|
||||
/**
|
||||
* Removes the data stored in this chunk by the specified manager.
|
||||
* @param manager The manager to remove the data from.
|
||||
* @return The data that was stored in this chunk by the specified manager.
|
||||
* @param <T> The type of the data.
|
||||
*/
|
||||
<T> T removeData(ChunkDataManager manager);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.falsepattern.chunk.api.exception;
|
||||
|
||||
import com.falsepattern.chunk.api.ChunkDataManager;
|
||||
|
||||
/**
|
||||
* This exception is thrown when a {@link ChunkDataManager} could not be registered.
|
||||
* <p>
|
||||
* Possible reasons include:
|
||||
* <ul>
|
||||
* <li>The manager is already registered</li>
|
||||
* <li>The manager's name collides with a different manager</li>
|
||||
* <li>The manager was registered too late (should be done in init)</li>
|
||||
* </ul>
|
||||
*/
|
||||
public class ManagerRegistrationException extends RuntimeException {
|
||||
public ManagerRegistrationException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ManagerRegistrationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ManagerRegistrationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ManagerRegistrationException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
protected ManagerRegistrationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* This package contains the classes that you will use to interact with the ChunkAPI library.
|
||||
*/
|
||||
package com.falsepattern.chunk.api;
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.falsepattern.chunk.internal;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
|
||||
@Mod(modid = Tags.MODID,
|
||||
version = Tags.VERSION,
|
||||
name = Tags.MODNAME,
|
||||
acceptedMinecraftVersions = "[1.7.10]",
|
||||
dependencies = "required-after:falsepatternlib@[0.10.15,);")
|
||||
public class ChunkAPI {
|
||||
@Getter
|
||||
private static boolean registrationStage = false;
|
||||
|
||||
@Mod.EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
registrationStage = true;
|
||||
}
|
||||
|
||||
@Mod.EventHandler
|
||||
public void postInit(FMLPostInitializationEvent event) {
|
||||
registrationStage = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.falsepattern.chunk.internal;
|
||||
|
||||
import com.falsepattern.chunk.api.ChunkDataManager;
|
||||
import com.falsepattern.chunk.api.exception.ManagerRegistrationException;
|
||||
import lombok.val;
|
||||
import lombok.var;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class ChunkDataRegistryImpl {
|
||||
private static final Map<String, ChunkDataManager> managers = new HashMap<>();
|
||||
private static final Set<String> disabledManagers = new HashSet<>();
|
||||
public static void registerDataManager(ChunkDataManager manager) throws ManagerRegistrationException {
|
||||
if (!ChunkAPI.isRegistrationStage()) {
|
||||
throw new ManagerRegistrationException("ChunkDataManager registration is not allowed at this time! " +
|
||||
"Please register your ChunkDataManager in the init phase of your mod.");
|
||||
}
|
||||
var id = manager.id();
|
||||
if (id == null) {
|
||||
throw new ManagerRegistrationException("ChunkDataManager " + manager + " has a null id!");
|
||||
}
|
||||
id = id.intern();
|
||||
if (managers.containsKey(id)) {
|
||||
throw new ManagerRegistrationException("ChunkDataManager " + manager + " has a duplicate id!");
|
||||
}
|
||||
|
||||
// If the manager was disabled, do not add it to the list of managers.
|
||||
if (disabledManagers.contains(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
managers.put(id, manager);
|
||||
}
|
||||
|
||||
public static void disableDataManager(String id) {
|
||||
if (!ChunkAPI.isRegistrationStage()) {
|
||||
throw new ManagerRegistrationException("ChunkDataManager disabling is not allowed at this time! " +
|
||||
"Please disable any ChunkDataManagers in the init phase of your mod.");
|
||||
}
|
||||
if (id == null) {
|
||||
throw new ManagerRegistrationException("ChunkDataManager id cannot be null!");
|
||||
}
|
||||
id = id.intern();
|
||||
Common.LOG.warn("Disabling ChunkDataManager " + id + ". See the stacktrace for the source of this event.", new Throwable());
|
||||
//Remove the manager from the list of managers, if it exists
|
||||
managers.remove(id);
|
||||
|
||||
//Add the manager to the list of disabled managers, in case it gets registered after this disable call.
|
||||
disabledManagers.add(id);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.falsepattern.chunk;
|
||||
package com.falsepattern.chunk.internal;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.falsepattern.chunk;
|
||||
package com.falsepattern.chunk.internal;
|
||||
|
||||
public class Tags {
|
||||
public static final String MODID = "GRADLETOKEN_MODID";
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.falsepattern.chunk.internal.mixin.mixins.common.vanilla;
|
||||
|
||||
import com.falsepattern.chunk.api.ChunkDataManager;
|
||||
import com.falsepattern.chunk.api.ModdedChunk;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* This mixin injects the {@link ModdedChunk} interface into {@link Chunk}, and also creates the data map.
|
||||
*/
|
||||
@Mixin(Chunk.class)
|
||||
public abstract class ChunkDataMixin implements ModdedChunk {
|
||||
private Map<String, Object> data;
|
||||
|
||||
@Inject(method = "<init>(Lnet/minecraft/world/World;II)V",
|
||||
at = @At("RETURN"))
|
||||
private void init(World p_i1995_1_, int p_i1995_2_, int p_i1995_3_, CallbackInfo ci) {
|
||||
data = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getData(ChunkDataManager manager) {
|
||||
return (T) data.get(manager.id());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setData(ChunkDataManager manager, T data) {
|
||||
this.data.put(manager.id(), data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasData(ChunkDataManager manager) {
|
||||
return data.containsKey(manager.id());
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T removeData(ChunkDataManager manager) {
|
||||
return (T) data.remove(manager.id());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -21,7 +21,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.falsepattern.chunk.mixin.plugin;
|
||||
package com.falsepattern.chunk.internal.mixin.plugin;
|
||||
|
||||
import com.falsepattern.lib.mixin.IMixin;
|
||||
import com.falsepattern.lib.mixin.ITargetedMod;
|
||||
+2
-2
@@ -21,9 +21,9 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.falsepattern.chunk.mixin.plugin;
|
||||
package com.falsepattern.chunk.internal.mixin.plugin;
|
||||
|
||||
import com.falsepattern.chunk.Tags;
|
||||
import com.falsepattern.chunk.internal.Tags;
|
||||
import com.falsepattern.lib.mixin.IMixin;
|
||||
import com.falsepattern.lib.mixin.IMixinPlugin;
|
||||
import com.falsepattern.lib.mixin.ITargetedMod;
|
||||
+1
-1
@@ -21,7 +21,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.falsepattern.chunk.mixin.plugin;
|
||||
package com.falsepattern.chunk.internal.mixin.plugin;
|
||||
|
||||
import com.falsepattern.lib.mixin.ITargetedMod;
|
||||
import lombok.Getter;
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* This package contains the internal classes of the ChunkAPI library. Do not use these classes directly.
|
||||
*/
|
||||
package com.falsepattern.chunk.internal;
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.falsepattern.chunk.internal.stubpackage.net.minecraft.world.chunk;
|
||||
|
||||
import com.falsepattern.chunk.api.ModdedChunk;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
|
||||
//Internal stub class, do not use
|
||||
public abstract class Chunk extends net.minecraft.world.chunk.Chunk implements ModdedChunk {
|
||||
private Chunk(World p_i1995_1_, int p_i1995_2_, int p_i1995_3_) {
|
||||
super(p_i1995_1_, p_i1995_2_, p_i1995_3_);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"required": true,
|
||||
"minVersion": "0.8.5",
|
||||
"package": "com.falsepattern.chunk.mixin.mixins",
|
||||
"plugin": "com.falsepattern.chunk.mixin.plugin.MixinPlugin",
|
||||
"package": "com.falsepattern.chunk.internal.mixin.mixins",
|
||||
"plugin": "com.falsepattern.chunk.internal.mixin.plugin.MixinPlugin",
|
||||
"refmap": "mixins.fplib_chunkapi.refmap.json",
|
||||
"target": "@env(DEFAULT)",
|
||||
"compatibilityLevel": "JAVA_8"
|
||||
|
||||
Reference in new issue
Block a user