Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subatom/fields #874

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
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
28 changes: 16 additions & 12 deletions src/main/java/be/minelabs/block/blocks/QuantumfieldBlock.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package be.minelabs.block.blocks;

import be.minelabs.Minelabs;
import be.minelabs.block.Blocks;
import be.minelabs.block.entity.QuantumFieldBlockEntity;
import be.minelabs.state.property.Properties;
import be.minelabs.world.dimension.ModDimensions;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
Expand Down Expand Up @@ -123,18 +125,18 @@ protected void appendProperties(StateManager.Builder<Block, BlockState> builder)
builder.add(AGE, MASTER, CLOUD);
}

public void removeQuantumBlockIfNeeded(BlockState state, ServerWorld world, BlockPos pos) {
if (MAX_AGE == getAge(state)) {
world.removeBlock(pos, false);
}
}

@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
removeQuantumBlockIfNeeded(state, world, pos);
super.scheduledTick(state, world, pos, random);
}

public void removeQuantumBlockIfNeeded(BlockState state, ServerWorld world, BlockPos pos) {
if (MAX_AGE == getAge(state)) {
world.removeBlock(pos, false);
}
}

@Override
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return isMaster(state) ? createMasterBlockEntity(pos, state) : null;
Expand All @@ -147,26 +149,27 @@ public BlockEntity createMasterBlockEntity(BlockPos pos, BlockState state) {
@Override
public void afterBreak(World world, PlayerEntity player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack stack) {
super.afterBreak(world, player, pos, state, blockEntity, stack);

// Place block back
world.setBlockState(pos, state, Block.NOTIFY_ALL);

shrinkField(world, pos);
}

private Optional<BlockPos> getMasterPos(World world, BlockPos pos) {
private Optional<BlockPos> getMasterPos(WorldAccess world, BlockPos pos) {
if (isMaster(world.getBlockState(pos))) return Optional.of(pos);

return collectFieldBlocks(world, pos).stream().filter(p -> isMaster(world.getBlockState(p))).findFirst();
}

/**
* Trace field by traversing all directions and count neighbors.
* Trace field by traversing all directions.
*/
private Set<BlockPos> collectFieldBlocks(World world, BlockPos pos) {
private Set<BlockPos> collectFieldBlocks(WorldAccess world, BlockPos pos) {
return collectFieldBlocks(world, pos, world.getBlockState(pos).getBlock(), new HashSet<>());
}

private Set<BlockPos> collectFieldBlocks(World world, BlockPos pos, Block block, Set<BlockPos> visited) {
private Set<BlockPos> collectFieldBlocks(WorldAccess world, BlockPos pos, Block block, Set<BlockPos> visited) {
visited.add(pos);
for (BlockPos neighbor : Arrays.stream(DIRECTIONS).map(pos::offset).toList()) {
if (!world.getBlockState(neighbor).isOf(block)) continue;
Expand All @@ -176,7 +179,7 @@ private Set<BlockPos> collectFieldBlocks(World world, BlockPos pos, Block block,
return visited;
}

private void shrinkField(World world, BlockPos pos) {
private void shrinkField(WorldAccess world, BlockPos pos) {
Set<BlockPos> field = collectFieldBlocks(world, pos);

Optional<BlockPos> masterOpt = getMasterPos(world, pos);
Expand All @@ -196,6 +199,7 @@ private void shrinkField(World world, BlockPos pos) {
@Override
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
super.onStateReplaced(state, world, pos, newState, moved);

if (state.isOf(newState.getBlock()))
return;

Expand All @@ -208,7 +212,7 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt
@Override
public VoxelShape getCollisionShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
// Only has collision from top
if (!context.isAbove(VoxelShapes.fullCube(), pos, true) || context.isDescending()) {
if (!context.isAbove(VoxelShapes.fullCube(), pos, true) || context.isDescending() && pos.getY() != AtomicFloor.ATOMIC_FLOOR_LAYER) {
return VoxelShapes.empty();
}
return VoxelShapes.fullCube();
Expand Down
32 changes: 28 additions & 4 deletions src/main/java/be/minelabs/event/ServerModEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
import be.minelabs.item.items.AtomItem;
import be.minelabs.item.ItemGroups;
import be.minelabs.item.Items;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.projectile.FishingBobberEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.Heightmap;

import java.util.List;


public class ServerModEvents {
Expand All @@ -27,7 +34,7 @@ public static void onInitialize() {
if (ItemGroups.ATOMS.contains(stack) || stack.isOf(Items.BOHR_BLUEPRINT)
|| stack.isOf(Items.ATOM_FLOOR) || stack.isEmpty()
|| ItemGroups.ELEMENTARY_PARTICLES.contains(stack)
|| ItemGroups.QUANTUM_FIELDS.contains(stack)){
|| ItemGroups.QUANTUM_FIELDS.contains(stack)) {
return ActionResult.PASS;
} else {
return ActionResult.FAIL;
Expand All @@ -41,15 +48,32 @@ public static void onInitialize() {
return ActionResult.PASS;
});

UseItemCallback.EVENT.register((player, world, hand) -> {
// Move players that fall out of the subatomic dimension back to the overworld.
ServerTickEvents.END_WORLD_TICK.register(world -> {
if (world.getRegistryKey() == ModDimensions.SUBATOM_KEY) {
List<ServerPlayerEntity> players = world.getPlayers(p -> !p.isSpectator() && p.getPos().y <= 0);
if (players.isEmpty()) return;
ServerWorld overworld = world.getServer().getOverworld();
players.forEach(p -> {
p.teleport(overworld,
p.getX(), overworld.getTopY(Heightmap.Type.WORLD_SURFACE, p.getBlockX(), p.getBlockZ()) + 30, p.getZ(),
p.getYaw(), p.getPitch());
p.addStatusEffect(new StatusEffectInstance(StatusEffects.SLOW_FALLING, 5 * 20));
});
}
});

UseItemCallback.EVENT.register((player, world, hand) ->

{
if (world.isClient || !player.getStackInHand(hand).isOf(net.minecraft.item.Items.FISHING_ROD))
return TypedActionResult.pass(player.getStackInHand(hand));

FishingBobberEntity fishHook = player.fishHook;
if (fishHook != null && fishHook.getHookedEntity() instanceof BohrBlueprintEntity entity){
if (fishHook != null && fishHook.getHookedEntity() instanceof BohrBlueprintEntity entity) {
ItemStack stack = entity.extractByRod((ServerPlayerEntity) player, fishHook);

if(!stack.isEmpty()){
if (!stack.isEmpty()) {
// advancement
if (stack.getItem() instanceof AtomItem)
Criteria.BOHR_CRITERION.trigger((ServerPlayerEntity) player, BohrCriterion.Type.REMOVE_ATOM, true);
Expand Down