trying random stuff, nothing serious yet

This commit is contained in:
FalsePattern committed 2023-03-09 21:44:17 +01:00
1 parent 32c32df053
commit 56e22834c7
19 files changed
+327 -23

No files matched your search

@@ -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,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,4 +1,4 @@
package com.falsepattern.chunk;
package com.falsepattern.chunk.internal;
public class Tags {
public static final String MODID = "GRADLETOKEN_MODID";
@@ -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());
}
}
@@ -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;
@@ -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;
@@ -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;
@@ -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_);
}
}