|
| 1 | +package com.altnoir.mia.common.item; |
| 2 | + |
| 3 | +import com.altnoir.mia.MiaConfig; |
| 4 | +import net.minecraft.core.particles.ParticleTypes; |
| 5 | +import net.minecraft.server.MinecraftServer; |
| 6 | +import net.minecraft.server.level.ServerLevel; |
| 7 | +import net.minecraft.world.entity.LivingEntity; |
| 8 | +import net.minecraft.world.entity.player.Player; |
| 9 | +import net.minecraft.world.item.DiggerItem; |
| 10 | +import net.minecraft.world.item.ItemStack; |
| 11 | +import net.minecraft.world.item.Items; |
| 12 | +import net.minecraft.world.item.Tiers; |
| 13 | +import net.minecraft.world.level.Level; |
| 14 | +import net.minecraft.world.level.Level.ExplosionInteraction; |
| 15 | +import net.minecraft.world.phys.Vec3; |
| 16 | +import net.minecraft.tags.BlockTags; |
| 17 | + |
| 18 | +import java.util.concurrent.Executors; |
| 19 | +import java.util.concurrent.ScheduledExecutorService; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.function.Predicate; |
| 22 | +import com.altnoir.mia.init.MiaItems; |
| 23 | + |
| 24 | +public class BlazeReapItem extends DiggerItem { |
| 25 | + |
| 26 | + private static final ScheduledExecutorService SCHEDULER = Executors.newSingleThreadScheduledExecutor(); |
| 27 | + |
| 28 | + public static final Predicate<ItemStack> BLAZE_REAP_FUEL = (stack) -> |
| 29 | + stack.is(Items.GUNPOWDER) || stack.is(MiaItems.PEACE_PHOBIA.get()); |
| 30 | + |
| 31 | + public BlazeReapItem(Properties properties) { |
| 32 | + super(Tiers.NETHERITE, BlockTags.MINEABLE_WITH_PICKAXE, properties); |
| 33 | + } |
| 34 | + |
| 35 | + public static Properties createAttributes() { |
| 36 | + return new Properties() |
| 37 | + .attributes(DiggerItem.createAttributes(Tiers.NETHERITE, 9.0F, -3.0F)); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean hurtEnemy(ItemStack stack, LivingEntity target, LivingEntity attacker) { |
| 42 | + Level level = attacker.level(); |
| 43 | + |
| 44 | + if (!level.isClientSide && attacker instanceof Player player) { |
| 45 | + |
| 46 | + ItemStack ammoStack = player.getProjectile(stack); |
| 47 | + |
| 48 | + if (ammoStack.isEmpty() || !BLAZE_REAP_FUEL.test(ammoStack)) { |
| 49 | + ammoStack = findAmmoInInventory(player, BLAZE_REAP_FUEL); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + if (!ammoStack.isEmpty() || player.getAbilities().instabuild) { |
| 54 | + |
| 55 | + |
| 56 | + if (!player.getAbilities().instabuild) { |
| 57 | + if (!ammoStack.is(MiaItems.PEACE_PHOBIA.get())) { |
| 58 | + ammoStack.shrink(1); |
| 59 | + if (ammoStack.isEmpty()) { |
| 60 | + player.getInventory().removeItem(ammoStack); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + triggerExplosionSequence(level, player, target); |
| 66 | + |
| 67 | + |
| 68 | + stack.hurtAndBreak(1, attacker, LivingEntity.getSlotForHand(attacker.getUsedItemHand())); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return super.hurtEnemy(stack, target, attacker); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + private ItemStack findAmmoInInventory(Player player, Predicate<ItemStack> predicate) { |
| 77 | + if (predicate.test(player.getOffhandItem())) { |
| 78 | + return player.getOffhandItem(); |
| 79 | + } |
| 80 | + for (int i = 0; i < player.getInventory().getContainerSize(); i++) { |
| 81 | + ItemStack is = player.getInventory().getItem(i); |
| 82 | + if (predicate.test(is)) { |
| 83 | + return is; |
| 84 | + } |
| 85 | + } |
| 86 | + return ItemStack.EMPTY; |
| 87 | + } |
| 88 | + |
| 89 | + private void triggerExplosionSequence(Level level, Player player, LivingEntity target) { |
| 90 | + int explosionCount = MiaConfig.BLAZE_REAP_EXPLOSION_COUNT.get(); |
| 91 | + double explosionRadius = MiaConfig.BLAZE_REAP_EXPLOSION_RADIUS.get(); |
| 92 | + MinecraftServer server = level.getServer(); |
| 93 | + |
| 94 | + for (int i = 0; i < explosionCount; i++) { |
| 95 | + int delay = i; |
| 96 | + SCHEDULER.schedule(() -> { |
| 97 | + if (server != null) { |
| 98 | + server.execute(() -> { |
| 99 | + if (target != null && target.isAlive() && !target.isRemoved()) { |
| 100 | + Vec3 pos = target.position(); |
| 101 | + |
| 102 | + double x = pos.x + (Math.random() - 0.5) * explosionRadius; |
| 103 | + double y = pos.y + Math.random() * target.getBbHeight(); |
| 104 | + double z = pos.z + (Math.random() - 0.5) * explosionRadius; |
| 105 | + |
| 106 | + if (level instanceof ServerLevel serverLevel) { |
| 107 | + serverLevel.sendParticles(ParticleTypes.FLAME, x, y, z, 5, 0.1, 0.1, 0.1, 0.05); |
| 108 | + } |
| 109 | + |
| 110 | + level.explode( |
| 111 | + player, |
| 112 | + null, |
| 113 | + null, |
| 114 | + x, y, z, |
| 115 | + 2.0f, |
| 116 | + false, |
| 117 | + ExplosionInteraction.NONE |
| 118 | + ); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + }, delay, TimeUnit.SECONDS); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments