Merge pull request #2529 from mrh0/furnaceEngineChanges

- Addons are now able to register non-furnace blocks as valid sources of a furnace engines' power
This commit is contained in:
simibubi 2022-01-08 13:45:14 +01:00 committed by GitHub
commit b75c844adb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 9 deletions

View file

@ -33,7 +33,7 @@ public class FurnaceEngineBlock extends EngineBlock implements ITE<FurnaceEngine
@Override
protected boolean isValidBaseBlock(BlockState baseBlock, BlockGetter world, BlockPos pos) {
return baseBlock.getBlock() instanceof AbstractFurnaceBlock;
return FurnaceEngineModifiers.get().getEngineState(baseBlock).isEngine();
}
@Override

View file

@ -2,7 +2,9 @@ package com.simibubi.create.content.contraptions.components.flywheel.engine;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import net.minecraft.world.level.block.AbstractFurnaceBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
@ -10,23 +12,73 @@ import net.minecraftforge.registries.IRegistryDelegate;
public class FurnaceEngineModifiers {
public final static FurnaceEngineModifiers INSTANCE = new FurnaceEngineModifiers();
private final static FurnaceEngineModifiers INSTANCE = new FurnaceEngineModifiers();
protected FurnaceEngineModifiers() {
blockModifiers = new HashMap<>();
blockActivators = new HashMap<>();
}
protected Map<IRegistryDelegate<Block>, Float> blockModifiers = new HashMap<>();
private final Map<IRegistryDelegate<Block>, Float> blockModifiers;
private final Map<IRegistryDelegate<Block>, Function<BlockState, EngineState>> blockActivators;
public void register(IRegistryDelegate<Block> block, float modifier) {
this.blockModifiers.put(block, modifier);
}
public void register(IRegistryDelegate<Block> block, float modifier, Function<BlockState, EngineState> engineState) {
this.blockModifiers.put(block, modifier);
this.blockActivators.put(block, engineState);
}
public float getModifierOrDefault(BlockState state, float defaultValue) {
private float getModifierOrDefault(BlockState state, float defaultValue) {
return blockModifiers.getOrDefault(state.getBlock().delegate, defaultValue);
}
private Function<BlockState, EngineState> getEngineStateOrDefault(BlockState state, Function<BlockState, EngineState> engineState) {
return blockActivators.getOrDefault(state.getBlock().delegate, engineState);
}
public float getModifier(BlockState state) {
return getModifierOrDefault(state, 1f);
}
public EngineState getEngineState(BlockState state) {
return getEngineStateOrDefault(state,
s -> s.getBlock() instanceof AbstractFurnaceBlock && s.hasProperty(AbstractFurnaceBlock.LIT) ?
(s.getValue(AbstractFurnaceBlock.LIT) ? EngineState.ACTIVE : EngineState.VALID) : EngineState.EMPTY).apply(state);
}
public static void register() {
INSTANCE.register(Blocks.BLAST_FURNACE.delegate, 2f);
get().register(Blocks.BLAST_FURNACE.delegate, 2f);
/*
Example:
get().register(Blocks.REDSTONE_LAMP.delegate, 1f,
s -> s.getBlock() instanceof RedstoneLampBlock && s.hasProperty(RedstoneLampBlock.LIT) ?
(s.getValue(RedstoneLampBlock.LIT) ? EngineState.ACTIVE : EngineState.VALID) : EngineState.EMPTY);
*/
}
public static FurnaceEngineModifiers get() {
return INSTANCE;
}
public enum EngineState {
EMPTY,
VALID,
ACTIVE;
public boolean isEngine() {
return this != EMPTY;
}
public boolean isActive() {
return this == ACTIVE;
}
public boolean isEmpty() {
return this == EMPTY;
}
}
}

View file

@ -1,10 +1,10 @@
package com.simibubi.create.content.contraptions.components.flywheel.engine;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.content.contraptions.components.flywheel.engine.FurnaceEngineModifiers.EngineState;
import com.simibubi.create.foundation.block.BlockStressValues;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.AbstractFurnaceBlock;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
@ -22,11 +22,12 @@ public class FurnaceEngineTileEntity extends EngineTileEntity {
public void updateFurnace() {
BlockState state = level.getBlockState(EngineBlock.getBaseBlockPos(getBlockState(), worldPosition));
if (!(state.getBlock() instanceof AbstractFurnaceBlock))
EngineState engineState = FurnaceEngineModifiers.get().getEngineState(state);
if (engineState.isEmpty())
return;
float modifier = FurnaceEngineModifiers.INSTANCE.getModifier(state);
boolean active = state.hasProperty(AbstractFurnaceBlock.LIT) && state.getValue(AbstractFurnaceBlock.LIT);
float modifier = FurnaceEngineModifiers.get().getModifier(state);
boolean active = engineState.isActive();
float speed = active ? 16 * modifier : 0;
float capacity =
(float) (active ? BlockStressValues.getCapacity(AllBlocks.FURNACE_ENGINE.get())