Skip to content

Commit

Permalink
Merge branch 'ZhuRuoLing-refactor/multiblock_crafting' into refactor/…
Browse files Browse the repository at this point in the history
…multiblock_crafting
  • Loading branch information
ZhuRuoLing committed Jul 18, 2024
2 parents 982a775 + 5dca30d commit 9d0d0ad
Show file tree
Hide file tree
Showing 431 changed files with 13,763 additions and 487 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class HeatedBlockRecorder {
ModBlocks.INCANDESCENT_NETHERITE.get()
));

BlockFamily emberMetalFamily = new BlockFamily(List.of(
ModBlocks.EMBER_METAL_BLOCK.get(),
ModBlocks.CUT_EMBER_METAL_BLOCK.get()
));

TRANSFORMS.put(Pair.of(ModBlocks.HEATED_TUNGSTEN.get(), 2), tungstenFamily);
TRANSFORMS.put(Pair.of(ModBlocks.REDHOT_TUNGSTEN.get(), 8), tungstenFamily);
TRANSFORMS.put(Pair.of(ModBlocks.GLOWING_TUNGSTEN.get(), 32), tungstenFamily);
Expand All @@ -47,6 +52,9 @@ public class HeatedBlockRecorder {
TRANSFORMS.put(Pair.of(ModBlocks.REDHOT_NETHERITE.get(), 8), netheriteFamily);
TRANSFORMS.put(Pair.of(ModBlocks.GLOWING_NETHERITE.get(), 32), netheriteFamily);
TRANSFORMS.put(Pair.of(ModBlocks.INCANDESCENT_NETHERITE.get(), 128), netheriteFamily);

TRANSFORMS.put(Pair.of(ModBlocks.EMBER_METAL_BLOCK.get(), 8), emberMetalFamily);
TRANSFORMS.put(Pair.of(ModBlocks.CUT_EMBER_METAL_BLOCK.get(), 2), emberMetalFamily);
}

private final LevelAccessor level;
Expand Down Expand Up @@ -111,6 +119,7 @@ public void remove(BlockPos pos, BlockEntity entity) {
/**
* 方塊改變
*/
@SuppressWarnings("unused")
public void onBlockStateChange(BlockPos pos, BlockState blockState, BlockState newState) {
if (record.containsKey(pos)) {
int level = record.get(pos).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
Expand Down Expand Up @@ -63,7 +64,15 @@ protected void createBlockStateDefinition(

@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite());
Player player = context.getPlayer();
if (player == null) {
return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite());
}
if (player.isShiftKeyDown()) {
return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection().getOpposite());
} else {
return this.defaultBlockState().setValue(FACING, context.getNearestLookingDirection());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ public BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
case EAST -> orientation = Orientation.EAST_UP;
}
}
orientation = orientation.opposite();
if (context.getPlayer() != null && context.getPlayer().isShiftKeyDown()) {
orientation = orientation.opposite();
}
return defaultBlockState().setValue(ORIENTATION, orientation);
}

Expand Down
106 changes: 106 additions & 0 deletions common/src/main/java/dev/dubhe/anvilcraft/block/EmberAnvilBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
import dev.dubhe.anvilcraft.inventory.EmberAnvilMenu;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
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.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nonnull;

public class EmberAnvilBlock extends AnvilBlock implements IHammerRemovable, EmberBlock {
private static final VoxelShape BASE = Block.box(2.0, 0.0, 2.0, 14.0, 4.0, 14.0);
private static final VoxelShape X_LEG1 = Block.box(4.0, 4.0, 5.0, 12.0, 10.0, 11.0);
private static final VoxelShape X_TOP = Block.box(0.0, 10.0, 3.0, 16.0, 16.0, 13.0);
private static final VoxelShape Z_LEG1 = Block.box(5.0, 4.0, 4.0, 11.0, 10.0, 12.0);
private static final VoxelShape Z_TOP = Block.box(3.0, 10.0, 0.0, 13.0, 16.0, 16.0);
private static final VoxelShape X_AXIS_AABB = Shapes.or(BASE, X_LEG1, X_TOP);
private static final VoxelShape Z_AXIS_AABB = Shapes.or(BASE, Z_LEG1, Z_TOP);
private static final Component CONTAINER_TITLE = Component.translatable("container.repair");

@Getter
@Setter
private BlockState checkBlockState;

public EmberAnvilBlock(Properties properties) {
super(properties);
}

@Override
public @NotNull VoxelShape getShape(
@NotNull BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context
) {
Direction direction = state.getValue(FACING);
if (direction.getAxis() == Direction.Axis.X) {
return X_AXIS_AABB;
}
return Z_AXIS_AABB;
}

@SuppressWarnings("UnreachableCode")
@Override
public @NotNull InteractionResult use(
@NotNull BlockState state, @NotNull Level level,
@NotNull BlockPos pos, @NotNull Player player,
@NotNull InteractionHand hand, @NotNull BlockHitResult hit
) {
if (level.isClientSide) return InteractionResult.SUCCESS;
ModMenuTypes.open((ServerPlayer) player, state.getMenuProvider(level, pos));
player.awardStat(Stats.INTERACT_WITH_ANVIL);
return InteractionResult.CONSUME;
}

@Override
@Nullable
public MenuProvider getMenuProvider(@Nonnull BlockState state, @Nonnull Level level, @Nonnull BlockPos pos) {
return new SimpleMenuProvider((i, inventory, player) ->
new EmberAnvilMenu(i, inventory, ContainerLevelAccess.create(level, pos)), CONTAINER_TITLE);
}

@Override
protected void falling(@NotNull FallingBlockEntity entity) {
entity.setHurtsEntities(2.0f, 80);
}

@Override
public boolean isRandomlyTicking(@NotNull BlockState state) {
return true;
}

@Override
public void randomTick(
@NotNull BlockState state,
@NotNull ServerLevel level,
@NotNull BlockPos pos,
@NotNull RandomSource random
) {
if (random.nextDouble() <= 0.5) {
tryAbsorbWater(level, pos);
}
}
}
80 changes: 80 additions & 0 deletions common/src/main/java/dev/dubhe/anvilcraft/block/EmberBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.init.ModFluidTags;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.BucketPickup;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import org.jetbrains.annotations.NotNull;

