Skip to content

Commit

Permalink
table interactions and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
IchHabeHunger54 committed Feb 29, 2024
1 parent c90dcfd commit 9d103bf
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ private static void registerMenuScreens(RegisterMenuScreensEvent event) {
@SubscribeEvent
private static void registerAdditional(ModelEvent.RegisterAdditional event) {
for (TableBlock.Type type : TableBlock.Type.values()) {
for (BibliocraftWoodType woodType : BibliocraftWoodTypeRegistry.get().getAll()) {
event.register(new ResourceLocation(Bibliocraft.MOD_ID, "block/" + woodType.getRegistrationPrefix() + "_table_" + type.getSerializedName()));
}
for (DyeColor color : DyeColor.values()) {
event.register(new ResourceLocation(Bibliocraft.MOD_ID, "block/table_cloth_" + type.getSerializedName() + "_" + color.getSerializedName()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static void rebuildClothModelCache() {
for (TableBlock.Type type : TableBlock.Type.values()) {
Map<DyeColor, BakedModel> map = new HashMap<>();
for (DyeColor color : DyeColor.values()) {
map.put(color, models.getModel(new ResourceLocation(Bibliocraft.MOD_ID, "table_cloth_" + type.getSerializedName() + "_" + color.getSerializedName())));
map.put(color, models.getModel(new ResourceLocation(Bibliocraft.MOD_ID, "block/table_cloth_" + type.getSerializedName() + "_" + color.getSerializedName())));
}
CLOTH_MAP.put(type, map);
}
Expand Down Expand Up @@ -154,6 +154,7 @@ public BakedModel applyTransform(ItemDisplayContext transformType, PoseStack pos
}
}

@SuppressWarnings("UnusedReturnValue")
public static class LoaderBuilder extends CustomLoaderBuilder<BlockModelBuilder> {
private final Map<TableBlock.Type, JsonObject> modelMap = new HashMap<>();
private ResourceLocation particle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.WoolCarpetBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;

Expand Down Expand Up @@ -62,6 +72,11 @@ public TableBlock(Properties properties) {
registerDefaultState(getStateDefinition().any().setValue(FACING, Direction.NORTH).setValue(WATERLOGGED, false).setValue(TYPE, Type.NONE));
}

@Nullable
public static DyeColor getCarpetColor(ItemStack stack) {
return stack.getItem() instanceof BlockItem bi && bi.getBlock() instanceof WoolCarpetBlock carpet ? carpet.getColor() : null;
}

@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new TableBlockEntity(pos, state);
Expand Down Expand Up @@ -93,6 +108,28 @@ public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, Co
};
}

@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (!(blockEntity instanceof TableBlockEntity table)) return super.use(state, level, pos, player, hand, hit);
Direction direction = hit.getDirection();
if (direction == Direction.DOWN) return super.use(state, level, pos, player, hand, hit);
ItemStack stack = player.getItemInHand(hand);
boolean useCarpet = direction != Direction.UP && (getCarpetColor(stack) != null || stack.isEmpty());
ItemStack originalStack = table.getItem(useCarpet ? 1 : 0);
if (ItemStack.isSameItem(stack, originalStack)) return InteractionResult.FAIL;
table.setItem(useCarpet ? 1 : 0, stack.copyWithCount(1));
stack.shrink(1);
if (!originalStack.isEmpty()) {
if (stack.isEmpty()) {
player.setItemInHand(hand, originalStack);
} else {
player.getInventory().add(originalStack);
}
}
return InteractionResult.SUCCESS;
}

public enum Type implements StringRepresentable {
NONE,
ONE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ public TableBlockEntity(BlockPos pos, BlockState state) {
super(BCBlockEntities.TABLE.get(), 2, pos, state);
}

public ItemStack getCarpet() {
return getItem(1);
}

public void setCarpet(ItemStack stack) {
setItem(1, stack);
@Override
public void setItem(int slot, ItemStack stack) {
super.setItem(slot, stack);
if (slot == 1) {
requestModelDataUpdate();
}
}

@Override
public ModelData getModelData() {
ModelData.Builder builder = ModelData.builder();
builder.with(TYPE_PROPERTY, getBlockState().getValue(TableBlock.TYPE));
if (getCarpet().getItem() instanceof BlockItem blockItem && blockItem.getBlock() instanceof WoolCarpetBlock carpet) {
builder.with(COLOR_PROPERTY, carpet.getColor());
DyeColor color = TableBlock.getCarpetColor(getItem(1));
if (color != null) {
builder.with(COLOR_PROPERTY, color);
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@
"up": {"uv": [0, 0, 10, 0], "texture": "#missing"},
"down": {"uv": [0, 0, 10, 0], "texture": "#missing"}
}
},
{
"name": "cloth_west",
"from": [0, 13, 0],
"to": [0, 16, 13],
"faces": {
"north": {"uv": [0, 0, 0, 3], "texture": "#missing"},
"east": {"uv": [3, 0, 16, 3], "texture": "#1"},
"south": {"uv": [0, 0, 0, 3], "texture": "#missing"},
"west": {"uv": [0, 0, 13, 3], "texture": "#1"},
"up": {"uv": [0, 0, 0, 10], "texture": "#missing"},
"down": {"uv": [0, 0, 0, 10], "texture": "#missing"}
}
}
]
}
2 changes: 1 addition & 1 deletion src/main/resources/blockbench/table.bbmodel

Large diffs are not rendered by default.

0 comments on commit 9d103bf

Please sign in to comment.