From 7886bfdafb4667205526201ca475a9067abcb1ed Mon Sep 17 00:00:00 2001 From: DauphalXir <3013875189@qq.com> Date: Tue, 3 Sep 2024 15:27:33 +0800 Subject: [PATCH] fix bug on side sync --- dependencies.gradle | 2 +- .../common/entity/EntityTimeAccelerator.java | 97 ++++++++++++------- .../common/items/timeVial/TimeVial.java | 74 ++++++++------ .../com/xir/NHUtilities/config/Config.java | 2 +- 4 files changed, 111 insertions(+), 64 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index 271d24c..02f2538 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -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") diff --git a/src/main/java/com/xir/NHUtilities/common/entity/EntityTimeAccelerator.java b/src/main/java/com/xir/NHUtilities/common/entity/EntityTimeAccelerator.java index 25311dd..f91a897 100644 --- a/src/main/java/com/xir/NHUtilities/common/entity/EntityTimeAccelerator.java +++ b/src/main/java/com/xir/NHUtilities/common/entity/EntityTimeAccelerator.java @@ -21,24 +21,24 @@ 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) { @@ -46,10 +46,13 @@ public void setTimeRate(int timeRate) { this.dataWatcher.updateObject(2, timeRate); } + public int getTimeRateForRender() { + return this.dataWatcher.getWatchableObjectInt(2); + } + public int getRemainingTime() { return remainingTime; } - // endregion // region Constructor @@ -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 @@ -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 diff --git a/src/main/java/com/xir/NHUtilities/common/items/timeVial/TimeVial.java b/src/main/java/com/xir/NHUtilities/common/items/timeVial/TimeVial.java index a9dde44..0cf45f5 100644 --- a/src/main/java/com/xir/NHUtilities/common/items/timeVial/TimeVial.java +++ b/src/main/java/com/xir/NHUtilities/common/items/timeVial/TimeVial.java @@ -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); @@ -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 box = world .getEntitiesWithinAABB( EntityTimeAccelerator.class, @@ -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"); diff --git a/src/main/java/com/xir/NHUtilities/config/Config.java b/src/main/java/com/xir/NHUtilities/config/Config.java index 0206c1a..463bc2a 100644 --- a/src/main/java/com/xir/NHUtilities/config/Config.java +++ b/src/main/java/com/xir/NHUtilities/config/Config.java @@ -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..