Skip to content

Commit

Permalink
Gifts + janky item model predicates and String Lights (WIP, placehold…
Browse files Browse the repository at this point in the history
…er function)
  • Loading branch information
crispytwig committed Dec 1, 2024
1 parent 758326d commit 1ec3777
Show file tree
Hide file tree
Showing 547 changed files with 6,422 additions and 215 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package com.starfish_studios.seasons_greetings.block;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.mojang.serialization.MapCodec;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.*;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
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.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class WrappedBlock extends Block {

public static final MapCodec<WrappedBlock> CODEC = simpleCodec(WrappedBlock::new);
public static final BooleanProperty NORTH = PipeBlock.NORTH;
public static final BooleanProperty EAST = PipeBlock.EAST;
public static final BooleanProperty SOUTH = PipeBlock.SOUTH;
public static final BooleanProperty WEST = PipeBlock.WEST;
public static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = ImmutableMap.copyOf((Map) Util.make(Maps.newEnumMap(Direction.class), (enumMap) -> {
enumMap.put(Direction.NORTH, NORTH);
enumMap.put(Direction.EAST, EAST);
enumMap.put(Direction.SOUTH, SOUTH);
enumMap.put(Direction.WEST, WEST);
}));
private static final VoxelShape WEST_AABB = Block.box(0.0, 0.0, 0.0, 1.0, 16.0, 16.0);
private static final VoxelShape EAST_AABB = Block.box(15.0, 0.0, 0.0, 16.0, 16.0, 16.0);
private static final VoxelShape NORTH_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 1.0);
private static final VoxelShape SOUTH_AABB = Block.box(0.0, 0.0, 15.0, 16.0, 16.0, 16.0);
private final Map<BlockState, VoxelShape> shapesCache;

public WrappedBlock(Properties properties) {
super(properties);
this.registerDefaultState(this.stateDefinition.any()
.setValue(NORTH, false)
.setValue(EAST, false)
.setValue(SOUTH, false)
.setValue(WEST, false));
this.shapesCache = ImmutableMap.copyOf((Map)this.stateDefinition.getPossibleStates().stream().collect(Collectors.toMap(Function.identity(), WrappedBlock::calculateShape)));
}

private static VoxelShape calculateShape(BlockState blockState) {
VoxelShape voxelShape = Shapes.empty();

if (blockState.getValue(NORTH)) {
voxelShape = Shapes.or(voxelShape, NORTH_AABB);
}

if (blockState.getValue(SOUTH)) {
voxelShape = Shapes.or(voxelShape, SOUTH_AABB);
}

if (blockState.getValue(EAST)) {
voxelShape = Shapes.or(voxelShape, EAST_AABB);
}

if (blockState.getValue(WEST)) {
voxelShape = Shapes.or(voxelShape, WEST_AABB);
}

return voxelShape.isEmpty() ? Shapes.block() : voxelShape;
}

protected @NotNull VoxelShape getShape(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos, CollisionContext collisionContext) {
return this.shapesCache.get(blockState);
}

protected boolean propagatesSkylightDown(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos) {
return true;
}

protected boolean canSurvive(BlockState blockState, LevelReader levelReader, BlockPos blockPos) {
return this.hasFaces(this.getUpdatedState(blockState, levelReader, blockPos));
}

private boolean hasFaces(BlockState blockState) {
return this.countFaces(blockState) > 0;
}

private int countFaces(BlockState blockState) {
int i = 0;
for (BooleanProperty booleanProperty : PROPERTY_BY_DIRECTION.values()) {
if (blockState.getValue(booleanProperty)) {
++i;
}
}

return i;
}

private boolean canSupportAtFace(BlockGetter blockGetter, BlockPos blockPos, Direction direction) {
BlockPos blockPos2 = blockPos.relative(direction);
if (isAcceptableNeighbour(blockGetter, blockPos2, direction)) {
return true;
} else if (direction.getAxis() == Direction.Axis.Y) {
return false;
} else {
BooleanProperty booleanProperty = PROPERTY_BY_DIRECTION.get(direction);
BlockState blockState = blockGetter.getBlockState(blockPos.above());
return blockState.is(this) && blockState.getValue(booleanProperty);
}
}

public static boolean isAcceptableNeighbour(BlockGetter blockGetter, BlockPos blockPos, Direction direction) {
return MultifaceBlock.canAttachTo(blockGetter, direction, blockPos, blockGetter.getBlockState(blockPos));
}

private BlockState getUpdatedState(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos) {
BlockPos blockPos2 = blockPos.above();

BlockState blockState2 = null;
Iterator<Direction> var6 = Direction.Plane.HORIZONTAL.iterator();

while(true) {
Direction direction;
BooleanProperty booleanProperty;
do {
if (!var6.hasNext()) {
return blockState;
}

direction = var6.next();
booleanProperty = getPropertyForFace(direction);
} while(!(Boolean)blockState.getValue(booleanProperty));

boolean bl = this.canSupportAtFace(blockGetter, blockPos, direction);
if (!bl) {
if (blockState2 == null) {
blockState2 = blockGetter.getBlockState(blockPos2);
}

bl = blockState2.is(this) && blockState2.getValue(booleanProperty);
}

blockState = blockState.setValue(booleanProperty, bl);
}
}

