Skip to content
Merged
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
71 changes: 62 additions & 9 deletions src/main/java/com/minecolonies/core/blocks/BlockScarecrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
import com.minecolonies.api.colony.IColony;
import com.minecolonies.api.colony.IColonyManager;
import com.minecolonies.api.colony.buildingextensions.registry.BuildingExtensionRegistries;
import com.minecolonies.api.util.InventoryUtils;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.core.client.gui.containers.WindowField;
import com.minecolonies.core.colony.buildingextensions.FarmField;
import com.minecolonies.core.tileentities.TileEntityScarecrow;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Explosion;
Expand All @@ -27,16 +31,21 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.storage.loot.LootParams;
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 net.neoforged.neoforge.items.wrapper.InvWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* The net.minecraft.core.Directions, placement and activation.
*/
Expand All @@ -45,13 +54,15 @@ public class BlockScarecrow extends AbstractBlockMinecoloniesDefault<BlockScarec
{
public static final EnumProperty<DoubleBlockHalf> HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;

public static final BooleanProperty LANTERN = BooleanProperty.create("lantern");

/**
* Constructor called on block placement.
*/
public BlockScarecrow()
{
super(Properties.of().mapColor(MapColor.WOOD).sound(SoundType.WOOD).strength(HARDNESS, RESISTANCE));
this.registerDefaultState(this.defaultBlockState().setValue(FACING, Direction.NORTH).setValue(HALF, DoubleBlockHalf.LOWER));
this.registerDefaultState(this.defaultBlockState().setValue(FACING, Direction.NORTH).setValue(HALF, DoubleBlockHalf.LOWER).setValue(LANTERN, false));
}

@Override
Expand All @@ -60,6 +71,12 @@ public ResourceLocation getRegistryName()
return new ResourceLocation(Constants.MOD_ID, REGISTRY_NAME);
}

@Override
public int getLightEmission(final BlockState state, final BlockGetter level, final BlockPos pos)
{
return state.getValue(LANTERN) ? Blocks.LANTERN.defaultBlockState().getLightEmission(level, pos) : 0;
}

@Nullable
@Override
public BlockEntity newBlockEntity(@NotNull final BlockPos blockPos, @NotNull final BlockState blockState)
Expand All @@ -73,14 +90,22 @@ public BlockEntity newBlockEntity(@NotNull final BlockPos blockPos, @NotNull fin

@Override
public ItemInteractionResult useItemOn(
final ItemStack stack,
final BlockState state,
final Level worldIn,
final BlockPos pos,
final Player player,
final InteractionHand hand,
final BlockHitResult ray)
final ItemStack stack,
final BlockState state,
final Level worldIn,
final BlockPos pos,
final Player player,
final InteractionHand hand,
final BlockHitResult ray)
{
if (player.getItemInHand(hand).is(Items.LANTERN) && !state.getValue(LANTERN))
{
worldIn.setBlock(pos, state.setValue(LANTERN, true), 3);
worldIn.playSound(player, pos, SoundEvents.LANTERN_PLACE, SoundSource.BLOCKS, 1.0F, 1.0F);
InventoryUtils.reduceStackInItemHandler(new InvWrapper(player.getInventory()), player.getItemInHand(hand));
return ItemInteractionResult.CONSUME_PARTIAL;
}

// If the world is client, open the inventory of the field.
if (worldIn.isClientSide)
{
Expand Down Expand Up @@ -146,6 +171,34 @@ public VoxelShape getShape(
);
}

@Override
public void neighborChanged(final BlockState state, final Level worldIn, final BlockPos pos, final Block block, final BlockPos fromPos, final boolean isMoving)
{
super.neighborChanged(state, worldIn, pos, block, fromPos, isMoving);
final DoubleBlockHalf half = state.getValue(HALF);
final BlockPos otherPos = half == DoubleBlockHalf.LOWER ? pos.above() : pos.below();
if (!fromPos.equals(otherPos))
{
return;
}
final BlockState otherState = worldIn.getBlockState(otherPos);
if (otherState.getBlock() == this && otherState.getValue(HALF) != half && otherState.getValue(LANTERN) != state.getValue(LANTERN))
{
worldIn.setBlock(pos, state.setValue(LANTERN, otherState.getValue(LANTERN)), UPDATE_ALL);
}
}

@Override
public List<ItemStack> getDrops(final BlockState state, final LootParams.Builder params)
{
final List<ItemStack> drops = super.getDrops(state, params);
if (state.getValue(HALF) == DoubleBlockHalf.LOWER && state.getValue(LANTERN))
{
drops.add(new ItemStack(Items.LANTERN));
}
return drops;
}

@Override
public void wasExploded(final Level worldIn, final BlockPos pos, final Explosion explosionIn)
{
Expand Down Expand Up @@ -208,7 +261,7 @@ public BlockState playerWillDestroy(final Level worldIn, @NotNull final BlockPos
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder)
{
builder.add(HALF, FACING);
builder.add(HALF, FACING, LANTERN);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
Expand Down Expand Up @@ -129,6 +136,36 @@ public void render(

final VertexConsumer vertexConsumer = getMaterial(te).buffer(iRenderTypeBuffer, RenderType::entitySolid);
this.model.renderToBuffer(matrixStack, vertexConsumer, lightA, lightB, -1);

if (te.getBlockState().getValue(BlockScarecrow.LANTERN))
{
renderLantern(matrixStack, iRenderTypeBuffer, te.getBlockPos(), te.getLevel());
}

matrixStack.popPose();
}

private static void renderLantern(final PoseStack matrixStack, final MultiBufferSource buffer, final BlockPos pos, final Level level)
{
matrixStack.pushPose();

matrixStack.mulPose(Axis.ZP.rotationDegrees(180f));
matrixStack.translate(0.6f, -0.6f, -0.375f);

matrixStack.scale(0.75f, 0.65f, 0.75f);

final int blockLight = level.getBrightness(LightLayer.BLOCK, pos);
final int skyLight = level.getBrightness(LightLayer.SKY, pos);

Minecraft.getInstance()
.getBlockRenderer()
.renderSingleBlock(
Blocks.LANTERN.defaultBlockState(),
matrixStack,
buffer,
LightTexture.pack(blockLight, skyLight),
OverlayTexture.NO_OVERLAY);

matrixStack.popPose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Tuple;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.DoorBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand Down Expand Up @@ -72,6 +73,10 @@ public List<ItemStack> getRequiredItems(
if (blockState.getValue(DoorBlock.HALF).equals(DoubleBlockHalf.LOWER))
{
itemList.add(BlockUtils.getItemStackFromBlockState(blockState));
if (blockState.getValue(BlockScarecrow.LANTERN))
{
itemList.add(new ItemStack(Items.LANTERN));
}
}

return itemList;
Expand Down
Loading