Block entry wrappers

- Simi wants his helpers back
This commit is contained in:
simibubi 2020-05-11 22:34:02 +02:00
parent 20ab49e46e
commit 3eee3d2c87
17 changed files with 142 additions and 110 deletions

View file

@ -2,6 +2,8 @@ package com.simibubi.create;
import static com.simibubi.create.modules.Sections.SCHEMATICS; import static com.simibubi.create.modules.Sections.SCHEMATICS;
import java.util.function.Supplier;
import com.simibubi.create.foundation.utility.data.BlockStateGen; import com.simibubi.create.foundation.utility.data.BlockStateGen;
import com.simibubi.create.modules.Sections; import com.simibubi.create.modules.Sections;
import com.simibubi.create.modules.contraptions.relays.elementary.CogWheelBlock; import com.simibubi.create.modules.contraptions.relays.elementary.CogWheelBlock;
@ -17,72 +19,96 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.block.SoundType; import net.minecraft.block.SoundType;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.registries.IForgeRegistryEntry;
public class AllBlocksNew { public class AllBlocksNew {
private static final CreateRegistrate REGISTRATE = Create.registrate(); private static final CreateRegistrate REGISTRATE = Create.registrate();
static { REGISTRATE.startSection(SCHEMATICS); }
public static final RegistryEntry<SchematicannonBlock> SCHEMATICANNON = REGISTRATE.block("schematicannon", SchematicannonBlock::new)
.initialProperties(() -> Blocks.DISPENSER)
.blockstate((ctx, prov) -> prov.simpleBlock(ctx.getEntry(), BlockStateGen.partialBaseModel(ctx, prov)))
.item()
.model(BlockStateGen::customItemModel)
.build()
.register();
public static final RegistryEntry<SchematicTableBlock> SCHEMATIC_TABLE = REGISTRATE.block("schematic_table", SchematicTableBlock::new) static {
.initialProperties(() -> Blocks.LECTERN) REGISTRATE.startSection(SCHEMATICS);
.blockstate((ctx, prov) -> prov.horizontalBlock(ctx.getEntry(), prov.models().getExistingFile(ctx.getId()), 0)) }
.simpleItem()
.register(); public static final BlockEntry<SchematicannonBlock> SCHEMATICANNON =
REGISTRATE.block("schematicannon", SchematicannonBlock::new, b -> b
static { REGISTRATE.startSection(Sections.KINETICS); } .initialProperties(() -> Blocks.DISPENSER)
.blockstate((ctx, prov) -> prov.simpleBlock(ctx.getEntry(), BlockStateGen.partialBaseModel(ctx, prov)))
public static final RegistryEntry<ShaftBlock> SHAFT = REGISTRATE.block("shaft", ShaftBlock::new) .item()
.model(BlockStateGen::customItemModel)
.build()
.register());
public static final BlockEntry<SchematicTableBlock> SCHEMATIC_TABLE =
REGISTRATE.block("schematic_table", SchematicTableBlock::new, b -> b
.initialProperties(() -> Blocks.LECTERN)
.blockstate((ctx, prov) -> prov.horizontalBlock(ctx.getEntry(), prov.models()
.getExistingFile(ctx.getId()), 0))
.simpleItem()
.register());
static {
REGISTRATE.startSection(Sections.KINETICS);
}
public static final BlockEntry<ShaftBlock> SHAFT = REGISTRATE.block("shaft", ShaftBlock::new, b -> b
.initialProperties(SharedProperties::kinetic) .initialProperties(SharedProperties::kinetic)
.blockstate((c, p) -> BlockStateGen.axisKineticBlock(c, p, BlockStateGen.standardModel(c, p))) .blockstate((c, p) -> BlockStateGen.axisKineticBlock(c, p, BlockStateGen.standardModel(c, p)))
.simpleItem() .simpleItem()
.register(); .register());
public static final RegistryEntry<CogWheelBlock> COGWHEEL = REGISTRATE.block("cogwheel", CogWheelBlock::small)
.initialProperties(SharedProperties::kinetic)
.properties(p -> p.sound(SoundType.WOOD))
.blockstate((c, p) -> BlockStateGen.axisKineticBlock(c, p, BlockStateGen.standardModel(c, p)))
.item(CogwheelBlockItem::new).build()
.register();
public static final RegistryEntry<CogWheelBlock> LARGE_COGWHEEL = REGISTRATE.block("large_cogwheel", CogWheelBlock::large)
.initialProperties(SharedProperties::kinetic)
.properties(p -> p.sound(SoundType.WOOD))
.blockstate((c, p) -> BlockStateGen.axisKineticBlock(c, p, BlockStateGen.standardModel(c, p)))
.item(CogwheelBlockItem::new).build()
.register();
public static final RegistryEntry<EncasedShaftBlock> ENCASED_SHAFT = REGISTRATE.block("encased_shaft", EncasedShaftBlock::new)
.initialProperties(SharedProperties::kinetic)
.blockstate((c, p) -> BlockStateGen.axisKineticBlock(c, p, BlockStateGen.partialBaseModel(c, p)))
.item()
.model(BlockStateGen::customItemModel)
.build()
.register();
public static void register() {}
// Ideally these following three methods would be part of the RegistryEntry instances public static final BlockEntry<CogWheelBlock> COGWHEEL = REGISTRATE.block("cogwheel", CogWheelBlock::small, b -> b
.initialProperties(SharedProperties::kinetic)
public static <T extends Block & IForgeRegistryEntry<Block>> boolean equals(RegistryEntry<T> entry, BlockState state) { .properties(p -> p.sound(SoundType.WOOD))
return entry.map(state.getBlock()::equals).get(); .blockstate((c, p) -> BlockStateGen.axisKineticBlock(c, p, BlockStateGen.standardModel(c, p)))
.item(CogwheelBlockItem::new)
.build()
.register());
public static final BlockEntry<CogWheelBlock> LARGE_COGWHEEL =
REGISTRATE.block("large_cogwheel", CogWheelBlock::large, b -> b
.initialProperties(SharedProperties::kinetic)
.properties(p -> p.sound(SoundType.WOOD))
.blockstate((c, p) -> BlockStateGen.axisKineticBlock(c, p, BlockStateGen.standardModel(c, p)))
.item(CogwheelBlockItem::new)
.build()
.register());
public static final BlockEntry<EncasedShaftBlock> ENCASED_SHAFT =
REGISTRATE.block("encased_shaft", EncasedShaftBlock::new, b -> b
.initialProperties(SharedProperties::kinetic)
.blockstate((c, p) -> BlockStateGen.axisKineticBlock(c, p, BlockStateGen.partialBaseModel(c, p)))
.item()
.model(BlockStateGen::customItemModel)
.build()
.register());
public static void register() {
} }
public static ItemStack asStack(RegistryEntry<? extends Block> entry) { public static class BlockEntry<B extends Block> implements Supplier<B> {
return new ItemStack(entry.get());
private RegistryEntry<B> delegate;
public BlockEntry(RegistryEntry<B> entry) {
this.delegate = entry;
}
public boolean typeOf(BlockState state) {
return get() == state.getBlock();
}
public ItemStack asStack() {
return new ItemStack(get());
}
public BlockState getDefault() {
return get().getDefaultState();
}
@Override
public B get() {
return delegate.get();
}
} }
public static BlockState getDefault(RegistryEntry<? extends Block> entry) {
return entry.get().getDefaultState();
}
} }