protected @NotNull BlockState updateShape(BlockState blockState, Direction direction, BlockState blockState2, LevelAccessor levelAccessor, BlockPos blockPos, BlockPos blockPos2) {
BlockState blockState3 = this.getUpdatedState(blockState, levelAccessor, blockPos);
return !this.hasFaces(blockState3) ? Blocks.AIR.defaultBlockState() : blockState3;
}

protected boolean canBeReplaced(BlockState blockState, BlockPlaceContext blockPlaceContext) {
BlockState blockState2 = blockPlaceContext.getLevel().getBlockState(blockPlaceContext.getClickedPos());
if (blockState2.is(this)) {
return this.countFaces(blockState2) < PROPERTY_BY_DIRECTION.size();
} else {
return super.canBeReplaced(blockState, blockPlaceContext);
}
}

@Nullable
public BlockState getStateForPlacement(BlockPlaceContext blockPlaceContext) {
BlockState blockState = blockPlaceContext.getLevel().getBlockState(blockPlaceContext.getClickedPos());
boolean bl = blockState.is(this);
BlockState blockState2 = super.getStateForPlacement(blockPlaceContext);
if (blockState2 != null && !blockState2.is(Blocks.AIR)) {
blockState2 = blockState2.setValue(NORTH, this.canSupportAtFace(blockPlaceContext.getLevel(), blockPlaceContext.getClickedPos(), Direction.NORTH));
blockState2 = blockState2.setValue(EAST, this.canSupportAtFace(blockPlaceContext.getLevel(), blockPlaceContext.getClickedPos(), Direction.EAST));
blockState2 = blockState2.setValue(SOUTH, this.canSupportAtFace(blockPlaceContext.getLevel(), blockPlaceContext.getClickedPos(), Direction.SOUTH));
blockState2 = blockState2.setValue(WEST, this.canSupportAtFace(blockPlaceContext.getLevel(), blockPlaceContext.getClickedPos(), Direction.WEST));
return blockState2;
} else {
return null;
}
}

protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(NORTH, EAST, SOUTH, WEST);
}

protected @NotNull BlockState rotate(BlockState blockState, Rotation rotation) {
switch (rotation) {
case CLOCKWISE_180 -> {
return blockState.setValue(NORTH, blockState.getValue(SOUTH)).setValue(EAST, blockState.getValue(WEST)).setValue(SOUTH, blockState.getValue(NORTH)).setValue(WEST, blockState.getValue(EAST));
}
case COUNTERCLOCKWISE_90 -> {
return blockState.setValue(NORTH, blockState.getValue(EAST)).setValue(EAST, blockState.getValue(SOUTH)).setValue(SOUTH, blockState.getValue(WEST)).setValue(WEST, blockState.getValue(NORTH));
}
case CLOCKWISE_90 -> {
return blockState.setValue(NORTH, blockState.getValue(WEST)).setValue(EAST, blockState.getValue(NORTH)).setValue(SOUTH, blockState.getValue(EAST)).setValue(WEST, blockState.getValue(SOUTH));
}
default -> {
return blockState;
}
}
}

protected @NotNull BlockState mirror(BlockState blockState, Mirror mirror) {
switch (mirror) {
case LEFT_RIGHT -> {
return blockState.setValue(NORTH, blockState.getValue(SOUTH)).setValue(SOUTH, blockState.getValue(NORTH));
}
case FRONT_BACK -> {
return blockState.setValue(EAST, blockState.getValue(WEST)).setValue(WEST, blockState.getValue(EAST));
}
default -> {
return super.mirror(blockState, mirror);
}
}
}

public static BooleanProperty getPropertyForFace(Direction direction) {
return PROPERTY_BY_DIRECTION.get(direction);
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,66 @@
package com.starfish_studios.seasons_greetings.client;

import com.starfish_studios.seasons_greetings.SeasonsGreetings;
import com.starfish_studios.seasons_greetings.client.gui.screens.GiftBoxScreen;
import com.starfish_studios.seasons_greetings.item.GiftBoxItem;
import com.starfish_studios.seasons_greetings.registry.SGBlocks;
import com.starfish_studios.seasons_greetings.registry.SGItems;
import com.starfish_studios.seasons_greetings.registry.SGMenus;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.gui.screens.MenuScreens;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;

import java.awt.*;
import java.util.Objects;

@Environment(EnvType.CLIENT)
public class SeasonsGreetingsClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
registerRenderers();
registerScreens();
}

// public static final ModelLayerLocation
// SNOW_GOLEM_DECOR = new ModelLayerLocation(SeasonsGreetings.id("snow_golem_decor"), mainString);

