diff options
author | Richard Hendricks <richardhendricks@pobox.com> | 2018-06-02 03:13:49 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-02 03:13:49 -0500 |
commit | 279ccdba6f6ca2d0f00fd8558ea054184684cc79 (patch) | |
tree | a1bb5aa42beae11240267b5edc6e459a716cc83c /src/main/java | |
parent | 10f8628173a2888a12d3964db5dd4a22ac3c04da (diff) | |
parent | fe08b96bd94a487a2e26654b1812691a91bd3407 (diff) | |
download | GT5-Unofficial-279ccdba6f6ca2d0f00fd8558ea054184684cc79.tar.gz GT5-Unofficial-279ccdba6f6ca2d0f00fd8558ea054184684cc79.tar.bz2 GT5-Unofficial-279ccdba6f6ca2d0f00fd8558ea054184684cc79.zip |
Merge pull request #124 from GTNewHorizons/debug/chestbuffer
Debug/chestbuffer
Diffstat (limited to 'src/main/java')
3 files changed, 116 insertions, 18 deletions
diff --git a/src/main/java/gregtech/api/metatileentity/BaseMetaPipeEntity.java b/src/main/java/gregtech/api/metatileentity/BaseMetaPipeEntity.java index a761afd5cb..0d06c4ec03 100644 --- a/src/main/java/gregtech/api/metatileentity/BaseMetaPipeEntity.java +++ b/src/main/java/gregtech/api/metatileentity/BaseMetaPipeEntity.java @@ -183,7 +183,7 @@ public class BaseMetaPipeEntity extends BaseTileEntity implements IGregTechTileE mMetaTileEntity.setBaseMetaTileEntity(this);
}
- long tTime = System.currentTimeMillis();
+ long tTime = System.nanoTime();
int tCode = 0;
try { for (tCode = 0; hasValidMetaTileEntity() && tCode >= 0; ) {
@@ -298,11 +298,11 @@ public class BaseMetaPipeEntity extends BaseTileEntity implements IGregTechTileE }
if (isServerSide() && hasValidMetaTileEntity()) {
- tTime = System.currentTimeMillis() - tTime;
+ tTime = System.nanoTime() - tTime;
if (mTimeStatistics.length > 0)
mTimeStatistics[mTimeStatisticsIndex = (mTimeStatisticsIndex + 1) % mTimeStatistics.length] = (int) tTime;
- if (tTime > 0 && tTime > GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING && mTickTimer > 1000 && getMetaTileEntity().doTickProfilingMessageDuringThisTick() && mLagWarningCount++ < 10)
- FMLLog.warning("WARNING: Possible Lag Source at [%s,%s,%s] in Dimension %s with %s ms caused by an instance of %s", xCoord, yCoord, zCoord, worldObj.provider.dimensionId, tTime, getMetaTileEntity().getClass());
+ if (tTime > 0 && tTime > (GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING*1000000) && mTickTimer > 1000 && getMetaTileEntity().doTickProfilingMessageDuringThisTick() && mLagWarningCount++ < 10)
+ FMLLog.warning("WARNING: Possible Lag Source at [%s,%s,%s] in Dimension %s with %s ns caused by an instance of %s", xCoord, yCoord, zCoord, worldObj.provider.dimensionId, tTime, getMetaTileEntity().getClass());
}
mWorkUpdate = mInventoryChanged = false;
@@ -395,8 +395,14 @@ public class BaseMetaPipeEntity extends BaseTileEntity implements IGregTechTileE if (aLogLevel > 1) {
if (mTimeStatistics.length > 0) {
double tAverageTime = 0;
- for (int tTime : mTimeStatistics) tAverageTime += tTime;
- tList.add("Average CPU-load of ~" + (tAverageTime / mTimeStatistics.length) + "ms since " + mTimeStatistics.length + " ticks.");
+ double tWorstTime = 0;
+ for (int tTime : mTimeStatistics) {
+ tAverageTime += tTime;
+ if (tTime > tWorstTime) {
+ tWorstTime = tTime;
+ }
+ }
+ tList.add("Average CPU-load of ~" + (tAverageTime / mTimeStatistics.length) + "ns since " + mTimeStatistics.length + " ticks with worst time of " + tWorstTime + "ns.");
}
if (mLagWarningCount > 0) {
tList.add("Caused " + (mLagWarningCount >= 10 ? "more than 10" : mLagWarningCount) + " Lag Spike Warnings (anything taking longer than " + GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING + "ms) on the Server.");
diff --git a/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java b/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java index d44ada526f..95f3416a00 100644 --- a/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java +++ b/src/main/java/gregtech/api/metatileentity/BaseMetaTileEntity.java @@ -277,7 +277,7 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE } mRunningThroughTick = true; - long tTime = System.currentTimeMillis(); + long tTime = System.nanoTime(); int tCode = 0; boolean aSideServer = isServerSide(); boolean aSideClient = isClientSide(); @@ -573,11 +573,11 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE } if (aSideServer && hasValidMetaTileEntity()) { - tTime = System.currentTimeMillis() - tTime; + tTime = System.nanoTime() - tTime; if (mTimeStatistics.length > 0) mTimeStatistics[mTimeStatisticsIndex = (mTimeStatisticsIndex + 1) % mTimeStatistics.length] = (int) tTime; - if (tTime > 0 && tTime > GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING && mTickTimer > 1000 && getMetaTileEntity().doTickProfilingMessageDuringThisTick() && mLagWarningCount++ < 10) - System.out.println("WARNING: Possible Lag Source at [" + xCoord + ", " + yCoord + ", " + zCoord + "] in Dimension " + worldObj.provider.dimensionId + " with " + tTime + "ms caused by an instance of " + getMetaTileEntity().getClass()); + if (tTime > 0 && tTime > (GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING*1000000) && mTickTimer > 1000 && getMetaTileEntity().doTickProfilingMessageDuringThisTick() && mLagWarningCount++ < 10) + System.out.println("WARNING: Possible Lag Source at [" + xCoord + ", " + yCoord + ", " + zCoord + "] in Dimension " + worldObj.provider.dimensionId + " with " + tTime + "ns caused by an instance of " + getMetaTileEntity().getClass()); } mWorkUpdate = mInventoryChanged = mRunningThroughTick = false; @@ -706,8 +706,16 @@ public class BaseMetaTileEntity extends BaseTileEntity implements IGregTechTileE if (aLogLevel > 1) { if (mTimeStatistics.length > 0) { double tAverageTime = 0; - for (int tTime : mTimeStatistics) tAverageTime += tTime; - tList.add("Average CPU-load of ~" + (tAverageTime / mTimeStatistics.length) + "ms since " + mTimeStatistics.length + " ticks."); + double tWorstTime = 0; + for (int tTime : mTimeStatistics) { + tAverageTime += tTime; + if (tTime > tWorstTime) { + tWorstTime = tTime; + } + // Uncomment this line to print out tick-by-tick times. + //tList.add("tTime " + tTime); + } + tList.add("Average CPU-load of ~" + (tAverageTime / mTimeStatistics.length) + "ns over " + mTimeStatistics.length + " ticks with worst time of " + tWorstTime + "ns."); } if (mLagWarningCount > 0) { tList.add("Caused " + (mLagWarningCount >= 10 ? "more than 10" : mLagWarningCount) + " Lag Spike Warnings (anything taking longer than " + GregTech_API.MILLISECOND_THRESHOLD_UNTIL_LAG_WARNING + "ms) on the Server."); diff --git a/src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java b/src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java index 7385bb31b1..e73631cd93 100644 --- a/src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java +++ b/src/main/java/gregtech/common/tileentities/automation/GT_MetaTileEntity_ChestBuffer.java @@ -10,6 +10,12 @@ import gregtech.api.util.GT_Utility; import gregtech.common.gui.GT_Container_ChestBuffer; import gregtech.common.gui.GT_GUIContainer_ChestBuffer; import net.minecraft.entity.player.InventoryPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemBlock; +import net.minecraft.item.ItemStack; + +import java.util.Arrays; +import java.util.Comparator; public class GT_MetaTileEntity_ChestBuffer extends GT_MetaTileEntity_Buffer { @@ -49,16 +55,94 @@ public class GT_MetaTileEntity_ChestBuffer } protected void moveItems(IGregTechTileEntity aBaseMetaTileEntity, long aTimer) { - fillStacksIntoFirstSlots(); - super.moveItems(aBaseMetaTileEntity, aTimer); - fillStacksIntoFirstSlots(); + if(aBaseMetaTileEntity.hasInventoryBeenModified()) { + fillStacksIntoFirstSlots(); + } + // mSuccess will be negative if the call is caused by the %200 aTimer, always try to push. Otherwise it will be positive. + // For the first 6 ticks after a successful move (49->44), push every tick. Then go to every 5 ticks. + if ( (mSuccess <= 0 ) || (mSuccess > 43) || ((mSuccess % 5) == 0 )){ + super.moveItems(aBaseMetaTileEntity, aTimer); + } + // mSuccesss is set to 50 on a successful move + if(mSuccess == 50) { + fillStacksIntoFirstSlots(); + } + if(mSuccess < 0) { + mSuccess = 0; + } + } + +// Implementation using Java built in sort algorithm +// Uses terribad string comparison to sort against. Would be better if we did something else? + protected void sortStacks() { + Arrays.sort(this.mInventory, new Comparator<ItemStack>() { + @Override + // Taken from https://gist.github.com/Choonster/876acc3217229e172e46 + public int compare(ItemStack o1, ItemStack o2) { + if( o2 == null ) + return -1; + if( o1 == null ) + return 1; + Item item1 = o1.getItem(); + Item item2 = o2.getItem(); + + // If item1 is a block and item2 isn't, sort item1 before item2 + if (((item1 instanceof ItemBlock)) && (!(item2 instanceof ItemBlock))) { + return -1; + } + + // If item2 is a block and item1 isn't, sort item1 after item2 + if (((item2 instanceof ItemBlock)) && (!(item1 instanceof ItemBlock))) { + return 1; + } + + // If the items are blocks, use the string comparison + if ((item1 instanceof ItemBlock)) { // only need to check one since we did the check above + String displayName1 = o1.getDisplayName(); + String displayName2 = o2.getDisplayName(); + int result = displayName1.compareToIgnoreCase(displayName2); + //System.out.println("sorter: " + displayName1 + " " + displayName2 + " " + result); + return result; + } else + { + // Not a block. Use the ID and damage to compare them. + int id1 = Item.getIdFromItem( item1 ); + int id2 = Item.getIdFromItem( item2 ); + if ( id1 < id2 ) { + return -1; + } + if ( id1 > id2 ) { + return 1; + } + // id1 must equal id2, get their damage and compare + id1 = o1.getItemDamage(); + id2 = o2.getItemDamage(); + + if ( id1 < id2 ) { + return -1; + } + if ( id1 > id2 ) { + return 1; + } + return 0; + } + } + }); } protected void fillStacksIntoFirstSlots() { - for (int i = 0; i < this.mInventory.length - 1; i++) { - for (int j = i + 1; j < this.mInventory.length - 1; j++) { - if ((this.mInventory[j] != null) && ((this.mInventory[i] == null) || (GT_Utility.areStacksEqual(this.mInventory[i], this.mInventory[j])))) { + sortStacks(); + // Merge small stacks together + for (int i = 0; i < this.mInventory.length-1;) { + //System.out.println( (this.mInventory[i] == null) ? "Slot empty " + i : "Slot " + i + " holds " + this.mInventory[i].getDisplayName()); + for (int j = i + 1; j < this.mInventory.length; j++) { + if ((this.mInventory[j] != null) && ((GT_Utility.areStacksEqual(this.mInventory[i], this.mInventory[j])))) { GT_Utility.moveStackFromSlotAToSlotB(getBaseMetaTileEntity(), getBaseMetaTileEntity(), j, i, (byte) 64, (byte) 1, (byte) 64, (byte) 1); + //System.out.println( "Moving slot " + j + " into slot " + i ); + } + else { + i=j; + break; // No more matching items for this i, do next i } } } |