View file

@ -23,7 +23,7 @@ public final class CreateItemGroup extends ItemGroup {
@Override @Override
public ItemStack createIcon() { public ItemStack createIcon() {
return AllBlocksNew.asStack(AllBlocksNew.COGWHEEL); return AllBlocksNew.COGWHEEL.asStack();
} }
@Override @Override

View file

@ -10,54 +10,52 @@ import com.tterrag.registrate.builders.BlockBuilder;
import com.tterrag.registrate.builders.Builder; import com.tterrag.registrate.builders.Builder;
import com.tterrag.registrate.util.NonNullLazyValue; import com.tterrag.registrate.util.NonNullLazyValue;
import com.tterrag.registrate.util.RegistryEntry; import com.tterrag.registrate.util.RegistryEntry;
import com.tterrag.registrate.util.nullness.NonNullFunction;
import com.tterrag.registrate.util.nullness.NonNullSupplier; import com.tterrag.registrate.util.nullness.NonNullSupplier;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.Block.Properties;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.IForgeRegistryEntry; import net.minecraftforge.registries.IForgeRegistryEntry;
public class CreateRegistrate extends AbstractRegistrate<CreateRegistrate> { public class CreateRegistrate extends AbstractRegistrate<CreateRegistrate> {
/** /**
* Create a new {@link CreateRegistrate} and register event listeners for registration and data generation. Used in lieu of adding side-effects to constructor, so that alternate initialization * Create a new {@link CreateRegistrate} and register event listeners for
* strategies can be done in subclasses. * registration and data generation. Used in lieu of adding side-effects to
* * constructor, so that alternate initialization strategies can be done in
* @param modid * subclasses.
* The mod ID for which objects will be registered *
* @return The {@link CreateRegistrate} instance * @param modid The mod ID for which objects will be registered
*/ * @return The {@link CreateRegistrate} instance
public static CreateRegistrate create(String modid) { */
return new CreateRegistrate(modid) public static CreateRegistrate create(String modid) {
.registerEventListeners(FMLJavaModLoadingContext.get().getModEventBus()) return new CreateRegistrate(modid).registerEventListeners(FMLJavaModLoadingContext.get()
.itemGroup(() -> Create.creativeTab); .getModEventBus())
} .itemGroup(() -> Create.creativeTab);
}
public static NonNullLazyValue<CreateRegistrate> lazy(String modid) {
return new NonNullLazyValue<>(() -> create(modid));
}
protected CreateRegistrate(String modid) { public static NonNullLazyValue<CreateRegistrate> lazy(String modid) {
super(modid); return new NonNullLazyValue<>(() -> create(modid));
} }
private Map<RegistryEntry<?>, Sections> sectionLookup = new IdentityHashMap<>(); protected CreateRegistrate(String modid) {
super(modid);
private Sections section; }
private Map<RegistryEntry<?>, Sections> sectionLookup = new IdentityHashMap<>();
private Sections section;
public CreateRegistrate startSection(Sections section) { public CreateRegistrate startSection(Sections section) {
this.section = section; this.section = section;
return self(); return self();
} }
public Sections currentSection() { public Sections currentSection() {
return section; return section;
} }
@Deprecated
public <T extends Block> BlockBuilder<T, CreateRegistrate> block(String name, NonNullSupplier<T> factory) {
return block(name, $ -> factory.get());
}
@Override @Override
protected <R extends IForgeRegistryEntry<R>, T extends R> RegistryEntry<T> accept(String name, protected <R extends IForgeRegistryEntry<R>, T extends R> RegistryEntry<T> accept(String name,
Class<? super R> type, Builder<R, T, ?, ?> builder, NonNullSupplier<? extends T> creator) { Class<? super R> type, Builder<R, T, ?, ?> builder, NonNullSupplier<? extends T> creator) {
@ -65,14 +63,22 @@ public class CreateRegistrate extends AbstractRegistrate<CreateRegistrate> {
sectionLookup.put(ret, currentSection()); sectionLookup.put(ret, currentSection());
return ret; return ret;
} }
// TODO: find a better way to wrap the registry entries
public <T extends Block> AllBlocksNew.BlockEntry<T> block(String name, NonNullFunction<Properties, T> factory,
NonNullFunction<BlockBuilder<T, CreateRegistrate>, RegistryEntry<T>> registerFunc) {
return new AllBlocksNew.BlockEntry<T>(registerFunc.apply(super.block(name, factory)));
}
public Sections getSection(RegistryEntry<?> entry) { public Sections getSection(RegistryEntry<?> entry) {
return sectionLookup.getOrDefault(entry, Sections.UNASSIGNED); return sectionLookup.getOrDefault(entry, Sections.UNASSIGNED);
} }
public Sections getSection(IForgeRegistryEntry<?> entry) { public Sections getSection(IForgeRegistryEntry<?> entry) {
return sectionLookup.entrySet().stream() return sectionLookup.entrySet()
.filter(e -> e.getKey().get() == entry) .stream()
.filter(e -> e.getKey()
.get() == entry)
.map(Entry::getValue) .map(Entry::getValue)
.findFirst() .findFirst()
.orElse(Sections.UNASSIGNED); .orElse(Sections.UNASSIGNED);

View file

@ -17,7 +17,7 @@ public abstract class AnimatedKinetics implements IDrawable {
} }
protected BlockState shaft(Axis axis) { protected BlockState shaft(Axis axis) {
return AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(BlockStateProperties.AXIS, axis); return AllBlocksNew.SHAFT.getDefault().with(BlockStateProperties.AXIS, axis);
} }
protected AllBlockPartials cogwheel() { protected AllBlockPartials cogwheel() {

View file

@ -17,7 +17,7 @@ public class MechanicalPistonTileEntityRenderer extends KineticTileEntityRendere
@Override @Override
protected BlockState getRenderedBlockState(KineticTileEntity te) { protected BlockState getRenderedBlockState(KineticTileEntity te) {
return AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(BlockStateProperties.AXIS, return AllBlocksNew.SHAFT.getDefault().with(BlockStateProperties.AXIS,
((IRotate) te.getBlockState().getBlock()).getRotationAxis(te.getBlockState())); ((IRotate) te.getBlockState().getBlock()).getRotationAxis(te.getBlockState()));
} }

View file

@ -139,7 +139,7 @@ public class DeployerTileEntityRenderer extends SafeTileEntityRenderer<DeployerT
BlockState state = te.getBlockState(); BlockState state = te.getBlockState();
if (!AllBlocks.DEPLOYER.typeOf(state)) if (!AllBlocks.DEPLOYER.typeOf(state))
return Blocks.AIR.getDefaultState(); return Blocks.AIR.getDefaultState();
return AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(AXIS, ((IRotate) state.getBlock()).getRotationAxis(state)); return AllBlocksNew.SHAFT.getDefault().with(AXIS, ((IRotate) state.getBlock()).getRotationAxis(state));
} }
private static SuperByteBuffer renderAndTransform(World world, AllBlockPartials renderBlock, private static SuperByteBuffer renderAndTransform(World world, AllBlockPartials renderBlock,

View file

@ -39,7 +39,7 @@ public class MechanicalPressTileEntityRenderer extends KineticTileEntityRenderer
@Override @Override
protected BlockState getRenderedBlockState(KineticTileEntity te) { protected BlockState getRenderedBlockState(KineticTileEntity te) {
return AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(BlockStateProperties.AXIS, return AllBlocksNew.SHAFT.getDefault().with(BlockStateProperties.AXIS,
te.getBlockState().get(HORIZONTAL_FACING).getAxis()); te.getBlockState().get(HORIZONTAL_FACING).getAxis());
} }

View file

@ -91,7 +91,7 @@ public class SawTileEntityRenderer extends SafeTileEntityRenderer<SawTileEntity>
protected BlockState getRenderedBlockState(KineticTileEntity te) { protected BlockState getRenderedBlockState(KineticTileEntity te) {
BlockState state = te.getBlockState(); BlockState state = te.getBlockState();
return AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(AXIS, ((IRotate) state.getBlock()).getRotationAxis(state)); return AllBlocksNew.SHAFT.getDefault().with(AXIS, ((IRotate) state.getBlock()).getRotationAxis(state));
} }
} }

View file

@ -30,7 +30,7 @@ public class SpeedControllerRenderer extends SmartTileEntityRenderer<SpeedContro
private SuperByteBuffer getRotatedModel(SpeedControllerTileEntity te) { private SuperByteBuffer getRotatedModel(SpeedControllerTileEntity te) {
BlockState state = te.getBlockState(); BlockState state = te.getBlockState();
return CreateClient.bufferCache.renderBlockIn(KineticTileEntityRenderer.KINETIC_TILE, return CreateClient.bufferCache.renderBlockIn(KineticTileEntityRenderer.KINETIC_TILE,
AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(ShaftBlock.AXIS, state.get(SpeedControllerBlock.HORIZONTAL_AXIS))); AllBlocksNew.SHAFT.getDefault().with(ShaftBlock.AXIS, state.get(SpeedControllerBlock.HORIZONTAL_AXIS)));
} }
} }

View file

@ -114,7 +114,7 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
drops.addAll(AllBlocks.BRASS_CASING.getDefault().getDrops(builder)); drops.addAll(AllBlocks.BRASS_CASING.getDefault().getDrops(builder));
TileEntity tileEntity = builder.get(LootParameters.BLOCK_ENTITY); TileEntity tileEntity = builder.get(LootParameters.BLOCK_ENTITY);
if (tileEntity instanceof BeltTileEntity && ((BeltTileEntity) tileEntity).hasPulley()) if (tileEntity instanceof BeltTileEntity && ((BeltTileEntity) tileEntity).hasPulley())
drops.addAll(AllBlocksNew.getDefault(AllBlocksNew.SHAFT).getDrops(builder)); drops.addAll(AllBlocksNew.SHAFT.getDefault().getDrops(builder));
return drops; return drops;
} }
@ -292,7 +292,7 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
belt.attachKinetics(); belt.attachKinetics();
} }
if (!player.isCreative()) if (!player.isCreative())
player.inventory.placeItemBackInInventory(world, AllBlocksNew.asStack(AllBlocksNew.SHAFT)); player.inventory.placeItemBackInInventory(world, AllBlocksNew.SHAFT.asStack());
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
} }
@ -504,7 +504,7 @@ public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEnt
} }
BlockState shaftState = BlockState shaftState =
AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(BlockStateProperties.AXIS, getRotationAxis(currentState)); AllBlocksNew.SHAFT.getDefault().with(BlockStateProperties.AXIS, getRotationAxis(currentState));
world.setBlockState(currentPos, hasPulley ? shaftState : Blocks.AIR.getDefaultState(), 3); world.setBlockState(currentPos, hasPulley ? shaftState : Blocks.AIR.getDefaultState(), 3);
world.playEvent(2001, currentPos, Block.getStateId(currentState)); world.playEvent(2001, currentPos, Block.getStateId(currentState));
} }

