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

@@ -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<TemplateEntity> 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<TemplateEntity> SLAB_ENTITY = Registry.register(
Registries.BLOCK_ENTITY_TYPE, id("slab"),
FabricBlockEntityTypeBuilder.create(Templates::makeSlabEntity, SLAB).build(null)

View File

@@ -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;

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;
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<Block, BlockState> 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<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);
}
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);
}
}

View File

@@ -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<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;
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));
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<SpriteIdentifier, Sprite> spriteLookup) {
public RetexturedJsonModelBakedModel(BakedModel baseModel, TemplateAppearanceManager tam, Function<SpriteIdentifier, Sprite> 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<CacheKey, Mesh> 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<Random> 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<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) {

View File

@@ -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<Identifier> getModelDependencies() {
@@ -37,7 +45,8 @@ public class RetexturedJsonModelUnbakedModel implements UnbakedModel {
return new RetexturedJsonModelBakedModel(
baker.bake(parent, modelBakeSettings),
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) {
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<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",
"rewards": {
"recipes": [
"minecraft:scaffolding"
"templates:slope",
"templates:slab"
]
},
"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": {
"item": "templates:slope",
"count": 4
}
},
"group": "templates"
}

View File

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