/**
* 余烬金属系方块
*/
public interface EmberBlock {
BlockState getCheckBlockState();

void setCheckBlockState(BlockState blockState);

/**
* 尝试吸水
*/
default void tryAbsorbWater(@NotNull Level level, @NotNull BlockPos pos) {
if (this.removeFluidBreadthFirstSearch(level, pos)) {
level.levelEvent(2001, pos, Block.getId(getCheckBlockState()));
level.playSound(null, pos, SoundEvents.LAVA_EXTINGUISH, SoundSource.BLOCKS, 1f, 1f);
}
}

private boolean removeFluidBreadthFirstSearch(Level level, BlockPos pos) {
return BlockPos.breadthFirstTraversal(pos, 6, 65, (posx, consumer) -> {

for (Direction direction : Direction.values()) {
consumer.accept(posx.relative(direction));
}

}, (checkedPos) -> {
if (checkedPos.equals(pos)) {
return true;
} else {
BlockState blockState = level.getBlockState(checkedPos);
FluidState fluidState = level.getFluidState(checkedPos);
if (!fluidState.is(ModFluidTags.MENGER_SPONGE_CAN_ABSORB)) {
return false;
} else {
Block block = blockState.getBlock();
if (block instanceof BucketPickup bucketPickup) {
if (!bucketPickup.pickupBlock(level, checkedPos, blockState).isEmpty()) {
setCheckBlockState(blockState);
return true;
}
}

if (blockState.getBlock() instanceof LiquidBlock) {
level.setBlock(checkedPos, Blocks.AIR.defaultBlockState(), 3);
} else {
if (!blockState.is(Blocks.KELP)
&& !blockState.is(Blocks.KELP_PLANT)
&& !blockState.is(Blocks.SEAGRASS)
&& !blockState.is(Blocks.TALL_SEAGRASS)) {
return false;
}

BlockEntity blockEntity = blockState.hasBlockEntity() ? level.getBlockEntity(checkedPos) : null;
Block.dropResources(blockState, level, checkedPos, blockEntity);
level.setBlock(checkedPos, Blocks.AIR.defaultBlockState(), 3);
}
setCheckBlockState(blockState);
return true;
}
}
}) > 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
import dev.dubhe.anvilcraft.inventory.EmberGrindstoneMenu;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.GrindstoneBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.NotNull;

public class EmberGrindstone extends GrindstoneBlock implements IHammerRemovable, EmberBlock {

private static final Component CONTAINER_TITLE = Component.translatable("container.grindstone_title");
@Getter
@Setter
private BlockState checkBlockState;

public EmberGrindstone(Properties properties) {
super(properties);
}

@SuppressWarnings("UnreachableCode")
@Override
public @NotNull InteractionResult use(
@NotNull BlockState state, @NotNull Level level,
@NotNull BlockPos pos, @NotNull Player player,
@NotNull InteractionHand hand, @NotNull BlockHitResult hit
) {
if (level.isClientSide) return InteractionResult.SUCCESS;
ModMenuTypes.open((ServerPlayer) player, state.getMenuProvider(level, pos));
player.awardStat(Stats.INTERACT_WITH_GRINDSTONE);
return InteractionResult.CONSUME;
}

@Override
public MenuProvider getMenuProvider(@NotNull BlockState state, @NotNull Level level, @NotNull BlockPos pos) {
return new SimpleMenuProvider((i, inventory, player) ->
new EmberGrindstoneMenu(i, inventory, ContainerLevelAccess.create(level, pos)), CONTAINER_TITLE);
}

@Override
public boolean isRandomlyTicking(@NotNull BlockState state) {
return true;
}

@Override
public void randomTick(
@NotNull BlockState state,
@NotNull ServerLevel level,
@NotNull BlockPos pos,
@NotNull RandomSource random
) {
if (random.nextDouble() <= 0.5) {
tryAbsorbWater(level, pos);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.dubhe.anvilcraft.block;

import lombok.Getter;
import lombok.Setter;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

public class EmberMetalBlock extends Block implements EmberBlock {
private final double waterAbsorptionChance;
@Getter
@Setter
private BlockState checkBlockState;

public EmberMetalBlock(Properties properties, double waterAbsorptionChance) {
super(properties);
this.waterAbsorptionChance = waterAbsorptionChance;
}

@Override
public boolean isRandomlyTicking(@NotNull BlockState state) {
return true;
}

@Override
public void randomTick(
@NotNull BlockState state,
@NotNull ServerLevel level,
@NotNull BlockPos pos,
@NotNull RandomSource random
) {
if (random.nextDouble() <= waterAbsorptionChance) {
tryAbsorbWater(level, pos);
}
}
}
Loading

0 comments on commit 9d0d0ad

Please sign in to comment.