Skip to content

Commit

Permalink
Use Forge ticket system
Browse files Browse the repository at this point in the history
- Fixes issue where entities would no longer be updated after some time
  • Loading branch information
jchung01 committed Sep 5, 2024
1 parent c5a47a9 commit bd1a01c
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public interface ITartarusCleaner
{
void ut$clean(World world, int boxId, boolean removeAll);
void ut$freeBoxes();
boolean ut$areBoxesInUse();
}
Original file line number Diff line number Diff line change
@@ -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<ForgeChunkManager.Ticket> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Integer> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/mixins.mods.woot.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"refmap": "universaltweaks.refmap.json",
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"mixins": ["UTEntitySpawnerMixin", "UTTartarusManagerMixin", "UTTartarusSchoolMixin"]
"mixins": ["UTEntitySpawnerMixin", "UTTartarusManagerMixin", "UTTartarusSchoolMixin", "UTWootMixin"]
}

0 comments on commit bd1a01c

Please sign in to comment.