NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemLootFinder.java

63 lines
2.7 KiB
Java
Raw Normal View History

2020-02-28 15:36:12 +01:00
package de.ellpeck.naturesaura.items;
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2021-12-15 16:24:53 +01:00
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
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.entity.SpawnerBlockEntity;
import net.minecraft.world.phys.AABB;
2022-09-03 12:06:41 +02:00
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import net.minecraftforge.registries.ForgeRegistries;
2020-02-28 15:36:12 +01:00
public class ItemLootFinder extends ItemImpl {
2021-12-15 16:24:53 +01:00
2020-02-28 15:36:12 +01:00
public ItemLootFinder() {
super("loot_finder");
}
@Override
2021-12-15 16:24:53 +01:00
public InteractionResultHolder<ItemStack> use(Level levelIn, Player playerIn, InteractionHand handIn) {
2021-12-15 16:30:22 +01:00
var stack = playerIn.getItemInHand(handIn);
var inst = NaturesAuraAPI.instance();
2020-02-28 15:36:12 +01:00
if (!inst.extractAuraFromPlayer(playerIn, 100000, false))
2021-12-15 16:24:53 +01:00
return new InteractionResultHolder<>(InteractionResult.FAIL, stack);
2021-12-04 15:40:09 +01:00
if (levelIn.isClientSide) {
2020-02-28 15:36:12 +01:00
inst.setParticleDepth(false);
inst.setParticleSpawnRange(64);
inst.setParticleCulling(false);
2021-12-15 16:30:22 +01:00
var pos = playerIn.blockPosition();
2021-12-04 19:17:21 +01:00
Helper.getBlockEntitiesInArea(levelIn, pos, 64, tile -> {
if (tile.getCapability(ForgeCapabilities.ITEM_HANDLER).isPresent() || tile instanceof SpawnerBlockEntity || ForgeRegistries.BLOCK_ENTITY_TYPES.getKey(tile.getType()).getNamespace().equals("lootr")) {
2020-02-28 15:36:12 +01:00
inst.spawnMagicParticle(
2021-12-15 16:24:53 +01:00
tile.getBlockPos().getX() + 0.5F, tile.getBlockPos().getY() + 0.5F, tile.getBlockPos().getZ() + 0.5F,
2020-02-28 15:36:12 +01:00
0F, 0F, 0F, 0xf5f10a, 6F, 20 * 60, 0F, false, true);
}
return false;
});
2021-12-15 16:30:22 +01:00
for (var entity : levelIn.getEntitiesOfClass(Entity.class, new AABB(pos).inflate(64))) {
2022-09-03 12:06:41 +02:00
if (!(entity instanceof LivingEntity) && entity.getCapability(ForgeCapabilities.ITEM_HANDLER).isPresent()) {
2020-02-28 15:36:12 +01:00
inst.spawnMagicParticle(
2021-12-15 16:24:53 +01:00
entity.getX(), entity.getEyeY(), entity.getZ(),
2020-02-28 15:36:12 +01:00
0F, 0F, 0F, 0xf5f10a, 6F, 20 * 60, 0F, false, true);
}
}
inst.setParticleDepth(true);
inst.setParticleSpawnRange(32);
inst.setParticleCulling(true);
2021-12-15 16:24:53 +01:00
playerIn.swing(handIn);
2020-02-28 15:36:12 +01:00
}
2021-12-15 16:24:53 +01:00
playerIn.getCooldowns().addCooldown(this, 20 * 60);
return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack);
2020-02-28 15:36:12 +01:00
}
2020-02-28 15:36:12 +01:00
}