View file

@ -91,11 +91,11 @@ public class CogWheelBlock extends ShaftBlock {
} }
public static boolean isSmallCog(BlockState state) { public static boolean isSmallCog(BlockState state) {
return AllBlocksNew.equals(AllBlocksNew.COGWHEEL, state); return AllBlocksNew.COGWHEEL.typeOf(state);
} }
public static boolean isLargeCog(BlockState state) { public static boolean isLargeCog(BlockState state) {
return AllBlocksNew.equals(AllBlocksNew.LARGE_COGWHEEL, state); return AllBlocksNew.LARGE_COGWHEEL.typeOf(state);
} }
public void fillItemGroup(ItemGroup group, NonNullList<ItemStack> items) { public void fillItemGroup(ItemGroup group, NonNullList<ItemStack> items) {

View file

@ -96,7 +96,7 @@ public class CogwheelBlockItem extends BlockItem {
continue; continue;
if (blockState.get(CogWheelBlock.AXIS) != axis) if (blockState.get(CogWheelBlock.AXIS) != axis)
continue; continue;
if (AllBlocksNew.equals(AllBlocksNew.LARGE_COGWHEEL, blockState) == large) if (AllBlocksNew.LARGE_COGWHEEL.typeOf(blockState) == large)
continue; continue;
AllTriggers.triggerFor(AllTriggers.SHIFTING_GEARS, player); AllTriggers.triggerFor(AllTriggers.SHIFTING_GEARS, player);
} }

View file

@ -66,7 +66,7 @@ public class ShaftBlock extends RotatedPillarKineticBlock {
} }
public static boolean isShaft(BlockState state) { public static boolean isShaft(BlockState state) {
return AllBlocksNew.equals(AllBlocksNew.SHAFT, state); return AllBlocksNew.SHAFT.typeOf(state);
} }
// IRotate: // IRotate:

