-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
- Loading branch information
There are no files selected for viewing
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; | ||
Check warning on line 5 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/HammerManager.java GitHub Actions / checkstyle
Check warning on line 5 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/HammerManager.java GitHub Actions / checkstyle
|
||
import java.util.Map; | ||
Check warning on line 6 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/HammerManager.java GitHub Actions / checkstyle
|
||
import java.util.function.Supplier; | ||
Check warning on line 7 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/HammerManager.java GitHub Actions / checkstyle
|
||
|
||
@SuppressWarnings("unused") | ||
Check warning on line 9 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/HammerManager.java GitHub Actions / checkstyle
|
||
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") | ||
Check warning on line 13 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/IHammerChangeableBlock.java GitHub Actions / checkstyle
|
||
public interface IHammerChangeableBlock extends IHammerChangeable { | ||
Check warning on line 14 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/IHammerChangeableBlock.java GitHub Actions / checkstyle
|
||
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) { | ||
Check warning on line 21 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/IHammerChangeableBlock.java GitHub Actions / checkstyle
|
||
return false; | ||
} | ||
}; | ||
|
||
@Override | ||
default boolean change(Player player, BlockPos blockPos, @NotNull Level level, ItemStack anvilHammer) { | ||
Check warning on line 27 in common/src/main/java/dev/dubhe/anvilcraft/api/hammer/IHammerChangeableBlock.java GitHub Actions / checkstyle
|
||
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |