From 96f0d498ac72d611d8dd9efd6900385ff8c9243b Mon Sep 17 00:00:00 2001 From: screret <68943070+screret@users.noreply.github.com> Date: Thu, 18 Apr 2024 10:22:40 +0300 Subject: [PATCH 1/2] fix world accelerator class formatting fix world accelerator using 2x energy fix world accelerator always accelerating BEs even in random tick mode --- .../machine/WorldAcceleratorRenderer.java | 18 +-- .../electric/WorldAcceleratorMachine.java | 130 +++++++++--------- 2 files changed, 71 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/client/renderer/machine/WorldAcceleratorRenderer.java b/src/main/java/com/gregtechceu/gtceu/client/renderer/machine/WorldAcceleratorRenderer.java index 3cdbf6c12d..44caa0d5c9 100644 --- a/src/main/java/com/gregtechceu/gtceu/client/renderer/machine/WorldAcceleratorRenderer.java +++ b/src/main/java/com/gregtechceu/gtceu/client/renderer/machine/WorldAcceleratorRenderer.java @@ -19,18 +19,18 @@ import java.util.function.Consumer; public class WorldAcceleratorRenderer extends TieredHullMachineRenderer{ - private final WorkableOverlayModel BEModeModel, RTModeModel; + private final WorkableOverlayModel blockEntityModeModel, randomTickModeModel; public WorldAcceleratorRenderer(int tier, ResourceLocation beModeModelPath, ResourceLocation rtModeModelPath) { super(tier, GTCEu.id("block/machine/hull_machine")); - BEModeModel = new WorkableOverlayModel(beModeModelPath); - RTModeModel = new WorkableOverlayModel(rtModeModelPath); + blockEntityModeModel = new WorkableOverlayModel(beModeModelPath); + randomTickModeModel = new WorkableOverlayModel(rtModeModelPath); } - private WorkableOverlayModel getModeModel(boolean isRandomTickMode){ - if (isRandomTickMode){ - return RTModeModel; + private WorkableOverlayModel getModeModel(boolean isRandomTickMode) { + if (isRandomTickMode) { + return randomTickModeModel; } - return BEModeModel; + return blockEntityModeModel; } @Override @@ -50,8 +50,8 @@ public void renderMachine(List quads, MachineDefinition definition, @ public void onPrepareTextureAtlas(ResourceLocation atlasName, Consumer register) { super.onPrepareTextureAtlas(atlasName, register); if (atlasName.equals(TextureAtlas.LOCATION_BLOCKS)) { - RTModeModel.registerTextureAtlas(register); - BEModeModel.registerTextureAtlas(register); + randomTickModeModel.registerTextureAtlas(register); + blockEntityModeModel.registerTextureAtlas(register); } } } diff --git a/src/main/java/com/gregtechceu/gtceu/common/machine/electric/WorldAcceleratorMachine.java b/src/main/java/com/gregtechceu/gtceu/common/machine/electric/WorldAcceleratorMachine.java index d3b6485ada..1ddaf1864e 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/machine/electric/WorldAcceleratorMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/common/machine/electric/WorldAcceleratorMachine.java @@ -25,6 +25,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerLevel; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; @@ -49,37 +50,17 @@ */ public class WorldAcceleratorMachine extends TieredEnergyMachine implements IControllable { - private static final Map> blacklistedClasses = new Object2ObjectOpenHashMap<>(); private static final Object2BooleanFunction> blacklistCache = new Object2BooleanOpenHashMap<>(); - private static boolean gatheredClasses = false; //Hard-coded blacklist for blockentities private static final List blockEntityClassNamesBlackList = new ArrayList<>(); - protected static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(WorldAcceleratorMachine.class, TieredEnergyMachine.MANAGED_FIELD_HOLDER); - - private TickableSubscription subscription; - - private static final long BEAmperage = 6; - private static final long RTAmperage = 3; - @Getter - @Persisted - @DescSynced - private boolean isWorkingEnabled = true; - @DescSynced - @Persisted - @Getter - private boolean isRandomTickMode = true; - - @DescSynced - @Persisted - @Getter - @RequireRerender - private boolean active = false; - + protected static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(WorldAcceleratorMachine.class, TieredEnergyMachine.MANAGED_FIELD_HOLDER); + private static final long blockEntityAmperage = 6; + private static final long randomTickAmperage = 3; // Variables for Random Tick mode optimization // limit = ((tier - min) / (max - min)) * 2^tier private static final int[] SUCCESS_LIMITS = { 1, 8, 27, 64, 125, 216, 343, 512 }; @@ -87,6 +68,16 @@ public class WorldAcceleratorMachine extends TieredEnergyMachine implements ICon private final int speed; private final int successLimit; private final int randRange; + @Getter + @Persisted @DescSynced + private boolean isWorkingEnabled = true; + @Getter + @Persisted @DescSynced + private boolean isRandomTickMode = true; + @Getter + @Persisted @DescSynced @RequireRerender + private boolean active = false; + private TickableSubscription tickSubs; public WorldAcceleratorMachine(IMachineBlockEntity holder, int tier, Object... args) { super(holder, tier, defaultTankSizeFunction, args); @@ -108,21 +99,20 @@ public WorldAcceleratorMachine(IMachineBlockEntity holder, int tier, Object... a } public void updateSubscription() { - if (drainEnergy(true) && isWorkingEnabled) { - subscription = subscribeServerTick(subscription, this::update); + if (isWorkingEnabled && drainEnergy(true)) { + tickSubs = subscribeServerTick(tickSubs, this::update); active = true; - } else if (subscription != null) { - subscription.unsubscribe(); - subscription = null; + } else if (tickSubs != null) { + tickSubs.unsubscribe(); + tickSubs = null; active = false; } } public void update() { - drainEnergy(false); - //handle random tick mode - if(isRandomTickMode){ + //handle random tick mode + if(isRandomTickMode) { BlockPos cornerPos = new BlockPos( getPos().getX() - getTier(), getPos().getY() - getTier(), @@ -135,59 +125,62 @@ public void update() { GTValues.RNG.nextInt(randRange), GTValues.RNG.nextInt(randRange)); if(randomPos.getY() > getLevel().getMaxBuildHeight() || randomPos.getY() < getLevel().getMinBuildHeight() || !getLevel().isLoaded(randomPos) || randomPos.equals(getPos())) continue; - if(getLevel().getBlockState(randomPos).isRandomlyTicking()) - getLevel().getBlockState(randomPos).randomTick(this.getLevel().getServer().getLevel(this.getLevel().dimension()), randomPos,GTValues.RNG); + if(getLevel().getBlockState(randomPos).isRandomlyTicking()) { + getLevel().getBlockState(randomPos).randomTick((ServerLevel) this.getLevel(), randomPos,GTValues.RNG); + } i++; } return; - } - // else handle block entity mode - for (Direction dir : GTUtil.DIRECTIONS){ - BlockEntity blockEntity = this.getLevel().getBlockEntity(this.getPos().relative(dir)); - if(blockEntity != null && canAccelerate(blockEntity)) - tickBlockEntity(blockEntity); + } else { + // else handle block entity mode + for (Direction dir : GTUtil.DIRECTIONS) { + BlockEntity blockEntity = this.getLevel().getBlockEntity(this.getPos().relative(dir)); + if(blockEntity != null && canAccelerate(blockEntity)) { + tickBlockEntity(blockEntity); + } + } } updateSubscription(); - } public boolean drainEnergy(boolean simulate) { - long toDrain = (isRandomTickMode?RTAmperage:BEAmperage)*GTValues.V[tier]; + long toDrain = (isRandomTickMode ? randomTickAmperage : blockEntityAmperage) * GTValues.V[tier]; long resultEnergy = energyContainer.getEnergyStored() - toDrain; if (resultEnergy >= 0L && resultEnergy <= energyContainer.getEnergyCapacity()) { - energyContainer.removeEnergy(toDrain); - if(!simulate) + if(!simulate) { energyContainer.removeEnergy(toDrain); + } return true; } return false; } - private void tickBlockEntity(@NotNull T blockEntity){ - BlockPos pos = blockEntity.getBlockPos(); - BlockEntityTicker blockEntityTicker = this.getLevel().getBlockState(pos).getTicker(this.getLevel(),(BlockEntityType) blockEntity.getType()); - if(blockEntityTicker==null) return; - for (int i = 0; i < speed-1; i++) - blockEntityTicker.tick(blockEntity.getLevel(), blockEntity.getBlockPos(), blockEntity.getBlockState(), blockEntity); + private void tickBlockEntity(@NotNull T blockEntity) { + BlockPos pos = blockEntity.getBlockPos(); + //noinspection unchecked + BlockEntityTicker blockEntityTicker = this.getLevel().getBlockState(pos).getTicker(this.getLevel(), (BlockEntityType) blockEntity.getType()); + if(blockEntityTicker == null) return; + for (int i = 0; i < speed-1; i++) { + blockEntityTicker.tick(blockEntity.getLevel(), blockEntity.getBlockPos(), blockEntity.getBlockState(), blockEntity); + } } - private boolean canAccelerate(BlockEntity blockEntity){ + private boolean canAccelerate(BlockEntity blockEntity) { if(blockEntity instanceof PipeBlockEntity || blockEntity instanceof IMachineBlockEntity) return false; generateWorldAcceleratorBlacklist(); - final Class blockEntityClass = blockEntity.getClass(); - if (blacklistCache.containsKey(blockEntityClass)) + if (blacklistCache.containsKey(blockEntityClass)) { return blacklistCache.getBoolean(blockEntityClass); + } - - for (Class clazz : blacklistedClasses.values()) + for (Class clazz : blacklistedClasses.values()) { if (clazz.isAssignableFrom(blockEntityClass)) { // Is a subclass, so it cannot be accelerated blacklistCache.put(blockEntityClass, false); return false; } - + } blacklistCache.put(blockEntityClass, true); return true; @@ -196,19 +189,20 @@ private boolean canAccelerate(BlockEntity blockEntity){ @Override public void onLoad() { super.onLoad(); - if(!isRemote()) + if(!isRemote()) { energyContainer.addChangedListener(this::updateSubscription); - + } } @Override public void onUnload() { super.onUnload(); - if(subscription!=null) - unsubscribe(subscription); + if(tickSubs != null) { + tickSubs.unsubscribe(); + tickSubs = null; + } } - public void setWorkingEnabled(boolean workingEnabled) { isWorkingEnabled = workingEnabled; updateSubscription(); @@ -239,29 +233,29 @@ protected InteractionResult onSoftMalletClick(Player playerIn, InteractionHand h protected @NotNull InteractionResult onScrewdriverClick(Player playerIn, InteractionHand hand, Direction gridSide, BlockHitResult hitResult) { if (!isRemote()) { isRandomTickMode = !isRandomTickMode; - playerIn.sendSystemMessage(Component.translatable(isRandomTickMode?"gtceu.machine.world_accelerator.mode_entity":"gtceu.machine.world_accelerator.mode_tile")); + playerIn.sendSystemMessage(Component.translatable(isRandomTickMode ? "gtceu.machine.world_accelerator.mode_entity" : "gtceu.machine.world_accelerator.mode_tile")); scheduleRenderUpdate(); } return InteractionResult.CONSUME; } - - private static void generateWorldAcceleratorBlacklist(){ + private static void generateWorldAcceleratorBlacklist() { if (!gatheredClasses) { - for (String name : ConfigHolder.INSTANCE.machines.worldAcceleratorBlacklist) - if (!blacklistedClasses.containsKey(name)) + for (String name : ConfigHolder.INSTANCE.machines.worldAcceleratorBlacklist) { + if (!blacklistedClasses.containsKey(name)) { try { blacklistedClasses.put(name, Class.forName(name)); } catch (ClassNotFoundException ignored) { GTCEu.LOGGER.warn("Could not find class {} for World Accelerator Blacklist!", name); } + } + } - - for(String className: blockEntityClassNamesBlackList) + for(String className: blockEntityClassNamesBlackList) { try { blacklistedClasses.put(className, Class.forName(className)); } catch (ClassNotFoundException ignored) {} - + } gatheredClasses = true; } From 91693940cfd7066fbf614d47c610765c77e9f862 Mon Sep 17 00:00:00 2001 From: screret <68943070+screret@users.noreply.github.com> Date: Fri, 19 Apr 2024 12:50:41 +0300 Subject: [PATCH 2/2] remove bad return --- .../gtceu/common/machine/electric/WorldAcceleratorMachine.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/gregtechceu/gtceu/common/machine/electric/WorldAcceleratorMachine.java b/src/main/java/com/gregtechceu/gtceu/common/machine/electric/WorldAcceleratorMachine.java index 1ddaf1864e..25a48c2cfb 100644 --- a/src/main/java/com/gregtechceu/gtceu/common/machine/electric/WorldAcceleratorMachine.java +++ b/src/main/java/com/gregtechceu/gtceu/common/machine/electric/WorldAcceleratorMachine.java @@ -130,7 +130,6 @@ public void update() { } i++; } - return; } else { // else handle block entity mode for (Direction dir : GTUtil.DIRECTIONS) {