Optimize
All checks were successful
EnergonRelics/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-07-27 14:06:46 -04:00
parent a90dd215ff
commit f079823bfe
14 changed files with 118 additions and 49 deletions

View File

@ -39,7 +39,7 @@ public class BlockBreakerBlockEntity extends EnergyReceiverBlockEntity {
} }
@Override @Override
protected void tickEnergy() { protected void energyTick() {
addAction(Action.createBlockStatePropertyAction(HardcodedConfig.BLOCK_BREAKER_ENERGY_REQUIRED_IDLE, BlockBreakerBlock.POWERED, true, false)); addAction(Action.createBlockStatePropertyAction(HardcodedConfig.BLOCK_BREAKER_ENERGY_REQUIRED_IDLE, BlockBreakerBlock.POWERED, true, false));
if (getCachedState().get(BlockBreakerBlock.POWERED)) { if (getCachedState().get(BlockBreakerBlock.POWERED)) {

View File

@ -220,7 +220,7 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
} }
@Override @Override
protected void tickEnergy() { protected void energyTick() {
assert getWorld() != null; assert getWorld() != null;
addAction(Action.createBlockStatePropertyAction(HardcodedConfig.DEFENSIVE_LASER_IDLE_ENERGY_REQUIRED, DefensiveLaserBlock.POWERED, true, false)); addAction(Action.createBlockStatePropertyAction(HardcodedConfig.DEFENSIVE_LASER_IDLE_ENERGY_REQUIRED, DefensiveLaserBlock.POWERED, true, false));
if (getCachedState().get(DefensiveLaserBlock.POWERED)) { if (getCachedState().get(DefensiveLaserBlock.POWERED)) {

View File

@ -12,7 +12,7 @@ public class EnergonLightBlockEntity extends EnergyReceiverBlockEntity {
} }
@Override @Override
protected void tickEnergy() { protected void energyTick() {
addAction(Action.createBlockStatePropertyAction(HardcodedConfig.ENERGON_LIGHT_ENERGY_REQUIRED, EnergonLightBlock.POWERED, true, false)); addAction(Action.createBlockStatePropertyAction(HardcodedConfig.ENERGON_LIGHT_ENERGY_REQUIRED, EnergonLightBlock.POWERED, true, false));
} }
} }

View File

@ -18,7 +18,7 @@ public class HolographicSkyBlockEntity extends EnergyReceiverBlockEntity {
} }
@Override @Override
protected void tickEnergy() { protected void energyTick() {
addAction(Action.createBlockStatePropertyAction(HardcodedConfig.HOLOGRAPHIC_SKY_ENERGY_REQUIRED, HolographicSkyBlock.POWERED, true, false)); addAction(Action.createBlockStatePropertyAction(HardcodedConfig.HOLOGRAPHIC_SKY_ENERGY_REQUIRED, HolographicSkyBlock.POWERED, true, false));
assert getWorld() != null; assert getWorld() != null;

View File

@ -1,7 +1,6 @@
package com.thebrokenrail.energonrelics.block.entity; package com.thebrokenrail.energonrelics.block.entity;
import com.thebrokenrail.energonrelics.config.HardcodedConfig; import com.thebrokenrail.energonrelics.config.HardcodedConfig;
import com.thebrokenrail.energonrelics.energy.core.EnergyProviderBlockEntity;
import com.thebrokenrail.energonrelics.energy.core.util.EnergyTickable; import com.thebrokenrail.energonrelics.energy.core.util.EnergyTickable;
import com.thebrokenrail.energonrelics.energy.helper.EnergyGeneratorBlockEntity; import com.thebrokenrail.energonrelics.energy.helper.EnergyGeneratorBlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;

View File

@ -10,7 +10,7 @@ public class SwitchBlockEntity extends EnergyReceiverBlockEntity {
} }
@Override @Override
protected void tickEnergy() { protected void energyTick() {
} }
@Override @Override

View File

@ -11,7 +11,6 @@ import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEntity implements EnergyGenerator { public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEntity implements EnergyGenerator {
public PassiveBatteryControllerBlockEntity(BlockEntityType<?> type) { public PassiveBatteryControllerBlockEntity(BlockEntityType<?> type) {
@ -19,12 +18,10 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
} }
private static class UnlimitedAction implements Action.PropagatedAction { private static class UnlimitedAction implements Action.PropagatedAction {
private final World world; private final PassiveBatteryControllerBlockEntity battery;
private final BlockPos pos;
private UnlimitedAction(World world, BlockPos pos) { private UnlimitedAction(PassiveBatteryControllerBlockEntity battery) {
this.world = world; this.battery = battery;
this.pos = pos;
} }
@Override @Override
@ -38,11 +35,7 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
@Override @Override
public void pay(long amount) { public void pay(long amount) {
BlockEntity entity = world.getBlockEntity(pos); battery.setEnergy(battery.getEnergy() + amount);
if (entity instanceof PassiveBatteryControllerBlockEntity) {
PassiveBatteryControllerBlockEntity battery = (PassiveBatteryControllerBlockEntity) entity;
battery.setEnergy(battery.getEnergy() + amount);
}
} }
@Override @Override
@ -51,18 +44,12 @@ public class PassiveBatteryControllerBlockEntity extends EnergyReceiverBlockEnti
} }
@Override @Override
protected void tickEnergy() { protected void energyTick() {
long charge = HardcodedConfig.BATTERY_CHARGE_RATE; long charge = HardcodedConfig.BATTERY_CHARGE_RATE;
if (!isActiveController()) { if (!isActiveController()) {
addAction(new Action(charge, (world, pos, state) -> { addAction(new Action(charge, (world, pos, state) -> PassiveBatteryControllerBlockEntity.this.setEnergy(PassiveBatteryControllerBlockEntity.this.getEnergy() + charge), (world, pos, state) -> {}));
BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof PassiveBatteryControllerBlockEntity) {
PassiveBatteryControllerBlockEntity battery = (PassiveBatteryControllerBlockEntity) entity;
battery.setEnergy(battery.getEnergy() + charge);
}
}, (world, pos, state) -> {}));
} else { } else {
propagateAction(new UnlimitedAction(getWorld(), getPos())); propagateAction(new UnlimitedAction(this));
} }
} }

View File

@ -17,7 +17,7 @@ public class ForcefieldProjectorBlockEntity extends EnergyReceiverBlockEntity {
} }
@Override @Override
protected void tickEnergy() { protected void energyTick() {
assert getWorld() != null; assert getWorld() != null;
addAction(Action.createBlockStatePropertyAction(HardcodedConfig.FORCEFIELD_PROJECTOR_ENERGY_REQUIRED, ForcefieldProjectorBlock.POWERED, true, false)); addAction(Action.createBlockStatePropertyAction(HardcodedConfig.FORCEFIELD_PROJECTOR_ENERGY_REQUIRED, ForcefieldProjectorBlock.POWERED, true, false));

View File

@ -17,8 +17,10 @@ import net.minecraft.world.World;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
public class NetworkComponent extends PersistentState { public class NetworkComponent extends PersistentState {
@ -90,12 +92,12 @@ public class NetworkComponent extends PersistentState {
} }
private Entry getOrCreate(int id) { private Entry getOrCreate(int id) {
markDirty();
for (Entry entry : networks) { for (Entry entry : networks) {
if (entry.id == id) { if (entry.id == id) {
return entry; return entry;
} }
} }
markDirty();
Entry entry = new Entry(); Entry entry = new Entry();
entry.id = id; entry.id = id;
entry.sources.clear(); entry.sources.clear();
@ -107,13 +109,47 @@ public class NetworkComponent extends PersistentState {
return Objects.requireNonNull(world.getServer().getWorld(World.OVERWORLD)).getPersistentStateManager().getOrCreate(NetworkComponent::new, EnergonRelics.NAMESPACE); return Objects.requireNonNull(world.getServer().getWorld(World.OVERWORLD)).getPersistentStateManager().getOrCreate(NetworkComponent::new, EnergonRelics.NAMESPACE);
} }
public List<BlockPosWithDimension> getSourcePos(int id) { public List<BlockPosWithDimension> getSourcesPos(int id) {
Entry entry = getOrCreate(id); Entry entry = getOrCreate(id);
return entry.sources; return entry.sources;
} }
public List<EnergyProviderBlockEntity> getSource(World world, int id) { private final Map<Integer, List<EnergyProviderBlockEntity>> cache = new HashMap<>();
List<BlockPosWithDimension> sources = getSourcePos(id);
private List<EnergyProviderBlockEntity> getSourcesFromCache(World world, int id) {
if (cache.containsKey(id)) {
world.getProfiler().push("getSourcesFromCache");
List<EnergyProviderBlockEntity> list = cache.get(id);
boolean valid = true;
for (EnergyProviderBlockEntity entity : list) {
if (!entity.isNetwork(id) || entity.isRemoved()) {
valid = false;
break;
}
}
world.getProfiler().pop();
if (valid) {
Collections.shuffle(list);
return list;
} else {
cache.remove(id);
return null;
}
} else {
return null;
}
}
private List<EnergyProviderBlockEntity> getSourcesFromCacheMiss(World world, int id) {
world.getProfiler().push("getSourcesFromCacheMiss");
List<BlockPosWithDimension> sources = getSourcesPos(id);
List<BlockPosWithDimension> valid = new ArrayList<>(); List<BlockPosWithDimension> valid = new ArrayList<>();
Iterator<BlockPosWithDimension> iterator = sources.iterator(); Iterator<BlockPosWithDimension> iterator = sources.iterator();
List<EnergyProviderBlockEntity> providers = new ArrayList<>(); List<EnergyProviderBlockEntity> providers = new ArrayList<>();
@ -138,9 +174,33 @@ public class NetworkComponent extends PersistentState {
markDirty(); markDirty();
} }
Collections.shuffle(providers); Collections.shuffle(providers);
cache.put(id, providers);
world.getProfiler().pop();
return providers; return providers;
} }
public List<EnergyProviderBlockEntity> getSources(World world, int id) {
world.getProfiler().push("getSources");
List<EnergyProviderBlockEntity> result;
List<EnergyProviderBlockEntity> cacheResult = getSourcesFromCache(world, id);
if (cacheResult != null) {
result = cacheResult;
} else {
result = getSourcesFromCacheMiss(world, id);
}
world.getProfiler().pop();
return result;
}
public void clearCache() {
cache.clear();
}
public void addSource(int id, BlockPosWithDimension pos) { public void addSource(int id, BlockPosWithDimension pos) {
Entry entry = getOrCreate(id); Entry entry = getOrCreate(id);
if (!entry.sources.contains(pos)) { if (!entry.sources.contains(pos)) {

View File

@ -87,9 +87,9 @@ public class EnergyProviderBlockEntity extends BlockEntity implements BlockEntit
@Override @Override
public List<EnergyTickable> startTick() { public List<EnergyTickable> startTick() {
if (isEnergyProvider()) { if (isEnergyProvider() && stack.getItem() == EnergonRelics.NETWORK_CHIP_ITEM) {
NetworkComponent component = NetworkComponent.getInstance((ServerWorld) Objects.requireNonNull(getWorld())); NetworkComponent component = NetworkComponent.getInstance((ServerWorld) Objects.requireNonNull(getWorld()));
List<BlockPosWithDimension> sources = component.getSourcePos(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack)); List<BlockPosWithDimension> sources = component.getSourcesPos(EnergonRelics.NETWORK_CHIP_ITEM.getID(stack));
if (!sources.contains(new BlockPosWithDimension(getPos(), getWorld().getRegistryKey()))) { if (!sources.contains(new BlockPosWithDimension(getPos(), getWorld().getRegistryKey()))) {
takeStack(getWorld()); takeStack(getWorld());
} }
@ -150,4 +150,9 @@ public class EnergyProviderBlockEntity extends BlockEntity implements BlockEntit
sync(); sync();
} }
} }
@Override
public String getID() {
return String.valueOf(BlockEntityType.getId(getType()));
}
} }

View File

@ -45,7 +45,7 @@ public abstract class EnergyReceiverBlockEntity extends EnergyProviderBlockEntit
} }
protected void addAction(Action action) { protected void addAction(Action action) {
propagateAction(new Action.PropagatedActionImpl(action, getWorld(), getPos(), getCachedState().getBlock())); propagateAction(new Action.PropagatedActionImpl(action, getWorld(), getPos(), getCachedState()));
} }
@Override @Override
@ -60,7 +60,7 @@ public abstract class EnergyReceiverBlockEntity extends EnergyProviderBlockEntit
providers.clear(); providers.clear();
for (int network : networks) { for (int network : networks) {
List<EnergyProviderBlockEntity> potentialProviders = component.getSource(world, network); List<EnergyProviderBlockEntity> potentialProviders = component.getSources(world, network);
for (EnergyProviderBlockEntity provider : potentialProviders) { for (EnergyProviderBlockEntity provider : potentialProviders) {
if (provider.getRegistryKey().equals(getWorld().getRegistryKey())) { if (provider.getRegistryKey().equals(getWorld().getRegistryKey())) {
Vec3d pos = new Vec3d(getPos().getX() + 0.5d, getPos().getY() + 0.5d, getPos().getZ() + 0.5d); Vec3d pos = new Vec3d(getPos().getX() + 0.5d, getPos().getY() + 0.5d, getPos().getZ() + 0.5d);
@ -82,7 +82,10 @@ public abstract class EnergyReceiverBlockEntity extends EnergyProviderBlockEntit
@Override @Override
public void logicTick() { public void logicTick() {
super.logicTick(); super.logicTick();
tickEnergy(); assert getWorld() != null;
getWorld().getProfiler().push("energyTick");
energyTick();
getWorld().getProfiler().pop();
} }
public long getTotalCost() { public long getTotalCost() {
@ -121,7 +124,7 @@ public abstract class EnergyReceiverBlockEntity extends EnergyProviderBlockEntit
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
protected abstract void tickEnergy(); protected abstract void energyTick();
public void toggle(int network) { public void toggle(int network) {
if (contains(network)) { if (contains(network)) {

View File

@ -1,6 +1,5 @@
package com.thebrokenrail.energonrelics.energy.core.util; package com.thebrokenrail.energonrelics.energy.core.util;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.state.property.Property; import net.minecraft.state.property.Property;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
@ -44,16 +43,16 @@ public class Action {
private final Action action; private final Action action;
private final World world; private final World world;
private final BlockPos pos; private final BlockPos pos;
private final Block block; private final BlockState state;
private int expectedPayments = 1; private int expectedPayments = 1;
private int payments = 0; private int payments = 0;
private long amountPaid = 0; private long amountPaid = 0;
public PropagatedActionImpl(Action action, World world, BlockPos pos, Block block) { public PropagatedActionImpl(Action action, World world, BlockPos pos, BlockState state) {
this.action = action; this.action = action;
this.world = world; this.world = world;
this.pos = pos; this.pos = pos;
this.block = block; this.state = state;
} }
@Override @Override
@ -73,8 +72,7 @@ public class Action {
public void pay(long amount) { public void pay(long amount) {
amountPaid = amountPaid + amount; amountPaid = amountPaid + amount;
payments++; payments++;
BlockState state = world.getBlockState(pos); if (state != null) {
if (state != null && state.getBlock() == block) {
if (amountOwed() <= 0) { if (amountOwed() <= 0) {
action.success.run(world, pos, state); action.success.run(world, pos, state);
} else if (payments >= expectedPayments) { } else if (payments >= expectedPayments) {

View File

@ -5,4 +5,5 @@ import java.util.List;
public interface EnergyTickable { public interface EnergyTickable {
List<EnergyTickable> startTick(); List<EnergyTickable> startTick();
void logicTick(); void logicTick();
String getID();
} }

View File

@ -1,5 +1,7 @@
package com.thebrokenrail.energonrelics.energy.core.util; package com.thebrokenrail.energonrelics.energy.core.util;
import com.thebrokenrail.energonrelics.component.NetworkComponent;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World; import net.minecraft.world.World;
import java.util.ArrayList; import java.util.ArrayList;
@ -16,15 +18,23 @@ public class EnergyTicker {
public static void tick(World world) { public static void tick(World world) {
if (Objects.requireNonNull(world.getServer()).getThread() == Thread.currentThread()) { if (Objects.requireNonNull(world.getServer()).getThread() == Thread.currentThread()) {
world.getProfiler().push("energyTicker");
NetworkComponent.getInstance((ServerWorld) world).clearCache();
List<EnergyTickable> started = new ArrayList<>(); List<EnergyTickable> started = new ArrayList<>();
List<EnergyTickable> temp = new ArrayList<>(scheduled); List<EnergyTickable> temp = new ArrayList<>(scheduled);
List<EnergyTickable> temp2 = new ArrayList<>(); List<EnergyTickable> temp2 = new ArrayList<>();
while (!temp.isEmpty()) { while (!temp.isEmpty()) {
for (EnergyTickable provider : temp) { for (EnergyTickable tickable : temp) {
if (!started.contains(provider)) { if (!started.contains(tickable)) {
temp2.addAll(provider.startTick()); world.getProfiler().push(() -> tickable.getID() + " startTick");
started.add(provider);
temp2.addAll(tickable.startTick());
started.add(tickable);
world.getProfiler().pop();
} }
} }
temp.clear(); temp.clear();
@ -33,9 +43,15 @@ public class EnergyTicker {
} }
Collections.shuffle(started); Collections.shuffle(started);
for (EnergyTickable provider : started) { for (EnergyTickable tickable : started) {
provider.logicTick(); world.getProfiler().push(() -> tickable.getID() + " logicTick");
tickable.logicTick();
world.getProfiler().pop();
} }
world.getProfiler().pop();
} }
scheduled.clear(); scheduled.clear();
} }