Skip to content

Commit

Permalink
add lit state to summoning table
Browse files Browse the repository at this point in the history
  • Loading branch information
afamiliarquiet committed Oct 18, 2024
1 parent 1f4eb64 commit 4755afb
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,37 @@
import io.github.afamiliarquiet.familiar_magic.block.entity.SummoningTableBlockEntity;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
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.gameevent.GameEvent;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.neoforge.common.ItemAbilities;
import net.neoforged.neoforge.common.ItemAbility;
import org.jetbrains.annotations.Nullable;

import javax.annotation.ParametersAreNonnullByDefault;
Expand All @@ -28,6 +43,9 @@
@ParametersAreNonnullByDefault
public class SummoningTableBlock extends BaseEntityBlock {
public static final MapCodec<SummoningTableBlock> CODEC = simpleCodec(SummoningTableBlock::new);

public static final BooleanProperty LIT = BlockStateProperties.LIT;

protected static final VoxelShape SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 12.0, 16.0);

@Override
Expand All @@ -37,6 +55,23 @@ protected MapCodec<? extends BaseEntityBlock> codec() {

public SummoningTableBlock(Properties properties) {
super(properties);
this.registerDefaultState(
this.stateDefinition
.any()
.setValue(LIT, Boolean.FALSE)
);
}

@Override
public @Nullable BlockState getToolModifiedState(BlockState state, UseOnContext context, ItemAbility itemAbility, boolean simulate) {
if (itemAbility == ItemAbilities.FIRESTARTER_LIGHT && !state.getValue(LIT)) {
// neoforge docs say calling state#setValue without saving it back into state does nothing, so..
// hopefully simulate is happy :)
// actually this shouldn't directly set lit - probably need to hand off to the blockentity here. but that's later
return state.setValue(LIT, Boolean.TRUE);
} else {
return super.getToolModifiedState(state, context, itemAbility, simulate);
}
}

protected boolean useShapeForLightOcclusion(BlockState state) {
Expand Down Expand Up @@ -79,8 +114,57 @@ protected InteractionResult useWithoutItem(BlockState state, Level level, BlockP
return InteractionResult.sidedSuccess(level.isClientSide);
}

@Override
protected ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (stack.isEmpty() && player.getAbilities().mayBuild && state.getValue(LIT)) {
extinguish(player, state, level, pos);
return ItemInteractionResult.sidedSuccess(level.isClientSide);
} else {
return super.useItemOn(stack, state, level, pos, player, hand, hitResult);
}
}

public static void extinguish(Player player, BlockState state, LevelAccessor level, BlockPos pos) {
level.setBlock(pos, state.setValue(LIT, false), UPDATE_ALL_IMMEDIATE);

level.addParticle(
ParticleTypes.SMOKE,
(double) pos.getX() + 0.5,
(double) pos.getY() + 0.75,
(double) pos.getZ() + 0.5,
0.0,
0.1F,
0.0
);

// using glass break to copy nether portal break for now. maybe will change later
level.playSound(null, pos, SoundEvents.GLASS_BREAK, SoundSource.BLOCKS, 1.0F, 1.0F);
level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
}

@Override
public void animateTick(BlockState state, Level level, BlockPos pos, RandomSource random) {
if (state.getValue(LIT)) {
if (random.nextInt(100) == 0) {
// truly just ripping this whole thing from respawn anchor. as usual, might change sound later. unlikely
level.playLocalSound(pos, SoundEvents.RESPAWN_ANCHOR_AMBIENT, SoundSource.BLOCKS, 1.0F, 1.0F, false);
}

double d0 = (double)pos.getX() + 0.5 + (0.5 - random.nextDouble());
double d1 = (double)pos.getY() + 0.75;
double d2 = (double)pos.getZ() + 0.5 + (0.5 - random.nextDouble());
double d3 = (double)random.nextFloat() * 0.04;
level.addParticle(ParticleTypes.REVERSE_PORTAL, d0, d1, d2, 0.0, d3, 0.0);
}
}

@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}

@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(LIT);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"variants": {
"": {
"lit=false": {
"model": "familiar_magic:block/summoning_table"
},
"lit=true": {
"model": "familiar_magic:block/summoning_table_lit"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"parent": "minecraft:block/block",
"textures": {
"particle": "familiar_magic:block/summoning_table_bottom",
"bottom": "familiar_magic:block/summoning_table_bottom",
"top": "familiar_magic:block/summoning_table_top_lit",
"side": "familiar_magic:block/summoning_table_side"
},
"elements": [
{ "from": [ 0, 0, 0 ],
"to": [ 16, 12, 16 ],
"faces": {
"down": { "uv": [ 0, 0, 16, 16 ], "texture": "#bottom", "cullface": "down" },
"up": { "uv": [ 0, 0, 16, 16 ], "texture": "#top" },
"north": { "uv": [ 0, 4, 16, 16 ], "texture": "#side", "cullface": "north" },
"south": { "uv": [ 0, 4, 16, 16 ], "texture": "#side", "cullface": "south" },
"west": { "uv": [ 0, 4, 16, 16 ], "texture": "#side", "cullface": "west" },
"east": { "uv": [ 0, 4, 16, 16 ], "texture": "#side", "cullface": "east" }
}
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"animation": {}
}

0 comments on commit 4755afb

Please sign in to comment.