Skip to content

Commit

Permalink
Merge pull request #229 from dmzz-yyhyy/hammer
Browse files Browse the repository at this point in the history
铁砧锤
  • Loading branch information
Gu-ZT authored Apr 12, 2024
2 parents df89f99 + d59d6f7 commit 6adf274
Show file tree
Hide file tree
Showing 31 changed files with 560 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.dubhe.anvilcraft.api.hammer;

import net.minecraft.world.level.block.Block;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

@SuppressWarnings("unused")
public class HammerManager {
private static final Map<Supplier<Block>, IHammerChangeable> INIT_CHANGE = new HashMap<>();
private static final Map<Block, IHammerChangeable> CHANGE = new HashMap<>();

public static void registerChange(Supplier<Block> block, IHammerChangeable changeable) {
HammerManager.INIT_CHANGE.put(block, changeable);
}

public static IHammerChangeable getChange(Block block) {
if (block instanceof IHammerChangeable changeable) return changeable;
return HammerManager.CHANGE.getOrDefault(block, IHammerChangeableBlock.EMPTY);
}

public static void register() {
for (Map.Entry<Supplier<Block>, IHammerChangeable> entry : INIT_CHANGE.entrySet()) {
HammerManager.CHANGE.put(entry.getKey().get(), entry.getValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.dubhe.anvilcraft.api.hammer;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

@FunctionalInterface
public interface IHammerChangeable {
/**
* 改变状态
*
* @param player 玩家
* @param blockPos 坐标
* @param level 世界
* @param anvilHammer 铁砧锤物品
* @return 是否改变成功
*/
boolean change(Player player, BlockPos blockPos, @NotNull Level level, ItemStack anvilHammer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dev.dubhe.anvilcraft.api.hammer;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("unused")
public interface IHammerChangeableBlock extends IHammerChangeable {
DirectionProperty FACING_HOPPER = BlockStateProperties.FACING_HOPPER;
DirectionProperty FACING = BlockStateProperties.FACING;
DirectionProperty HORIZONTAL_FACING = BlockStateProperties.HORIZONTAL_FACING;
IHammerChangeableBlock DEFAULT = new IHammerChangeableBlock() {
};
IHammerChangeableBlock EMPTY = new IHammerChangeableBlock() {
public boolean change(Player player, BlockPos blockPos, @NotNull Level level, ItemStack anvilHammer) {
return false;
}
};

@Override
default boolean change(Player player, BlockPos blockPos, @NotNull Level level, ItemStack anvilHammer) {
BlockState state = level.getBlockState(blockPos);
if (state.hasProperty(FACING)) {
state = IHammerChangeableBlock.rotate(state);
} else if (state.hasProperty(FACING_HOPPER)) {
state = IHammerChangeableBlock.hopperRotate(state);
} else if (state.hasProperty(HORIZONTAL_FACING)) {
state = IHammerChangeableBlock.horizontalRotate(state);
}
level.setBlockAndUpdate(blockPos, state);
return true;
}

private static @NotNull BlockState rotate(@NotNull BlockState state) {
Direction direction = state.getValue(FACING);
return switch (direction) {
case WEST -> state.setValue(FACING, Direction.UP);
case UP -> state.setValue(FACING, Direction.DOWN);
case DOWN -> state.setValue(FACING, Direction.NORTH);
default -> state.setValue(FACING, direction.getClockWise());
};
}

private static @NotNull BlockState hopperRotate(@NotNull BlockState state) {
Direction direction = state.getValue(FACING_HOPPER);
return switch (direction) {
case WEST -> state.setValue(FACING_HOPPER, Direction.DOWN);
case DOWN -> state.setValue(FACING_HOPPER, Direction.NORTH);
default -> state.setValue(FACING_HOPPER, direction.getClockWise());
};
}

@SuppressWarnings("SameParameterValue")
private static @NotNull BlockState horizontalRotate(@NotNull BlockState state) {
return state.setValue(HORIZONTAL_FACING, state.getValue(HORIZONTAL_FACING).getClockWise());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package dev.dubhe.anvilcraft.api.hammer;

public interface IHammerRemovable {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.depository.FilteredItemDepository;
import dev.dubhe.anvilcraft.api.hammer.IHammerChangeableBlock;
import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.block.entity.AutoCrafterBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
Expand Down Expand Up @@ -38,7 +40,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class AutoCrafterBlock extends BaseEntityBlock {
public class AutoCrafterBlock extends BaseEntityBlock implements IHammerChangeableBlock, IHammerRemovable {
public static final DirectionProperty FACING = DirectionalBlock.FACING;
public static final BooleanProperty LIT = RedstoneTorchBlock.LIT;

Expand Down
18 changes: 17 additions & 1 deletion common/src/main/java/dev/dubhe/anvilcraft/block/ChuteBlock.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.depository.FilteredItemDepository;
import dev.dubhe.anvilcraft.api.hammer.IHammerChangeableBlock;
import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.block.entity.ChuteBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModBlocks;
Expand Down Expand Up @@ -46,7 +48,7 @@
import javax.annotation.Nonnull;
import java.util.stream.Stream;

public class ChuteBlock extends BaseEntityBlock {
public class ChuteBlock extends BaseEntityBlock implements IHammerChangeableBlock, IHammerRemovable {
public static final DirectionProperty FACING = BlockStateProperties.FACING_HOPPER;
public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;

Expand Down Expand Up @@ -259,4 +261,18 @@ public static boolean hasChuteFacing(Level level, BlockPos pos) {
}
return false;
}

// @Override
// public boolean change(Player player, BlockPos pos, @NotNull Level level, ItemStack anvilHammer) {
// BlockState state = level.getBlockState(pos);
// BlockState facingState = level.getBlockState(pos.relative(state.getValue(FACING)));
// if (facingState.is(ModBlocks.CHUTE.get()) || facingState.is(ModBlocks.SIMPLE_CHUTE.get())) {
// if (facingState.getValue(FACING).getOpposite() == state.getValue(FACING)) {
// level.destroyBlock(pos, true);
// return true;
// }
// }
// IHammerChangeableBlock.DEFAULT.change(player, pos, level, anvilHammer);
// return true;
// }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
import dev.dubhe.anvilcraft.inventory.RoyalAnvilMenu;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -28,7 +29,7 @@

import javax.annotation.Nonnull;

public class RoyalAnvilBlock extends AnvilBlock {
public class RoyalAnvilBlock extends AnvilBlock implements IHammerRemovable {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
import dev.dubhe.anvilcraft.inventory.RoyalGrindstoneMenu;
import net.minecraft.core.BlockPos;
Expand All @@ -18,7 +19,7 @@
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.NotNull;

public class RoyalGrindstone extends GrindstoneBlock {
public class RoyalGrindstone extends GrindstoneBlock implements IHammerRemovable {

private static final Component CONTAINER_TITLE = Component.translatable("container.grindstone_title");

Expand All @@ -33,6 +34,7 @@ public RoyalGrindstone(Properties properties) {
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 RoyalGrindstoneMenu(i, inventory, ContainerLevelAccess.create(level, pos)), CONTAINER_TITLE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.init.ModMenuTypes;
import dev.dubhe.anvilcraft.inventory.RoyalSmithingMenu;
import net.minecraft.core.BlockPos;
Expand All @@ -18,7 +19,7 @@
import net.minecraft.world.phys.BlockHitResult;
import org.jetbrains.annotations.NotNull;

public class RoyalSmithingTableBlock extends SmithingTableBlock {
public class RoyalSmithingTableBlock extends SmithingTableBlock implements IHammerRemovable {
private static final Component CONTAINER_TITLE = Component.translatable("container.upgrade");

public RoyalSmithingTableBlock(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.depository.ItemDepository;
import dev.dubhe.anvilcraft.api.hammer.IHammerChangeable;
import dev.dubhe.anvilcraft.api.hammer.IHammerChangeableBlock;
import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import dev.dubhe.anvilcraft.block.entity.SimpleChuteBlockEntity;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModBlocks;
Expand All @@ -9,6 +12,8 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.Containers;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
Expand All @@ -35,7 +40,7 @@

import javax.annotation.Nonnull;

public class SimpleChuteBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
public class SimpleChuteBlock extends BaseEntityBlock implements SimpleWaterloggedBlock, IHammerChangeable, IHammerRemovable {
public static final DirectionProperty FACING = BlockStateProperties.FACING_HOPPER;
public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
Expand Down Expand Up @@ -184,4 +189,18 @@ public int getAnalogOutputSignal(@NotNull BlockState blockState, @NotNull Level
public FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}

@Override
public boolean change(Player player, BlockPos pos, @NotNull Level level, ItemStack anvilHammer) {
IHammerChangeableBlock.DEFAULT.change(player, pos, level, anvilHammer);
BlockState state = level.getBlockState(pos);
BlockState facingState = level.getBlockState(pos.relative(state.getValue(FACING)));
if (facingState.is(ModBlocks.CHUTE.get()) || facingState.is(ModBlocks.SIMPLE_CHUTE.get())) {
if (facingState.getValue(FACING).getOpposite() == state.getValue(FACING)) {
level.destroyBlock(pos, true);
return true;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.anvilcraft.block;

import dev.dubhe.anvilcraft.api.hammer.IHammerRemovable;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
Expand All @@ -19,7 +20,7 @@
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;

public class StampingPlatformBlock extends Block implements SimpleWaterloggedBlock {
public class StampingPlatformBlock extends Block implements SimpleWaterloggedBlock, IHammerRemovable {
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
private static final VoxelShape REDUCE_AABB = Shapes.or(
Block.box(2.0, 12.0, 2.0, 14.0, 16.0, 14.0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,57 @@ public static void init(@NotNull RegistrateTagsProvider<Block> provider) {
.add(Blocks.BROWN_MUSHROOM_BLOCK)
.add(Blocks.RED_MUSHROOM_BLOCK)
.add(Blocks.MUSHROOM_STEM);
provider.addTag(ModBlockTags.HAMMER_CHANGEABLE).setReplace(false)
.add(Blocks.OBSERVER)
.add(Blocks.HOPPER)
.add(Blocks.DROPPER)
.add(Blocks.DISPENSER)
.add(Blocks.LIGHTNING_ROD);
provider.addTag(ModBlockTags.HAMMER_REMOVABLE).setReplace(false)
.add(Blocks.BELL)
.add(Blocks.REDSTONE_LAMP)
.add(Blocks.IRON_DOOR)
.add(Blocks.RAIL)
.add(Blocks.ACTIVATOR_RAIL)
.add(Blocks.DETECTOR_RAIL)
.add(Blocks.POWERED_RAIL)
.add(Blocks.NOTE_BLOCK)
.add(Blocks.OBSERVER)
.add(Blocks.HOPPER)
.add(Blocks.DROPPER)
.add(Blocks.DISPENSER)
.add(Blocks.HONEY_BLOCK)
.add(Blocks.SLIME_BLOCK)
.add(Blocks.PISTON)
.add(Blocks.STICKY_PISTON)
.add(Blocks.LIGHTNING_ROD)
.add(Blocks.DAYLIGHT_DETECTOR)
.add(Blocks.LECTERN)
.add(Blocks.TRIPWIRE_HOOK)
.add(Blocks.SCULK_SHRIEKER)
.add(Blocks.LEVER)
.add(Blocks.STONE_BUTTON)
.add(Blocks.OAK_PRESSURE_PLATE)
.add(Blocks.STONE_PRESSURE_PLATE)
.add(Blocks.LIGHT_WEIGHTED_PRESSURE_PLATE)
.add(Blocks.HEAVY_WEIGHTED_PRESSURE_PLATE)
.add(Blocks.SCULK_SENSOR)
.add(Blocks.CALIBRATED_SCULK_SENSOR)
.add(Blocks.REDSTONE_WIRE)
.add(Blocks.REDSTONE_TORCH)
.add(Blocks.REDSTONE_WALL_TORCH)
.add(Blocks.REDSTONE_BLOCK)
.add(Blocks.REPEATER)
.add(Blocks.COMPARATOR)
.add(Blocks.TARGET)
.add(Blocks.IRON_TRAPDOOR)
.add(Blocks.CAULDRON)
.add(Blocks.LAVA_CAULDRON)
.add(Blocks.WATER_CAULDRON)
.add(Blocks.POWDER_SNOW_CAULDRON)
.add(Blocks.CAMPFIRE)
.add(Blocks.ANVIL)
.add(Blocks.CHIPPED_ANVIL)
.add(Blocks.DAMAGED_ANVIL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import dev.dubhe.anvilcraft.api.event.SubscribeEvent;
import dev.dubhe.anvilcraft.api.event.server.ServerEndDataPackReloadEvent;
import dev.dubhe.anvilcraft.api.event.server.ServerStartedEvent;
import dev.dubhe.anvilcraft.api.hammer.HammerManager;
import dev.dubhe.anvilcraft.data.recipe.anvil.AnvilRecipe;
import dev.dubhe.anvilcraft.data.recipe.anvil.outcome.SpawnItem;
import dev.dubhe.anvilcraft.data.recipe.anvil.predicate.HasBlock;
import dev.dubhe.anvilcraft.data.recipe.anvil.predicate.HasItemIngredient;
import dev.dubhe.anvilcraft.init.ModHammerInits;
import net.minecraft.advancements.critereon.BlockPredicate;
import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess;
Expand All @@ -36,6 +38,8 @@ public class ServerEventListener {
public void onServerStarted(@NotNull ServerStartedEvent event) {
MinecraftServer server = event.getServer();
ServerEventListener.processRecipes(server);
ModHammerInits.init();
HammerManager.register();
}

@SubscribeEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ModBlockTags {
public static final TagKey<Block> REDSTONE_TORCH = bind("redstone_torch");
public static final TagKey<Block> MUSHROOM_BLOCK = bind("mushroom_block");
public static final TagKey<Block> CANT_BROKEN_ANVIL = bind("cant_broken_anvil");
public static final TagKey<Block> HAMMER_REMOVABLE = bind("hammer_removable");
public static final TagKey<Block> HAMMER_CHANGEABLE = bind("hammer_changeable");

private static @NotNull TagKey<Block> bindC(String id) {
return TagKey.create(Registries.BLOCK, new ResourceLocation("c", id));
Expand Down
16 changes: 16 additions & 0 deletions common/src/main/java/dev/dubhe/anvilcraft/init/ModHammerInits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.dubhe.anvilcraft.init;

import dev.dubhe.anvilcraft.api.hammer.HammerManager;
import dev.dubhe.anvilcraft.api.hammer.IHammerChangeableBlock;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.Block;

public class ModHammerInits {
@SuppressWarnings("deprecation")
public static void init() {
for (Block block : BuiltInRegistries.BLOCK) {
if (!block.builtInRegistryHolder().is(ModBlockTags.HAMMER_CHANGEABLE)) continue;
HammerManager.registerChange(() -> block, IHammerChangeableBlock.DEFAULT);
}
}
}
Loading

0 comments on commit 6adf274

Please sign in to comment.