diff --git a/src/main/java/mod/acgaming/universaltweaks/mods/woot/ITartarusCleaner.java b/src/main/java/mod/acgaming/universaltweaks/mods/woot/ITartarusCleaner.java index 8ceb76b8..117c0623 100644 --- a/src/main/java/mod/acgaming/universaltweaks/mods/woot/ITartarusCleaner.java +++ b/src/main/java/mod/acgaming/universaltweaks/mods/woot/ITartarusCleaner.java @@ -5,4 +5,6 @@ public interface ITartarusCleaner { void ut$clean(World world, int boxId, boolean removeAll); + void ut$freeBoxes(); + boolean ut$areBoxesInUse(); } diff --git a/src/main/java/mod/acgaming/universaltweaks/mods/woot/UTWootTicketManager.java b/src/main/java/mod/acgaming/universaltweaks/mods/woot/UTWootTicketManager.java new file mode 100644 index 00000000..3ee1c4e2 --- /dev/null +++ b/src/main/java/mod/acgaming/universaltweaks/mods/woot/UTWootTicketManager.java @@ -0,0 +1,64 @@ +package mod.acgaming.universaltweaks.mods.woot; + +import java.util.List; + +import net.minecraft.util.math.ChunkPos; +import net.minecraft.world.World; +import net.minecraft.world.WorldServer; +import net.minecraftforge.common.ForgeChunkManager; + +import ipsis.Woot; +import ipsis.woot.dimension.WootDimensionManager; +import ipsis.woot.util.DebugSetup; +import mod.acgaming.universaltweaks.UniversalTweaks; + +public class UTWootTicketManager +{ + public static final ChunkPos CHUNK = new ChunkPos(WootDimensionManager.CHUNK_X, WootDimensionManager.CHUNK_Z); + public static ForgeChunkManager.Ticket ticket; + + public static void forceChunk(World world, int boxId) + { + WorldServer worldWoot = Woot.wootDimensionManager.getWorldServer(world); + if (ticket == null) + { + Woot.debugSetup.trace(DebugSetup.EnumDebugType.TARTARUS, "UTWootTicketManager:callback", "requesting a ticket"); + ticket = ForgeChunkManager.requestTicket(Woot.instance, worldWoot, ForgeChunkManager.Type.NORMAL); + if (ticket == null) + { + UniversalTweaks.LOGGER.error("UTWootTicketManager ::: Could not get a ticket for Tartarus (Woot)! Please report to Universal Tweaks."); + return; + } + } + Woot.debugSetup.trace(DebugSetup.EnumDebugType.TARTARUS, "UTWootTicketManager:callback", "forcing chunk for boxId " + boxId); + ForgeChunkManager.forceChunk(ticket, CHUNK); + } + + public static void releaseChunk(int boxId) + { + if (ticket == null) return; + Woot.debugSetup.trace(DebugSetup.EnumDebugType.TARTARUS, "UTWootTicketManager:callback", "trying to release chunk for boxId " + boxId); + if (!((ITartarusCleaner) Woot.tartarusManager).ut$areBoxesInUse()) + { + Woot.debugSetup.trace(DebugSetup.EnumDebugType.TARTARUS, "UTWootTicketManager:callback", "releasing ticket"); + ForgeChunkManager.releaseTicket(ticket); + ticket = null; + } + } + + public static void callback(List tickets, World world) + { + int dim = world.provider.getDimension(); + if (dim != Woot.wootDimensionManager.getDimensionId()) return; + // Sanity check + if (tickets.size() > 1) + { + for (ForgeChunkManager.Ticket ticket : tickets) + { + ForgeChunkManager.releaseTicket(ticket); + } + } + UTWootTicketManager.ticket = tickets.get(0); + ((ITartarusCleaner) Woot.tartarusManager).ut$freeBoxes(); + } +} diff --git a/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTTartarusManagerMixin.java b/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTTartarusManagerMixin.java index d97f9588..3ac3ca59 100644 --- a/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTTartarusManagerMixin.java +++ b/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTTartarusManagerMixin.java @@ -39,4 +39,23 @@ public class UTTartarusManagerMixin implements ITartarusCleaner worldWoot.removeEntityDangerously(entity); } } + + @Override + public void ut$freeBoxes() + { + for (SpawnBox box : spawnBoxMap.values()) + { + box.clearUsed(); + } + } + + @Override + public boolean ut$areBoxesInUse() + { + for (SpawnBox box: spawnBoxMap.values()) + { + if (box.isUsed()) return true; + } + return false; + } } diff --git a/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTTartarusSchoolMixin.java b/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTTartarusSchoolMixin.java index 7aeaf799..c485719d 100644 --- a/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTTartarusSchoolMixin.java +++ b/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTTartarusSchoolMixin.java @@ -1,20 +1,21 @@ package mod.acgaming.universaltweaks.mods.woot.mixin; -import net.minecraft.entity.item.EntityItem; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import net.minecraft.world.WorldServer; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import ipsis.Woot; import ipsis.woot.farming.ITickTracker; import ipsis.woot.farmstructure.IFarmSetup; +import ipsis.woot.loot.schools.TartarusManager; import ipsis.woot.loot.schools.TartarusSchool; import mod.acgaming.universaltweaks.mods.woot.ITartarusCleaner; +import mod.acgaming.universaltweaks.mods.woot.UTWootTicketManager; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; // Courtesy of jchung01 @@ -27,24 +28,18 @@ public class UTTartarusSchoolMixin @Inject(method = "tick", at = @At(value = "INVOKE", target = "Lipsis/woot/loot/schools/TartarusManager;spawnInBox(Lnet/minecraft/world/World;ILipsis/woot/util/WootMobName;Lipsis/woot/util/EnumEnchantKey;)V", shift = At.Shift.AFTER)) private void ut$cleanupOnDeath(ITickTracker tickTracker, World world, BlockPos origin, IFarmSetup farmSetup, CallbackInfo ci) { + if (spawnId == -1) return; ((ITartarusCleaner) Woot.tartarusManager).ut$clean(world, spawnId, false); + UTWootTicketManager.forceChunk(world, spawnId); } - // Here for redundancy. - @Inject(method = "tick", at = @At(value = "INVOKE", target = "Lipsis/woot/loot/schools/TartarusManager;freeSpawnBoxId(I)I")) - private void ut$cleanupOnFree(ITickTracker tickTracker, World world, BlockPos origin, IFarmSetup farmSetup, CallbackInfo ci) + @WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lipsis/woot/loot/schools/TartarusManager;freeSpawnBoxId(I)I")) + private int ut$cleanupOnFree(TartarusManager instance, int id, Operation original, ITickTracker tickTracker, World world) { - ((ITartarusCleaner) Woot.tartarusManager).ut$clean(world, spawnId, true); + int oldSpawnId = spawnId; + ((ITartarusCleaner) Woot.tartarusManager).ut$clean(world, oldSpawnId, true); + spawnId = original.call(instance, oldSpawnId); + UTWootTicketManager.releaseChunk(oldSpawnId); + return spawnId; } - - /** - * @reason Force remove even if chunks stop ticking. - */ - @Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/item/EntityItem;setDead()V", remap = true)) - private void ut$cleanupOnLearn(EntityItem instance, ITickTracker tickTracker, World world) - { - WorldServer worldWoot = Woot.wootDimensionManager.getWorldServer(world); - if (worldWoot != null) worldWoot.removeEntityDangerously(instance); - } - } diff --git a/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTWootMixin.java b/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTWootMixin.java new file mode 100644 index 00000000..e01e0d59 --- /dev/null +++ b/src/main/java/mod/acgaming/universaltweaks/mods/woot/mixin/UTWootMixin.java @@ -0,0 +1,21 @@ +package mod.acgaming.universaltweaks.mods.woot.mixin; + +import net.minecraftforge.common.ForgeChunkManager; + +import ipsis.Woot; +import mod.acgaming.universaltweaks.mods.woot.UTWootTicketManager; +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; + +// Courtesy of jchung01 +@Mixin(value = Woot.class, remap = false) +public class UTWootMixin +{ + @Inject(method = "postInit", at = @At(value = "TAIL")) + private void utRegisterTicketCallback(CallbackInfo ci) + { + ForgeChunkManager.setForcedChunkLoadingCallback(Woot.instance, UTWootTicketManager::callback); + } +} diff --git a/src/main/resources/mixins.mods.woot.json b/src/main/resources/mixins.mods.woot.json index 9309479e..8781edb6 100644 --- a/src/main/resources/mixins.mods.woot.json +++ b/src/main/resources/mixins.mods.woot.json @@ -3,5 +3,5 @@ "refmap": "universaltweaks.refmap.json", "minVersion": "0.8", "compatibilityLevel": "JAVA_8", - "mixins": ["UTEntitySpawnerMixin", "UTTartarusManagerMixin", "UTTartarusSchoolMixin"] + "mixins": ["UTEntitySpawnerMixin", "UTTartarusManagerMixin", "UTTartarusSchoolMixin", "UTWootMixin"] } \ No newline at end of file