-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
NitraBlock.java
119 lines (103 loc) · 4.91 KB
/
NitraBlock.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
112
113
114
115
116
117
118
119
package net.id.paradiselost.blocks.mechanical;
import net.id.paradiselost.world.ExplosionExtensions;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.SandBlock;
import net.minecraft.entity.Entity;
import net.minecraft.entity.FallingBlockEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.stat.Stats;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
import net.minecraft.world.explosion.Explosion;
import org.jetbrains.annotations.Nullable;
public class NitraBlock extends Block {
public NitraBlock(Settings settings) {
super(settings);
}
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
if (!oldState.isOf(state.getBlock())) {
if (world.isReceivingRedstonePower(pos)) {
world.createAndScheduleBlockTick(pos, this, 1);
}
}
}
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) {
if (world.isReceivingRedstonePower(pos)) {
world.createAndScheduleBlockTick(pos, this, 1);
}
}
public void onDestroyedByExplosion(World world, BlockPos pos, Explosion explosion) {
float sourcePower = ((ExplosionExtensions) explosion).getPower();
if (!world.isClient && sourcePower > 0.5F) {
ignite(world, pos, sourcePower - 0.5F);
}
}
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
ignite(world, pos, 2F, null);
world.setBlockState(pos, Blocks.AIR.getDefaultState(), 11);
world.spawnParticles(ParticleTypes.EXPLOSION_EMITTER, pos.getX(), pos.getY(), pos.getZ(), 1, 0.0, 0.0, 0.0, 0.0);
world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.ENTITY_GENERIC_EXPLODE, SoundCategory.BLOCKS, 4.0F, (1.0F + (world.random.nextFloat() - world.random.nextFloat()) * 0.2F) * 0.7F);
}
public static void ignite(World world, BlockPos pos, float power) {
ignite(world, pos, power, null);
}
private static void ignite(World world, BlockPos pos, float power, @Nullable LivingEntity igniter) {
Explosion explosion = new Explosion(world, igniter, null, null, pos.getX(), pos.getY() + 0.5D, pos.getZ(), power, false, Explosion.DestructionType.BREAK);
System.out.println(world.isClient);
if (!world.isClient) {
explosion.collectBlocksAndDamageEntities();
world.emitGameEvent(igniter, GameEvent.PRIME_FUSE, pos);
}
((ExplosionExtensions) explosion).affectWorld(true, SoundEvents.ENTITY_GENERIC_EXPLODE);
}
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
ItemStack itemStack = player.getStackInHand(hand);
if (!itemStack.isOf(Items.FLINT_AND_STEEL) && !itemStack.isOf(Items.FIRE_CHARGE)) {
return super.onUse(state, world, pos, player, hand, hit);
} else {
ignite(world, pos, 2F, player);
world.setBlockState(pos, Blocks.AIR.getDefaultState(), 11);
Item item = itemStack.getItem();
if (!player.isCreative()) {
if (itemStack.isOf(Items.FLINT_AND_STEEL)) {
itemStack.damage(1, player, (playerx) -> {
playerx.sendToolBreakStatus(hand);
});
} else {
itemStack.decrement(1);
}
}
player.incrementStat(Stats.USED.getOrCreateStat(item));
return ActionResult.success(world.isClient);
}
}
public void onProjectileHit(World world, BlockState state, BlockHitResult hit, ProjectileEntity projectile) {
if (!world.isClient) {
BlockPos blockPos = hit.getBlockPos();
Entity entity = projectile.getOwner();
if (projectile.isOnFire() && projectile.canModifyAt(world, blockPos)) {
ignite(world, blockPos, 2F, entity instanceof LivingEntity ? (LivingEntity) entity : null);
world.removeBlock(blockPos, false);
}
}
}
public boolean shouldDropItemsOnExplosion(Explosion explosion) {
return false;
}
}