Update Registrate, use onRegister function instead of CreateBlockBuilder

This commit is contained in:
tterrag 2020-05-14 00:53:12 -04:00
parent deb03d10de
commit c3cdf346f7
6 changed files with 35 additions and 103 deletions

View file

@ -97,7 +97,7 @@ configurations {
dependencies { dependencies {
minecraft 'net.minecraftforge:forge:1.15.2-31.1.36' minecraft 'net.minecraftforge:forge:1.15.2-31.1.36'
def registrate = "com.tterrag.registrate:Registrate:MC1.15.2-0.0.3.9" def registrate = "com.tterrag.registrate:Registrate:MC1.15.2-0.0.3.11"
implementation fg.deobf(registrate) implementation fg.deobf(registrate)
shade registrate shade registrate

View file

@ -29,7 +29,7 @@ public class AllBlocksNew {
} }
public static final BlockEntry<SchematicannonBlock> SCHEMATICANNON = public static final BlockEntry<SchematicannonBlock> SCHEMATICANNON =
REGISTRATE.createBlock("schematicannon", SchematicannonBlock::new) REGISTRATE.block("schematicannon", SchematicannonBlock::new)
.initialProperties(() -> Blocks.DISPENSER) .initialProperties(() -> Blocks.DISPENSER)
.blockstate((ctx, prov) -> prov.simpleBlock(ctx.getEntry(), AssetLookup.partialBaseModel(ctx, prov))) .blockstate((ctx, prov) -> prov.simpleBlock(ctx.getEntry(), AssetLookup.partialBaseModel(ctx, prov)))
.item() .item()
@ -38,7 +38,7 @@ public class AllBlocksNew {
.register(); .register();
public static final BlockEntry<SchematicTableBlock> SCHEMATIC_TABLE = public static final BlockEntry<SchematicTableBlock> SCHEMATIC_TABLE =
REGISTRATE.createBlock("schematic_table", SchematicTableBlock::new) REGISTRATE.block("schematic_table", SchematicTableBlock::new)
.initialProperties(() -> Blocks.LECTERN) .initialProperties(() -> Blocks.LECTERN)
.blockstate((ctx, prov) -> prov.horizontalBlock(ctx.getEntry(), prov.models() .blockstate((ctx, prov) -> prov.horizontalBlock(ctx.getEntry(), prov.models()
.getExistingFile(ctx.getId()), 0)) .getExistingFile(ctx.getId()), 0))
@ -49,13 +49,13 @@ public class AllBlocksNew {
REGISTRATE.startSection(Sections.KINETICS); REGISTRATE.startSection(Sections.KINETICS);
} }
public static final BlockEntry<ShaftBlock> SHAFT = REGISTRATE.createBlock("shaft", ShaftBlock::new) public static final BlockEntry<ShaftBlock> SHAFT = REGISTRATE.block("shaft", ShaftBlock::new)
.initialProperties(SharedProperties::kinetic) .initialProperties(SharedProperties::kinetic)
.blockstate(BlockStateGen.axisBlockProvider(false)) .blockstate(BlockStateGen.axisBlockProvider(false))
.simpleItem() .simpleItem()
.register(); .register();
public static final BlockEntry<CogWheelBlock> COGWHEEL = REGISTRATE.createBlock("cogwheel", CogWheelBlock::small) public static final BlockEntry<CogWheelBlock> COGWHEEL = REGISTRATE.block("cogwheel", CogWheelBlock::small)
.initialProperties(SharedProperties::kinetic) .initialProperties(SharedProperties::kinetic)
.properties(p -> p.sound(SoundType.WOOD)) .properties(p -> p.sound(SoundType.WOOD))
.blockstate(BlockStateGen.axisBlockProvider(false)) .blockstate(BlockStateGen.axisBlockProvider(false))
@ -64,7 +64,7 @@ public class AllBlocksNew {
.register(); .register();
public static final BlockEntry<CogWheelBlock> LARGE_COGWHEEL = public static final BlockEntry<CogWheelBlock> LARGE_COGWHEEL =
REGISTRATE.createBlock("large_cogwheel", CogWheelBlock::large) REGISTRATE.block("large_cogwheel", CogWheelBlock::large)
.initialProperties(SharedProperties::kinetic) .initialProperties(SharedProperties::kinetic)
.properties(p -> p.sound(SoundType.WOOD)) .properties(p -> p.sound(SoundType.WOOD))
.blockstate(BlockStateGen.axisBlockProvider(false)) .blockstate(BlockStateGen.axisBlockProvider(false))
@ -73,7 +73,7 @@ public class AllBlocksNew {
.register(); .register();
public static final BlockEntry<EncasedShaftBlock> ENCASED_SHAFT = public static final BlockEntry<EncasedShaftBlock> ENCASED_SHAFT =
REGISTRATE.createBlock("encased_shaft", EncasedShaftBlock::new) REGISTRATE.block("encased_shaft", EncasedShaftBlock::new)
.initialProperties(SharedProperties::kinetic) .initialProperties(SharedProperties::kinetic)
.blockstate(BlockStateGen.axisBlockProvider(true)) .blockstate(BlockStateGen.axisBlockProvider(true))
.item() .item()
@ -81,7 +81,7 @@ public class AllBlocksNew {
.build() .build()
.register(); .register();
public static final BlockEntry<GearboxBlock> GEARBOX = REGISTRATE.createBlock("gearbox", GearboxBlock::new) public static final BlockEntry<GearboxBlock> GEARBOX = REGISTRATE.block("gearbox", GearboxBlock::new)
.initialProperties(SharedProperties::kinetic) .initialProperties(SharedProperties::kinetic)
.blockstate(BlockStateGen.axisBlockProvider(true)) .blockstate(BlockStateGen.axisBlockProvider(true))
.item() .item()
@ -89,7 +89,7 @@ public class AllBlocksNew {
.build() .build()
.register(); .register();
public static final BlockEntry<ClutchBlock> CLUTCH = REGISTRATE.createBlock("clutch", ClutchBlock::new) public static final BlockEntry<ClutchBlock> CLUTCH = REGISTRATE.block("clutch", ClutchBlock::new)
.initialProperties(SharedProperties::kinetic) .initialProperties(SharedProperties::kinetic)
.blockstate((c, p) -> BlockStateGen.axisBlock(c, p, AssetLookup.forPowered(c, p))) .blockstate((c, p) -> BlockStateGen.axisBlock(c, p, AssetLookup.forPowered(c, p)))
.item() .item()
@ -97,7 +97,7 @@ public class AllBlocksNew {
.build() .build()
.register(); .register();
public static final BlockEntry<GearshiftBlock> GEARSHIFT = REGISTRATE.createBlock("gearshift", GearshiftBlock::new) public static final BlockEntry<GearshiftBlock> GEARSHIFT = REGISTRATE.block("gearshift", GearshiftBlock::new)
.initialProperties(SharedProperties::kinetic) .initialProperties(SharedProperties::kinetic)
.blockstate((c, p) -> BlockStateGen.axisBlock(c, p, AssetLookup.forPowered(c, p))) .blockstate((c, p) -> BlockStateGen.axisBlock(c, p, AssetLookup.forPowered(c, p)))
.item() .item()

View file

@ -1,70 +0,0 @@
package com.simibubi.create.foundation.registrate;
import java.util.LinkedList;
import java.util.List;
import com.simibubi.create.CreateClient;
import com.simibubi.create.foundation.block.connected.CTModel;
import com.simibubi.create.foundation.block.connected.ConnectedTextureBehaviour;
import com.tterrag.registrate.AbstractRegistrate;
import com.tterrag.registrate.builders.BlockBuilder;
import com.tterrag.registrate.builders.BuilderCallback;
import com.tterrag.registrate.util.entry.BlockEntry;
import com.tterrag.registrate.util.nullness.NonNullConsumer;
import com.tterrag.registrate.util.nullness.NonNullFunction;
import com.tterrag.registrate.util.nullness.NonNullSupplier;
import net.minecraft.block.Block;
import net.minecraft.block.Block.Properties;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor;
public class CreateBlockBuilder<T extends Block, P> extends BlockBuilder<T, P> {
private List<NonNullConsumer<BlockEntry<T>>> registerCallbacks;
protected CreateBlockBuilder(AbstractRegistrate<?> owner, P parent, String name, BuilderCallback callback,
NonNullFunction<Properties, T> factory, NonNullSupplier<Properties> initialProperties) {
super(owner, parent, name, callback, factory, initialProperties);
registerCallbacks = new LinkedList<>();
}
public static <T extends Block, P> CreateBlockBuilder<T, P> create(AbstractRegistrate<?> owner, P parent,
String name, BuilderCallback callback, NonNullFunction<Block.Properties, T> factory, Material material) {
return (CreateBlockBuilder<T, P>) new CreateBlockBuilder<>(owner, parent, name, callback, factory,
() -> Block.Properties.create(material)).defaultBlockstate()
.defaultLoot()
.defaultLang();
}
public CreateBlockBuilder<T, P> connectedTextures(ConnectedTextureBehaviour behaviour) {
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> registerConnectedTexture(behaviour));
return this;
}
public CreateBlockBuilder<T, P> onRegister(NonNullConsumer<BlockEntry<T>> callback) {
registerCallbacks.add(callback);
return this;
}
@Override
public BlockEntry<T> register() {
BlockEntry<T> register = super.register();
registerCallbacks.forEach(func -> func.accept(register));
return register;
}
@OnlyIn(Dist.CLIENT)
private void registerConnectedTexture(ConnectedTextureBehaviour behaviour) {
registerModelSwap(model -> new CTModel(model, behaviour));
}
@OnlyIn(Dist.CLIENT)
private void registerModelSwap(NonNullFunction<IBakedModel, ? extends IBakedModel> modelFunc) {
onRegister(entry -> CreateClient.getCustomBlockModels().register(entry, modelFunc));
}
}

View file

@ -11,13 +11,12 @@ import com.tterrag.registrate.util.entry.RegistryEntry;
import com.tterrag.registrate.util.nullness.NonNullFunction; 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.material.Material;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.IForgeRegistryEntry; import net.minecraftforge.registries.IForgeRegistryEntry;
public class CreateRegistrateBase<C extends AbstractRegistrate<C>> extends AbstractRegistrate<C> { public class CreateRegistrateBase<S extends CreateRegistrateBase<S>> extends AbstractRegistrate<S> {
protected CreateRegistrateBase(String modid, NonNullSupplier<ItemGroup> creativeTab) { protected CreateRegistrateBase(String modid, NonNullSupplier<ItemGroup> creativeTab) {
super(modid); super(modid);
@ -26,6 +25,8 @@ public class CreateRegistrateBase<C extends AbstractRegistrate<C>> extends Abstr
itemGroup(creativeTab); itemGroup(creativeTab);
} }
/* Section Tracking */
private static Map<RegistryEntry<?>, Sections> sectionLookup = new IdentityHashMap<>(); private static Map<RegistryEntry<?>, Sections> sectionLookup = new IdentityHashMap<>();
private Sections section; private Sections section;
@ -39,24 +40,13 @@ public class CreateRegistrateBase<C extends AbstractRegistrate<C>> extends Abstr
@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,
RegistryEntry<T> ret = super.accept(name, type, builder, creator); NonNullFunction<RegistryObject<T>, ? extends RegistryEntry<T>> entryFactory) {
RegistryEntry<T> ret = super.accept(name, type, builder, creator, entryFactory);
sectionLookup.put(ret, currentSection()); sectionLookup.put(ret, currentSection());
return ret; return ret;
} }
@SuppressWarnings("unchecked")
public <T extends Block, P> CreateBlockBuilder<T, P> createBlock(String name,
NonNullFunction<Block.Properties, T> factory) {
return (CreateBlockBuilder<T, P>) super.block(name, factory);
}
@Override
public <T extends Block, P> CreateBlockBuilder<T, P> block(P parent, String name,
NonNullFunction<Block.Properties, T> factory, Material material) {
return CreateBlockBuilder.create(this, parent, name, this::accept, factory, material);
}
public void addToSection(RegistryEntry<?> entry, Sections section) { public void addToSection(RegistryEntry<?> entry, Sections section) {
sectionLookup.put(entry, section); sectionLookup.put(entry, section);
} }
@ -74,5 +64,4 @@ public class CreateRegistrateBase<C extends AbstractRegistrate<C>> extends Abstr
.findFirst() .findFirst()
.orElse(Sections.UNASSIGNED); .orElse(Sections.UNASSIGNED);
} }
} }

View file

@ -61,7 +61,7 @@ public class AllPaletteBlocks {
// Create stone variants // Create stone variants
public static final BlockEntry<SandBlock> LIMESAND = public static final BlockEntry<SandBlock> LIMESAND =
REGISTRATE.createBlock("limesand", p -> new SandBlock(0xD7D7C7, p)) REGISTRATE.block("limesand", p -> new SandBlock(0xD7D7C7, p))
.initialProperties(() -> Blocks.SAND) .initialProperties(() -> Blocks.SAND)
.blockstate(palettesCubeAll()) .blockstate(palettesCubeAll())
.register(); .register();
@ -103,7 +103,7 @@ public class AllPaletteBlocks {
.register()); .register());
public static final BlockEntry<ScoriaBlock> NATURAL_SCORIA = public static final BlockEntry<ScoriaBlock> NATURAL_SCORIA =
REGISTRATE.createBlock("natural_scoria", ScoriaBlock::new) REGISTRATE.block("natural_scoria", ScoriaBlock::new)
.initialProperties(() -> Blocks.ANDESITE) .initialProperties(() -> Blocks.ANDESITE)
.blockstate(palettesCubeAll()) .blockstate(palettesCubeAll())
.simpleItem() .simpleItem()

View file

@ -4,6 +4,8 @@ import java.util.function.Supplier;
import com.simibubi.create.AllCTs; import com.simibubi.create.AllCTs;
import com.simibubi.create.Create; import com.simibubi.create.Create;
import com.simibubi.create.CreateClient;
import com.simibubi.create.foundation.block.connected.CTModel;
import com.simibubi.create.foundation.block.connected.ConnectedTextureBehaviour; import com.simibubi.create.foundation.block.connected.ConnectedTextureBehaviour;
import com.simibubi.create.foundation.block.connected.HorizontalCTBehaviour; import com.simibubi.create.foundation.block.connected.HorizontalCTBehaviour;
import com.simibubi.create.foundation.registrate.CreateRegistrateBase; import com.simibubi.create.foundation.registrate.CreateRegistrateBase;
@ -14,12 +16,15 @@ import com.tterrag.registrate.util.NonNullLazyValue;
import com.tterrag.registrate.util.entry.BlockEntry; import com.tterrag.registrate.util.entry.BlockEntry;
import com.tterrag.registrate.util.nullness.NonNullFunction; import com.tterrag.registrate.util.nullness.NonNullFunction;
import com.tterrag.registrate.util.nullness.NonNullSupplier; import com.tterrag.registrate.util.nullness.NonNullSupplier;
import com.tterrag.registrate.util.nullness.NonNullUnaryOperator;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.Block.Properties; import net.minecraft.block.Block.Properties;
import net.minecraft.block.Blocks; import net.minecraft.block.Blocks;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
public class PalettesRegistrate extends CreateRegistrateBase<PalettesRegistrate> { public class PalettesRegistrate extends CreateRegistrateBase<PalettesRegistrate> {
@ -72,7 +77,8 @@ public class PalettesRegistrate extends CreateRegistrateBase<PalettesRegistrate>
public BlockEntry<WindowBlock> windowBlock(String name, AllCTs ct, Supplier<Supplier<RenderType>> renderType, public BlockEntry<WindowBlock> windowBlock(String name, AllCTs ct, Supplier<Supplier<RenderType>> renderType,
NonNullFunction<String, ResourceLocation> endTexture, NonNullFunction<String, ResourceLocation> sideTexture) { NonNullFunction<String, ResourceLocation> endTexture, NonNullFunction<String, ResourceLocation> sideTexture) {
return createBlock(name, WindowBlock::new).connectedTextures(new HorizontalCTBehaviour(ct.get())) return block(name, WindowBlock::new)
.transform(connectedTextures(new HorizontalCTBehaviour(ct.get())))
.addLayer(renderType) .addLayer(renderType)
.initialProperties(() -> Blocks.GLASS) .initialProperties(() -> Blocks.GLASS)
.blockstate((c, p) -> p.simpleBlock(c.get(), p.models() .blockstate((c, p) -> p.simpleBlock(c.get(), p.models()
@ -82,7 +88,8 @@ public class PalettesRegistrate extends CreateRegistrateBase<PalettesRegistrate>
} }
public BlockEntry<ConnectedGlassBlock> framedGlass(String name, ConnectedTextureBehaviour behaviour) { public BlockEntry<ConnectedGlassBlock> framedGlass(String name, ConnectedTextureBehaviour behaviour) {
return createBlock(name, ConnectedGlassBlock::new).connectedTextures(behaviour) return block(name, ConnectedGlassBlock::new)
.transform(connectedTextures(behaviour))
.addLayer(() -> RenderType::getTranslucent) .addLayer(() -> RenderType::getTranslucent)
.initialProperties(() -> Blocks.GLASS) .initialProperties(() -> Blocks.GLASS)
.blockstate((c, p) -> BlockStateGen.cubeAll(c, p, "palettes/", "framed_glass")) .blockstate((c, p) -> BlockStateGen.cubeAll(c, p, "palettes/", "framed_glass"))
@ -93,4 +100,10 @@ public class PalettesRegistrate extends CreateRegistrateBase<PalettesRegistrate>
.register(); .register();
} }
private <T extends Block> NonNullUnaryOperator<BlockBuilder<T, PalettesRegistrate>> connectedTextures(
ConnectedTextureBehaviour behavior) {
return b -> b.onRegister(entry -> DistExecutor.runWhenOn(Dist.CLIENT, () -> () ->
CreateClient.getCustomBlockModels()
.register(entry.delegate, model -> new CTModel(model, behavior))));
}
} }