diff --git a/README.md b/README.md index 155c5dc..d15d917 100644 --- a/README.md +++ b/README.md @@ -22,31 +22,68 @@ Template blocks can be placed in the world, then right-clicked with a full-size * Upside-down slopes would be nice... * More templates !! -## For addon developers +# For addon developers -You may create your block any way you like, just make sure it has a block entity that returns a `BlockState` object from `RenderAttachmentBlockEntity.getRenderAttachmentData` and implements `ThemeableBlockEntity`. If you don't already have a block, the stock implementations in `TemplateBlock` and `TemplateEntity` are considered public API - they also implement the light- and redstone-emission features, just remember to feed the `AbstractBlock.Settings` through `TemplateBlock.configureSettings`. +## Creating your block -(Really the important part is implementing `RenderAttachmentBlockEntity` with a `BlockState`; that's the only thing Templates's bakedmodels assume. `ThemeableBlockEntity` opts-in to a couple more things such as overriding the break/sprint/fall particles.) +So there are various block interactions in Templates, like adding glowstone to make the block illuminate, adding redstone to make it a power source, etc. In an ideal world, I'd be able to simply provide the `TemplateBlock` class implementing all those features and ask that you extend it from your block. And if your block doesn't already have a superclass I recommend doing this - alas, if it does you can't extend `TemplateBlock`, so the implementation of everything has been farmed out to `public static` methods in `TemplateInteractionUtil` in the api package. Simply wire everything up. Don't forget to feed your `Block.Settings` through `TemplateInteractionUtil.configureSettings` too. -## `Mesh`-based models +The only other requirement is that your block *must* have a block entity that both returns a `BlockState` object from `RenderAttachmentBlockEntity.getRenderAttachmentData` and implements `ThemeableBlockEntity`. `TemplateEntity` is an implementation of this (and also implements the other half of the features from `TemplateInteractionUtil`); I recommend using it if you can. -We will construct a `RetexturedMeshUnbakedModel`. You need two things - the ID of a parent model, and a `Supplier` to retexture. +## Creating the custom model -Fill in the parent model field with the ID of any model. Ideally, this model should have a parent of `block/block`, or at least define *some* non-default rotations (smokey the bear voice *Only You Can Prevent Weirdly Rotated First-Person Models*), set `"gui_light": "front"` (lest the item model look weird), and define a particle texture. +TL;DR: Look at `assets/templates/blockstates`, look at `assets/templates/models/block`, and look at the bottom of `TemplatesClient`. -When building the `Mesh`, if you want a face to be dynamically retextured, `.tag()` it with the `.ordinal() + 1` of the `Direction` it corresponds to and give the face U/V coordinates ranging from 0 to 1. (For example, if you tag a face with `3` (`Direction.NORTH.ordinal() + 1`), it will be retextured to the north side of the template's theme.) +### Using a JSON model + +We will construct a `RetexturedJsonModelUnbakedModel`. You need the ID of the json model to retexture. + +The base model can have a `parent` of anything you want. To make a surface retexturable, give it the texture `templates:templates_special/down`, `templates:templates_special/north`, `templates:templates_special/east` (etc). These textures will be *replaced* at runtime by the corresponding region of the corresponding texture of the template's theme block. (For example, a surface textured using `templates:templates_special/north` will look like the north side of the template's theme.) + +If possible, I don't recommend making a wholly *new* json model. If you have an existing model that defines `north`, `south`, `east` etc texture variables, just use this: + +```json +{ + "parent": "yourmod:your/cool/json/model", + "textures": { + "down": "templates:templates_special/down", + "up": "templates:templates_special/up", + "north": "templates:templates_special/north", + "south": "templates:templates_special/south", + "west": "templates:templates_special/west", + "east": "templates:templates_special/east", + + "particle": "minecraft:block/scaffolding_top" + } +} +``` + +(If your model has texture variables that look more like `up/down/side` instead of `up/down/north/south/east/west`, you *can* cheat and assign something like `templates:templates_special/east` to the `side` texture slot - but imagine a player retexturing your template to look like a sideways log, which is not rotationally symmetric. It won't work as expected.) + +Finally, if your base model is a multipart model, *and* you plan on using it as an item model, I need a representative blockstate in order to select the correct configuration of the base model. Pick one and pass it as the second parameter to `RetexturedJsonModelUnbakedModel`. If the base model is not a multipart model, this is not required. + +That's all you need to construct a `RetexturedJsonModelUnbakedModel`, so to finish things off, [register it.](#registering-the-custom-model) + +### Using a custom `Mesh`-based model + +If you have a shape in mind that can't be represented with a json model (like a perfect 45 degree wedge), this is a good option. + +We will construct a `RetexturedMeshUnbakedModel`. You need two things - the ID of a parent model, and a `Supplier<>` of a `Mesh` to retexture. + +Ideally, the parent model should have a parent of `block/block`, or at least define *some* non-default rotations (smokey the bear voice *Only You Can Prevent Weirdly Rotated First-Person Models*), set `"gui_light": "front"` (lest the item model look weird), and define a particle texture. You should use a json model for this, but keep in mind that no quads will be read from the json model - it's only used to source all the miscellaneous `BakedModel` options. + +When building the `Mesh`, if you want a face to be dynamically retextured, `.tag()` it with the `.ordinal() + 1` of the `Direction` it corresponds to, and give the face UV coordinates ranging from 0 to 1. (For example, if you tag a face with `3` (`Direction.NORTH.ordinal() + 1`), it will be retextured to the north side of the template's theme.) (TODO: implement a system for baking unchanging `Sprite`s onto the mesh, potentially by "registering" more tags; the problem is you don't have access to sprite uvs at mesh building time. Or just provide a way to get sprite UVs at mesh building time...?) -That's all you need in order to construct a `RetexturedMeshUnbakedModel`, so to finish things off: +That's all you need in order to construct a `RetexturedMeshUnbakedModel`, so to finish things off, [register it.](#registering-the-custom-model) -* Come up with an ID for it -* Register it using `TemplatesClient.provider.addTemplateModel` (a thin wrapper around Fabric's `ModelResourceProvider`) -* Create a blockstate for your block, point it at your model's ID - * You may rotate the blockmodel with the `x` and `y` properties. +## Registering the custom model -You may create a regular item model (JSON or otherwise), or use ours by calling `TemplatesClient.provider.assignItemModel` (a thin wrapper around Fabric's `ModelVariantProvider`) passing the same model ID the block used. (This is a bit of a kludge. The reason you have to do this, instead of simply creating a regular item model and setting its `parent`, is that `JsonUnbakedModel`s can't have non-`JsonUnbakedModel`s as their `parent`, and even a trivial item model with only the `parent` field set counts as a `JsonUnbakedModel`. Blockstates are a layer of indirection before model loading, so it's not a problem for blocks.) +1. Decide on an ID for your special model that's *different* from the ID used for the base model. + * If your base model lives at `yourmod:block/awesome_template`, something like `yourmod:awesome_template_special` would do. +2. Register it using `TemplatesClient.provider.addTemplateModel`. +3. Create a blockstate json for your block, and point it at the ID you decided for your special model in 1). + * You may rotate the blockmodel with the `x` and `y` properties. -## JSON models - -Soon:tm: \ No newline at end of file +You may create a regular item model, or use ours by calling `TemplatesClient.provider.assignItemModel`, passing the ID of the special model & the items you want to assign it to. (The reason you have to do this instead of simply creating a regular item model and setting its `parent`, is that `JsonUnbakedModel`s can't have non-`JsonUnbakedModel`s as their `parent`, and even a trivial item model with only the `parent` field set counts as a `JsonUnbakedModel`. This isn't a problem for block models because blockstates are a layer of indirection before model loading.) \ No newline at end of file diff --git a/src/main/java/io/github/cottonmc/templates/Templates.java b/src/main/java/io/github/cottonmc/templates/Templates.java index 6dcbdd9..1f5c85e 100644 --- a/src/main/java/io/github/cottonmc/templates/Templates.java +++ b/src/main/java/io/github/cottonmc/templates/Templates.java @@ -1,7 +1,8 @@ package io.github.cottonmc.templates; -import io.github.cottonmc.templates.block.SlopeBlock; +import io.github.cottonmc.templates.api.TemplateInteractionUtil; import io.github.cottonmc.templates.block.TemplateSlabBlock; +import io.github.cottonmc.templates.block.TemplateSlopeBlock; import io.github.cottonmc.templates.block.entity.TemplateEntity; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; @@ -21,13 +22,13 @@ import java.util.function.BiConsumer; public class Templates implements ModInitializer { public static final String MODID = "templates"; - public static final Block SLOPE = Registry.register(Registries.BLOCK, id("slope"), new SlopeBlock()); + public static final Block SLOPE = Registry.register(Registries.BLOCK, id("slope"), new TemplateSlopeBlock(TemplateInteractionUtil.makeSettings())); public static final BlockEntityType SLOPE_ENTITY = Registry.register( Registries.BLOCK_ENTITY_TYPE, id("slope"), FabricBlockEntityTypeBuilder.create(Templates::makeSlopeEntity, SLOPE).build(null) ); - public static final Block SLAB = Registry.register(Registries.BLOCK, id("slab"), new TemplateSlabBlock()); + public static final Block SLAB = Registry.register(Registries.BLOCK, id("slab"), new TemplateSlabBlock(TemplateInteractionUtil.makeSettings())); public static final BlockEntityType SLAB_ENTITY = Registry.register( Registries.BLOCK_ENTITY_TYPE, id("slab"), FabricBlockEntityTypeBuilder.create(Templates::makeSlabEntity, SLAB).build(null) diff --git a/src/main/java/io/github/cottonmc/templates/TemplatesModelProvider.java b/src/main/java/io/github/cottonmc/templates/TemplatesModelProvider.java index 2bc08c3..03a3239 100644 --- a/src/main/java/io/github/cottonmc/templates/TemplatesModelProvider.java +++ b/src/main/java/io/github/cottonmc/templates/TemplatesModelProvider.java @@ -1,6 +1,5 @@ package io.github.cottonmc.templates; -import io.github.cottonmc.templates.model.TemplateAppearance; import io.github.cottonmc.templates.model.TemplateAppearanceManager; import net.fabricmc.fabric.api.client.model.ModelProviderContext; import net.fabricmc.fabric.api.client.model.ModelProviderException; diff --git a/src/main/java/io/github/cottonmc/templates/api/TemplateInteractionUtil.java b/src/main/java/io/github/cottonmc/templates/api/TemplateInteractionUtil.java new file mode 100644 index 0000000..c746b9a --- /dev/null +++ b/src/main/java/io/github/cottonmc/templates/api/TemplateInteractionUtil.java @@ -0,0 +1,140 @@ +package io.github.cottonmc.templates.api; + +import io.github.cottonmc.templates.block.entity.TemplateEntity; +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.BlockEntityProvider; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemPlacementContext; +import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemUsageContext; +import net.minecraft.item.Items; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.sound.BlockSoundGroup; +import net.minecraft.sound.SoundCategory; +import net.minecraft.sound.SoundEvents; +import net.minecraft.state.StateManager; +import net.minecraft.state.property.BooleanProperty; +import net.minecraft.state.property.IntProperty; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.ItemScatterer; +import net.minecraft.util.collection.DefaultedList; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.world.BlockView; +import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; + +public class TemplateInteractionUtil { + public static final IntProperty LIGHT = IntProperty.of("light", 0, 15); + public static final BooleanProperty REDSTONE = BooleanProperty.of("redstone"); + + public static StateManager.Builder appendProperties(StateManager.Builder builder) { + return builder.add(LIGHT, REDSTONE); + } + + public static AbstractBlock.Settings makeSettings() { + return configureSettings(AbstractBlock.Settings.create()); + } + + public static AbstractBlock.Settings configureSettings(AbstractBlock.Settings s) { + return s.luminance(TemplateInteractionUtil::luminance).nonOpaque().sounds(BlockSoundGroup.WOOD).hardness(0.2f); + } + + public static BlockState setDefaultStates(BlockState in) { + return in.with(LIGHT, 0).with(REDSTONE, false); + } + + public static ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + if(!(world.getBlockEntity(pos) instanceof TemplateEntity be)) return ActionResult.PASS; + if(!player.canModifyBlocks() || !world.canPlayerModifyAt(player, pos)) return ActionResult.PASS; + + ItemStack held = player.getStackInHand(hand); + + //Glowstone + if(state.contains(LIGHT) && held.getItem() == Items.GLOWSTONE_DUST && state.get(LIGHT) != 15 && !be.hasSpentGlowstoneDust()) { + world.setBlockState(pos, state.with(LIGHT, 15)); + be.spentGlowstoneDust(); + + if(!player.isCreative()) held.decrement(1); + world.playSound(player, pos, SoundEvents.BLOCK_GLASS_HIT, SoundCategory.BLOCKS, 1f, 1f); + return ActionResult.SUCCESS; + } + + //Redstone + if(state.contains(REDSTONE) && held.getItem() == Blocks.REDSTONE_TORCH.asItem() && !state.get(REDSTONE) && !be.hasSpentRedstoneTorch()) { + world.setBlockState(pos, state.with(REDSTONE, true)); + be.spentRedstoneTorch(); + + if(!player.isCreative()) held.decrement(1); + world.playSound(player, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 1f, 1f); + return ActionResult.SUCCESS; + } + + //Changing the theme + if(held.getItem() instanceof BlockItem bi && be.getThemeState().getBlock() == Blocks.AIR) { + Block block = bi.getBlock(); + ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hit)); + BlockState placementState = block.getPlacementState(ctx); + if(placementState != null && Block.isShapeFullCube(placementState.getCollisionShape(world, pos)) && !(block instanceof BlockEntityProvider)) { + if(!world.isClient) be.setRenderedState(placementState); + + world.setBlockState(pos, state + .with(LIGHT, be.hasSpentGlowstoneDust() ? 15 : placementState.getLuminance()) + .with(REDSTONE, be.hasSpentRedstoneTorch() || placementState.getWeakRedstonePower(world, pos, Direction.NORTH) != 0)); + + if(!player.isCreative()) held.decrement(1); + world.playSound(player, pos, state.getSoundGroup().getPlaceSound(), SoundCategory.BLOCKS, 1f, 1f); + return ActionResult.SUCCESS; + } + } + + return ActionResult.PASS; + } + + public static void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { + if(!state.isOf(newState.getBlock()) && world.getBlockEntity(pos) instanceof TemplateEntity template) { + DefaultedList drops = DefaultedList.of(); + + //TODO: remember the specific ItemStack + Block theme = template.getThemeState().getBlock(); + if(theme != Blocks.AIR) drops.add(new ItemStack(theme)); + + if(template.hasSpentRedstoneTorch()) drops.add(new ItemStack(Items.REDSTONE_TORCH)); + if(template.hasSpentGlowstoneDust()) drops.add(new ItemStack(Items.GLOWSTONE_DUST)); + + ItemScatterer.spawn(world, pos, drops); + } + } + + public static void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { + //Load the BlockEntityTag clientside, which fixes the template briefly showing its default state when placing it. + //I'm surprised this doesn't happen by default; the BlockEntityTag stuff is only done serverside. + if(world.isClient && world.getBlockEntity(pos) instanceof TemplateEntity be) { + NbtCompound tag = BlockItem.getBlockEntityNbt(stack); + if(tag != null) be.readNbt(tag); + } + } + + public static boolean emitsRedstonePower(BlockState state) { + return state.contains(REDSTONE) ? state.get(REDSTONE) : false; + } + + public static int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { + return state.contains(REDSTONE) && state.get(REDSTONE) ? 15 : 0; + } + + public static int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { + return state.contains(REDSTONE) && state.get(REDSTONE) ? 15 : 0; + } + + public static int luminance(BlockState state) { + return state.contains(LIGHT) ? state.get(LIGHT) : 0; + } +} diff --git a/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java b/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java index d188824..254bee0 100644 --- a/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java +++ b/src/main/java/io/github/cottonmc/templates/block/TemplateBlock.java @@ -1,27 +1,16 @@ package io.github.cottonmc.templates.block; -import io.github.cottonmc.templates.block.entity.TemplateEntity; +import io.github.cottonmc.templates.api.TemplateInteractionUtil; import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; +import net.minecraft.block.entity.BlockEntity; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.item.BlockItem; -import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; -import net.minecraft.item.ItemUsageContext; -import net.minecraft.item.Items; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvents; import net.minecraft.state.StateManager; -import net.minecraft.state.property.BooleanProperty; -import net.minecraft.state.property.IntProperty; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; -import net.minecraft.util.ItemScatterer; -import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; @@ -30,120 +19,48 @@ import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; public abstract class TemplateBlock extends Block implements BlockEntityProvider { - public static final IntProperty LIGHT = IntProperty.of("light", 0, 15); - public static final BooleanProperty REDSTONE = BooleanProperty.of("redstone"); - public TemplateBlock(Settings settings) { super(settings); - - setDefaultState(getDefaultState().with(LIGHT, 0).with(REDSTONE, false)); - } - - public static Settings configureSettings(Settings s) { - return s - .luminance(state -> ((TemplateBlock) state.getBlock()).luminance(state)) - .nonOpaque(); + setDefaultState(TemplateInteractionUtil.setDefaultStates(getDefaultState())); } + @Override + public abstract @Nullable BlockEntity createBlockEntity(BlockPos blockPos, BlockState blockState); + @Override protected void appendProperties(StateManager.Builder builder) { - super.appendProperties(builder.add(LIGHT, REDSTONE)); + super.appendProperties(TemplateInteractionUtil.appendProperties(builder)); } @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { - if(!state.isOf(this) || !(world.getBlockEntity(pos) instanceof TemplateEntity be)) return ActionResult.PASS; //shouldn't happen - if(!player.canModifyBlocks() || !world.canPlayerModifyAt(player, pos)) return ActionResult.PASS; - - ItemStack held = player.getStackInHand(hand); - - //Glowstone - if(held.getItem() == Items.GLOWSTONE_DUST && state.get(LIGHT) != 15 && !be.hasSpentGlowstoneDust()) { - world.setBlockState(pos, state.with(LIGHT, 15)); - be.spentGlowstoneDust(); - - if(!player.isCreative()) held.decrement(1); - world.playSound(player, pos, SoundEvents.BLOCK_GLASS_HIT, SoundCategory.BLOCKS, 1f, 1f); - return ActionResult.SUCCESS; - } - - //Redstone - if(held.getItem() == Blocks.REDSTONE_TORCH.asItem() && !state.get(REDSTONE) && !be.hasSpentRedstoneTorch()) { - world.setBlockState(pos, state.with(REDSTONE, true)); - be.spentRedstoneTorch(); - - if(!player.isCreative()) held.decrement(1); - world.playSound(player, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 1f, 1f); - return ActionResult.SUCCESS; - } - - //Changing the theme - if(held.getItem() instanceof BlockItem bi && be.getThemeState().getBlock() == Blocks.AIR) { - Block block = bi.getBlock(); - ItemPlacementContext ctx = new ItemPlacementContext(new ItemUsageContext(player, hand, hit)); - BlockState placementState = block.getPlacementState(ctx); - if(placementState != null && Block.isShapeFullCube(placementState.getCollisionShape(world, pos)) && !(block instanceof BlockEntityProvider)) { - if(!world.isClient) be.setRenderedState(placementState); - - world.setBlockState(pos, state - .with(LIGHT, be.hasSpentGlowstoneDust() ? 15 : placementState.getLuminance()) - .with(REDSTONE, be.hasSpentRedstoneTorch() || placementState.getWeakRedstonePower(world, pos, Direction.NORTH) != 0)); - - if(!player.isCreative()) held.decrement(1); - world.playSound(player, pos, state.getSoundGroup().getPlaceSound(), SoundCategory.BLOCKS, 1f, 1f); - return ActionResult.SUCCESS; - } - } - - return ActionResult.PASS; + return TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit); } @Override public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { - if(!state.isOf(newState.getBlock()) && world.getBlockEntity(pos) instanceof TemplateEntity template) { - DefaultedList drops = DefaultedList.of(); - - //TODO: remember the specific ItemStack - Block theme = template.getThemeState().getBlock(); - if(theme != Blocks.AIR) drops.add(new ItemStack(theme)); - - if(template.hasSpentRedstoneTorch()) drops.add(new ItemStack(Items.REDSTONE_TORCH)); - if(template.hasSpentGlowstoneDust()) drops.add(new ItemStack(Items.GLOWSTONE_DUST)); - - ItemScatterer.spawn(world, pos, drops); - } - + TemplateInteractionUtil.onStateReplaced(state, world, pos, newState, moved); super.onStateReplaced(state, world, pos, newState, moved); } @Override public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { - //Load the BlockEntityTag clientside, which fixes the template briefly showing its default state when placing it. - //I'm surprised this doesn't happen by default; the BlockEntityTag stuff is only done serverside. - if(world.isClient && world.getBlockEntity(pos) instanceof TemplateEntity be) { - NbtCompound tag = BlockItem.getBlockEntityNbt(stack); - if(tag != null) be.readNbt(tag); - } - + TemplateInteractionUtil.onPlaced(world, pos, state, placer, stack); super.onPlaced(world, pos, state, placer, stack); } @Override public boolean emitsRedstonePower(BlockState state) { - return state.get(REDSTONE); + return TemplateInteractionUtil.emitsRedstonePower(state); } @Override public int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { - return state.get(REDSTONE) ? 15 : 0; + return TemplateInteractionUtil.getWeakRedstonePower(state, view, pos, dir); } @Override public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { - return state.get(REDSTONE) ? 15 : 0; - } - - public int luminance(BlockState state) { - return state.get(LIGHT); + return TemplateInteractionUtil.getStrongRedstonePower(state, view, pos, dir); } } diff --git a/src/main/java/io/github/cottonmc/templates/block/TemplateSlabBlock.java b/src/main/java/io/github/cottonmc/templates/block/TemplateSlabBlock.java index a9f0712..9d4720d 100644 --- a/src/main/java/io/github/cottonmc/templates/block/TemplateSlabBlock.java +++ b/src/main/java/io/github/cottonmc/templates/block/TemplateSlabBlock.java @@ -1,24 +1,29 @@ package io.github.cottonmc.templates.block; import io.github.cottonmc.templates.Templates; +import io.github.cottonmc.templates.api.TemplateInteractionUtil; +import net.minecraft.block.Block; import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockState; import net.minecraft.block.SlabBlock; import net.minecraft.block.entity.BlockEntity; -import net.minecraft.sound.BlockSoundGroup; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.state.StateManager; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.world.BlockView; +import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; public class TemplateSlabBlock extends SlabBlock implements BlockEntityProvider { public TemplateSlabBlock(Settings settings) { super(settings); - } - - public TemplateSlabBlock() { - //super(TemplateBlock.configureSettings(Settings.create()) //TODO - super(Settings.create().nonOpaque() - .sounds(BlockSoundGroup.WOOD) - .hardness(0.2f)); //TODO: Material.WOOD + setDefaultState(TemplateInteractionUtil.setDefaultStates(getDefaultState())); } @Nullable @@ -26,4 +31,40 @@ public class TemplateSlabBlock extends SlabBlock implements BlockEntityProvider public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { return Templates.SLAB_ENTITY.instantiate(pos, state); } + + @Override + protected void appendProperties(StateManager.Builder builder) { + super.appendProperties(TemplateInteractionUtil.appendProperties(builder)); + } + + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + return TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit); + } + + @Override + public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { + TemplateInteractionUtil.onStateReplaced(state, world, pos, newState, moved); + super.onStateReplaced(state, world, pos, newState, moved); + } + + @Override + public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { + TemplateInteractionUtil.onPlaced(world, pos, state, placer, stack); + } + + @Override + public boolean emitsRedstonePower(BlockState state) { + return TemplateInteractionUtil.emitsRedstonePower(state); + } + + @Override + public int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { + return TemplateInteractionUtil.getWeakRedstonePower(state, view, pos, dir); + } + + @Override + public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { + return TemplateInteractionUtil.getStrongRedstonePower(state, view, pos, dir); + } } diff --git a/src/main/java/io/github/cottonmc/templates/block/SlopeBlock.java b/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java similarity index 87% rename from src/main/java/io/github/cottonmc/templates/block/SlopeBlock.java rename to src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java index 921d691..a3c2a8a 100644 --- a/src/main/java/io/github/cottonmc/templates/block/SlopeBlock.java +++ b/src/main/java/io/github/cottonmc/templates/block/TemplateSlopeBlock.java @@ -1,13 +1,11 @@ package io.github.cottonmc.templates.block; import io.github.cottonmc.templates.Templates; -import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.ShapeContext; import net.minecraft.block.entity.BlockEntity; import net.minecraft.item.ItemPlacementContext; -import net.minecraft.sound.BlockSoundGroup; import net.minecraft.state.StateManager; import net.minecraft.state.property.DirectionProperty; import net.minecraft.state.property.Properties; @@ -19,7 +17,7 @@ import net.minecraft.world.BlockView; import javax.annotation.Nullable; -public class SlopeBlock extends TemplateBlock { +public class TemplateSlopeBlock extends TemplateBlock { public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING; public static final VoxelShape BASE = VoxelShapes.cuboid(0f, 0f, 0f, 1f, 0.5f, 1f); @@ -28,10 +26,8 @@ public class SlopeBlock extends TemplateBlock { public static final VoxelShape EAST = VoxelShapes.cuboid(0.5f, 0.5f, 0f, 1f, 1f, 1f); public static final VoxelShape WEST = VoxelShapes.cuboid(0f, 0.5f, 0f, 0.5f, 1f, 1f); - public SlopeBlock() { - super(TemplateBlock.configureSettings(Settings.create()) - .sounds(BlockSoundGroup.WOOD) - .hardness(0.2f)); //TODO: Material.WOOD + public TemplateSlopeBlock(Settings settings) { + super(settings); setDefaultState(getDefaultState().with(FACING, Direction.NORTH)); } diff --git a/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinEntity.java b/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinEntity.java index 314bbe5..4d9cd52 100644 --- a/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinEntity.java +++ b/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinEntity.java @@ -2,7 +2,6 @@ package io.github.cottonmc.templates.mixin.particles; import io.github.cottonmc.templates.api.ThemeableBlockEntity; import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; import net.minecraft.entity.Entity; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; diff --git a/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinLivingEntity.java b/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinLivingEntity.java index 7137a6a..741044d 100644 --- a/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinLivingEntity.java +++ b/src/main/java/io/github/cottonmc/templates/mixin/particles/MixinLivingEntity.java @@ -2,7 +2,6 @@ package io.github.cottonmc.templates.mixin.particles; import io.github.cottonmc.templates.api.ThemeableBlockEntity; import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; import net.minecraft.util.math.BlockPos; diff --git a/src/main/java/io/github/cottonmc/templates/model/RetexturedJsonModelBakedModel.java b/src/main/java/io/github/cottonmc/templates/model/RetexturedJsonModelBakedModel.java index a6e9bed..33a27f3 100644 --- a/src/main/java/io/github/cottonmc/templates/model/RetexturedJsonModelBakedModel.java +++ b/src/main/java/io/github/cottonmc/templates/model/RetexturedJsonModelBakedModel.java @@ -17,7 +17,11 @@ import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.BakedQuad; import net.minecraft.client.texture.Sprite; import net.minecraft.client.util.SpriteIdentifier; +import net.minecraft.item.BlockItem; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtHelper; +import net.minecraft.registry.Registries; import net.minecraft.screen.PlayerScreenHandler; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; @@ -31,9 +35,10 @@ import java.util.function.Function; import java.util.function.Supplier; public class RetexturedJsonModelBakedModel extends ForwardingBakedModel { - public RetexturedJsonModelBakedModel(BakedModel baseModel, TemplateAppearanceManager tam, Function spriteLookup) { + public RetexturedJsonModelBakedModel(BakedModel baseModel, TemplateAppearanceManager tam, Function spriteLookup, BlockState itemModelState) { this.wrapped = baseModel; this.tam = tam; + this.itemModelState = itemModelState; for(int i = 0; i < DIRECTIONS.length; i++) { SpriteIdentifier id = new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, Templates.id("templates_special/" + DIRECTIONS[i].getName())); @@ -41,11 +46,13 @@ public class RetexturedJsonModelBakedModel extends ForwardingBakedModel { } } + //TODO: Check that TemplateAppearance equals() behavior is what i want, and also that it's fast private record CacheKey(BlockState state, TemplateAppearance appearance) {} private final TemplateAppearanceManager tam; private final ConcurrentHashMap meshCache = new ConcurrentHashMap<>(); private final Sprite[] specialSprites = new Sprite[DIRECTIONS.length]; + private final BlockState itemModelState; @Override public boolean isVanillaAdapter() { @@ -54,8 +61,8 @@ public class RetexturedJsonModelBakedModel extends ForwardingBakedModel { @Override public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier randomSupplier, RenderContext context) { - BlockState template = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null; - TemplateAppearance ta = template == null || template.isAir() ? tam.getDefaultAppearance() : tam.getAppearance(template); + BlockState theme = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null; + TemplateAppearance ta = theme == null || theme.isAir() ? tam.getDefaultAppearance() : tam.getAppearance(theme); CacheKey key = new CacheKey(state, ta); context.meshConsumer().accept(meshCache.computeIfAbsent(key, this::makeMesh)); @@ -63,7 +70,17 @@ public class RetexturedJsonModelBakedModel extends ForwardingBakedModel { @Override public void emitItemQuads(ItemStack stack, Supplier randomSupplier, RenderContext context) { - super.emitItemQuads(stack, randomSupplier, context); + TemplateAppearance nbtAppearance = null; + + //cheeky: if the item has NBT data, pluck out the blockstate from it + NbtCompound tag = BlockItem.getBlockEntityNbt(stack); + if(tag != null && tag.contains("BlockState")) { + BlockState theme = NbtHelper.toBlockState(Registries.BLOCK.getReadOnlyWrapper(), tag.getCompound("BlockState")); + if(!theme.isAir()) nbtAppearance = tam.getAppearance(theme); + } + + CacheKey key = new CacheKey(itemModelState, nbtAppearance == null ? tam.getDefaultAppearance() : nbtAppearance); + context.meshConsumer().accept(meshCache.computeIfAbsent(key, this::makeMesh)); } protected Mesh makeMesh(CacheKey key) { diff --git a/src/main/java/io/github/cottonmc/templates/model/RetexturedJsonModelUnbakedModel.java b/src/main/java/io/github/cottonmc/templates/model/RetexturedJsonModelUnbakedModel.java index 0d02b31..20bd8c8 100644 --- a/src/main/java/io/github/cottonmc/templates/model/RetexturedJsonModelUnbakedModel.java +++ b/src/main/java/io/github/cottonmc/templates/model/RetexturedJsonModelUnbakedModel.java @@ -1,6 +1,8 @@ package io.github.cottonmc.templates.model; import io.github.cottonmc.templates.TemplatesClient; +import net.minecraft.block.BlockState; +import net.minecraft.block.Blocks; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.Baker; import net.minecraft.client.render.model.ModelBakeSettings; @@ -16,10 +18,16 @@ import java.util.function.Function; public class RetexturedJsonModelUnbakedModel implements UnbakedModel { public RetexturedJsonModelUnbakedModel(Identifier parent) { + this(parent, Blocks.AIR.getDefaultState()); + } + + public RetexturedJsonModelUnbakedModel(Identifier parent, BlockState itemModelState) { this.parent = parent; + this.itemModelState = itemModelState; } protected final Identifier parent; + protected final BlockState itemModelState; @Override public Collection getModelDependencies() { @@ -37,7 +45,8 @@ public class RetexturedJsonModelUnbakedModel implements UnbakedModel { return new RetexturedJsonModelBakedModel( baker.bake(parent, modelBakeSettings), TemplatesClient.provider.getOrCreateTemplateApperanceManager(spriteLookup), - spriteLookup + spriteLookup, + itemModelState ); } } diff --git a/src/main/java/io/github/cottonmc/templates/model/RetexturedMeshBakedModel.java b/src/main/java/io/github/cottonmc/templates/model/RetexturedMeshBakedModel.java index 81b645a..18be0ce 100644 --- a/src/main/java/io/github/cottonmc/templates/model/RetexturedMeshBakedModel.java +++ b/src/main/java/io/github/cottonmc/templates/model/RetexturedMeshBakedModel.java @@ -57,12 +57,12 @@ public final class RetexturedMeshBakedModel extends ForwardingBakedModel { } public @NotNull RenderContext.QuadTransform retexturingBlockTransformer(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier randomSupplier) { - BlockState template = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null; - if(template == null || template.isAir()) return new RetexturingTransformer(tam.getDefaultAppearance(), 0xFFFFFFFF, facePermutation); + BlockState theme = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null; + if(theme == null || theme.isAir()) return new RetexturingTransformer(tam.getDefaultAppearance(), 0xFFFFFFFF, facePermutation); - BlockColorProvider prov = ColorProviderRegistry.BLOCK.get(template.getBlock()); + BlockColorProvider prov = ColorProviderRegistry.BLOCK.get(theme.getBlock()); int globalTint = prov != null ? prov.getColor(state, blockView, pos, 1) : 0xFFFFFFFF; - return new RetexturingTransformer(tam.getAppearance(template), globalTint, facePermutation); + return new RetexturingTransformer(tam.getAppearance(theme), globalTint, facePermutation); } public @NotNull RenderContext.QuadTransform retexturingItemTransformer(ItemStack stack, Supplier randomSupplier) { diff --git a/src/main/resources/assets/templates/textures/templates_special/down.png b/src/main/resources/assets/templates/textures/templates_special/down.png index d4b05aa..d8f2843 100644 Binary files a/src/main/resources/assets/templates/textures/templates_special/down.png and b/src/main/resources/assets/templates/textures/templates_special/down.png differ diff --git a/src/main/resources/assets/templates/textures/templates_special/east.png b/src/main/resources/assets/templates/textures/templates_special/east.png index 7b8b4ce..9810e8c 100644 Binary files a/src/main/resources/assets/templates/textures/templates_special/east.png and b/src/main/resources/assets/templates/textures/templates_special/east.png differ diff --git a/src/main/resources/assets/templates/textures/templates_special/north.png b/src/main/resources/assets/templates/textures/templates_special/north.png index 947c62f..b7ec03f 100644 Binary files a/src/main/resources/assets/templates/textures/templates_special/north.png and b/src/main/resources/assets/templates/textures/templates_special/north.png differ diff --git a/src/main/resources/assets/templates/textures/templates_special/south.png b/src/main/resources/assets/templates/textures/templates_special/south.png index 9e51596..0b474c3 100644 Binary files a/src/main/resources/assets/templates/textures/templates_special/south.png and b/src/main/resources/assets/templates/textures/templates_special/south.png differ diff --git a/src/main/resources/assets/templates/textures/templates_special/up.png b/src/main/resources/assets/templates/textures/templates_special/up.png index c16d6c1..3e586ad 100644 Binary files a/src/main/resources/assets/templates/textures/templates_special/up.png and b/src/main/resources/assets/templates/textures/templates_special/up.png differ diff --git a/src/main/resources/assets/templates/textures/templates_special/west.png b/src/main/resources/assets/templates/textures/templates_special/west.png index f5fe7f7..52a2957 100644 Binary files a/src/main/resources/assets/templates/textures/templates_special/west.png and b/src/main/resources/assets/templates/textures/templates_special/west.png differ diff --git a/src/main/resources/data/templates/advancements/recipes/decorations/slope.json b/src/main/resources/data/templates/advancements/recipes/decorations/templates.json similarity index 91% rename from src/main/resources/data/templates/advancements/recipes/decorations/slope.json rename to src/main/resources/data/templates/advancements/recipes/decorations/templates.json index b7d2074..4d3ea97 100644 --- a/src/main/resources/data/templates/advancements/recipes/decorations/slope.json +++ b/src/main/resources/data/templates/advancements/recipes/decorations/templates.json @@ -2,7 +2,8 @@ "parent": "minecraft:recipes/root", "rewards": { "recipes": [ - "minecraft:scaffolding" + "templates:slope", + "templates:slab" ] }, "criteria": { diff --git a/src/main/resources/data/templates/loot_tables/blocks/slab.json b/src/main/resources/data/templates/loot_tables/blocks/slab.json new file mode 100644 index 0000000..58d8f8b --- /dev/null +++ b/src/main/resources/data/templates/loot_tables/blocks/slab.json @@ -0,0 +1,19 @@ +{ + "type": "minecraft:block", + "pools": [ + { + "rolls": 1, + "entries": [ + { + "type": "minecraft:item", + "name": "templates:slab" + } + ], + "conditions": [ + { + "condition": "minecraft:survives_explosion" + } + ] + } + ] +} \ No newline at end of file diff --git a/src/main/resources/data/templates/recipes/slab.json b/src/main/resources/data/templates/recipes/slab.json new file mode 100644 index 0000000..389430e --- /dev/null +++ b/src/main/resources/data/templates/recipes/slab.json @@ -0,0 +1,20 @@ +{ + "type": "minecraft:crafting_shaped", + "pattern": [ + " ~ ", + "III" + ], + "key": { + "I": { + "item": "minecraft:bamboo" + }, + "~": { + "item": "minecraft:string" + } + }, + "result": { + "item": "templates:slab", + "count": 6 + }, + "group": "templates" +} \ No newline at end of file diff --git a/src/main/resources/data/templates/recipes/slope.json b/src/main/resources/data/templates/recipes/slope.json index 4cb8707..5b4bde5 100644 --- a/src/main/resources/data/templates/recipes/slope.json +++ b/src/main/resources/data/templates/recipes/slope.json @@ -16,5 +16,6 @@ "result": { "item": "templates:slope", "count": 4 - } + }, + "group": "templates" } \ No newline at end of file diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 846c0fa..a17a060 100755 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -5,7 +5,7 @@ "name": "Templates", "icon": "assets/templates/icon.png", "description": "An API for templated blocks", - "licence": "MIT", + "license": "MIT", "contact": { "sources": "https://github.com/CottonMC/Templates" },