View file

@ -16,7 +16,7 @@ public class EncasedShaftTileEntityRenderer extends KineticTileEntityRenderer {
@Override @Override
protected BlockState getRenderedBlockState(KineticTileEntity te) { protected BlockState getRenderedBlockState(KineticTileEntity te) {
return AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(BlockStateProperties.AXIS, return AllBlocksNew.SHAFT.getDefault().with(BlockStateProperties.AXIS,
te.getBlockState().get(BlockStateProperties.AXIS)); te.getBlockState().get(BlockStateProperties.AXIS));
} }

View file

@ -60,7 +60,7 @@ public class GaugeTileEntityRenderer extends KineticTileEntityRenderer {
@Override @Override
protected BlockState getRenderedBlockState(KineticTileEntity te) { protected BlockState getRenderedBlockState(KineticTileEntity te) {
return AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(BlockStateProperties.AXIS, return AllBlocksNew.SHAFT.getDefault().with(BlockStateProperties.AXIS,
((IRotate) te.getBlockState().getBlock()).getRotationAxis(te.getBlockState())); ((IRotate) te.getBlockState().getBlock()).getRotationAxis(te.getBlockState()));
} }

View file

@ -152,7 +152,7 @@ public abstract class LaunchedItem {
BlockPos offset = BeltBlock.nextSegmentPosition(state, BlockPos.ZERO, isStart); BlockPos offset = BeltBlock.nextSegmentPosition(state, BlockPos.ZERO, isStart);
int i = length - 1; int i = length - 1;
Axis axis = state.get(BeltBlock.HORIZONTAL_FACING).rotateY().getAxis(); Axis axis = state.get(BeltBlock.HORIZONTAL_FACING).rotateY().getAxis();
world.setBlockState(target, AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(ShaftBlock.AXIS, axis)); world.setBlockState(target, AllBlocksNew.SHAFT.getDefault().with(ShaftBlock.AXIS, axis));
BeltConnectorItem BeltConnectorItem
.createBelts(world, target, target.add(offset.getX() * i, offset.getY() * i, offset.getZ() * i)); .createBelts(world, target, target.add(offset.getX() * i, offset.getY() * i, offset.getZ() * i));
} }

View file

@ -504,7 +504,7 @@ public class SchematicannonTileEntity extends SmartTileEntity implements INamedC
} }
if (!isLastSegment) if (!isLastSegment)
blockState = (blockState.get(BeltBlock.PART) == Part.MIDDLE) ? Blocks.AIR.getDefaultState() blockState = (blockState.get(BeltBlock.PART) == Part.MIDDLE) ? Blocks.AIR.getDefaultState()
: AllBlocksNew.getDefault(AllBlocksNew.SHAFT).with(ShaftBlock.AXIS, facing.rotateY().getAxis()); : AllBlocksNew.SHAFT.getDefault().with(ShaftBlock.AXIS, facing.rotateY().getAxis());
return blockState; return blockState;
} }