feat: tile load events

This commit is contained in:
Timo Ley 2024-05-07 14:05:05 +02:00
parent 4aa294bd7a
commit 3ff34ff648
6 changed files with 159 additions and 3 deletions

View file

@ -7,7 +7,7 @@ buildscript {
}
maven {
name = "sonatype"
url = "https://oss.sonatype.org/content/repositories/snapshots/"
url = "https://oss.sonatype.orgP/content/repositories/snapshots/"
}
}
dependencies {
@ -60,7 +60,11 @@ jar {
}
manifest {
attributes "FMLAT": "anvillib_at.cfg"
attributes([
"FMLCorePlugin": "net.anvilcraft.anvillib.AnvilCore",
"FMLCorePluginContainsFMLMod": "true",
"FMLAT": "anvillib_at.cfg"
])
}
}
@ -78,7 +82,11 @@ task deobfJar(type: Jar) {
classifier = 'deobf'
manifest {
attributes "FMLAT": "anvillib_at.cfg"
attributes([
"FMLCorePlugin": "net.anvilcraft.anvillib.AnvilCore",
"FMLCorePluginContainsFMLMod": "true",
"FMLAT": "anvillib_at.cfg"
])
}
}
@ -114,3 +122,7 @@ publishing {
}
}
}
mixin {
mixinRefMapName = 'anvillib.refmap.json'
}

View file

@ -0,0 +1,58 @@
package net.anvilcraft.anvillib;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import cpw.mods.fml.common.DummyModContainer;
import cpw.mods.fml.common.ModMetadata;
import cpw.mods.fml.relauncher.IFMLLoadingPlugin;
import io.github.tox1cozz.mixinbooterlegacy.IEarlyMixinLoader;
public class AnvilCore implements IFMLLoadingPlugin, IEarlyMixinLoader {
@Override
public List<String> getMixinConfigs() {
List<String> mixins = new ArrayList<>();
mixins.add("anvillib.mixins.json");
return mixins;
}
@Override
public String[] getASMTransformerClass() {
return new String[0];
}
@Override
public String getModContainerClass() {
return "net.anvilcraft.anvillib.AnvilCore$Container";
}
@Override
public String getSetupClass() {
return null;
}
@Override
public void injectData(Map<String, Object> data) {
}
@Override
public String getAccessTransformerClass() {
return null;
}
public static class Container extends DummyModContainer {
public Container() {
super(new ModMetadata());
ModMetadata meta = getMetadata();
meta.modId = "anvillib-core";
meta.name = "AnvilLib Core Mod";
meta.version = "1.0.0";
}
}
}

View file

@ -0,0 +1,14 @@
package net.anvilcraft.anvillib.event;
import cpw.mods.fml.common.eventhandler.Event;
import net.minecraft.tileentity.TileEntity;
public class TileLoadedEvent extends Event {
public TileEntity tile;
public TileLoadedEvent(TileEntity tile) {
this.tile = tile;
}
}

View file

@ -0,0 +1,14 @@
package net.anvilcraft.anvillib.event;
import cpw.mods.fml.common.eventhandler.Event;
import net.minecraft.tileentity.TileEntity;
public class TileUnloadedEvent extends Event {
public TileEntity tile;
public TileUnloadedEvent(TileEntity tile) {
this.tile = tile;
}
}

View file

@ -0,0 +1,45 @@
package net.anvilcraft.anvillib.mixins;
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.anvilcraft.anvillib.event.TileLoadedEvent;
import net.anvilcraft.anvillib.event.TileUnloadedEvent;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.MinecraftForge;
@Mixin(TileEntity.class)
public class MixinTileEntity {
public boolean anvillibTileLoaded = false;
@Inject(method = "updateEntity()V", at = @At("HEAD"), remap = false)
private void onUpdate(CallbackInfo ci) {
TileEntity self = (TileEntity)(Object)this;
if (!this.anvillibTileLoaded && !self.getWorldObj().isRemote) {
this.anvillibTileLoaded = true;
MinecraftForge.EVENT_BUS.post(new TileLoadedEvent(self));
}
}
@Inject(method = "invalidate()V", at = @At("HEAD"), remap = false)
private void onInvalidate(CallbackInfo ci) {
TileEntity self = (TileEntity)(Object)this;
if (!self.getWorldObj().isRemote) {
this.anvillibTileLoaded = false;
MinecraftForge.EVENT_BUS.post(new TileUnloadedEvent(self));
}
}
@Inject(method = "onChunkUnload()V", at = @At("HEAD"), remap = false)
private void onOnChunkUnload(CallbackInfo ci) {
TileEntity self = (TileEntity)(Object)this;
if (!self.getWorldObj().isRemote) {
this.anvillibTileLoaded = false;
MinecraftForge.EVENT_BUS.post(new TileUnloadedEvent(self));
}
}
}

View file

@ -0,0 +1,13 @@
{
"package": "net.anvilcraft.anvillib.mixins",
"refmap": "anvillib.refmap.json",
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinTileEntity"
],
"client": [],
"injectors": {
"defaultRequire": 1
}
}