docs, TemplateInteractionUtil, recipes, loottable, tweaks

This commit is contained in:
quat1024 2023-07-04 02:51:41 -04:00
parent 614be779b8
commit e3dc78fa6a
23 changed files with 341 additions and 145 deletions

View File

@ -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... * Upside-down slopes would be nice...
* More templates !! * 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<Mesh>` 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...?) (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 ## Registering the custom model
* 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.
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 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.)
Soon:tm:

View File

@ -1,7 +1,8 @@
package io.github.cottonmc.templates; 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.TemplateSlabBlock;
import io.github.cottonmc.templates.block.TemplateSlopeBlock;
import io.github.cottonmc.templates.block.entity.TemplateEntity; import io.github.cottonmc.templates.block.entity.TemplateEntity;
import net.fabricmc.api.ModInitializer; import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; 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 class Templates implements ModInitializer {
public static final String MODID = "templates"; 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<TemplateEntity> SLOPE_ENTITY = Registry.register( public static final BlockEntityType<TemplateEntity> SLOPE_ENTITY = Registry.register(
Registries.BLOCK_ENTITY_TYPE, id("slope"), Registries.BLOCK_ENTITY_TYPE, id("slope"),
FabricBlockEntityTypeBuilder.create(Templates::makeSlopeEntity, SLOPE).build(null) 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<TemplateEntity> SLAB_ENTITY = Registry.register( public static final BlockEntityType<TemplateEntity> SLAB_ENTITY = Registry.register(
Registries.BLOCK_ENTITY_TYPE, id("slab"), Registries.BLOCK_ENTITY_TYPE, id("slab"),
FabricBlockEntityTypeBuilder.create(Templates::makeSlabEntity, SLAB).build(null) FabricBlockEntityTypeBuilder.create(Templates::makeSlabEntity, SLAB).build(null)

View File

@ -1,6 +1,5 @@
package io.github.cottonmc.templates; package io.github.cottonmc.templates;
import io.github.cottonmc.templates.model.TemplateAppearance;
import io.github.cottonmc.templates.model.TemplateAppearanceManager; import io.github.cottonmc.templates.model.TemplateAppearanceManager;
import net.fabricmc.fabric.api.client.model.ModelProviderContext; import net.fabricmc.fabric.api.client.model.ModelProviderContext;
import net.fabricmc.fabric.api.client.model.ModelProviderException; import net.fabricmc.fabric.api.client.model.ModelProviderException;

View File

@ -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<Block, BlockState> appendProperties(StateManager.Builder<Block, BlockState> 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<ItemStack> 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;
}
}

View File

@ -1,27 +1,16 @@
package io.github.cottonmc.templates.block; 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.Block;
import net.minecraft.block.BlockEntityProvider; import net.minecraft.block.BlockEntityProvider;
import net.minecraft.block.BlockState; 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.LivingEntity;
import net.minecraft.entity.player.PlayerEntity; 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.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.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand; 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.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
@ -30,120 +19,48 @@ import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public abstract class TemplateBlock extends Block implements BlockEntityProvider { 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) { public TemplateBlock(Settings settings) {
super(settings); super(settings);
setDefaultState(TemplateInteractionUtil.setDefaultStates(getDefaultState()));
setDefaultState(getDefaultState().with(LIGHT, 0).with(REDSTONE, false));
}
public static Settings configureSettings(Settings s) {
return s
.luminance(state -> ((TemplateBlock) state.getBlock()).luminance(state))
.nonOpaque();
} }
@Override
public abstract @Nullable BlockEntity createBlockEntity(BlockPos blockPos, BlockState blockState);
@Override @Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
super.appendProperties(builder.add(LIGHT, REDSTONE)); super.appendProperties(TemplateInteractionUtil.appendProperties(builder));
} }
@Override @Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { 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 return TemplateInteractionUtil.onUse(state, world, pos, player, hand, hit);
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;
} }
@Override @Override
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
if(!state.isOf(newState.getBlock()) && world.getBlockEntity(pos) instanceof TemplateEntity template) { TemplateInteractionUtil.onStateReplaced(state, world, pos, newState, moved);
DefaultedList<ItemStack> 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);
}
super.onStateReplaced(state, world, pos, newState, moved); super.onStateReplaced(state, world, pos, newState, moved);
} }
@Override @Override
public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { 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. TemplateInteractionUtil.onPlaced(world, pos, state, placer, stack);
//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);
}
super.onPlaced(world, pos, state, placer, stack); super.onPlaced(world, pos, state, placer, stack);
} }
@Override @Override
public boolean emitsRedstonePower(BlockState state) { public boolean emitsRedstonePower(BlockState state) {
return state.get(REDSTONE); return TemplateInteractionUtil.emitsRedstonePower(state);
} }
@Override @Override
public int getWeakRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { 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 @Override
public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) { public int getStrongRedstonePower(BlockState state, BlockView view, BlockPos pos, Direction dir) {
return state.get(REDSTONE) ? 15 : 0; return TemplateInteractionUtil.getStrongRedstonePower(state, view, pos, dir);
}
public int luminance(BlockState state) {
return state.get(LIGHT);
} }
} }

View File

@ -1,24 +1,29 @@
package io.github.cottonmc.templates.block; package io.github.cottonmc.templates.block;
import io.github.cottonmc.templates.Templates; 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.BlockEntityProvider;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.SlabBlock; import net.minecraft.block.SlabBlock;
import net.minecraft.block.entity.BlockEntity; 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.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public class TemplateSlabBlock extends SlabBlock implements BlockEntityProvider { public class TemplateSlabBlock extends SlabBlock implements BlockEntityProvider {
public TemplateSlabBlock(Settings settings) { public TemplateSlabBlock(Settings settings) {
super(settings); super(settings);
} setDefaultState(TemplateInteractionUtil.setDefaultStates(getDefaultState()));
public TemplateSlabBlock() {
//super(TemplateBlock.configureSettings(Settings.create()) //TODO
super(Settings.create().nonOpaque()
.sounds(BlockSoundGroup.WOOD)
.hardness(0.2f)); //TODO: Material.WOOD
} }
@Nullable @Nullable
@ -26,4 +31,40 @@ public class TemplateSlabBlock extends SlabBlock implements BlockEntityProvider
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return Templates.SLAB_ENTITY.instantiate(pos, state); return Templates.SLAB_ENTITY.instantiate(pos, state);
} }
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> 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);
}
} }

View File

@ -1,13 +1,11 @@
package io.github.cottonmc.templates.block; package io.github.cottonmc.templates.block;
import io.github.cottonmc.templates.Templates; 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.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.ShapeContext; import net.minecraft.block.ShapeContext;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemPlacementContext;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
import net.minecraft.state.property.DirectionProperty; import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties; import net.minecraft.state.property.Properties;
@ -19,7 +17,7 @@ import net.minecraft.world.BlockView;
import javax.annotation.Nullable; 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 DirectionProperty FACING = Properties.HORIZONTAL_FACING;
public static final VoxelShape BASE = VoxelShapes.cuboid(0f, 0f, 0f, 1f, 0.5f, 1f); 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 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 static final VoxelShape WEST = VoxelShapes.cuboid(0f, 0.5f, 0f, 0.5f, 1f, 1f);
public SlopeBlock() { public TemplateSlopeBlock(Settings settings) {
super(TemplateBlock.configureSettings(Settings.create()) super(settings);
.sounds(BlockSoundGroup.WOOD)
.hardness(0.2f)); //TODO: Material.WOOD
setDefaultState(getDefaultState().with(FACING, Direction.NORTH)); setDefaultState(getDefaultState().with(FACING, Direction.NORTH));
} }

View File

@ -2,7 +2,6 @@ package io.github.cottonmc.templates.mixin.particles;
import io.github.cottonmc.templates.api.ThemeableBlockEntity; import io.github.cottonmc.templates.api.ThemeableBlockEntity;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;

View File

@ -2,7 +2,6 @@ package io.github.cottonmc.templates.mixin.particles;
import io.github.cottonmc.templates.api.ThemeableBlockEntity; import io.github.cottonmc.templates.api.ThemeableBlockEntity;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;

View File

@ -17,7 +17,11 @@ import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad; import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.texture.Sprite; import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.SpriteIdentifier; import net.minecraft.client.util.SpriteIdentifier;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack; 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.screen.PlayerScreenHandler;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
@ -31,9 +35,10 @@ import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
public class RetexturedJsonModelBakedModel extends ForwardingBakedModel { public class RetexturedJsonModelBakedModel extends ForwardingBakedModel {
public RetexturedJsonModelBakedModel(BakedModel baseModel, TemplateAppearanceManager tam, Function<SpriteIdentifier, Sprite> spriteLookup) { public RetexturedJsonModelBakedModel(BakedModel baseModel, TemplateAppearanceManager tam, Function<SpriteIdentifier, Sprite> spriteLookup, BlockState itemModelState) {
this.wrapped = baseModel; this.wrapped = baseModel;
this.tam = tam; this.tam = tam;
this.itemModelState = itemModelState;
for(int i = 0; i < DIRECTIONS.length; i++) { for(int i = 0; i < DIRECTIONS.length; i++) {
SpriteIdentifier id = new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, Templates.id("templates_special/" + DIRECTIONS[i].getName())); 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 record CacheKey(BlockState state, TemplateAppearance appearance) {}
private final TemplateAppearanceManager tam; private final TemplateAppearanceManager tam;
private final ConcurrentHashMap<CacheKey, Mesh> meshCache = new ConcurrentHashMap<>(); private final ConcurrentHashMap<CacheKey, Mesh> meshCache = new ConcurrentHashMap<>();
private final Sprite[] specialSprites = new Sprite[DIRECTIONS.length]; private final Sprite[] specialSprites = new Sprite[DIRECTIONS.length];
private final BlockState itemModelState;
@Override @Override
public boolean isVanillaAdapter() { public boolean isVanillaAdapter() {
@ -54,8 +61,8 @@ public class RetexturedJsonModelBakedModel extends ForwardingBakedModel {
@Override @Override
public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) { public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
BlockState template = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null; BlockState theme = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null;
TemplateAppearance ta = template == null || template.isAir() ? tam.getDefaultAppearance() : tam.getAppearance(template); TemplateAppearance ta = theme == null || theme.isAir() ? tam.getDefaultAppearance() : tam.getAppearance(theme);
CacheKey key = new CacheKey(state, ta); CacheKey key = new CacheKey(state, ta);
context.meshConsumer().accept(meshCache.computeIfAbsent(key, this::makeMesh)); context.meshConsumer().accept(meshCache.computeIfAbsent(key, this::makeMesh));
@ -63,7 +70,17 @@ public class RetexturedJsonModelBakedModel extends ForwardingBakedModel {
@Override @Override
public void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context) { public void emitItemQuads(ItemStack stack, Supplier<Random> 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) { protected Mesh makeMesh(CacheKey key) {

View File

@ -1,6 +1,8 @@
package io.github.cottonmc.templates.model; package io.github.cottonmc.templates.model;
import io.github.cottonmc.templates.TemplatesClient; 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.BakedModel;
import net.minecraft.client.render.model.Baker; import net.minecraft.client.render.model.Baker;
import net.minecraft.client.render.model.ModelBakeSettings; import net.minecraft.client.render.model.ModelBakeSettings;
@ -16,10 +18,16 @@ import java.util.function.Function;
public class RetexturedJsonModelUnbakedModel implements UnbakedModel { public class RetexturedJsonModelUnbakedModel implements UnbakedModel {
public RetexturedJsonModelUnbakedModel(Identifier parent) { public RetexturedJsonModelUnbakedModel(Identifier parent) {
this(parent, Blocks.AIR.getDefaultState());
}
public RetexturedJsonModelUnbakedModel(Identifier parent, BlockState itemModelState) {
this.parent = parent; this.parent = parent;
this.itemModelState = itemModelState;
} }
protected final Identifier parent; protected final Identifier parent;
protected final BlockState itemModelState;
@Override @Override
public Collection<Identifier> getModelDependencies() { public Collection<Identifier> getModelDependencies() {
@ -37,7 +45,8 @@ public class RetexturedJsonModelUnbakedModel implements UnbakedModel {
return new RetexturedJsonModelBakedModel( return new RetexturedJsonModelBakedModel(
baker.bake(parent, modelBakeSettings), baker.bake(parent, modelBakeSettings),
TemplatesClient.provider.getOrCreateTemplateApperanceManager(spriteLookup), TemplatesClient.provider.getOrCreateTemplateApperanceManager(spriteLookup),
spriteLookup spriteLookup,
itemModelState
); );
} }
} }

View File

@ -57,12 +57,12 @@ public final class RetexturedMeshBakedModel extends ForwardingBakedModel {
} }
public @NotNull RenderContext.QuadTransform retexturingBlockTransformer(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier) { public @NotNull RenderContext.QuadTransform retexturingBlockTransformer(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier) {
BlockState template = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null; BlockState theme = (((RenderAttachedBlockView) blockView).getBlockEntityRenderAttachment(pos) instanceof BlockState s) ? s : null;
if(template == null || template.isAir()) return new RetexturingTransformer(tam.getDefaultAppearance(), 0xFFFFFFFF, facePermutation); 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; 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<Random> randomSupplier) { public @NotNull RenderContext.QuadTransform retexturingItemTransformer(ItemStack stack, Supplier<Random> randomSupplier) {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 536 B

After

Width:  |  Height:  |  Size: 504 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 528 B

After

Width:  |  Height:  |  Size: 508 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 547 B

After

Width:  |  Height:  |  Size: 511 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 564 B

After

Width:  |  Height:  |  Size: 518 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 525 B

After

Width:  |  Height:  |  Size: 500 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 545 B

After

Width:  |  Height:  |  Size: 505 B

View File

@ -2,7 +2,8 @@
"parent": "minecraft:recipes/root", "parent": "minecraft:recipes/root",
"rewards": { "rewards": {
"recipes": [ "recipes": [
"minecraft:scaffolding" "templates:slope",
"templates:slab"
] ]
}, },
"criteria": { "criteria": {

View File

@ -0,0 +1,19 @@
{
"type": "minecraft:block",
"pools": [
{
"rolls": 1,
"entries": [
{
"type": "minecraft:item",
"name": "templates:slab"
}
],
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
]
}
]
}

View File

@ -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"
}

View File

@ -16,5 +16,6 @@
"result": { "result": {
"item": "templates:slope", "item": "templates:slope",
"count": 4 "count": 4
} },
"group": "templates"
} }

View File

@ -5,7 +5,7 @@
"name": "Templates", "name": "Templates",
"icon": "assets/templates/icon.png", "icon": "assets/templates/icon.png",
"description": "An API for templated blocks", "description": "An API for templated blocks",
"licence": "MIT", "license": "MIT",
"contact": { "contact": {
"sources": "https://github.com/CottonMC/Templates" "sources": "https://github.com/CottonMC/Templates"
}, },