Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ default boolean isHorizontalSlab(ItemStack stack) {
Half getHalf(BlockGetter level, BlockPos pos, BlockState state);

BlockState getStateForHalf(BlockGetter level, BlockPos pos, BlockState state, Half half);


boolean isHalf(BlockState state, Half half);
boolean isDoubleSlab(BlockState state);

default boolean areSameTypeOfSlab(BlockState state, ItemStack stack) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cjminecraft.doubleslabs.client.model;

import cjminecraft.doubleslabs.api.helpers.IHorizontalSlabHelper;
import cjminecraft.doubleslabs.api.state.Half;
import cjminecraft.doubleslabs.common.Constants;
import cjminecraft.doubleslabs.common.Internal;
import cjminecraft.doubleslabs.library.helpers.VerticalSlabModelHelper;
Expand All @@ -10,19 +11,22 @@
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.*;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class VerticalSlabModelBaker {

private final Map<BlockState, Map<Direction, BakedModel>> verticalModels = new HashMap<>();
private final Map<Block, Map<Direction, BakedModel>> verticalModels = new HashMap<>();
private final Map<Item, BakedModel> itemModels = new HashMap<>();

protected final PlatformIndependentModelBaker modelBaker;
Expand All @@ -44,7 +48,7 @@ public void bakeBlocks(final Iterable<Block> blocks) {
final var helper = Internal.getSlabHelper().getHorizontalSlabHelper(block);

if (helper.isPresent()) {
bakeVariants(block, helper.get());
bakeVariants(block);
count++;
}
}
Expand Down Expand Up @@ -82,24 +86,24 @@ private void bake(final Item item, final ResourceLocation registryName) {
itemModels.put(item, bakedModel);
}

private void bakeVariants(final Block block, final IHorizontalSlabHelper helper) {
block.getStateDefinition().getPossibleStates().stream()
.filter(Predicates.not(helper::isDoubleSlab)
.and(state -> !state.hasProperty(BlockStateProperties.WATERLOGGED)
|| !state.getValue(BlockStateProperties.WATERLOGGED)))
.forEach(this::bake);
private void bakeVariants(final Block block) {
final var directionalModels = Maps.<Direction, BakedModel>newEnumMap(Direction.class);
var resourceLocation = BuiltInRegistries.BLOCK.getKey(block).withPrefix("block/");
directionalModels.put(Direction.NORTH, bakeVariant(resourceLocation, Direction.NORTH));
directionalModels.put(Direction.SOUTH, bakeVariant(resourceLocation, Direction.SOUTH));
directionalModels.put(Direction.EAST, bakeVariant(resourceLocation, Direction.EAST));
directionalModels.put(Direction.WEST, bakeVariant(resourceLocation, Direction.WEST));
verticalModels.put(block, directionalModels);
}

private void bake(final BlockState state) {
final var resourceLocation = BlockModelShaper.stateToModelLocation(state).id().withPrefix("block/");

final var directionalModels = Maps.<Direction, BakedModel>newEnumMap(Direction.class);
directionalModels.put(Direction.NORTH, modelBaker.bake(resourceLocation, BlockModelRotation.X90_Y180));
directionalModels.put(Direction.EAST, modelBaker.bake(resourceLocation, BlockModelRotation.X90_Y270));
directionalModels.put(Direction.SOUTH, modelBaker.bake(resourceLocation, BlockModelRotation.X90_Y0));
directionalModels.put(Direction.WEST, modelBaker.bake(resourceLocation, BlockModelRotation.X90_Y90));

verticalModels.put(state, directionalModels);
}


private @Nullable BakedModel bakeVariant(final ResourceLocation resourceLocation, final Direction direction) {
//final var resourceLocationVertical = resourceLocation.withSuffix("_vertical_" + direction.toString());
final var resourceLocationHorizontal = (direction == Direction.SOUTH || direction == Direction.EAST) ? resourceLocation.withSuffix("_top") : resourceLocation;
final var rotation = switch (direction) {
case Direction.NORTH, Direction.SOUTH -> BlockModelRotation.X90_Y180;
case Direction.WEST, Direction.EAST -> BlockModelRotation.X90_Y90;
default -> BlockModelRotation.X0_Y0;
};
return modelBaker.bake(resourceLocationHorizontal, rotation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.Direction;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;

import java.util.Map;

public class VerticalSlabModelHelper implements IVerticalSlabModelHelper {

private final Map<BlockState, Map<Direction, BakedModel>> verticalModels;
private final Map<Block, Map<Direction, BakedModel>> verticalModels;
private final Map<Item, BakedModel> itemModels;

public VerticalSlabModelHelper(Map<BlockState, Map<Direction, BakedModel>> verticalModels, Map<Item, BakedModel> itemModels) {
public VerticalSlabModelHelper(Map<Block, Map<Direction, BakedModel>> verticalModels, Map<Item, BakedModel> itemModels) {
this.verticalModels = verticalModels;
this.itemModels = itemModels;
}

@Override
public BakedModel getVerticalSlabModel(BlockState state, Direction side) {
return verticalModels.get(state).get(side);
return verticalModels.get(state.getBlock()).get(side);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public Half getHalf(BlockGetter level, BlockPos pos, BlockState state) {
case DOUBLE -> throw new IllegalStateException("Cannot get the half for a double slab");
};
}

@Override
public boolean isHalf(BlockState state, Half half) {
return switch (state.getValue(BlockStateProperties.SLAB_TYPE)) {
case BOTTOM -> half == Half.NEGATIVE;
case TOP -> half == Half.POSITIVE;
default -> false;
};
}

@Override
public boolean isDoubleSlab(BlockState state) {
Expand Down