-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathBanglumNukeHandler.java
111 lines (89 loc) · 4.18 KB
/
BanglumNukeHandler.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package nourl.mythicmetals.blocks;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.block.BlockState;
import net.minecraft.block.DispenserBlock;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.ActionResult;
import net.minecraft.util.math.BlockPointer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
import nourl.mythicmetals.data.MythicTags;
import nourl.mythicmetals.entity.BanglumNukeEntity;
import nourl.mythicmetals.registry.RegisterSounds;
public class BanglumNukeHandler {
public static void init() {
UseBlockCallback.EVENT.register((player, world, hand, hitResult) -> {
var stack = player.getStackInHand(hand);
if (!stack.isOf(Items.FLINT_AND_STEEL)) return ActionResult.PASS;
var targetBlock = world.getBlockState(hitResult.getBlockPos());
if (!targetBlock.isOf(MythicBlocks.BANGLUM.getStorageBlock())
&& !targetBlock.isOf(MythicBlocks.MORKITE.getStorageBlock()))
return ActionResult.PASS;
var pos = hitResult.getBlockPos();
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
for (int z = 0; z < 3; z++) {
if (tryLightBigTntAt(world, player, pos.getX() - x, pos.getY() - y, pos.getZ() - z)) {
stack.damage(1, player, LivingEntity.getSlotForHand(hand));
return ActionResult.SUCCESS;
}
}
}
}
return ActionResult.PASS;
});
}
public static boolean tryLightBigTntWithDispenser(BlockPointer dispenser) {
var world = dispenser.world();
BlockState state = world.getBlockState(dispenser.pos().offset(dispenser.state().get(DispenserBlock.FACING)));
var pos = dispenser.pos().offset(dispenser.state().get(DispenserBlock.FACING));
if (!state.isOf(MythicBlocks.BANGLUM.getStorageBlock())
&& !state.isOf(MythicBlocks.MORKITE.getStorageBlock()))
return false;
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
for (int z = 0; z < 3; z++) {
if (tryLightBigTntAt(world, null, pos.getX() - x, pos.getY() - y, pos.getZ() - z)) {
return true;
}
}
}
}
return false;
}
private static boolean tryLightBigTntAt(World world, PlayerEntity player, int x, int y, int z) {
BlockPos.Mutable mutablePos = new BlockPos.Mutable();
for (int ox = 0; ox < 2; ox++) {
for (int oy = 0; oy < 2; oy++) {
for (int oz = 0; oz < 2; oz++) {
if (ox == 1 && oy == 1 && oz == 1) continue;
BlockState neededState = (ox + oy + oz) % 2 == 0
? MythicBlocks.BANGLUM.getStorageBlock().getDefaultState()
: MythicBlocks.MORKITE.getStorageBlock().getDefaultState();
mutablePos.set(x + ox, y + oy, z + oz);
if (world.getBlockState(mutablePos) != neededState)
return false;
}
}
}
mutablePos.set(x + 1, y + 1, z + 1);
BlockState coreState = world.getBlockState(mutablePos);
if (!coreState.isIn(MythicTags.NUKE_CORES)) return false;
for (var pos : BlockPos.iterate(x, y, z, x + 2, y + 2, z + 2)) {
world.removeBlock(pos, false);
}
if (!world.isClient) {
BanglumNukeEntity nuke = new BanglumNukeEntity(world, x + 1.5, y, z + 1.5, player, coreState.getBlock());
world.spawnEntity(nuke);
world.playSound(
null, nuke.getX(), nuke.getY(), nuke.getZ(), RegisterSounds.BANGLUM_NUKE_IGNITE, SoundCategory.BLOCKS, 1.0F, 1.0F
);
world.emitGameEvent(player, GameEvent.PRIME_FUSE, new BlockPos(x, y, z));
}
return true;
}
}