ColorRegistrate, Part II

- Added functional interface for block vertex coloring
- Removed several unused type-check-based behaviours
- Belt now registers its color handler in the builder
- Natural scoria now registers its color handler in the builder
- Fixed wrong offset in colored vertex models
This commit is contained in:
simibubi 2020-05-24 12:31:05 +02:00
parent e9c26eed0a
commit 4e9cd1db49
17 changed files with 66 additions and 205 deletions

View file

@ -34,8 +34,8 @@ import com.simibubi.create.content.contraptions.components.saw.SawGenerator;
import com.simibubi.create.content.contraptions.components.structureMovement.bearing.ClockworkBearingBlock;
import com.simibubi.create.content.contraptions.components.structureMovement.bearing.MechanicalBearingBlock;
import com.simibubi.create.content.contraptions.components.structureMovement.chassis.LinearChassisBlock;
import com.simibubi.create.content.contraptions.components.structureMovement.chassis.RadialChassisBlock;
import com.simibubi.create.content.contraptions.components.structureMovement.chassis.LinearChassisBlock.ChassisCTBehaviour;
import com.simibubi.create.content.contraptions.components.structureMovement.chassis.RadialChassisBlock;
import com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerBlock;
import com.simibubi.create.content.contraptions.components.structureMovement.mounted.CartAssemblerBlock.MinecartAnchorBlock;
import com.simibubi.create.content.contraptions.components.structureMovement.piston.MechanicalPistonBlock;
@ -49,6 +49,7 @@ import com.simibubi.create.content.contraptions.relays.advanced.SpeedControllerB
import com.simibubi.create.content.contraptions.relays.advanced.sequencer.SequencedGearshiftBlock;
import com.simibubi.create.content.contraptions.relays.advanced.sequencer.SequencedGearshiftGenerator;
import com.simibubi.create.content.contraptions.relays.belt.BeltBlock;
import com.simibubi.create.content.contraptions.relays.belt.BeltColor;
import com.simibubi.create.content.contraptions.relays.belt.BeltGenerator;
import com.simibubi.create.content.contraptions.relays.elementary.CogWheelBlock;
import com.simibubi.create.content.contraptions.relays.elementary.CogwheelBlockItem;
@ -240,6 +241,7 @@ public class AllBlocks {
.initialProperties(SharedProperties.beltMaterial, MaterialColor.GRAY)
.properties(p -> p.sound(SoundType.CLOTH))
.transform(StressConfigDefaults.setImpact(1.0))
.transform(CreateRegistrate.blockColors(() -> BeltColor::new))
.blockstate(new BeltGenerator()::generate)
.register();

View file

@ -3,6 +3,9 @@ package com.simibubi.create;
import java.util.HashMap;
import java.util.Map;
import com.simibubi.create.foundation.block.IBlockVertexColor;
import com.simibubi.create.foundation.block.render.ColoredVertexModel;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
@ -19,8 +22,9 @@ import net.minecraft.world.biome.BiomeColors;
public class AllColorHandlers {
private static Map<Block, IBlockColor> coloredBlocks = new HashMap<>();
private static Map<IItemProvider, IItemColor> coloredItems = new HashMap<>();
private Map<Block, IBlockVertexColor> coloredVertexBlocks = new HashMap<>();
private Map<Block, IBlockColor> coloredBlocks = new HashMap<>();
private Map<IItemProvider, IItemColor> coloredItems = new HashMap<>();
//
@ -36,15 +40,19 @@ public class AllColorHandlers {
//
public static void register(Block block, IBlockColor color) {
public void register(Block block, IBlockColor color) {
coloredBlocks.put(block, color);
}
public static void register(IItemProvider item, IItemColor color) {
public void register(Block block, IBlockVertexColor color) {
coloredVertexBlocks.put(block, color);
}
public void register(IItemProvider item, IItemColor color) {
coloredItems.put(item, color);
}
public static void registerColorHandlers() {
public void init() {
BlockColors blockColors = Minecraft.getInstance()
.getBlockColors();
ItemColors itemColors = Minecraft.getInstance()
@ -52,6 +60,8 @@ public class AllColorHandlers {
coloredBlocks.forEach((block, color) -> blockColors.register(color, block));
coloredItems.forEach((item, color) -> itemColors.register(color, item));
coloredVertexBlocks.forEach((block, color) -> CreateClient.getCustomBlockModels()
.register(() -> block, model -> new ColoredVertexModel(model, color)));
}
//

View file

@ -236,6 +236,7 @@ public enum AllItems {
}
@OnlyIn(Dist.CLIENT)
@Deprecated // Use CreateRegistrate#itemColor when porting AllItems
public static void registerColorHandlers() {
ItemColors itemColors = Minecraft.getInstance().getItemColors();
for (AllItems item : values()) {

View file

@ -47,7 +47,9 @@ public class CreateClient {
public static SchematicAndQuillHandler schematicAndQuillHandler;
public static SuperByteBufferCache bufferCache;
public static Outliner outliner;
private static CustomBlockModels customBlockModels;
private static AllColorHandlers colorHandlers;
public static void addListeners(IEventBus modEventBus) {
DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> {
@ -74,7 +76,7 @@ public class CreateClient {
AllTileEntities.registerRenderers();
AllItems.registerColorHandlers();
AllEntityTypes.registerRenderers();
AllColorHandlers.registerColorHandlers();
getColorHandler().init();
IResourceManager resourceManager = Minecraft.getInstance()
.getResourceManager();
@ -174,5 +176,11 @@ public class CreateClient {
customBlockModels = new CustomBlockModels();
return customBlockModels;
}
public static AllColorHandlers getColorHandler() {
if (colorHandlers == null)
colorHandlers = new AllColorHandlers();
return colorHandlers;
}
}

View file

@ -15,7 +15,6 @@ import com.simibubi.create.content.logistics.block.belts.tunnel.BeltTunnelBlock;
import com.simibubi.create.content.schematics.ISpecialBlockItemRequirement;
import com.simibubi.create.content.schematics.ItemRequirement;
import com.simibubi.create.content.schematics.ItemRequirement.ItemUseType;
import com.simibubi.create.foundation.block.IHaveColorHandler;
import com.simibubi.create.foundation.block.ITE;
import com.simibubi.create.foundation.utility.Iterate;
import com.simibubi.create.foundation.utility.Lang;
@ -27,7 +26,6 @@ import net.minecraft.block.Blocks;
import net.minecraft.block.material.Material;
import net.minecraft.client.particle.DiggingParticle;
import net.minecraft.client.particle.ParticleManager;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraft.entity.Entity;
import net.minecraft.entity.MobEntity;
import net.minecraft.entity.item.ItemEntity;
@ -69,8 +67,7 @@ import net.minecraftforge.common.Tags;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
public class BeltBlock extends HorizontalKineticBlock
implements ITE<BeltTileEntity>, IHaveColorHandler, ISpecialBlockItemRequirement {
public class BeltBlock extends HorizontalKineticBlock implements ITE<BeltTileEntity>, ISpecialBlockItemRequirement {
public static final IProperty<Slope> SLOPE = EnumProperty.create("slope", Slope.class);
public static final IProperty<Part> PART = EnumProperty.create("part", Part.class);
@ -415,11 +412,6 @@ public class BeltBlock extends HorizontalKineticBlock
: BlockRenderType.ENTITYBLOCK_ANIMATED;
}
// @Override // TODO 1.15 register layer
// public boolean canRenderInLayer(BlockState state, BlockRenderLayer layer) {
// return state.get(CASING) && state.get(SLOPE) != Slope.VERTICAL && layer == getRenderLayer();
// }
public static void initBelt(World world, BlockPos pos) {
if (world.isRemote || world.getWorldType() == WorldType.DEBUG_ALL_BLOCK_STATES)
return;
@ -627,12 +619,6 @@ public class BeltBlock extends HorizontalKineticBlock
return true;
}
@Override
@OnlyIn(Dist.CLIENT)
public IBlockColor getColorHandler() {
return new BeltColor();
}
@Override
public Class<BeltTileEntity> getTileEntityClass() {
return BeltTileEntity.class;

View file

@ -9,7 +9,7 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
class BeltColor implements IBlockColor {
public class BeltColor implements IBlockColor {
@Override
public int getColor(BlockState state, ILightReader reader, BlockPos pos, int layer) {

View file

@ -125,8 +125,9 @@ public class AllPaletteBlocks {
public static final PalettesVariantEntry GABBRO_VARIANTS =
new PalettesVariantEntry(PaletteStoneVariants.GABBRO, PaletteBlockPatterns.standardRange, GABBRO);
public static final BlockEntry<ScoriaBlock> NATURAL_SCORIA = REGISTRATE.block("natural_scoria", ScoriaBlock::new)
public static final BlockEntry<Block> NATURAL_SCORIA = REGISTRATE.block("natural_scoria", Block::new)
.initialProperties(() -> Blocks.ANDESITE)
.transform(CreateRegistrate.blockVertexColors(new ScoriaVertexColor()))
.blockstate(palettesCubeAll())
.simpleItem()
.register();

View file

@ -1,24 +1,9 @@
package com.simibubi.create.content.palettes;
import com.simibubi.create.foundation.block.IHaveColoredVertices;
import com.simibubi.create.foundation.block.IBlockVertexColor;
import com.simibubi.create.foundation.utility.ColorHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.material.MaterialColor;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
public class ScoriaBlock extends Block implements IHaveColoredVertices {
public ScoriaBlock(Properties p_i48440_1_) {
super(p_i48440_1_);
}
@Override
public MaterialColor getMaterialColor(BlockState state, IBlockReader worldIn, BlockPos pos) {
return MaterialColor.GRAY_TERRACOTTA;
}
public class ScoriaVertexColor implements IBlockVertexColor {
@Override
public int getColor(float x, float y, float z) {

View file

@ -1,58 +0,0 @@
package com.simibubi.create.foundation.block;
import com.simibubi.create.AllContainerTypes;
import net.minecraft.client.Minecraft;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
public abstract class AbstractTileEntityContainer<T extends TileEntity> extends Container {
public T te;
public PlayerEntity player;
@SuppressWarnings("unchecked")
public AbstractTileEntityContainer(AllContainerTypes containerType, int id, PlayerInventory inv,
PacketBuffer extraData) {
super(containerType.type, id);
ClientWorld world = Minecraft.getInstance().world;
this.te = (T) world.getTileEntity(extraData.readBlockPos());
this.te.handleUpdateTag(extraData.readCompoundTag());
this.player = inv.player;
init();
}
public AbstractTileEntityContainer(AllContainerTypes containerType, int id, PlayerInventory inv, T te) {
super(containerType.type, id);
this.te = te;
this.player = inv.player;
init();
}
protected abstract void init();
protected void addPlayerSlots(int x, int y) {
for (int row = 0; row < 3; ++row)
for (int col = 0; col < 9; ++col)
this.addSlot(new Slot(player.inventory, col + row * 9 + 9, x + col * 18, y + row * 18));
for (int hotbarSlot = 0; hotbarSlot < 9; ++hotbarSlot)
this.addSlot(new Slot(player.inventory, hotbarSlot, x + hotbarSlot * 18, y + 3 * 18 + 4));
}
@Override
public boolean canInteractWith(PlayerEntity playerIn) {
return true;
}
@Override
public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
return ItemStack.EMPTY;
}
}

View file

@ -0,0 +1,8 @@
package com.simibubi.create.foundation.block;
@FunctionalInterface
public interface IBlockVertexColor {
public int getColor(float x, float y, float z);
}

View file

@ -1,12 +0,0 @@
package com.simibubi.create.foundation.block;
import net.minecraft.client.renderer.color.IBlockColor;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public interface IHaveColorHandler {
@OnlyIn(Dist.CLIENT)
public IBlockColor getColorHandler();
}

View file

@ -1,19 +0,0 @@
package com.simibubi.create.foundation.block;
import com.simibubi.create.foundation.block.render.ColoredVertexModel;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public interface IHaveColoredVertices extends IHaveCustomBlockModel {
public int getColor(float x, float y, float z);
@Override
@OnlyIn(Dist.CLIENT)
default IBakedModel createModel(IBakedModel original) {
return new ColoredVertexModel(original, this);
}
}

View file

@ -1,14 +0,0 @@
package com.simibubi.create.foundation.block;
import javax.annotation.Nullable;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
public interface IHaveCustomBlockModel {
@OnlyIn(value = Dist.CLIENT)
public IBakedModel createModel(@Nullable IBakedModel original);
}

View file

@ -1,8 +0,0 @@
package com.simibubi.create.foundation.block;
/**
* Blocks only registered for use in other blocks' renderers.
*/
public interface IRenderUtilityBlock {
}

View file

@ -1,36 +0,0 @@
package com.simibubi.create.foundation.block;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
public interface IWithContainer<T extends TileEntity, C extends AbstractTileEntityContainer<T>> extends INamedContainerProvider {
public IContainerFactory<T, C> getContainerFactory();
@SuppressWarnings("unchecked")
@Override
default Container createMenu(int id, PlayerInventory inv, PlayerEntity player) {
return getContainerFactory().create(id, inv, ((T) this));
}
@Override
default ITextComponent getDisplayName() {
return new StringTextComponent(((TileEntity) this).getType().getRegistryName().toString());
}
public interface IContainerFactory<T extends TileEntity, C extends AbstractTileEntityContainer<? extends TileEntity>> {
public C create(int id, PlayerInventory inv, T te);
}
default void sendToContainer(PacketBuffer buffer) {
buffer.writeBlockPos(((TileEntity) this).getPos());
buffer.writeCompoundTag(((TileEntity) this).getUpdateTag());
}
}

View file

@ -5,7 +5,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Random;
import com.simibubi.create.foundation.block.IHaveColoredVertices;
import com.simibubi.create.foundation.block.IBlockVertexColor;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.model.BakedQuad;
@ -22,12 +22,12 @@ import net.minecraftforge.client.model.data.ModelProperty;
public class ColoredVertexModel extends BakedModelWrapper<IBakedModel> {
private IHaveColoredVertices colorer;
private IBlockVertexColor color;
private static ModelProperty<BlockPos> POSITION_PROPERTY = new ModelProperty<>();
public ColoredVertexModel(IBakedModel originalModel, IHaveColoredVertices colorer) {
public ColoredVertexModel(IBakedModel originalModel, IBlockVertexColor color) {
super(originalModel);
this.colorer = colorer;
this.color = color;
}
@Override
@ -38,13 +38,11 @@ public class ColoredVertexModel extends BakedModelWrapper<IBakedModel> {
@Override
public List<BakedQuad> getQuads(BlockState state, Direction side, Random rand, IModelData extraData) {
List<BakedQuad> quads = new ArrayList<>(super.getQuads(state, side, rand, extraData));
if (!extraData.hasProperty(POSITION_PROPERTY))
return quads;
for (int i = 0; i < quads.size(); i++) {
BakedQuad quad = quads.get(i);
BakedQuad newQuad = new BakedQuad(Arrays.copyOf(quad.getVertexData(), quad.getVertexData().length),
quad.getTintIndex(), quad.getFace(), quad.getSprite(), quad.shouldApplyDiffuseLighting());
@ -52,16 +50,12 @@ public class ColoredVertexModel extends BakedModelWrapper<IBakedModel> {
int[] vertexData = newQuad.getVertexData();
BlockPos data = extraData.getData(POSITION_PROPERTY);
// Direction direction = quad.getFace();
// if (direction.getAxis().isHorizontal())
// continue;
for (int vertex = 0; vertex < vertexData.length; vertex += format.getIntegerSize()) {
int colorOffset = 16 / 4; // TODO 1.15 is this the right offset?
int colorOffset = 12 / 4;
float x = Float.intBitsToFloat(vertexData[vertex]);
float y = Float.intBitsToFloat(vertexData[vertex + 1]);
float z = Float.intBitsToFloat(vertexData[vertex + 2]);
int color = colorer.getColor(x + data.getX(), y + data.getY(), z + data.getZ());
int color = this.color.getColor(x + data.getX(), y + data.getY(), z + data.getZ());
vertexData[vertex + colorOffset] = color;
}

View file

@ -7,9 +7,9 @@ import java.util.Map.Entry;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import com.simibubi.create.AllColorHandlers;
import com.simibubi.create.CreateClient;
import com.simibubi.create.content.AllSections;
import com.simibubi.create.foundation.block.IBlockVertexColor;
import com.simibubi.create.foundation.block.connected.CTModel;
import com.simibubi.create.foundation.block.connected.ConnectedTextureBehaviour;
import com.tterrag.registrate.AbstractRegistrate;
@ -119,6 +119,11 @@ public class CreateRegistrate extends AbstractRegistrate<CreateRegistrate> {
return b -> b.onRegister(entry -> onClient(() -> () -> registerBlockColor(entry, colorFunc)));
}
public static <T extends Block> NonNullUnaryOperator<BlockBuilder<T, CreateRegistrate>> blockVertexColors(
IBlockVertexColor colorFunc) {
return b -> b.onRegister(entry -> onClient(() -> () -> registerBlockVertexColor(entry, colorFunc)));
}
public static NonNullUnaryOperator<ItemBuilder<? extends BlockItem, ?>> itemColors(
Supplier<Supplier<IItemColor>> colorFunc) {
return b -> b.onRegister(entry -> onClient(() -> () -> registerItemColor(entry, colorFunc)));
@ -136,14 +141,22 @@ public class CreateRegistrate extends AbstractRegistrate<CreateRegistrate> {
@OnlyIn(Dist.CLIENT)
private static void registerBlockColor(Block entry, Supplier<Supplier<IBlockColor>> colorFunc) {
AllColorHandlers.register(entry, colorFunc.get()
.get());
CreateClient.getColorHandler()
.register(entry, colorFunc.get()
.get());
}
@OnlyIn(Dist.CLIENT)
private static void registerBlockVertexColor(Block entry, IBlockVertexColor colorFunc) {
CreateClient.getColorHandler()
.register(entry, colorFunc);
}
@OnlyIn(Dist.CLIENT)
private static void registerItemColor(IItemProvider entry, Supplier<Supplier<IItemColor>> colorFunc) {
AllColorHandlers.register(entry, colorFunc.get()
.get());
CreateClient.getColorHandler()
.register(entry, colorFunc.get()
.get());
}
}