Skip to content

Commit

Permalink
fix world accelerator issues (#1126)
Browse files Browse the repository at this point in the history
* fix world accelerator class formatting
fix world accelerator using 2x energy
fix world accelerator always accelerating BEs even in random tick mode

* remove bad return
  • Loading branch information
screret authored Apr 20, 2024
1 parent 2e5930d commit 5062577
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,8 +50,8 @@ public void renderMachine(List<BakedQuad> quads, MachineDefinition definition, @
public void onPrepareTextureAtlas(ResourceLocation atlasName, Consumer<ResourceLocation> register) {
super.onPrepareTextureAtlas(atlasName, register);
if (atlasName.equals(TextureAtlas.LOCATION_BLOCKS)) {
RTModeModel.registerTextureAtlas(register);
BEModeModel.registerTextureAtlas(register);
randomTickModeModel.registerTextureAtlas(register);
blockEntityModeModel.registerTextureAtlas(register);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,44 +50,34 @@
*/

public class WorldAcceleratorMachine extends TieredEnergyMachine implements IControllable {

private static final Map<String, Class<?>> blacklistedClasses = new Object2ObjectOpenHashMap<>();
private static final Object2BooleanFunction<Class<? extends BlockEntity>> blacklistCache = new Object2BooleanOpenHashMap<>();

private static boolean gatheredClasses = false;

//Hard-coded blacklist for blockentities
private static final List<String> 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 };

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);
Expand All @@ -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(),
Expand All @@ -135,59 +125,61 @@ 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 <T extends BlockEntity> void tickBlockEntity(@NotNull T blockEntity){
BlockPos pos = blockEntity.getBlockPos();
BlockEntityTicker<T> blockEntityTicker = this.getLevel().getBlockState(pos).getTicker(this.getLevel(),(BlockEntityType<T>) blockEntity.getType());
if(blockEntityTicker==null) return;
for (int i = 0; i < speed-1; i++)
blockEntityTicker.tick(blockEntity.getLevel(), blockEntity.getBlockPos(), blockEntity.getBlockState(), blockEntity);
private <T extends BlockEntity> void tickBlockEntity(@NotNull T blockEntity) {
BlockPos pos = blockEntity.getBlockPos();
//noinspection unchecked
BlockEntityTicker<T> blockEntityTicker = this.getLevel().getBlockState(pos).getTicker(this.getLevel(), (BlockEntityType<T>) 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<? extends BlockEntity> 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;
Expand All @@ -196,19 +188,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();
Expand Down Expand Up @@ -239,29 +232,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;
}
Expand Down

0 comments on commit 5062577

Please sign in to comment.