aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core
diff options
context:
space:
mode:
authorMaxim <maxim235@gmx.de>2022-08-12 12:54:15 +0200
committerGitHub <noreply@github.com>2022-08-12 11:54:15 +0100
commitc2e1697d9a7d4697f8a96bf838cd0c038fce2f01 (patch)
treedcda7df812829d997cde89af31768edc1b28441e /src/main/java/gtPlusPlus/core
parentb47d63d17819f579bf200247e66774c6b977635c (diff)
downloadGT5-Unofficial-c2e1697d9a7d4697f8a96bf838cd0c038fce2f01.tar.gz
GT5-Unofficial-c2e1697d9a7d4697f8a96bf838cd0c038fce2f01.tar.bz2
GT5-Unofficial-c2e1697d9a7d4697f8a96bf838cd0c038fce2f01.zip
Chunkloader code rework (#274)
* Remodeled chunkloader code after RC chunkloaders to make them actually work * Added limit to chunk radius * Some cleanup
Diffstat (limited to 'src/main/java/gtPlusPlus/core')
-rw-r--r--src/main/java/gtPlusPlus/core/chunkloading/GTPP_ChunkManager.java217
-rw-r--r--src/main/java/gtPlusPlus/core/chunkloading/StaticChunkFunctions.java63
2 files changed, 52 insertions, 228 deletions
diff --git a/src/main/java/gtPlusPlus/core/chunkloading/GTPP_ChunkManager.java b/src/main/java/gtPlusPlus/core/chunkloading/GTPP_ChunkManager.java
index ddfe9b8227..b403646a2a 100644
--- a/src/main/java/gtPlusPlus/core/chunkloading/GTPP_ChunkManager.java
+++ b/src/main/java/gtPlusPlus/core/chunkloading/GTPP_ChunkManager.java
@@ -1,34 +1,20 @@
package gtPlusPlus.core.chunkloading;
-import com.google.common.collect.HashMultimap;
-import cpw.mods.fml.common.eventhandler.EventPriority;
-import cpw.mods.fml.common.eventhandler.SubscribeEvent;
-import gregtech.GT_Mod;
-import gregtech.api.enums.GT_Values;
+import com.google.common.collect.LinkedListMultimap;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
-import gregtech.api.util.GT_Log;
import gtPlusPlus.GTplusplus;
-import gtPlusPlus.api.interfaces.IChunkLoader;
-import gtPlusPlus.core.lib.CORE;
-import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
-import net.minecraft.command.CommandBase;
-import net.minecraft.command.ICommandSender;
-import net.minecraft.nbt.NBTTagCompound;
-import net.minecraft.nbt.NBTUtil;
+import gtPlusPlus.xmod.gregtech.common.tileentities.machines.basic.GregtechMetaTileEntityChunkLoader;
+import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.ForgeChunkManager.Ticket;
import net.minecraftforge.common.MinecraftForge;
-import net.minecraftforge.event.world.WorldEvent;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
/**
*
@@ -37,181 +23,82 @@ import java.util.Map;
* @author Repo-Alt, Alkalus
*
*/
-public class GTPP_ChunkManager implements ForgeChunkManager.OrderedLoadingCallback, ForgeChunkManager.PlayerOrderedLoadingCallback {
-
-
- private Map<TileEntity, Ticket> registeredTickets = new HashMap<>();
-
- private static GTPP_ChunkManager instance = new GTPP_ChunkManager();
+public class GTPP_ChunkManager implements ForgeChunkManager.LoadingCallback, ForgeChunkManager.OrderedLoadingCallback, ForgeChunkManager.PlayerOrderedLoadingCallback {
+ private static GTPP_ChunkManager instance;
public static boolean enableChunkloaders = true;
public static boolean alwaysReloadChunkloaders = false;
public static boolean debugChunkloaders = false;
+ public static GTPP_ChunkManager getInstance() {
+ if (instance == null) {
+ instance = new GTPP_ChunkManager();
+ }
+ return instance;
+ }
+
public static void init() {
if (enableChunkloaders) {
- ForgeChunkManager.setForcedChunkLoadingCallback(GTplusplus.instance, instance);
+ ForgeChunkManager.setForcedChunkLoadingCallback(GTplusplus.instance, getInstance());
+ MinecraftForge.EVENT_BUS.register(getInstance());
}
}
@Override
- public void ticketsLoaded(List<Ticket> tickets, World world) {}
-
- // Determine if tickets should be kept. Based on if the ticket is a machine or working chunk ticket.
- // Working chunk tickets are tossed and re-created when the machine re-activates.
- // Machine tickets are kept only if the config alwaysReloadChunkloaders is true. Otherwise
- // machine chunks are tossed and re-created only when the machine re-activates, similar to a Passive Anchor.
- @Override
- public List<Ticket> ticketsLoaded(List<Ticket> tickets, World world, int maxTicketCount) {
- List<Ticket> validTickets = new ArrayList<>();
- if (alwaysReloadChunkloaders) {
- for (Ticket ticket : tickets) {
- int x = ticket.getModData().getInteger("OwnerX");
- int y = ticket.getModData().getInteger("OwnerY");
- int z = ticket.getModData().getInteger("OwnerZ");
- if (y > 0) {
+ public void ticketsLoaded(List<Ticket> tickets, World world) {
+ for (Ticket ticket : tickets) {
+ if (ticket.isPlayerTicket())
+ continue;
+ Entity entity = ticket.getEntity();
+ if (entity == null) {
+ int x = ticket.getModData().getInteger("xCoord");
+ int y = ticket.getModData().getInteger("yCoord");
+ int z = ticket.getModData().getInteger("zCoord");
+
+ if (y >= 0) {
TileEntity tile = world.getTileEntity(x, y, z);
- if (tile != null && tile instanceof IGregTechTileEntity && ((IGregTechTileEntity)tile).isAllowedToWork()) {
- ForgeChunkManager.forceChunk(ticket, new ChunkCoordIntPair(x >> 4, z >> 4));
- if (!registeredTickets.containsKey(tile)) {
- registeredTickets.put(tile, ticket);
- if (((IGregTechTileEntity)tile).getMetaTileEntity() instanceof IChunkLoader) {
- ForgeChunkManager.forceChunk(ticket, ((IChunkLoader)((IGregTechTileEntity)tile).getMetaTileEntity()).getResidingChunk());
- }
- validTickets.add(ticket);
- }
+ if (((IGregTechTileEntity)tile).getMetaTileEntity() instanceof GregtechMetaTileEntityChunkLoader) {
+ ((GregtechMetaTileEntityChunkLoader)((IGregTechTileEntity)tile).getMetaTileEntity()).forceChunkLoading(ticket);
}
}
}
}
- return validTickets;
}
- // Determine if player tickets should be kept. This is where a ticket list per player would be created and maintained. When
- // a player join event occurs, their name/UUID/whatevs is compared against tickets on this list and those tickets reactivated.
- // Since that info would be maintained/dealt with on a per-player startup, the list returned back to Forge is empty.
@Override
- public ListMultimap<String, Ticket> playerTicketsLoaded(ListMultimap<String, Ticket> tickets, World world) {
- // Not currently used, so just return an empty list.
- return ArrayListMultimap.create();
- }
-
- // Request a chunk to be loaded for this machine
- // may pass null chunk to load just the machine itself, if "alwaysReloadChunkloaders" is enabled in config
- static public boolean requestPlayerChunkLoad(TileEntity owner, ChunkCoordIntPair chunkXZ, String player) {
- if (!enableChunkloaders) {
- return false;
- }
- if (!alwaysReloadChunkloaders && chunkXZ == null) {
- return false;
- }
- if (debugChunkloaders && chunkXZ != null) {
- GT_Log.out.println("GT_ChunkManager: Chunk request: (" + chunkXZ.chunkXPos + ", " + chunkXZ.chunkZPos + ")");
- }
- if (instance.registeredTickets.containsKey(owner)) {
- ForgeChunkManager.forceChunk(instance.registeredTickets.get(owner), chunkXZ);
- }
- else {
- Ticket ticket = null;
- if (player != "") {
- ticket = ForgeChunkManager.requestPlayerTicket(GT_Mod.instance, player, owner.getWorldObj(), ForgeChunkManager.Type.NORMAL);
- }
- else {
- ticket = ForgeChunkManager.requestTicket(GT_Mod.instance, owner.getWorldObj(), ForgeChunkManager.Type.NORMAL);
- }
- if (ticket == null) {
- if (debugChunkloaders) {
- GT_Log.out.println("GT_ChunkManager: ForgeChunkManager.requestTicket failed");
+ public List<Ticket> ticketsLoaded(List<Ticket> tickets, World world, int maxTicketCount) {
+ Set<Ticket> validTickets = new HashSet<>();
+ for (Ticket ticket : tickets) {
+ Entity entity = ticket.getEntity();
+ if (entity == null) {
+ int x = ticket.getModData().getInteger("xCoord");
+ int y = ticket.getModData().getInteger("yCoord");
+ int z = ticket.getModData().getInteger("zCoord");
+
+ if (y >= 0) {
+ TileEntity tile = world.getTileEntity(x, y, z);
+ if (((IGregTechTileEntity)tile).getMetaTileEntity() instanceof GregtechMetaTileEntityChunkLoader) {
+ validTickets.add(ticket);
+ }
}
- return false;
}
- if (debugChunkloaders) {
- GT_Log.out.println("GT_ChunkManager: ticket issued for machine at: (" + owner.xCoord + ", " + owner.yCoord + ", " + owner.zCoord + ")" );
- }
- NBTTagCompound tag = ticket.getModData();
- tag.setInteger("OwnerX", owner.xCoord);
- tag.setInteger("OwnerY", owner.yCoord);
- tag.setInteger("OwnerZ", owner.zCoord);
- ForgeChunkManager.forceChunk(ticket, chunkXZ);
- if (alwaysReloadChunkloaders) {
- ForgeChunkManager.forceChunk(ticket, new ChunkCoordIntPair(owner.xCoord << 4, owner.zCoord << 4));
- }
- instance.registeredTickets.put(owner, ticket);
}
- return true;
- }
-
- static public boolean requestChunkLoad(TileEntity owner, ChunkCoordIntPair chunkXZ) {
- return requestPlayerChunkLoad(owner, chunkXZ, "");
- }
- static public void releaseChunk(TileEntity owner, ChunkCoordIntPair chunkXZ) {
- if (!enableChunkloaders) {
- return;
- }
- Ticket ticket = instance.registeredTickets.get(owner);
- if (ticket != null) {
- if (debugChunkloaders) {
- GT_Log.out.println("GT_ChunkManager: Chunk release: (" + chunkXZ.chunkXPos + ", " + chunkXZ.chunkZPos + ")");
- }
- ForgeChunkManager.unforceChunk(ticket, chunkXZ);
- }
+ return new LinkedList<>(validTickets);
}
- static public void releaseTicket(TileEntity owner) {
- if (!enableChunkloaders) {
- return;
- }
- Ticket ticket = instance.registeredTickets.get(owner);
- if (ticket != null) {
- if (debugChunkloaders) {
- GT_Log.out.println("GT_ChunkManager: ticket released by machine at: (" + owner.xCoord + ", " + owner.yCoord + ", " + owner.zCoord + ")" );
- for (ChunkCoordIntPair chunk : ticket.getChunkList()) {
- GT_Log.out.println("GT_ChunkManager: Chunk release: (" + chunk.chunkXPos + ", " + chunk.chunkZPos + ")");
- }
- }
- ForgeChunkManager.releaseTicket(ticket);
- instance.registeredTickets.remove(owner);
- }
+ @Override
+ public ListMultimap<String, Ticket> playerTicketsLoaded(ListMultimap<String, Ticket> tickets, World world) {
+ return LinkedListMultimap.create();
}
- public static void printTickets() {
- if (!debugChunkloaders) {
- return;
- }
- GT_Log.out.println("GT_ChunkManager: Start forced chunks dump:");
- instance.registeredTickets.forEach((machine, ticket) -> {
- GT_Log.out.print("GT_ChunkManager: Chunks forced by the machine at (" + machine.xCoord + ", " + machine.yCoord + ", " + machine.zCoord + ")");
- if (ticket.isPlayerTicket()) {
- GT_Log.out.print(" Owner: " + ticket.getPlayerName());
+ public static Set<ChunkCoordIntPair> getChunksAround(int xChunk, int zChunk, int radius) {
+ Set<ChunkCoordIntPair> chunkList = new HashSet<>();
+ for (int xx = xChunk - radius; xx <= xChunk + radius; xx++) {
+ for (int zz = zChunk - radius; zz <= zChunk + radius; zz++) {
+ chunkList.add(new ChunkCoordIntPair(xx, zz));
}
- GT_Log.out.print(" :");
- for (ChunkCoordIntPair c : ticket.getChunkList()) {
- GT_Log.out.print("(");
- GT_Log.out.print(c.chunkXPos);
- GT_Log.out.print(", ");
- GT_Log.out.print(c.chunkZPos);
- GT_Log.out.print("), ");
- }
- });
- GT_Log.out.println("GT_ChunkManager: End forced chunks dump:");
- }
- public static class DebugCommand extends CommandBase {
- @Override
- public String getCommandName() {
- return "gtpp:dump_chunks";
- }
- @Override
- public int getRequiredPermissionLevel() {
- return 0;
- }
- @Override
- public String getCommandUsage(ICommandSender sender) {
- return "/" + getCommandName();
- }
- @Override
- public void processCommand(ICommandSender sender, String[] args) {
- printTickets();
}
+ return chunkList;
}
}
diff --git a/src/main/java/gtPlusPlus/core/chunkloading/StaticChunkFunctions.java b/src/main/java/gtPlusPlus/core/chunkloading/StaticChunkFunctions.java
deleted file mode 100644
index 36d9fa670c..0000000000
--- a/src/main/java/gtPlusPlus/core/chunkloading/StaticChunkFunctions.java
+++ /dev/null
@@ -1,63 +0,0 @@
-package gtPlusPlus.core.chunkloading;
-
-import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
-import gtPlusPlus.api.interfaces.IChunkLoader;
-import gtPlusPlus.xmod.gregtech.common.tileentities.machines.basic.GregtechMetaTileEntityChunkLoader;
-import net.minecraft.nbt.NBTTagCompound;
-import net.minecraft.tileentity.TileEntity;
-import net.minecraft.world.ChunkCoordIntPair;
-
-public class StaticChunkFunctions {
-
- public static void saveNBTDataForTileEntity(IGregTechTileEntity aBaseMetaTileEntity, NBTTagCompound aNBT) {
- IChunkLoader aTileEntity = getChunkLoader(aBaseMetaTileEntity);
- aNBT.setBoolean("chunkLoadingEnabled", aTileEntity.getChunkLoadingActive());
- aNBT.setBoolean("isChunkloading", aTileEntity.getResidingChunk() != null);
- if (aTileEntity.getResidingChunk() != null) {
- aNBT.setInteger("loadedChunkXPos", aTileEntity.getResidingChunk().chunkXPos);
- aNBT.setInteger("loadedChunkZPos", aTileEntity.getResidingChunk().chunkZPos);
- }
- }
-
- public static void loadNBTDataForTileEntity(IGregTechTileEntity aBaseMetaTileEntity, NBTTagCompound aNBT) {
- IChunkLoader aTileEntity = getChunkLoader(aBaseMetaTileEntity);
- if (aNBT.hasKey("chunkLoadingEnabled")) {
- aTileEntity.setChunkLoadingActive(aNBT.getBoolean("chunkLoadingEnabled"));
- }
- if (aNBT.getBoolean("isChunkloading")) {
- aTileEntity.setResidingChunk(new ChunkCoordIntPair(aNBT.getInteger("loadedChunkXPos"), aNBT.getInteger("loadedChunkZPos")));
- }
- }
-
- public static void onRemoval(IGregTechTileEntity aBaseMetaTileEntity) {
- IChunkLoader aTileEntity = getChunkLoader(aBaseMetaTileEntity);
- if (aTileEntity.getChunkLoadingActive()) {
- GTPP_ChunkManager.releaseTicket((TileEntity)aBaseMetaTileEntity);
- }
- }
-
- public static boolean onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
- IChunkLoader aTileEntity = getChunkLoader(aBaseMetaTileEntity);
- if (aBaseMetaTileEntity.isServerSide() && aTileEntity.getResidingChunk() != null && !aTileEntity.getDoesWorkChunkNeedReload() && !aBaseMetaTileEntity.isAllowedToWork()) {
- // if machine has stopped, stop chunkloading
- GTPP_ChunkManager.releaseTicket((TileEntity)aBaseMetaTileEntity);
- aTileEntity.setDoesWorkChunkNeedReload(true);
- return false;
- }
- return true;
- }
-
- public static void createInitialWorkingChunk(IGregTechTileEntity aBaseMetaTileEntity, int aChunkX, int aDrillZ) {
- final int centerX = aChunkX >> 4;
- final int centerZ = aDrillZ >> 4;
- IChunkLoader aTileEntity = getChunkLoader(aBaseMetaTileEntity);
- aTileEntity.addChunkToLoadedList(new ChunkCoordIntPair(centerX, centerZ));
- GTPP_ChunkManager.requestChunkLoad((TileEntity)aBaseMetaTileEntity.getMetaTileEntity(), aTileEntity.getResidingChunk());
- aTileEntity.setDoesWorkChunkNeedReload(false);
- }
-
- private static final IChunkLoader getChunkLoader(IGregTechTileEntity aTile) {
- return (IChunkLoader) ((IGregTechTileEntity)aTile).getMetaTileEntity();
- }
-
-}