Improve handling of dyed blocks

- Create DyedBlockList for storing and accessing a set of dyed blocks
- Switch usage of BlockEntry<?>[] to DyedBlockList in AllBlocks
- These changes also finally fix the compilation error related to generic arrays
This commit is contained in:
PepperBell 2021-06-05 10:43:16 -07:00
parent fec329e3d2
commit 823520e047
9 changed files with 108 additions and 88 deletions

View file

@ -155,6 +155,7 @@ import com.simibubi.create.content.logistics.block.redstone.RedstoneLinkGenerato
import com.simibubi.create.content.logistics.block.redstone.StockpileSwitchBlock;
import com.simibubi.create.content.schematics.block.SchematicTableBlock;
import com.simibubi.create.content.schematics.block.SchematicannonBlock;
import com.simibubi.create.foundation.block.DyedBlockList;
import com.simibubi.create.foundation.block.ItemUseOverrides;
import com.simibubi.create.foundation.config.StressConfigDefaults;
import com.simibubi.create.foundation.data.AssetLookup;
@ -626,23 +627,19 @@ public class AllBlocks {
.transform(BuilderTransformers.valveHandle(null))
.register();
public static final BlockEntry<?>[] DYED_VALVE_HANDLES = new BlockEntry<?>[DyeColor.values().length];
static {
for (DyeColor colour : DyeColor.values()) {
String colourName = colour.getString();
DYED_VALVE_HANDLES[colour.ordinal()] = REGISTRATE.block(colourName + "_valve_handle", ValveHandleBlock::dyed)
.transform(BuilderTransformers.valveHandle(colour))
.recipe((c, p) -> ShapedRecipeBuilder.shapedRecipe(c.get())
.patternLine("#")
.patternLine("-")
.key('#', DyeHelper.getTagOfDye(colour))
.key('-', AllItemTags.VALVE_HANDLES.tag)
.addCriterion("has_valve", RegistrateRecipeProvider.hasItem(AllItemTags.VALVE_HANDLES.tag))
.build(p, Create.asResource("crafting/kinetics/" + c.getName() + "_from_other_valve_handle")))
.register();
}
}
public static final DyedBlockList<ValveHandleBlock> DYED_VALVE_HANDLES = new DyedBlockList<>(colour -> {
String colourName = colour.getString();
return REGISTRATE.block(colourName + "_valve_handle", ValveHandleBlock::dyed)
.transform(BuilderTransformers.valveHandle(colour))
.recipe((c, p) -> ShapedRecipeBuilder.shapedRecipe(c.get())
.patternLine("#")
.patternLine("-")
.key('#', DyeHelper.getTagOfDye(colour))
.key('-', AllItemTags.VALVE_HANDLES.tag)
.addCriterion("has_valve", RegistrateRecipeProvider.hasItem(AllItemTags.VALVE_HANDLES.tag))
.build(p, Create.asResource("crafting/kinetics/" + c.getName() + "_from_other_valve_handle")))
.register();
});
public static final BlockEntry<FluidTankBlock> FLUID_TANK = REGISTRATE.block("fluid_tank", FluidTankBlock::regular)
.initialProperties(SharedProperties::softMetal)
@ -961,47 +958,41 @@ public class AllBlocks {
.simpleItem()
.register();
public static final BlockEntry<?>[] SEATS = new BlockEntry<?>[DyeColor.values().length];
static {
// SEATS
for (DyeColor colour : DyeColor.values()) {
String colourName = colour.getString();
SeatMovementBehaviour movementBehaviour = new SeatMovementBehaviour();
SEATS[colour.ordinal()] =
REGISTRATE.block(colourName + "_seat", p -> new SeatBlock(p, colour == DyeColor.RED))
.initialProperties(SharedProperties::wooden)
.onRegister(addMovementBehaviour(movementBehaviour))
.blockstate((c, p) -> {
p.simpleBlock(c.get(), p.models()
.withExistingParent(colourName + "_seat", p.modLoc("block/seat"))
.texture("1", p.modLoc("block/seat/top_" + colourName))
.texture("2", p.modLoc("block/seat/side_" + colourName)));
})
.recipe((c, p) -> {
ShapedRecipeBuilder.shapedRecipe(c.get())
.patternLine("#")
.patternLine("-")
.key('#', DyeHelper.getWoolOfDye(colour))
.key('-', ItemTags.WOODEN_SLABS)
.addCriterion("has_wool", RegistrateRecipeProvider.hasItem(ItemTags.WOOL))
.build(p, Create.asResource("crafting/kinetics/" + c.getName()));
ShapedRecipeBuilder.shapedRecipe(c.get())
.patternLine("#")
.patternLine("-")
.key('#', DyeHelper.getTagOfDye(colour))
.key('-', AllItemTags.SEATS.tag)
.addCriterion("has_seat", RegistrateRecipeProvider.hasItem(AllItemTags.SEATS.tag))
.build(p, Create.asResource("crafting/kinetics/" + c.getName() + "_from_other_seat"));
})
.onRegisterAfter(Item.class, v -> TooltipHelper.referTo(v, "block.create.seat"))
.tag(AllBlockTags.SEATS.tag)
.item()
.tag(AllItemTags.SEATS.tag)
.build()
.register();
}
}
public static final DyedBlockList<SeatBlock> SEATS = new DyedBlockList<>(colour -> {
String colourName = colour.getString();
SeatMovementBehaviour movementBehaviour = new SeatMovementBehaviour();
return REGISTRATE.block(colourName + "_seat", p -> new SeatBlock(p, colour == DyeColor.RED))
.initialProperties(SharedProperties::wooden)
.onRegister(addMovementBehaviour(movementBehaviour))
.blockstate((c, p) -> {
p.simpleBlock(c.get(), p.models()
.withExistingParent(colourName + "_seat", p.modLoc("block/seat"))
.texture("1", p.modLoc("block/seat/top_" + colourName))
.texture("2", p.modLoc("block/seat/side_" + colourName)));
})
.recipe((c, p) -> {
ShapedRecipeBuilder.shapedRecipe(c.get())
.patternLine("#")
.patternLine("-")
.key('#', DyeHelper.getWoolOfDye(colour))
.key('-', ItemTags.WOODEN_SLABS)
.addCriterion("has_wool", RegistrateRecipeProvider.hasItem(ItemTags.WOOL))
.build(p, Create.asResource("crafting/kinetics/" + c.getName()));
ShapedRecipeBuilder.shapedRecipe(c.get())
.patternLine("#")
.patternLine("-")
.key('#', DyeHelper.getTagOfDye(colour))
.key('-', AllItemTags.SEATS.tag)
.addCriterion("has_seat", RegistrateRecipeProvider.hasItem(AllItemTags.SEATS.tag))
.build(p, Create.asResource("crafting/kinetics/" + c.getName() + "_from_other_seat"));
})
.onRegisterAfter(Item.class, v -> TooltipHelper.referTo(v, "block.create.seat"))
.tag(AllBlockTags.SEATS.tag)
.item()
.tag(AllItemTags.SEATS.tag)
.build()
.register();
});
public static final BlockEntry<SailBlock> SAIL_FRAME = REGISTRATE.block("sail_frame", p -> SailBlock.frame(p))
.initialProperties(SharedProperties::wooden)
@ -1012,8 +1003,6 @@ public class AllBlocks {
.simpleItem()
.register();
public static final BlockEntry<?>[] DYED_SAILS = new BlockEntry<?>[DyeColor.values().length];
public static final BlockEntry<SailBlock> SAIL = REGISTRATE.block("white_sail", p -> SailBlock.withCanvas(p))
.initialProperties(SharedProperties::wooden)
.properties(Block.Properties::nonOpaque)
@ -1022,26 +1011,22 @@ public class AllBlocks {
.simpleItem()
.register();
static {
// DYED SAILS
for (DyeColor colour : DyeColor.values()) {
if (colour == DyeColor.WHITE) {
DYED_SAILS[colour.ordinal()] = SAIL;
continue;
}
String colourName = colour.getString();
DYED_SAILS[colour.ordinal()] = REGISTRATE.block(colourName + "_sail", p -> SailBlock.withCanvas(p))
.properties(Block.Properties::nonOpaque)
.initialProperties(SharedProperties::wooden)
.blockstate((c, p) -> p.directionalBlock(c.get(), p.models()
.withExistingParent(colourName + "_sail", p.modLoc("block/white_sail"))
.texture("0", p.modLoc("block/sail/canvas_" + colourName))))
.tag(AllBlockTags.WINDMILL_SAILS.tag)
.tag(AllBlockTags.SAILS.tag)
.loot((p, b) -> p.registerDropping(b, SAIL.get()))
.register();
public static final DyedBlockList<SailBlock> DYED_SAILS = new DyedBlockList<>(colour -> {
if (colour == DyeColor.WHITE) {
return SAIL;
}
}
String colourName = colour.getString();
return REGISTRATE.block(colourName + "_sail", p -> SailBlock.withCanvas(p))
.properties(Block.Properties::nonOpaque)
.initialProperties(SharedProperties::wooden)
.blockstate((c, p) -> p.directionalBlock(c.get(), p.models()
.withExistingParent(colourName + "_sail", p.modLoc("block/white_sail"))
.texture("0", p.modLoc("block/sail/canvas_" + colourName))))
.tag(AllBlockTags.WINDMILL_SAILS.tag)
.tag(AllBlockTags.SAILS.tag)
.loot((p, b) -> p.registerDropping(b, SAIL.get()))
.register();
});
public static final BlockEntry<CasingBlock> ANDESITE_CASING = REGISTRATE.block("andesite_casing", CasingBlock::new)
.transform(BuilderTransformers.casing(AllSpriteShifts.ANDESITE_CASING))

View file

@ -248,7 +248,7 @@ public class AllTileEntities {
.tileEntity("hand_crank", HandCrankTileEntity::new)
.instance(() -> HandCrankInstance::new)
.validBlocks(AllBlocks.HAND_CRANK, AllBlocks.COPPER_VALVE_HANDLE)
.validBlocks(AllBlocks.DYED_VALVE_HANDLES)
.validBlocks(AllBlocks.DYED_VALVE_HANDLES.toArray())
.renderer(() -> HandCrankRenderer::new)
.register();

View file

@ -101,7 +101,7 @@ public class SeatBlock extends Block {
if (world.isRemote)
return ActionResultType.SUCCESS;
BlockState newState = AllBlocks.SEATS[color.ordinal()].getDefaultState();
BlockState newState = AllBlocks.SEATS.get(color).getDefaultState();
if (newState != state)
world.setBlockState(pos, newState);
return ActionResultType.SUCCESS;

View file

@ -48,7 +48,7 @@ public class ValveHandleBlock extends HandCrankBlock {
if (worldIn.isRemote)
return ActionResultType.SUCCESS;
BlockState newState = AllBlocks.DYED_VALVE_HANDLES[color.ordinal()]
BlockState newState = AllBlocks.DYED_VALVE_HANDLES.get(color)
.getDefaultState()
.with(FACING, state.get(FACING));
if (newState != state)

View file

@ -97,7 +97,7 @@ public class SailBlock extends ProperDirectionalBlock {
protected void applyDye(BlockState state, World world, BlockPos pos, @Nullable DyeColor color) {
BlockState newState =
(color == null ? AllBlocks.SAIL_FRAME : AllBlocks.DYED_SAILS[color.ordinal()]).getDefaultState()
(color == null ? AllBlocks.SAIL_FRAME : AllBlocks.DYED_SAILS.get(color)).getDefaultState()
.with(FACING, state.get(FACING));
// Dye the block itself

View file

@ -0,0 +1,33 @@
package com.simibubi.create.foundation.block;
import java.util.Arrays;
import java.util.function.Function;
import com.tterrag.registrate.util.entry.BlockEntry;
import net.minecraft.block.Block;
import net.minecraft.item.DyeColor;
public class DyedBlockList<T extends Block> {
private static final int COLOR_AMOUNT = DyeColor.values().length;
private final BlockEntry<?>[] values = new BlockEntry<?>[COLOR_AMOUNT];
public DyedBlockList(Function<DyeColor, BlockEntry<? extends T>> filler) {
for (DyeColor color : DyeColor.values()) {
values[color.ordinal()] = filler.apply(color);
}
}
@SuppressWarnings("unchecked")
public BlockEntry<T> get(DyeColor color) {
return (BlockEntry<T>) values[color.ordinal()];
}
@SuppressWarnings("unchecked")
public BlockEntry<T>[] toArray() {
return (BlockEntry<T>[]) Arrays.copyOf(values, values.length);
}
}

View file

@ -632,7 +632,7 @@ public class BearingScenes {
.withItem(new ItemStack(Items.BLUE_DYE));
scene.overlay.showControls(input, 30);
scene.idle(7);
scene.world.setBlock(util.grid.at(2, 3, 1), AllBlocks.DYED_SAILS[DyeColor.BLUE.ordinal()].getDefaultState()
scene.world.setBlock(util.grid.at(2, 3, 1), AllBlocks.DYED_SAILS.get(DyeColor.BLUE).getDefaultState()
.with(SailBlock.FACING, Direction.WEST), false);
scene.idle(10);
scene.overlay.showText(40)
@ -645,7 +645,7 @@ public class BearingScenes {
scene.overlay.showControls(input, 30);
scene.idle(7);
scene.world.replaceBlocks(util.select.fromTo(2, 2, 1, 2, 4, 1),
AllBlocks.DYED_SAILS[DyeColor.BLUE.ordinal()].getDefaultState()
AllBlocks.DYED_SAILS.get(DyeColor.BLUE).getDefaultState()
.with(SailBlock.FACING, Direction.WEST),
false);

View file

@ -26,6 +26,7 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.FurnaceBlock;
import net.minecraft.block.RedstoneWireBlock;
import net.minecraft.item.DyeColor;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particles.ParticleTypes;
@ -631,7 +632,7 @@ public class KineticsScenes {
scene.overlay.showControls(new InputWindowElement(centerOf, Pointing.DOWN).rightClick()
.withItem(new ItemStack(Items.BLUE_DYE)), 40);
scene.idle(7);
scene.world.modifyBlock(util.grid.at(2, 2, 2), s -> AllBlocks.DYED_VALVE_HANDLES[11].getDefaultState()
scene.world.modifyBlock(util.grid.at(2, 2, 2), s -> AllBlocks.DYED_VALVE_HANDLES.get(DyeColor.BLUE).getDefaultState()
.with(ValveHandleBlock.FACING, Direction.UP), true);
scene.idle(10);
scene.overlay.showText(70)

View file

@ -5,6 +5,7 @@ import com.simibubi.create.AllItems;
import com.simibubi.create.foundation.ponder.PonderRegistry;
import net.minecraft.block.Blocks;
import net.minecraft.item.DyeColor;
public class PonderIndex {
@ -62,7 +63,7 @@ public class PonderIndex {
PonderRegistry.addStoryBoard(AllBlocks.COPPER_VALVE_HANDLE, "valve_handle", KineticsScenes::valveHandle,
PonderTag.KINETIC_SOURCES);
PonderRegistry.forComponents(AllBlocks.DYED_VALVE_HANDLES)
PonderRegistry.forComponents(AllBlocks.DYED_VALVE_HANDLES.toArray())
.addStoryBoard("valve_handle", KineticsScenes::valveHandle);
PonderRegistry.addStoryBoard(AllBlocks.ENCASED_CHAIN_DRIVE, "chain_drive/relay",
@ -408,7 +409,7 @@ public class PonderIndex {
.add(AllBlocks.MECHANICAL_BEARING)
.add(AllBlocks.ANDESITE_FUNNEL)
.add(AllBlocks.BRASS_FUNNEL)
.add(AllBlocks.SEATS[0])
.add(AllBlocks.SEATS.get(DyeColor.WHITE))
.add(AllBlocks.REDSTONE_CONTACT)
.add(Blocks.BELL)
.add(Blocks.DISPENSER)