From 3a1a707eff3d91107229ed994b5236919cabe972 Mon Sep 17 00:00:00 2001 From: Justin Parsell Date: Mon, 29 Mar 2021 22:02:24 -0400 Subject: [PATCH] introduction of new registry handler --- src/main/java/net/parsell/cherry/Cherry.java | 7 + .../net/parsell/cherry/core/CherryBlocks.java | 143 ++++++++---------- .../net/parsell/cherry/core/CherryItems.java | 13 +- .../parsell/cherry/core/CherryRegistry.java | 75 +++++++++ 4 files changed, 152 insertions(+), 86 deletions(-) create mode 100644 src/main/java/net/parsell/cherry/core/CherryRegistry.java diff --git a/src/main/java/net/parsell/cherry/Cherry.java b/src/main/java/net/parsell/cherry/Cherry.java index c3159f6..b6a51b4 100644 --- a/src/main/java/net/parsell/cherry/Cherry.java +++ b/src/main/java/net/parsell/cherry/Cherry.java @@ -4,8 +4,10 @@ import net.fabricmc.api.ModInitializer; import net.parsell.cherry.core.CherryBlocks; import net.parsell.cherry.core.CherryFeatures; import net.parsell.cherry.core.CherryItems; +import net.parsell.cherry.core.CherryRegistry; public class Cherry implements ModInitializer { + public static final String MODID = "Cherry"; @Override public void onInitialize() { @@ -14,8 +16,13 @@ public class Cherry implements ModInitializer { // Proceed with mild caution. System.out.println("Cherry Initilizating..."); + CherryRegistry.init(); + CherryBlocks.init(); CherryItems.init(); CherryFeatures.init(); + + CherryRegistry.finish(); + System.out.println("Cherry finished successfully!"); } } diff --git a/src/main/java/net/parsell/cherry/core/CherryBlocks.java b/src/main/java/net/parsell/cherry/core/CherryBlocks.java index 4076bbb..b14252d 100644 --- a/src/main/java/net/parsell/cherry/core/CherryBlocks.java +++ b/src/main/java/net/parsell/cherry/core/CherryBlocks.java @@ -1,11 +1,6 @@ package net.parsell.cherry.core; -import com.google.common.collect.ImmutableMap.Builder; - -import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap; -import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; -import net.fabricmc.fabric.api.registry.FlammableBlockRegistry; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; @@ -18,146 +13,128 @@ import net.minecraft.block.SignBlock; import net.minecraft.block.SlabBlock; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.SignBlockEntity; -import net.minecraft.client.render.RenderLayer; -import net.minecraft.util.Identifier; import net.minecraft.util.SignType; -import net.minecraft.util.registry.Registry; import net.parsell.cherry.common.blocks.CherryButtonBlock; import net.parsell.cherry.common.blocks.CherryDoorBlock; import net.parsell.cherry.common.blocks.CherryPressurePlate; import net.parsell.cherry.common.blocks.CherryStairsBlock; import net.parsell.cherry.common.blocks.CherryTrapdoorBlock; -import net.parsell.cherry.mixin.AxeItemAccessor; public class CherryBlocks { - private static Builder STRIPPABLE_BLOCKS = new Builder(); - - public static final Block CHERRYLOGSTRIPPED = registerPillar("stripped_cherry_log", FabricBlockSettings.copyOf(Blocks.STRIPPED_OAK_LOG), 5, 5); - public static final Block CHERRYLOG = registerPillar("cherry_log", FabricBlockSettings.copyOf(Blocks.OAK_LOG), 5, 5, CHERRYLOGSTRIPPED); - public static final Block CHERRYWOOD = registerPillar("cherry_wood", FabricBlockSettings.copyOf(Blocks.OAK_WOOD), 5, 5); - public static final Block CHERRYWOODSTRIPPED = registerPillar("stripped_cherry_wood", FabricBlockSettings.copyOf(Blocks.STRIPPED_OAK_WOOD), 5, 5); - public static final Block CHERRYLEAVES = registerLeaves("cherry_leaves", FabricBlockSettings.copyOf(Blocks.OAK_LEAVES), 30, 60, 6649929); - public static final Block CHERRYPLANKS = registerBlock("cherry_planks", FabricBlockSettings.copyOf(Blocks.OAK_PLANKS), 5, 20); - public static final Block CHERRYFENCE = registerFence("cherry_fence", FabricBlockSettings.copyOf(Blocks.OAK_FENCE), 5, 20); // TODO: tags (as required) - public static final Block CHERRYFENCEGATE = registerFenceGate("cherry_fence_gate", FabricBlockSettings.copyOf(Blocks.OAK_FENCE_GATE), 5, 20); // TODO: recipe, loot_tables, tags (as required) - public static final Block CHERRYDOOR = registerDoor("cherry_door", FabricBlockSettings.copyOf(Blocks.OAK_DOOR), 5, 20); // TODO: recipe, loot_tables, tags (as required) - public static final Block CHERRYTRAPDOOR = registerTrapDoor("cherry_trapdoor", FabricBlockSettings.copyOf(Blocks.OAK_TRAPDOOR), 5, 20); //TODO: recipe, loot_tables, tags (as required) - public static final Block CHERRYSLAB = registerSlab("cherry_slab", FabricBlockSettings.copyOf(Blocks.OAK_SLAB), 5, 20); // TODO: recipe, loot tables, tags (as required) - public static final Block CHERRYSTAIRS = registerStair("cherry_stairs", CHERRYPLANKS.getDefaultState(), FabricBlockSettings.copyOf(Blocks.OAK_STAIRS), 5, 20); //TODO: recipe, loot tables, tags (as required) - public static final Block CHERRYBUTTON = registerButton("cherry_button", FabricBlockSettings.copyOf(Blocks.OAK_BUTTON), 5, 20); //TODO: recipe, loot tables, tags (as required) - public static final Block CHERRYPRESSUREPLATE = registerPressurePlate("cherry_pressure_plate", PressurePlateBlock.ActivationRule.EVERYTHING, FabricBlockSettings.copyOf(Blocks.OAK_PRESSURE_PLATE), 5, 20); //TODO: recipe, loot tables, tags (as required) - public static final Block CHERRYSIGN = registerSign("cherry_sign", SignType.OAK, FabricBlockSettings.copyOf(Blocks.OAK_SIGN), 5, 20); // TODO: recipe, loot tables + public static final Block CHERRYLOGSTRIPPED = createPillar("stripped_cherry_log", FabricBlockSettings.copyOf(Blocks.STRIPPED_OAK_LOG), 5, 5); + public static final Block CHERRYLOG = createPillar("cherry_log", FabricBlockSettings.copyOf(Blocks.OAK_LOG), 5, 5, CHERRYLOGSTRIPPED); + public static final Block CHERRYWOOD = createPillar("cherry_wood", FabricBlockSettings.copyOf(Blocks.OAK_WOOD), 5, 5); + public static final Block CHERRYWOODSTRIPPED = createPillar("stripped_cherry_wood", FabricBlockSettings.copyOf(Blocks.STRIPPED_OAK_WOOD), 5, 5); + public static final Block CHERRYLEAVES = createLeaves("cherry_leaves", FabricBlockSettings.copyOf(Blocks.OAK_LEAVES), 30, 60, 6649929); + public static final Block CHERRYPLANKS = createBlock("cherry_planks", FabricBlockSettings.copyOf(Blocks.OAK_PLANKS), 5, 20); + public static final Block CHERRYFENCE = createFence("cherry_fence", FabricBlockSettings.copyOf(Blocks.OAK_FENCE), 5, 20); // TODO: tags (as required) + public static final Block CHERRYFENCEGATE = createFenceGate("cherry_fence_gate", FabricBlockSettings.copyOf(Blocks.OAK_FENCE_GATE), 5, 20); // TODO: recipe, loot_tables, tags (as required) + public static final Block CHERRYDOOR = createDoor("cherry_door", FabricBlockSettings.copyOf(Blocks.OAK_DOOR), 5, 20); // TODO: recipe, loot_tables, tags (as required) + public static final Block CHERRYTRAPDOOR = createTrapDoor("cherry_trapdoor", FabricBlockSettings.copyOf(Blocks.OAK_TRAPDOOR), 5, 20); //TODO: recipe, loot_tables, tags (as required) + public static final Block CHERRYSLAB = createSlab("cherry_slab", FabricBlockSettings.copyOf(Blocks.OAK_SLAB), 5, 20); // TODO: recipe, loot tables, tags (as required) + public static final Block CHERRYSTAIRS = createStair("cherry_stairs", CHERRYPLANKS.getDefaultState(), FabricBlockSettings.copyOf(Blocks.OAK_STAIRS), 5, 20); //TODO: recipe, loot tables, tags (as required) + public static final Block CHERRYBUTTON = createButton("cherry_button", FabricBlockSettings.copyOf(Blocks.OAK_BUTTON), 5, 20); //TODO: recipe, loot tables, tags (as required) + public static final Block CHERRYPRESSUREPLATE = createPressurePlate("cherry_pressure_plate", PressurePlateBlock.ActivationRule.EVERYTHING, FabricBlockSettings.copyOf(Blocks.OAK_PRESSURE_PLATE), 5, 20); //TODO: recipe, loot tables, tags (as required) + public static final SignType CHERRYSIGN_TYPE = CherryRegistry.register("cherry"); + public static final Block CHERRYSIGN = createSign("cherry_sign", CHERRYSIGN_TYPE, FabricBlockSettings.copyOf(Blocks.OAK_SIGN), 5, 20); // TODO: recipe, loot tables public static final BlockEntityType CHERRYSIGN_ENTITY = BlockEntityType.Builder.create(SignBlockEntity::new, CHERRYSIGN).build(null); - private static Block registerBlock(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { + private static Block createBlock(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { Block block = new Block(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerSlab(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { + private static Block createSlab(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { Block block = new SlabBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerStair(String blockID, BlockState baseBlockState, FabricBlockSettings settings, int burnChance, int burnSpread) { + private static Block createStair(String blockID, BlockState baseBlockState, FabricBlockSettings settings, int burnChance, int burnSpread) { Block block = new CherryStairsBlock(baseBlockState, settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerButton(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { + private static Block createButton(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { Block block = new CherryButtonBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerPressurePlate(String blockID, PressurePlateBlock.ActivationRule rule, FabricBlockSettings settings, int burnChance, int burnSpread) { + private static Block createPressurePlate(String blockID, PressurePlateBlock.ActivationRule rule, FabricBlockSettings settings, int burnChance, int burnSpread) { Block block = new CherryPressurePlate(rule, settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerSign(String blockID, SignType type, FabricBlockSettings settings, int burnChance, int burnSpread) { + private static Block createSign(String blockID, SignType type, FabricBlockSettings settings, int burnChance, int burnSpread) { Block block = new SignBlock(settings, type); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerFence(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { + private static Block createFence(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { Block block = new FenceBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerFenceGate(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { + private static Block createFenceGate(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread) { Block block = new FenceGateBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerPillar(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread){ + private static Block createPillar(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread){ Block block = new PillarBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); return block; } - private static Block registerPillar(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread, Block strippedVariant){ + private static Block createPillar(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread, Block strippedVariant){ Block block = new PillarBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); - STRIPPABLE_BLOCKS.put(block, strippedVariant); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); + CherryRegistry.addStrippable(block, strippedVariant); return block; } - private static Block registerLeaves(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread, int color){ + private static Block createLeaves(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread, int color){ Block block = new LeavesBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); - ColorProviderRegistry.BLOCK.register((state, world, pos, tintIndex) -> { - return color; - }, block); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); + CherryRegistry.registerColor(block, color); return block; } - private static Block registerDoor(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread){ + private static Block createDoor(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread){ Block block = new CherryDoorBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); - BlockRenderLayerMap.INSTANCE.putBlock(block, RenderLayer.getCutout()); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); + CherryRegistry.registerTransparentLayer(block); return block; } - private static Block registerTrapDoor(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread){ + private static Block createTrapDoor(String blockID, FabricBlockSettings settings, int burnChance, int burnSpread){ Block block = new CherryTrapdoorBlock(settings); - Registry.register(Registry.BLOCK, new Identifier("cherry", blockID), block); - FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); - BlockRenderLayerMap.INSTANCE.putBlock(block, RenderLayer.getCutout()); + CherryRegistry.register(block, blockID); + CherryRegistry.registerBurnable(block, burnChance, burnSpread); + CherryRegistry.registerTransparentLayer(block); return block; } public static void init(){ - // Ensure not to overwrite on previous strippable logs - AxeItemAccessor.getStrippedLogs().forEach((log, strippedLog) -> { - STRIPPABLE_BLOCKS.put(log, strippedLog); - }); - - // Register unique logs - AxeItemAccessor.setStrippedLogs(STRIPPABLE_BLOCKS.build()); - System.out.println(CHERRYSIGN_ENTITY.toString()); - if(!(CHERRYSIGN_ENTITY == null)) - Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier("cherry", "cherry_sign"), CHERRYSIGN_ENTITY); - else - System.out.println("cherry:cherry_sign entity is null"); + CherryRegistry.register(CHERRYSIGN_ENTITY, "cherry_sign"); } } \ No newline at end of file diff --git a/src/main/java/net/parsell/cherry/core/CherryItems.java b/src/main/java/net/parsell/cherry/core/CherryItems.java index ee3e158..e702185 100644 --- a/src/main/java/net/parsell/cherry/core/CherryItems.java +++ b/src/main/java/net/parsell/cherry/core/CherryItems.java @@ -10,13 +10,14 @@ import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; public class CherryItems { + public static final BlockItem CherryPlanks; + private static void registerBlockItems(){ registerBlockItem("cherry_log", CherryBlocks.CHERRYLOG, ItemGroup.BUILDING_BLOCKS); registerBlockItem("cherry_wood", CherryBlocks.CHERRYWOOD, ItemGroup.BUILDING_BLOCKS); registerBlockItem("stripped_cherry_log", CherryBlocks.CHERRYLOGSTRIPPED, ItemGroup.BUILDING_BLOCKS); registerBlockItem("stripped_cherry_wood", CherryBlocks.CHERRYWOODSTRIPPED, ItemGroup.BUILDING_BLOCKS); registerBlockItem("cherry_leaves", CherryBlocks.CHERRYLEAVES, ItemGroup.BUILDING_BLOCKS, 6649929); - registerBlockItem("cherry_planks", CherryBlocks.CHERRYPLANKS, ItemGroup.BUILDING_BLOCKS); registerBlockItem("cherry_fence", CherryBlocks.CHERRYFENCE, ItemGroup.BUILDING_BLOCKS); registerBlockItem("cherry_fence_gate", CherryBlocks.CHERRYFENCEGATE, ItemGroup.BUILDING_BLOCKS); registerBlockItem("cherry_door", CherryBlocks.CHERRYDOOR, ItemGroup.BUILDING_BLOCKS); @@ -29,8 +30,10 @@ public class CherryItems { } // Register a standard blockItem - private static void registerBlockItem(String itemID, Block block, ItemGroup group){ - Registry.register(Registry.ITEM, new Identifier("cherry", itemID), new BlockItem(block, new FabricItemSettings().group(group))); + private static BlockItem registerBlockItem(String itemID, Block block, ItemGroup group){ + BlockItem blockItem = new BlockItem(block, new FabricItemSettings().group(group)); + Registry.register(Registry.ITEM, new Identifier("cherry", itemID), blockItem); + return blockItem; } // Register a blockItem that require in-game color (leaves) @@ -50,4 +53,8 @@ public class CherryItems { public static void init(){ registerBlockItems(); } + + static{ + CherryPlanks = registerBlockItem("cherry_planks", CherryBlocks.CHERRYPLANKS, ItemGroup.BUILDING_BLOCKS); + } } diff --git a/src/main/java/net/parsell/cherry/core/CherryRegistry.java b/src/main/java/net/parsell/cherry/core/CherryRegistry.java new file mode 100644 index 0000000..df3a1be --- /dev/null +++ b/src/main/java/net/parsell/cherry/core/CherryRegistry.java @@ -0,0 +1,75 @@ +package net.parsell.cherry.core; + +import net.parsell.cherry.Cherry; +import net.parsell.cherry.common.util.CherrySignType; +import net.parsell.cherry.mixin.*; + +import com.google.common.collect.ImmutableMap.Builder; + +import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap; +import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder; +import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry; +import net.fabricmc.fabric.api.registry.FlammableBlockRegistry; +import net.minecraft.block.Block; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; +import net.minecraft.client.render.RenderLayer; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemGroup; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; +import net.minecraft.util.SignType; +import net.minecraft.util.registry.Registry; + +public class CherryRegistry { + private static Builder STRIPPABLE_BLOCKS = new Builder(); + public static final ItemGroup CHERRY_GROUP = FabricItemGroupBuilder + .create(new Identifier(Cherry.MODID, "General")) + .icon(() -> new ItemStack(CherryItems.CherryPlanks)) + .build(); + + public static void init(){ + AxeItemAccessor.getStrippedLogs().forEach((log, strippedLog) -> { + STRIPPABLE_BLOCKS.put(log, strippedLog); + }); + } + + public static void finish(){ + AxeItemAccessor.setStrippedLogs(STRIPPABLE_BLOCKS.build()); + } + + public static void register(Block block, String id){ + Registry.register(Registry.BLOCK, new Identifier(Cherry.MODID, id), block); + } + + public static void register(BlockItem item, String id){ + Registry.register(Registry.ITEM, new Identifier(Cherry.MODID, id), item); + } + + public static void register(BlockEntityType blockEntityType, String id){ + Registry.register(Registry.BLOCK_ENTITY_TYPE, new Identifier(Cherry.MODID, id), blockEntityType); + } + + // This registry is unique since I need to save the SignType, and the only way to do that is to register it. So.. + public static SignType register(String type) throws IllegalAccessException{ + return SignTypeInvoker.register(new CherrySignType(type)); + } + + public static void registerBurnable(Block block, int burnChance, int burnSpread){ + FlammableBlockRegistry.getDefaultInstance().add(block, burnChance, burnSpread); + } + + public static void registerColor(Block block, int color){ + ColorProviderRegistry.BLOCK.register((state, world, pos, tintIndex) -> { + return color; + }, block); + } + + public static void registerTransparentLayer(Block block){ + BlockRenderLayerMap.INSTANCE.putBlock(block, RenderLayer.getCutout()); + } + + public static void addStrippable(Block block, Block stripped){ + STRIPPABLE_BLOCKS.put(block, stripped); + } +}