ItemProperties.register(SGItems.RED_GIFT_BOX, SeasonsGreetings.id("bow_color"), (stack, world, entity, num) -> {
CustomData customData = stack.get(DataComponents.BLOCK_ENTITY_DATA);
String variantString = "bow";
if (customData != null) {
CompoundTag tag = customData.copyTag();
return tag.contains(variantString) ? switch (tag.getString(variantString)) {
case "white" -> 0.0625F;
case "orange" -> 0.125F;
case "magenta" -> 0.1875F;
case "light_blue" -> 0.25F;
case "yellow" -> 0.3125F;
case "lime" -> 0.375F;
case "pink" -> 0.4375F;
case "gray" -> 0.5F;
case "light_gray" -> 0.5625F;
case "cyan" -> 0.625F;
case "purple" -> 0.6875F;
case "blue" -> 0.75F;
case "brown" -> 0.8125F;
case "green" -> 0.875F;
case "red" -> 0.9375F;
case "black" -> 1.0F;
default -> 0.0F;
} : 0.0F;
} else {
return 0.0F;
}
});
}

public static void registerScreens() {
MenuScreens.register(SGMenus.GIFT_BOX, GiftBoxScreen::new);
Expand All @@ -29,6 +69,8 @@ public static void registerScreens() {
@SuppressWarnings("all")
public static void registerRenderers() {
BlockRenderLayerMap.INSTANCE.putBlocks(RenderType.cutout(),
SGBlocks.STRING_LIGHTS,

SGBlocks.WHITE_GIFT_BOX,
SGBlocks.LIGHT_GRAY_GIFT_BOX,
SGBlocks.GRAY_GIFT_BOX,
Expand All @@ -47,6 +89,7 @@ public static void registerRenderers() {
SGBlocks.PINK_GIFT_BOX
);


// EntityModelLayerRegistry.registerModelLayer(SNOW_GOLEM_DECOR, () -> SnowGolemModel.createBodyLayer());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public class SnowGolemNosesLayer extends RenderLayer<SnowGolem, SnowGolemModel<S
hashMap.put(Items.WHEAT, SeasonsGreetings.id("textures/entity/snow_golem/nose/wheat.png"));
});

// if (this.isAlive() && this.random.nextInt(1000) < this.ambientSoundTime++) {
// Explain how this works in real words.


public SnowGolemNosesLayer(RenderLayerParent<SnowGolem, SnowGolemModel<SnowGolem>> renderLayerParent) {
super(renderLayerParent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@
import net.minecraft.world.level.block.Block;

import java.util.List;
import java.util.Objects;

public class GiftBoxItem extends BlockItem {
public GiftBoxItem(Block block, Properties properties) {
super(block, properties);
}

public void appendHoverText(ItemStack itemStack, TooltipContext tooltipContext, List<Component> list, TooltipFlag tooltipFlag) {
super.appendHoverText(itemStack, tooltipContext, list, tooltipFlag);
@Override
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, TooltipFlag flag) {
CustomData data = stack.get(DataComponents.BLOCK_ENTITY_DATA);
if (data != null && !data.isEmpty()) {
CompoundTag tag = data.copyTag();

if (tag.contains("bow")) {
tooltip.add(
Component.translatable("color.minecraft." + tag.getString("bow"))
.withStyle(ChatFormatting.GRAY)
.append(" ")
.append(Component.translatable("tooltip.seasons_greetings.bow")));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import com.starfish_studios.seasons_greetings.SeasonsGreetings;
import com.starfish_studios.seasons_greetings.block.GiftBoxBlock;
import com.starfish_studios.seasons_greetings.block.WrappedBlock;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.material.PushReaction;

public class SGBlocks {
// WHITE, LIGHT GRAY, GRAY, BLACK, BROWN, RED, ORANGE, YELLOW, LIME, GREEN, CYAN, LIGHT BLUE, BLUE, PURPLE, MAGENTA, PINK

public static final Block.Properties giftBoxProperties = Block.Properties.of().pushReaction(PushReaction.DESTROY).destroyTime(0F).noOcclusion().forceSolidOn();

public static final Block.Properties giftBoxProperties = Block.Properties.of().pushReaction(PushReaction.DESTROY).instabreak().noOcclusion();

public static final Block STRING_LIGHTS = registerBlock("string_lights", new WrappedBlock(Block.Properties.of().instabreak().noCollission().noOcclusion()));

public static final Block WHITE_GIFT_BOX = registerBlock("white_gift_box", new GiftBoxBlock(DyeColor.WHITE, giftBoxProperties));
public static final Block LIGHT_GRAY_GIFT_BOX = registerBlock("light_gray_gift_box", new GiftBoxBlock(DyeColor.LIGHT_GRAY, giftBoxProperties));
Expand All @@ -30,6 +35,9 @@ public class SGBlocks {
public static final Block MAGENTA_GIFT_BOX = registerBlock("magenta_gift_box", new GiftBoxBlock(DyeColor.MAGENTA, giftBoxProperties));
public static final Block PINK_GIFT_BOX = registerBlock("pink_gift_box", new GiftBoxBlock(DyeColor.PINK, giftBoxProperties));

public static final Block[] GIFT_BOXES = new Block[DyeColor.values().length];


// public static final Block GIFT_BOX = registerBlock("gift_box", new GiftBoxBlock(DyeColor.WHITE, Block.Properties.of()));

// Registry
Expand Down
Loading

0 comments on commit 1ec3777

Please sign in to comment.