aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/xmod/gregtech/api
diff options
context:
space:
mode:
authorAlkalus <3060479+draknyte1@users.noreply.github.com>2018-05-29 16:22:10 +1000
committerAlkalus <3060479+draknyte1@users.noreply.github.com>2018-05-29 16:22:10 +1000
commit45c071b2ac73abc28671bbaa087dec075e4254c0 (patch)
tree86ffb289dab9caf41b1887559632bba9b2dfa13f /src/Java/gtPlusPlus/xmod/gregtech/api
parentaaed15d6c833f634b785cad80913e8cd691cd88c (diff)
downloadGT5-Unofficial-45c071b2ac73abc28671bbaa087dec075e4254c0.tar.gz
GT5-Unofficial-45c071b2ac73abc28671bbaa087dec075e4254c0.tar.bz2
GT5-Unofficial-45c071b2ac73abc28671bbaa087dec075e4254c0.zip
$ Reduced cpu usage in EIO tooltip handler.
$ Made GTPP clean up any left-over threads when the server shuts down (Mostly for SSP memory leaks). $ Made each buffer get a unique thread. Thread life reduced greatly and capped out at 32k seconds.
Diffstat (limited to 'src/Java/gtPlusPlus/xmod/gregtech/api')
-rw-r--r--src/Java/gtPlusPlus/xmod/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_ThreadedBuffer.java17
-rw-r--r--src/Java/gtPlusPlus/xmod/gregtech/api/objects/GregtechBufferThread.java94
2 files changed, 77 insertions, 34 deletions
diff --git a/src/Java/gtPlusPlus/xmod/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_ThreadedBuffer.java b/src/Java/gtPlusPlus/xmod/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_ThreadedBuffer.java
index c7914331b7..8dc0a8c2cf 100644
--- a/src/Java/gtPlusPlus/xmod/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_ThreadedBuffer.java
+++ b/src/Java/gtPlusPlus/xmod/gregtech/api/metatileentity/implementations/GT_MetaTileEntity_ThreadedBuffer.java
@@ -6,6 +6,7 @@ import net.minecraft.entity.player.EntityPlayer;
import gregtech.api.enums.GT_Values;
import gregtech.api.util.GT_Utility;
+import gtPlusPlus.api.objects.minecraft.BlockPos;
import gtPlusPlus.xmod.gregtech.api.objects.GregtechBufferThread;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
@@ -18,6 +19,7 @@ import gregtech.api.interfaces.ITexture;
public abstract class GT_MetaTileEntity_ThreadedBuffer extends GT_MetaTileEntity_Buffer {
protected GregtechBufferThread mLogicThread;
+ protected BlockPos mPos;
public final ItemStack[] mInventorySynchro;
public GT_MetaTileEntity_ThreadedBuffer(final int aID, final String aName, final String aNameRegional, final int aTier,
@@ -69,7 +71,7 @@ public abstract class GT_MetaTileEntity_ThreadedBuffer extends GT_MetaTileEntity
return mLogicThread;
}
else {
- return this.mLogicThread = GregtechBufferThread.getBufferThread(this.getBaseMetaTileEntity().getWorld());
+ return this.mLogicThread = GregtechBufferThread.getBufferThread(mPos);
}
}
@@ -297,8 +299,17 @@ public abstract class GT_MetaTileEntity_ThreadedBuffer extends GT_MetaTileEntity
}
public synchronized void onPostTick(final IGregTechTileEntity aBaseMetaTileEntity, final long aTimer) {
- if (aBaseMetaTileEntity.isServerSide())
- getLogicThread().onPostTick(aBaseMetaTileEntity, aTimer, this);
+ if (aBaseMetaTileEntity.isServerSide()) {
+ if (mPos == null) {
+ mPos = new BlockPos(this.getBaseMetaTileEntity().getXCoord(), this.getBaseMetaTileEntity().getYCoord(), this.getBaseMetaTileEntity().getZCoord(), this.getBaseMetaTileEntity().getWorld());
+ }
+ if (mLogicThread == null) {
+ mLogicThread = GregtechBufferThread.getBufferThread(mPos);
+ }
+ if (mLogicThread!= null) {
+ getLogicThread().onPostTick(aBaseMetaTileEntity, aTimer, this);
+ }
+ }
}
public void onFirstTick(final IGregTechTileEntity aBaseMetaTileEntity) {
diff --git a/src/Java/gtPlusPlus/xmod/gregtech/api/objects/GregtechBufferThread.java b/src/Java/gtPlusPlus/xmod/gregtech/api/objects/GregtechBufferThread.java
index 15f4ad503f..47c53d18f1 100644
--- a/src/Java/gtPlusPlus/xmod/gregtech/api/objects/GregtechBufferThread.java
+++ b/src/Java/gtPlusPlus/xmod/gregtech/api/objects/GregtechBufferThread.java
@@ -7,51 +7,55 @@ import java.util.Map;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
-import net.minecraft.world.World;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.util.GT_Utility;
import gtPlusPlus.api.objects.Logger;
+import gtPlusPlus.api.objects.minecraft.BlockPos;
import gtPlusPlus.xmod.gregtech.api.metatileentity.implementations.GT_MetaTileEntity_ThreadedBuffer;
import gtPlusPlus.xmod.gregtech.common.tileentities.automation.GT_MetaTileEntity_ThreadedChestBuffer;
public class GregtechBufferThread extends Thread {
public static final Map<String, GregtechBufferThread> mBufferThreadAllocation = new HashMap<String, GregtechBufferThread>();
- private final World mWorldRef;
- private short mLifeCycleTime = 900;
+ private final BlockPos mBlockPos;
+ private int mLifeCycleTime = 300;
+ private final String mID;
- public static synchronized final GregtechBufferThread getBufferThread(World world) {
- if (world != null && mBufferThreadAllocation.containsKey(""+world.provider.dimensionId)){
+ public static synchronized final GregtechBufferThread getBufferThread(BlockPos pos) {
+ if (pos != null && mBufferThreadAllocation.containsKey(""+pos.getUniqueIdentifier())){
Logger.INFO("[SB] Found an existing thread for this dimension.");
- return mBufferThreadAllocation.get(""+world.provider.dimensionId);
+ return mBufferThreadAllocation.get(""+pos.getUniqueIdentifier());
}
else {
- return new GregtechBufferThread(world);
+ return new GregtechBufferThread(pos);
}
}
- public GregtechBufferThread(World world) {
+ public GregtechBufferThread(BlockPos pos) {
super();
- int mID = world != null ? world.provider.dimensionId : Short.MIN_VALUE;
- if (world != null && !mBufferThreadAllocation.containsKey(""+mID)){
- mWorldRef = world;
- mBufferThreadAllocation.put(""+mID, this);
+ String aID = pos != null ? pos.getUniqueIdentifier() : ""+Short.MIN_VALUE;
+ this.mID = aID;
+ if (pos != null && !mBufferThreadAllocation.containsKey(mID)){
+ mBlockPos = pos;
+ mBufferThreadAllocation.put(mID, this);
}
else {
this.mLifeCycleTime = 1;
- mWorldRef = null;
+ mBlockPos = null;
}
- this.setName("GTPP_SuperBuffer-Dim("+mID+")");
- if (mWorldRef != null && !this.isAlive()) {
- this.start();
+ this.setName("GTPP-SuperBuffer("+mID+")");
+ if (mBlockPos != null && !this.isAlive()) {
+ start();
Logger.INFO("[SB] Created a SuperBuffer Thread for dimension "+mID+".");
}
}
public synchronized void fillStacksIntoFirstSlots(GT_MetaTileEntity_ThreadedChestBuffer mBuffer) {
- mLifeCycleTime += 100;
+ if (mLifeCycleTime < (Short.MAX_VALUE-10)){
+ mLifeCycleTime += 10;
+ }
for (int i = 0; i < mBuffer.mInventorySynchro.length - 1; ++i) {
for (int j = i + 1; j < mBuffer.mInventorySynchro.length - 1; ++j) {
if (mBuffer.mInventorySynchro[j] != null && (mBuffer.mInventorySynchro[i] == null
@@ -64,7 +68,9 @@ public class GregtechBufferThread extends Thread {
}
public synchronized boolean moveItems(final IGregTechTileEntity aBaseMetaTileEntity, final long aTimer, GT_MetaTileEntity_ThreadedBuffer mBuffer) {
- mLifeCycleTime += 100;
+ if (mLifeCycleTime < (Short.MAX_VALUE-10)){
+ mLifeCycleTime += 10;
+ }
final byte mTargetStackSize = (byte) mBuffer.mTargetStackSize;
final int tCost = GT_Utility.moveOneItemStack((Object) aBaseMetaTileEntity,
(Object) aBaseMetaTileEntity.getTileEntityAtSide(aBaseMetaTileEntity.getBackFacing()),
@@ -85,6 +91,10 @@ public class GregtechBufferThread extends Thread {
&& (aBaseMetaTileEntity.hasWorkJustBeenEnabled() || aBaseMetaTileEntity.hasInventoryBeenModified()
|| aTimer % 200L == 0L || mBuffer.mSuccess > 0)) {
--mBuffer.mSuccess;
+ if (mLifeCycleTime < (Short.MAX_VALUE-1)){
+ mLifeCycleTime += 1;
+ }
+ //Logger.INFO("Ticking SB @ "+mBuffer.getLogicThread().mBlockPos.getUniqueIdentifier() + " | Time Left: "+mLifeCycleTime);
moveItems(aBaseMetaTileEntity, aTimer, mBuffer);
for (byte b = 0; b < 6; ++b) {
aBaseMetaTileEntity.setInternalOutputRedstoneSignal(b, (byte) (mBuffer.bInvert ? 15 : 0));
@@ -168,23 +178,45 @@ public class GregtechBufferThread extends Thread {
return 0;
}
+ //Logic Vars
+ private boolean mRunning = true;
+
@Override
- public void run() {
- while (mLifeCycleTime > 0) {
- mLifeCycleTime--;
- Logger.INFO("[SB] Ticking Thread for dimension. "+mLifeCycleTime);
- try {
- this.sleep(1000);
+ public void run() {
+ //While thread is alive.
+ while (mRunning) {
+ //While thread is active, lets tick it's life down.
+ while (mLifeCycleTime > 0) {
+ //Remove invalid threads
+ if (this.mBlockPos.world == null) {
+ mLifeCycleTime = 0;
+ }
+ //Prevent Overflows
+ if (mLifeCycleTime > Short.MAX_VALUE) {
+ mLifeCycleTime = Short.MAX_VALUE;
+ }
+ try {
+ sleep(1000);
+ mLifeCycleTime--;
+ Logger.INFO("[SB] Ticking Thread "+mID+" | Remaining: "+mLifeCycleTime+"s");
+ }
+ catch (InterruptedException e) {
+ mLifeCycleTime = 0;
+ }
}
- catch (InterruptedException e) {
-
+ if (mLifeCycleTime <= 0) {
+ destroy();
}
}
- if (mLifeCycleTime <= 0) {
- int mID = (mWorldRef != null ? mWorldRef.provider.dimensionId : Short.MIN_VALUE);
- GregtechBufferThread.mBufferThreadAllocation.remove(""+mID, this);
- Logger.INFO("[SB] Removing Thread for dimension "+mID);
- }
+ }
+
+ @Override
+ public void destroy() {
+ GregtechBufferThread.mBufferThreadAllocation.remove(mID, this);
+ Logger.INFO("[SB] Removing Thread "+mID);
+ mRunning = false;
+ mLifeCycleTime = 0;
+ stop();
}