-
Notifications
You must be signed in to change notification settings - Fork 18
/
BlockSmashingAnvil.java
65 lines (56 loc) · 2.03 KB
/
BlockSmashingAnvil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package knightminer.inspirations.recipes.block;
import knightminer.inspirations.library.InspirationsRegistry;
import knightminer.inspirations.recipes.InspirationsRecipes;
import net.minecraft.block.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nullable;
import java.util.Objects;
public class BlockSmashingAnvil extends AnvilBlock {
public BlockSmashingAnvil(Block original) {
super(Block.Properties.from(original));
setRegistryName(Objects.requireNonNull(original.getRegistryName()));
}
// Replace this to handle our different blocks.
@Nullable
public static BlockState damage(BlockState state) {
Block block = state.getBlock();
if (block == Blocks.ANVIL || block == InspirationsRecipes.fullAnvil) {
return InspirationsRecipes.chippedAnvil.getDefaultState().with(FACING, state.get(FACING));
} else {
if (block == Blocks.CHIPPED_ANVIL || block == InspirationsRecipes.chippedAnvil)
return InspirationsRecipes.damagedAnvil.getDefaultState().with(FACING, state.get(FACING));
else return null;
}
}
@Override
public void onEndFalling(World world, BlockPos pos, BlockState anvil, BlockState state) {
BlockPos down = pos.down();
if(!smashBlock(world, down, world.getBlockState(down))) {
super.onEndFalling(world, pos, anvil, state);
}
}
public static boolean smashBlock(World world, BlockPos pos, BlockState state) {
// if we started on air, just return true
if(state.getBlock() == Blocks.AIR) {
return true;
}
// if the block is unbreakable, leave it
if(state.getBlockHardness(world, pos) == -1) {
return false;
}
BlockState transformation = InspirationsRegistry.getAnvilSmashResult(state);
if(transformation == null) {
return false;
}
// if the result is air, break the block
if(transformation.getBlock() == Blocks.AIR) {
world.destroyBlock(pos, true);
} else {
// breaking particles
world.playEvent(2001, pos, Block.getStateId(state));
world.setBlockState(pos, transformation);
}
return true;
}
}