-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from dmzz-yyhyy/hammer
铁砧锤
- Loading branch information
Showing
31 changed files
with
560 additions
and
24 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
common/src/main/java/dev/dubhe/anvilcraft/api/hammer/HammerManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
common/src/main/java/dev/dubhe/anvilcraft/api/hammer/IHammerChangeable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
63 changes: 63 additions & 0 deletions
63
common/src/main/java/dev/dubhe/anvilcraft/api/hammer/IHammerChangeableBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
common/src/main/java/dev/dubhe/anvilcraft/api/hammer/IHammerRemovable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package dev.dubhe.anvilcraft.api.hammer; | ||
|
||
public interface IHammerRemovable { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
common/src/main/java/dev/dubhe/anvilcraft/init/ModHammerInits.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.