Skip to content

Commit

Permalink
Rewrite block registration in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Jul 30, 2024
1 parent 622d17a commit 5e1936b
Show file tree
Hide file tree
Showing 31 changed files with 716 additions and 651 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public final class AdornBlockEntities {
public static final Registered<BlockEntityType<KitchenSinkBlockEntity>> KITCHEN_SINK =
register("kitchen_sink", PlatformBridges.Companion.getBlockEntities()::createKitchenSink, KitchenSinkBlock.class);
public static final Registered<BlockEntityType<TradingStationBlockEntity>> TRADING_STATION =
register("trading_station", TradingStationBlockEntity::new, AdornBlocks.INSTANCE::getTRADING_STATION);
register("trading_station", TradingStationBlockEntity::new, AdornBlocks.TRADING_STATION);
public static final Registered<BlockEntityType<BrewerBlockEntity>> BREWER =
register("brewer", PlatformBridges.Companion.getBlockEntities()::createBrewer, AdornBlocks.INSTANCE::getBREWER);
register("brewer", PlatformBridges.Companion.getBlockEntities()::createBrewer, AdornBlocks.BREWER);

private static <E extends BlockEntity> Registered<BlockEntityType<E>> register(String name, BlockEntityFactory<E> factory, Supplier<? extends Block> block) {
return BLOCK_ENTITIES.register(name, () -> BlockEntityType.Builder.create(factory, block.get()).build(null));
Expand Down
181 changes: 181 additions & 0 deletions common/src/main/java/juuxel/adorn/block/AdornBlocks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package juuxel.adorn.block;

import juuxel.adorn.block.variant.BlockVariant;
import juuxel.adorn.item.TradingStationItem;
import juuxel.adorn.lib.AdornSounds;
import juuxel.adorn.lib.registry.Registered;
import juuxel.adorn.lib.registry.Registrar;
import juuxel.adorn.lib.registry.RegistrarFactory;
import juuxel.adorn.lib.registry.RegistryHelper;
import juuxel.adorn.platform.PlatformBridges;
import juuxel.adorn.util.ExtensionsKt;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.block.MapColor;
import net.minecraft.block.Oxidizable;
import net.minecraft.block.TorchBlock;
import net.minecraft.block.WallTorchBlock;
import net.minecraft.item.Item;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.util.DyeColor;

import java.util.Map;

public final class AdornBlocks {
public static final Registrar<Block> BLOCKS = RegistrarFactory.get().create(RegistryKeys.BLOCK);
public static final Registrar<Item> ITEMS = RegistrarFactory.get().create(RegistryKeys.ITEM);
private static final RegistryHelper HELPER = new RegistryHelper(BLOCKS, ITEMS);

public static final Registered<Map<DyeColor, SofaBlock>> SOFAS = ExtensionsKt.associateLazily(
DyeColor.values(),
color -> HELPER.registerBlock(
color.asString() + "_sofa",
() -> PlatformBridges.Companion.getBlockFactory().createSofa(BlockVariant.wool(color))
)
);

public static final Registered<Block> BRICK_CHIMNEY = HELPER.registerBlock("brick_chimney",
() -> new ChimneyBlock(AbstractChimneyBlock.createBlockSettings(MapColor.RED)));
public static final Registered<Block> STONE_BRICK_CHIMNEY = HELPER.registerBlock("stone_brick_chimney",
() -> new ChimneyBlock(AbstractChimneyBlock.createBlockSettings(MapColor.STONE_GRAY)));
public static final Registered<Block> NETHER_BRICK_CHIMNEY = HELPER.registerBlock("nether_brick_chimney",
() -> new ChimneyBlock(AbstractChimneyBlock.createBlockSettings(MapColor.DARK_RED)));
public static final Registered<Block> RED_NETHER_BRICK_CHIMNEY = HELPER.registerBlock("red_nether_brick_chimney",
() -> new ChimneyBlock(AbstractChimneyBlock.createBlockSettings(MapColor.DARK_RED)));
public static final Registered<Block> COBBLESTONE_CHIMNEY = HELPER.registerBlock("cobblestone_chimney",
() -> new ChimneyBlock(AbstractChimneyBlock.createBlockSettings(MapColor.STONE_GRAY)));
public static final Registered<Block> PRISMARINE_CHIMNEY = HELPER.registerBlock("prismarine_chimney",
() -> new PrismarineChimneyBlock(AbstractChimneyBlock.createBlockSettings(MapColor.CYAN, 1.5f)));
public static final Registered<Block> MAGMATIC_PRISMARINE_CHIMNEY = HELPER.registerBlock("magmatic_prismarine_chimney",
() -> new PrismarineChimneyBlock.WithColumn(true, AbstractChimneyBlock.createBlockSettings(MapColor.CYAN, 1.5f).luminance(state -> 3)));
public static final Registered<Block> SOULFUL_PRISMARINE_CHIMNEY = HELPER.registerBlock("soulful_prismarine_chimney",
() -> new PrismarineChimneyBlock.WithColumn(false, AbstractChimneyBlock.createBlockSettings(MapColor.CYAN, 1.5f)));

public static final Registered<Map<DyeColor, Block>> TABLE_LAMPS = ExtensionsKt.associateLazily(
DyeColor.values(),
color -> HELPER.registerBlock(color.asString() + "_table_lamp", () -> new TableLampBlock(TableLampBlock.createBlockSettings(color)))
);

public static final Registered<Block> TRADING_STATION = HELPER.registerBlock(
"trading_station",
block -> new TradingStationItem(block, new Item.Settings()),
() -> new TradingStationBlock(AbstractBlock.Settings.create().mapColor(MapColor.GREEN).strength(2.5f).sounds(BlockSoundGroup.WOOD))
);

public static final Registered<Block> STONE_TORCH_GROUND = HELPER.registerBlockWithoutItem("stone_torch",
() -> new TorchBlock(
ParticleTypes.FLAME,
AbstractBlock.Settings.copy(Blocks.TORCH)
.sounds(BlockSoundGroup.STONE)
.luminance(state -> 15)
));

public static final Registered<Block> STONE_TORCH_WALL = HELPER.registerBlockWithoutItem("wall_stone_torch",
() -> new WallTorchBlock(
ParticleTypes.FLAME,
AbstractBlock.Settings.copy(STONE_TORCH_GROUND.get()).dropsLike(STONE_TORCH_GROUND.get())
));

public static final Registered<Block> CRATE = HELPER.registerBlock("crate",
() -> new Block(ExtensionsKt.copySettingsSafely(Blocks.OAK_PLANKS)));
public static final Registered<Block> APPLE_CRATE = registerCrate("apple_crate");
public static final Registered<Block> WHEAT_CRATE = registerCrate("wheat_crate");
public static final Registered<Block> CARROT_CRATE = registerCrate("carrot_crate");
public static final Registered<Block> POTATO_CRATE = registerCrate("potato_crate");
public static final Registered<Block> MELON_CRATE = registerCrate("melon_crate");
public static final Registered<Block> WHEAT_SEED_CRATE = registerCrate("wheat_seed_crate");
public static final Registered<Block> MELON_SEED_CRATE = registerCrate("melon_seed_crate");
public static final Registered<Block> PUMPKIN_SEED_CRATE = registerCrate("pumpkin_seed_crate");
public static final Registered<Block> BEETROOT_CRATE = registerCrate("beetroot_crate");
public static final Registered<Block> BEETROOT_SEED_CRATE = registerCrate("beetroot_seed_crate");
public static final Registered<Block> SWEET_BERRY_CRATE = registerCrate("sweet_berry_crate");
public static final Registered<Block> COCOA_BEAN_CRATE = registerCrate("cocoa_bean_crate");
public static final Registered<Block> NETHER_WART_CRATE = registerCrate("nether_wart_crate");
public static final Registered<Block> SUGAR_CANE_CRATE = registerCrate("sugar_cane_crate");
public static final Registered<Block> EGG_CRATE = registerCrate("egg_crate");
public static final Registered<Block> HONEYCOMB_CRATE = registerCrate("honeycomb_crate");
public static final Registered<Block> LIL_TATER_CRATE = registerCrate("lil_tater_crate");

public static final Registered<Block> PICKET_FENCE = HELPER.registerBlock("picket_fence",
() -> new PicketFenceBlock(AbstractBlock.Settings.copy(Blocks.OAK_FENCE).nonOpaque()));
public static final Registered<Block> CHAIN_LINK_FENCE = HELPER.registerBlock("chain_link_fence",
() -> new ChainLinkFenceBlock(
AbstractBlock.Settings.copy(Blocks.IRON_BARS)
.sounds(AdornSounds.CHAIN_LINK_FENCE)
));
public static final Registered<Block> STONE_LADDER = HELPER.registerBlock("stone_ladder",
() -> new StoneLadderBlock(AbstractBlock.Settings.copy(Blocks.STONE).nonOpaque()));
public static final Registered<Block> BREWER = HELPER.registerBlock("brewer",
() -> new BrewerBlock(
AbstractBlock.Settings.create()
.mapColor(MapColor.DEEPSLATE_GRAY)
.solid()
.strength(0.8F)
.requiresTool()
));

public static final Registered<Block> CANDLELIT_LANTERN = HELPER.registerBlock("candlelit_lantern",
() -> new CandlelitLanternBlock(CandlelitLanternBlock.createBlockSettings()));
public static final Registered<Map<DyeColor, Block>> DYED_CANDLELIT_LANTERNS = ExtensionsKt.associateLazily(
DyeColor.values(),
color -> HELPER.registerBlock(
color.asString() + "_candlelit_lantern",
() -> new CandlelitLanternBlock(CandlelitLanternBlock.createBlockSettings())
)
);

public static final Registered<Block> COPPER_PIPE = HELPER.registerBlock("copper_pipe",
() -> new OxidizableCopperPipeBlock(
Oxidizable.OxidationLevel.UNAFFECTED,
AbstractBlock.Settings.create()
.requiresTool()
.strength(3f, 5f)
.sounds(BlockSoundGroup.COPPER)
.mapColor(MapColor.ORANGE)
));
public static final Registered<Block> EXPOSED_COPPER_PIPE = HELPER.registerBlock("exposed_copper_pipe",
() -> new OxidizableCopperPipeBlock(
Oxidizable.OxidationLevel.EXPOSED,
AbstractBlock.Settings.create()
.requiresTool()
.strength(3f, 5f)
.sounds(BlockSoundGroup.COPPER)
.mapColor(MapColor.TERRACOTTA_LIGHT_GRAY)
));
public static final Registered<Block> WEATHERED_COPPER_PIPE = HELPER.registerBlock("weathered_copper_pipe",
() -> new OxidizableCopperPipeBlock(
Oxidizable.OxidationLevel.WEATHERED,
AbstractBlock.Settings.create()
.requiresTool()
.strength(3f, 5f)
.sounds(BlockSoundGroup.COPPER)
.mapColor(MapColor.DARK_AQUA)
));
public static final Registered<Block> OXIDIZED_COPPER_PIPE = HELPER.registerBlock("oxidized_copper_pipe",
() -> new OxidizableCopperPipeBlock(
Oxidizable.OxidationLevel.OXIDIZED,
AbstractBlock.Settings.create()
.requiresTool()
.strength(3f, 5f)
.sounds(BlockSoundGroup.COPPER)
.mapColor(MapColor.TEAL)
));
public static final Registered<Block> WAXED_COPPER_PIPE = HELPER.registerBlock("waxed_copper_pipe",
() -> new CopperPipeBlock(AbstractBlock.Settings.copy(COPPER_PIPE.get())));
public static final Registered<Block> WAXED_EXPOSED_COPPER_PIPE = HELPER.registerBlock("waxed_exposed_copper_pipe",
() -> new CopperPipeBlock(AbstractBlock.Settings.copy(EXPOSED_COPPER_PIPE.get())));
public static final Registered<Block> WAXED_WEATHERED_COPPER_PIPE = HELPER.registerBlock("waxed_weathered_copper_pipe",
() -> new CopperPipeBlock(AbstractBlock.Settings.copy(WEATHERED_COPPER_PIPE.get())));
public static final Registered<Block> WAXED_OXIDIZED_COPPER_PIPE = HELPER.registerBlock("waxed_oxidized_copper_pipe",
() -> new CopperPipeBlock(AbstractBlock.Settings.copy(OXIDIZED_COPPER_PIPE.get())));

public static void init() {
}

private static Registered<Block> registerCrate(String name) {
return HELPER.registerBlock(name, () -> new Item.Settings().recipeRemainder(CRATE.get().asItem()), () -> new Block(ExtensionsKt.copySettingsSafely(CRATE.get())));
}
}
2 changes: 1 addition & 1 deletion common/src/main/java/juuxel/adorn/block/SofaBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public String getDescriptionKey() {
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
var stack = player.getStackInHand(hand);
if (stack.getItem() instanceof DyeItem dye) {
world.setBlockState(pos, AdornBlocks.INSTANCE.getSOFAS().get(dye.getColor()).getStateWithProperties(state));
world.setBlockState(pos, AdornBlocks.SOFAS.get().get(dye.getColor()).getStateWithProperties(state));
world.playSound(player, pos, SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.BLOCKS, 1f, 0.8f);
if (!player.getAbilities().creativeMode) stack.decrement(1);
if (!world.isClient) player.incrementStat(AdornStats.DYE_SOFA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void appendProperties(StateManager.Builder<Block, BlockState> builder)
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
var stack = player.getStackInHand(hand);
if (stack.getItem() instanceof DyeItem dye) {
world.setBlockState(pos, AdornBlocks.INSTANCE.getTABLE_LAMPS().get(dye.getColor()).getStateWithProperties(state));
world.setBlockState(pos, AdornBlocks.TABLE_LAMPS.get().get(dye.getColor()).getStateWithProperties(state));
world.playSound(player, pos, SoundEvents.BLOCK_WOOL_PLACE, SoundCategory.BLOCKS, 1f, 0.8f);
if (!player.getAbilities().creativeMode) stack.decrement(1);
if (!world.isClient) player.incrementStat(AdornStats.DYE_TABLE_LAMP);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package juuxel.adorn.block.variant
package juuxel.adorn.block.variant;

enum class BlockKind {
public enum BlockKind {
CHAIR,
TABLE,
DRAWER,
Expand Down
127 changes: 127 additions & 0 deletions common/src/main/java/juuxel/adorn/block/variant/BlockVariant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package juuxel.adorn.block.variant;

import juuxel.adorn.util.ExtensionsKt;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.util.DyeColor;
import net.minecraft.util.Identifier;

import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import java.util.stream.Collectors;

public interface BlockVariant {
char MOD_ID_SEPARATOR = '/';

Map<DyeColor, BlockVariant> WOOLS = Collections.unmodifiableMap(
new EnumMap<DyeColor, BlockVariant>(
Arrays.stream(DyeColor.values())
.map(color -> Map.entry(color, variant(color.asString(), Blocks.WHITE_WOOL)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
)
);

BlockVariant IRON = variant("iron", Blocks.IRON_BARS);
BlockVariant OAK = variant("oak", Blocks.OAK_PLANKS);
BlockVariant SPRUCE = variant("spruce", Blocks.SPRUCE_PLANKS);
BlockVariant BIRCH = variant("birch", Blocks.BIRCH_PLANKS);
BlockVariant JUNGLE = variant("jungle", Blocks.JUNGLE_PLANKS);
BlockVariant ACACIA = variant("acacia", Blocks.ACACIA_PLANKS);
BlockVariant DARK_OAK = variant("dark_oak", Blocks.DARK_OAK_PLANKS);
BlockVariant MANGROVE = variant("mangrove", Blocks.MANGROVE_PLANKS);
BlockVariant CHERRY = variant("cherry", Blocks.CHERRY_PLANKS);
BlockVariant BAMBOO = variant("bamboo", Blocks.BAMBOO_PLANKS);
BlockVariant CRIMSON = variant("crimson", Blocks.CRIMSON_PLANKS);
BlockVariant WARPED = variant("warped", Blocks.WARPED_PLANKS);
BlockVariant STONE = variant("stone", Blocks.STONE);
BlockVariant COBBLESTONE = variant("cobblestone", Blocks.COBBLESTONE);
BlockVariant SANDSTONE = variant("sandstone", Blocks.SANDSTONE);
BlockVariant DIORITE = variant("diorite", Blocks.DIORITE);
BlockVariant ANDESITE = variant("andesite", Blocks.ANDESITE);
BlockVariant GRANITE = variant("granite", Blocks.GRANITE);
BlockVariant BRICK = variant("brick", Blocks.BRICKS);
BlockVariant STONE_BRICK = variant("stone_brick", Blocks.STONE_BRICKS);
BlockVariant RED_SANDSTONE = variant("red_sandstone", Blocks.RED_SANDSTONE);
BlockVariant NETHER_BRICK = variant("nether_brick", Blocks.NETHER_BRICKS);
BlockVariant BASALT = variant("basalt", Blocks.BASALT);
BlockVariant BLACKSTONE = variant("blackstone", Blocks.BLACKSTONE);
BlockVariant RED_NETHER_BRICK = variant("red_nether_brick", Blocks.RED_NETHER_BRICKS);
BlockVariant PRISMARINE = variant("prismarine", Blocks.PRISMARINE);
BlockVariant QUARTZ = variant("quartz", Blocks.QUARTZ_BLOCK);
BlockVariant END_STONE_BRICK = variant("end_stone_brick", Blocks.END_STONE_BRICKS);
BlockVariant PURPUR = variant("purpur", Blocks.PURPUR_BLOCK);
BlockVariant POLISHED_BLACKSTONE = variant("polished_blackstone", Blocks.POLISHED_BLACKSTONE);
BlockVariant POLISHED_BLACKSTONE_BRICK = variant("polished_blackstone_brick", Blocks.POLISHED_BLACKSTONE_BRICKS);
BlockVariant POLISHED_DIORITE = variant("polished_diorite", Blocks.POLISHED_DIORITE);
BlockVariant POLISHED_ANDESITE = variant("polished_andesite", Blocks.POLISHED_ANDESITE);
BlockVariant POLISHED_GRANITE = variant("polished_granite", Blocks.POLISHED_GRANITE);
BlockVariant PRISMARINE_BRICK = variant("prismarine_brick", Blocks.PRISMARINE_BRICKS);
BlockVariant DARK_PRISMARINE = variant("dark_prismarine", Blocks.DARK_PRISMARINE);
BlockVariant CUT_SANDSTONE = variant("cut_sandstone", Blocks.CUT_SANDSTONE);
BlockVariant SMOOTH_SANDSTONE = variant("smooth_sandstone", Blocks.SMOOTH_SANDSTONE);
BlockVariant CUT_RED_SANDSTONE = variant("cut_red_sandstone", Blocks.CUT_RED_SANDSTONE);
BlockVariant SMOOTH_RED_SANDSTONE = variant("smooth_red_sandstone", Blocks.SMOOTH_RED_SANDSTONE);
BlockVariant SMOOTH_STONE = variant("smooth_stone", Blocks.SMOOTH_STONE);
BlockVariant MOSSY_COBBLESTONE = variant("mossy_cobblestone", Blocks.MOSSY_COBBLESTONE);
BlockVariant MOSSY_STONE_BRICK = variant("mossy_stone_brick", Blocks.MOSSY_STONE_BRICKS);
BlockVariant DEEPSLATE = variant("deepslate", Blocks.DEEPSLATE);
BlockVariant COBBLED_DEEPSLATE = variant("cobbled_deepslate", Blocks.COBBLED_DEEPSLATE);
BlockVariant POLISHED_DEEPSLATE = variant("polished_deepslate", Blocks.POLISHED_DEEPSLATE);
BlockVariant DEEPSLATE_BRICK = variant("deepslate_brick", Blocks.DEEPSLATE_BRICKS);
BlockVariant DEEPSLATE_TILE = variant("deepslate_tile", Blocks.DEEPSLATE_TILES);

/**
* The name of this variant. Must be a valid identifier path.
*/
String name();

default Identifier nameAsIdentifier() {
var name = this.name();
var separatorIndex = name.indexOf(MOD_ID_SEPARATOR);
if (separatorIndex >= 0) {
return new Identifier(name.substring(0, separatorIndex), name.substring(separatorIndex + 1));
} else {
return new Identifier(Identifier.DEFAULT_NAMESPACE, name);
}
}

/**
* Creates a <em>new</em> {@code AbstractBlock.Settings}.
*/
AbstractBlock.Settings createSettings();

static BlockVariant variant(String name, Block base) {
return new BlockVariant() {
@Override
public String name() {
return name;
}

@Override
public AbstractBlock.Settings createSettings() {
return ExtensionsKt.copySettingsSafely(base);
}
};
}

static BlockVariant wool(DyeColor color) {
return WOOLS.get(color);
}

record Wood(String name) implements BlockVariant {
@Override
public AbstractBlock.Settings createSettings() {
return ExtensionsKt.copySettingsSafely(Blocks.OAK_PLANKS);
}
}

record Stone(String name) implements BlockVariant {
@Override
public AbstractBlock.Settings createSettings() {
return ExtensionsKt.copySettingsSafely(Blocks.COBBLESTONE);
}
}
}
Loading

0 comments on commit 5e1936b

Please sign in to comment.