Skip to content

Commit

Permalink
Merge pull request #14 from Keriils/optimize-&-fix-bug
Browse files Browse the repository at this point in the history
fix bug on side sync
  • Loading branch information
Keriils authored Sep 3, 2024
2 parents 87647a4 + 7886bfd commit d094df1
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 64 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
api("com.github.GTNewHorizons:GT5-Unofficial:5.09.48.66:dev")
implementation("com.github.GTNewHorizons:Baubles:1.0.4:dev")
implementation("com.github.GTNewHorizons:Draconic-Evolution:1.3.5-GTNH:dev")
implementation("com.github.GTNewHorizons:EnderIO:2.7.6:dev")
implementation("com.github.GTNewHorizons:EnderIO:2.8.9:dev")
implementation("com.github.GTNewHorizons:Botania:1.11.3-GTNH:dev")
implementation("com.github.GTNewHorizons:Avaritia:1.49:dev")
implementation("com.github.GTNewHorizons:Avaritiaddons:1.7.1-GTNH:dev")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,38 @@
public class EntityTimeAccelerator extends Entity {

// region Fields
protected int timeRate = enableTimeAcceleratorBoost ? 8 : 4; // must be set in here for texture render init
protected int remainingTime = 600;
protected boolean isGregTechMachineMode = true;
private int timeRate = enableTimeAcceleratorBoost ? 8 : 4; // must be set in here for texture render init
private int remainingTime = 600;
private boolean isGregTechMachineMode = true;

protected int targetIntX;
protected int targetIntY;
protected int targetIntZ;
private int targetIntX;
private int targetIntY;
private int targetIntZ;

public void setGregTechMachineMode(boolean setMode) {
this.isGregTechMachineMode = setMode;
public void setGregTechMachineMode(boolean mode) {
this.isGregTechMachineMode = mode;
}

public int getTimeRate() {
return timeRate;
public boolean getGregTechMachineMode() {
return this.isGregTechMachineMode;
}

public int getTimeRateForRender() {
return this.dataWatcher.getWatchableObjectInt(2);
public int getTimeRate() {
return timeRate;
}

public void setTimeRate(int timeRate) {
this.timeRate = timeRate;
this.dataWatcher.updateObject(2, timeRate);
}

public int getTimeRateForRender() {
return this.dataWatcher.getWatchableObjectInt(2);
}

public int getRemainingTime() {
return remainingTime;
}

// endregion

// region Constructor
Expand All @@ -67,6 +70,7 @@ public EntityTimeAccelerator(World worldIn, int targetIntX, int targetIntY, int
this.targetIntX = targetIntX;
this.targetIntY = targetIntY;
this.targetIntZ = targetIntZ;
this.setPosition(targetIntX + 0.5D, targetIntY + 0.5D, targetIntZ + 0.5D);
}
// endregion

Expand All @@ -76,62 +80,87 @@ public EntityTimeAccelerator(World worldIn, int targetIntX, int targetIntY, int
*/
@Override
public void onEntityUpdate() {
if (this.worldObj.isRemote) return;
if (remainingTime-- > 0) this.tAccelerate();
if (remainingTime <= 0 && !this.worldObj.isRemote) this.setDead();
if (remainingTime <= 0) {
this.setDead();
this.resetTileEntity();
}
}

public void resetTileEntity() {
TileEntity tileEntity = this.worldObj.getTileEntity(targetIntX, targetIntY, targetIntZ);
if (shouldAccelerate(tileEntity)) {
this.worldObj.loadedTileEntityList.removeIf(k -> k == tileEntity);
this.worldObj.loadedTileEntityList.add(tileEntity);
}
}

private void tAccelerate() {

Block block = this.worldObj.getBlock(targetIntX, targetIntY, targetIntZ);
TileEntity tileEntity = this.worldObj.getTileEntity(targetIntX, targetIntY, targetIntZ);

// Referenced GTNH to control the performance in 1ms
long tMaxTime = System.nanoTime() + 1000000;

if (shouldAccelerate(block)) {
accelerateBlock(block, tMaxTime);
}

if (shouldAccelerate(tileEntity)) {
if (isGregTechMachineMode && tileEntity instanceof ITileEntityTickAcceleration tileEntityITEA) {
if (tileEntityITEA.tickAcceleration(timeRate)) return;
}
accelerateTileEntity(tileEntity, tMaxTime);
} else if (shouldAccelerate(block)) {
accelerateBlock(block, tMaxTime);
}
}

private void accelerateBlock(Block block, long tMaxTime) {
private boolean shouldAccelerate(TileEntity tileEntity) {
return tileEntity != null && !tileEntity.isInvalid() && tileEntity.canUpdate();
}

private boolean shouldAccelerate(Block block) {
return enableBlockMode && block != null
&& block.getTickRandomly()
&& worldObj.getTotalWorldTime() % accelerateBlockInterval == 0;
}

private void accelerateTileEntity(TileEntity tileEntity, long tMaxTime) {
try {
for (int i = 0; i < timeRate; i++) {
block.updateTick(worldObj, targetIntX, targetIntY, targetIntZ, worldObj.rand);
tileEntity.updateEntity();
if (System.nanoTime() > tMaxTime) {
break;
}
}
} catch (Exception e) {
LOG.warn("An error occurred accelerating block at ( {}, {}, {})", targetIntX, targetIntY, targetIntZ);
e.printStackTrace();
LOG.warn(
"An error occurred accelerating TileEntity at ( {}, {}, {}, {})",
targetIntX,
targetIntY,
targetIntZ,
e.getMessage());
}
}

private void accelerateTileEntity(TileEntity tileEntity, long tMaxTime) {
private void accelerateBlock(Block block, long tMaxTime) {
try {
for (int i = 0; i < timeRate; i++) {
tileEntity.updateEntity();
block.updateTick(worldObj, targetIntX, targetIntY, targetIntZ, worldObj.rand);
if (System.nanoTime() > tMaxTime) {
break;
}
}
} catch (Exception e) {
LOG.warn("An error occurred accelerating TileEntity at ( {}, {}, {})", targetIntX, targetIntY, targetIntZ);
e.printStackTrace();
LOG.warn(
"An error occurred accelerating block at ( {}, {}, {}, {})",
targetIntX,
targetIntY,
targetIntZ,
e.getMessage());
}
}

private boolean shouldAccelerate(TileEntity tileEntity) {
return tileEntity != null && !tileEntity.isInvalid() && tileEntity.canUpdate();
}

private boolean shouldAccelerate(Block block) {
return enableBlockMode && block != null
&& block.getTickRandomly()
&& worldObj.getTotalWorldTime() % accelerateBlockInterval == 0;
}
// endregion

// region NBT Setting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public void addInformation(final @NotNull ItemStack stack, final EntityPlayer pl
list.add(StatCollector.translateToLocal("text.NHUtilities.TimeVial.details_9"));
list.add(StatCollector.translateToLocal("text.NHUtilities.TimeVial.details_10"));
list.add(StatCollector.translateToLocal("text.NHUtilities.TimeVial.details_11"));
list.add(dividingLine);
list.add(StatCollector.translateToLocal("text.NHUtilities.TimeVial.details_12"));
list.add(dividingLine);
list.add(StatCollector.translateToLocal("text.NHUtilities.TimeVial.details_13"));
list.add(StatCollector.translateToLocal("text.NHUtilities.TimeVial.details_14"));
list.add(dividingLine);
Expand Down Expand Up @@ -97,16 +97,15 @@ public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, @NotNull Wor
double targetPosY = y + 0.5D;
double targetPosZ = z + 0.5D;

// 碰撞箱的最小坐标
// 计算碰撞箱大小
double minX = targetPosX - tHalfSize;
double minY = targetPosY - tHalfSize;
double minZ = targetPosZ - tHalfSize;

// 碰撞箱的最大坐标
double maxX = targetPosX + tHalfSize;
double maxY = targetPosY + tHalfSize;
double maxZ = targetPosZ + tHalfSize;

// 获取碰撞箱对应实体
Optional<EntityTimeAccelerator> box = world
.getEntitiesWithinAABB(
EntityTimeAccelerator.class,
Expand All @@ -115,45 +114,64 @@ public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, @NotNull Wor
.findFirst();

if (box.isPresent()) {

EntityTimeAccelerator eta = box.get();

int currentRate = eta.getTimeRate();
int nextRateTimeRequired = (int) (currentRate * eta.getRemainingTime() * timeVialDiscountValue); // no
// why
if (enableLogInfo) LOG.info("xxxxx {} xxxxx", nextRateTimeRequired);
int nextRateTimeRequired = (int) (currentRate * eta.getRemainingTime() * timeVialDiscountValue);

//// Invert the GregTechMachineMode
// if (player.isSneaking()) {
// eta.setGregTechMachineMode(!eta.getGregTechMachineMode());
// }

if (currentRate < MAX_ACCELERATION && shouldAndConsumeTimeData(stack, nextRateTimeRequired)) {
eta.setTimeRate(currentRate * 2);
int i = (int) (Math.log(currentRate) / Math.log(2)) - (enableTimeAcceleratorBoost ? 2 : 1);
world.playSoundEffect(
targetPosX,
targetPosY,
targetPosZ,
"note.harp",
defaultTimeVialVolumeValue,
SOUND_ARRAY_F[i]);
etaInteract(eta, world, targetPosX, targetPosY, targetPosZ);
}
} else if (shouldAndConsumeTimeData(stack, TIME_INIT_RATE * 600)) {
EntityTimeAccelerator eta = new EntityTimeAccelerator(world, x, y, z);

// set the GregTechMachineMode
if (player.isSneaking()) eta.setGregTechMachineMode(false);
eta.setPosition(targetPosX, targetPosY, targetPosZ);

world.spawnEntityInWorld(eta);
if (enableLogInfo) LOG.info(
"An entity entityTimeAccelerator has been spawned ({}, {}, {}).",
targetPosX,
targetPosY,
targetPosZ);
world.playSoundEffect(
targetPosX,
targetPosY,
targetPosZ,
"note.harp",
defaultTimeVialVolumeValue,
SOUND_ARRAY_F[0]);

etaInteract(eta, world, targetPosX, targetPosY, targetPosZ);
}
return true;
}
return false;
}

private void etaInteract(@NotNull EntityTimeAccelerator eta, World world, double targetPosX, double targetPosY,
double targetPosZ) {
this.playSoundForRateChange(world, targetPosX, targetPosY, targetPosZ, eta.getTimeRate());
if (enableLogInfo) {
this.debugInfo(targetPosX, targetPosY, targetPosZ, eta.getRemainingTime());
}
}

private void playSoundForRateChange(@NotNull World world, double targetPosX, double targetPosY, double targetPosZ,
int currentRate) {
int i = (int) (Math.log(currentRate) / Math.log(2)) - (enableTimeAcceleratorBoost ? 3 : 2);
// security considerations
if (i < 0 || i >= SOUND_ARRAY_F.length) i = 0;
world.playSoundEffect(
targetPosX,
targetPosY,
targetPosZ,
"note.harp",
defaultTimeVialVolumeValue,
SOUND_ARRAY_F[i]);
}

private void debugInfo(double targetPosX, double targetPosY, double targetPosZ, int remainingTime) {
LOG.info("xxxxx remainingTime: {} xxxxx", remainingTime);
LOG.info("An entity entityTimeAccelerator has been spawned ({}, {}, {}).", targetPosX, targetPosY, targetPosZ);

}

protected boolean shouldAndConsumeTimeData(@NotNull ItemStack stack, int consumedTick) {
int timeTick = stack.getTagCompound()
.getInteger("storedTimeTick");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/xir/NHUtilities/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Config {
public static boolean enableEternityVialCosmicRender = true;
public static boolean enableEternityVialCosmicRenderDeepening = true;
public static boolean enableBlockMode = true;
public static int accelerateBlockInterval = 10;
public static int accelerateBlockInterval = 2;
public static boolean enableLogInfo = false;
public static boolean limitOneTimeVial = true;
public static float timeVialDiscountValue = 0.9965F; // e..
Expand Down

0 comments on commit d094df1

Please sign in to comment.