|
| 1 | +package com.bokmcdok.butterflies.client.model.generators; |
| 2 | + |
| 3 | +import com.bokmcdok.butterflies.ButterfliesMod; |
| 4 | +import com.bokmcdok.butterflies.registries.BlockRegistry; |
| 5 | +import com.bokmcdok.butterflies.world.block.ButterflyOrigamiBlock; |
| 6 | +import com.bokmcdok.butterflies.world.block.FlowerCropBlock; |
| 7 | +import net.minecraft.core.FrontAndTop; |
| 8 | +import net.minecraft.data.PackOutput; |
| 9 | +import net.minecraft.resources.ResourceLocation; |
| 10 | +import net.minecraft.world.level.block.Block; |
| 11 | +import net.minecraft.world.level.block.CropBlock; |
| 12 | +import net.minecraft.world.level.block.state.BlockState; |
| 13 | +import net.minecraftforge.client.model.generators.BlockStateProvider; |
| 14 | +import net.minecraftforge.client.model.generators.ConfiguredModel; |
| 15 | +import net.minecraftforge.client.model.generators.ModelFile; |
| 16 | +import net.minecraftforge.common.data.ExistingFileHelper; |
| 17 | +import net.minecraftforge.registries.RegistryObject; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +/** |
| 23 | + * Registers block states. |
| 24 | + */ |
| 25 | +public class ModBlockStateProvider extends BlockStateProvider { |
| 26 | + |
| 27 | + /** |
| 28 | + * Construction. |
| 29 | + * @param output The pack to output data to. |
| 30 | + * @param existingFileHelper Helper to access existing files. |
| 31 | + */ |
| 32 | + public ModBlockStateProvider(PackOutput output, |
| 33 | + ExistingFileHelper existingFileHelper) { |
| 34 | + super(output, ButterfliesMod.MOD_ID, existingFileHelper); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Entry point. |
| 39 | + */ |
| 40 | + @Override |
| 41 | + protected void registerStatesAndModels() { |
| 42 | + |
| 43 | + // Bottled items |
| 44 | + for (int i = 0; i < BlockRegistry.BOTTLED_BUTTERFLY_BLOCKS.size(); i++) { |
| 45 | + registerBottledButterfly(i); |
| 46 | + registerBottledCaterpillar(i); |
| 47 | + } |
| 48 | + |
| 49 | + // Flower buds |
| 50 | + registerFlowerBud(BlockRegistry.ALLIUM_BUD); |
| 51 | + registerFlowerBud(BlockRegistry.AZURE_BLUET_BUD); |
| 52 | + registerFlowerBud(BlockRegistry.BLUE_ORCHID_BUD); |
| 53 | + registerFlowerBud(BlockRegistry.CORNFLOWER_BUD); |
| 54 | + registerFlowerBud(BlockRegistry.DANDELION_BUD); |
| 55 | + registerFlowerBud(BlockRegistry.LILY_OF_THE_VALLEY_BUD); |
| 56 | + registerFlowerBud(BlockRegistry.ORANGE_TULIP_BUD); |
| 57 | + registerFlowerBud(BlockRegistry.OXEYE_DAISY_BUD); |
| 58 | + registerFlowerBud(BlockRegistry.PINK_TULIP_BUD); |
| 59 | + registerFlowerBud(BlockRegistry.POPPY_BUD); |
| 60 | + registerFlowerBud(BlockRegistry.RED_TULIP_BUD); |
| 61 | + registerFlowerBud(BlockRegistry.WHITE_TULIP_BUD); |
| 62 | + registerFlowerBud(BlockRegistry.WITHER_ROSE_BUD); |
| 63 | + |
| 64 | + // Functional Blocks |
| 65 | + simpleBlockWithItem(BlockRegistry.BUTTERFLY_FEEDER.get(), models().getExistingFile(BlockRegistry.BUTTERFLY_FEEDER.getId())); |
| 66 | + simpleBlockWithItem(BlockRegistry.BUTTERFLY_MICROSCOPE.get(), models().getExistingFile(BlockRegistry.BUTTERFLY_MICROSCOPE.getId())); |
| 67 | + |
| 68 | + // Origami |
| 69 | + for(RegistryObject<Block> origami : BlockRegistry.BUTTERFLY_ORIGAMI) { |
| 70 | + registerButterflyOrigami(origami); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Registers a bottled butterfly. |
| 76 | + * @param index The butterfly index. |
| 77 | + */ |
| 78 | + private void registerBottledButterfly(int index) { |
| 79 | + registerBottledEntity(index, BlockRegistry.BOTTLED_BUTTERFLY_BLOCKS); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Registers a bottled caterpillar. |
| 84 | + * @param index The butterfly index. |
| 85 | + */ |
| 86 | + private void registerBottledCaterpillar(int index) { |
| 87 | + registerBottledEntity(index, BlockRegistry.BOTTLED_CATERPILLAR_BLOCKS); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Registers a bottled entity. |
| 92 | + * @param index The butterfly index. |
| 93 | + * @param blocks The block list to register from. |
| 94 | + */ |
| 95 | + private void registerBottledEntity(int index, |
| 96 | + List<RegistryObject<Block>> blocks) { |
| 97 | + final ModelFile bottleModel = models().getExistingFile(new ResourceLocation(ButterfliesMod.MOD_ID, "bottle")); |
| 98 | + simpleBlock(blocks.get(index).get(), bottleModel); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Registers a flower bud block state. |
| 103 | + * @param block The block to create a block state for. |
| 104 | + */ |
| 105 | + private void registerFlowerBud(RegistryObject<Block> block) { |
| 106 | + |
| 107 | + if (block.get() instanceof FlowerCropBlock flowerBud) { |
| 108 | + |
| 109 | + // This should never be null. |
| 110 | + assert block.getId() != null; |
| 111 | + |
| 112 | + String textureName = block.getId().getPath().substring(4); |
| 113 | + |
| 114 | + Function<BlockState, ConfiguredModel[]> function = |
| 115 | + state -> generateCropState(state, flowerBud, textureName); |
| 116 | + |
| 117 | + getVariantBuilder(flowerBud).forAllStates(function); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void registerButterflyOrigami(RegistryObject<Block> block) { |
| 122 | + |
| 123 | + Function<BlockState, ConfiguredModel[]> function = |
| 124 | + state -> generateOrientationState(state, block); |
| 125 | + |
| 126 | + getVariantBuilder(block.get()).forAllStates(function); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Generates a block state variant for a specific age. |
| 131 | + * @param state The current block state. |
| 132 | + * @param block The block to generate. |
| 133 | + * @param textureName The name of the texture to use. |
| 134 | + * @return A new variant of the block state. |
| 135 | + */ |
| 136 | + private ConfiguredModel[] generateCropState(BlockState state, |
| 137 | + CropBlock block, |
| 138 | + String textureName) { |
| 139 | + ConfiguredModel[] models = new ConfiguredModel[1]; |
| 140 | + int age = Math.min(state.getValue(((FlowerCropBlock) block).getAgeProperty()), 6); |
| 141 | + if (age < 5 && textureName.contains("tulip")) { |
| 142 | + textureName = "tulip"; |
| 143 | + } |
| 144 | + |
| 145 | + models[0] = new ConfiguredModel(models().cross("block/flower_buds/" + textureName +"_stage" + age, |
| 146 | + new ResourceLocation(ButterfliesMod.MOD_ID, "block/flower_bud/" + textureName +"_stage" + age)).renderType("cutout")); |
| 147 | + |
| 148 | + return models; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Generates a block state variant based on orientation. |
| 153 | + * @param state The current block state. |
| 154 | + * @param block The block to generate. |
| 155 | + * @return A new variant of the block state. |
| 156 | + */ |
| 157 | + private ConfiguredModel[] generateOrientationState(BlockState state, |
| 158 | + RegistryObject<Block> block) { |
| 159 | + ConfiguredModel[] models = new ConfiguredModel[1]; |
| 160 | + FrontAndTop orientation = state.getValue(ButterflyOrigamiBlock.ORIENTATION); |
| 161 | + int x = 0; |
| 162 | + int y = 0; |
| 163 | + switch (orientation) { |
| 164 | + case DOWN_EAST: |
| 165 | + x = 180; |
| 166 | + y = 270; |
| 167 | + break; |
| 168 | + |
| 169 | + case DOWN_NORTH: |
| 170 | + x = 180; |
| 171 | + y = 180; |
| 172 | + break; |
| 173 | + |
| 174 | + case DOWN_SOUTH: |
| 175 | + x = 180; |
| 176 | + break; |
| 177 | + |
| 178 | + case DOWN_WEST: |
| 179 | + x = 180; |
| 180 | + y = 90; |
| 181 | + break; |
| 182 | + |
| 183 | + case EAST_UP: |
| 184 | + x = 270; |
| 185 | + y = 270; |
| 186 | + break; |
| 187 | + |
| 188 | + case NORTH_UP: |
| 189 | + x = 270; |
| 190 | + y = 180; |
| 191 | + break; |
| 192 | + |
| 193 | + case SOUTH_UP: |
| 194 | + x = 270; |
| 195 | + break; |
| 196 | + |
| 197 | + case UP_EAST: |
| 198 | + y = 270; |
| 199 | + break; |
| 200 | + |
| 201 | + case UP_NORTH: |
| 202 | + y = 180; |
| 203 | + break; |
| 204 | + |
| 205 | + case UP_SOUTH: |
| 206 | + break; |
| 207 | + |
| 208 | + case UP_WEST: |
| 209 | + y = 90; |
| 210 | + break; |
| 211 | + |
| 212 | + case WEST_UP: |
| 213 | + x = 270; |
| 214 | + y = 90; |
| 215 | + break; |
| 216 | + } |
| 217 | + |
| 218 | + // This should never be null. |
| 219 | + assert block.getId() != null; |
| 220 | + |
| 221 | + final ResourceLocation parent = new ResourceLocation(ButterfliesMod.MOD_ID, "block/butterfly_origami"); |
| 222 | + String path = block.getId().getPath(); |
| 223 | + models[0] = new ConfiguredModel( |
| 224 | + models().withExistingParent("block/" + path, parent).texture("all", new ResourceLocation("block/" + path.substring(18) + "_wool")), |
| 225 | + x, y, false); |
| 226 | + |
| 227 | + simpleBlockItem(block.get(), models[0].model); |
| 228 | + |
| 229 | + return models; |
| 230 | + } |
| 231 | +} |
0 commit comments