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

67 lines
2.9 KiB
Java
Raw Normal View History

2019-02-19 17:23:03 +01:00
package de.ellpeck.naturesaura.items;
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.SpawnPlacements;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
2019-02-19 17:23:03 +01:00
2020-01-26 01:41:49 +01:00
public class ItemCaveFinder extends ItemImpl {
2021-12-15 16:24:53 +01:00
2020-01-26 01:41:49 +01:00
public ItemCaveFinder() {
2021-12-15 16:24:53 +01:00
super("cave_finder", new Properties().stacksTo(1));
2019-02-19 17:23:03 +01:00
}
@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();
2021-12-04 15:40:09 +01:00
if (!inst.extractAuraFromPlayer(playerIn, 20000, levelIn.isClientSide))
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) {
2019-02-19 17:23:03 +01:00
inst.setParticleDepth(false);
inst.setParticleSpawnRange(64);
2020-02-28 15:36:12 +01:00
inst.setParticleCulling(false);
2021-12-15 16:30:22 +01:00
var pos = playerIn.blockPosition();
var range = 30;
for (var x = -range; x <= range; x++)
for (var y = -range; y <= range; y++)
for (var z = -range; z <= range; z++) {
var offset = pos.offset(x, y, z);
var state = levelIn.getBlockState(offset);
try {
2021-12-15 16:24:53 +01:00
if (!state.getBlock().isValidSpawn(state, levelIn, offset, SpawnPlacements.Type.ON_GROUND, null))
continue;
} catch (Exception e) {
2019-02-19 17:23:03 +01:00
continue;
}
2019-02-19 17:23:03 +01:00
2021-12-15 16:30:22 +01:00
var offUp = offset.above();
var stateUp = levelIn.getBlockState(offUp);
2021-12-15 16:24:53 +01:00
if (stateUp.isCollisionShapeFullBlock(levelIn, offUp) || stateUp.getMaterial().isLiquid())
2019-02-19 17:23:03 +01:00
continue;
2021-12-15 16:30:22 +01:00
var sky = levelIn.getBrightness(LightLayer.SKY, offUp);
var block = levelIn.getBrightness(LightLayer.BLOCK, offUp);
2019-02-19 17:23:03 +01:00
if (sky > 7 || block > 7)
continue;
inst.spawnMagicParticle(
offset.getX() + 0.5F, offset.getY() + 1.5F, offset.getZ() + 0.5F,
0F, 0F, 0F, 0x992101, 2.5F, 20 * 30, 0F, false, true);
}
inst.setParticleDepth(true);
inst.setParticleSpawnRange(32);
2020-02-28 15:36:12 +01:00
inst.setParticleCulling(true);
2019-02-19 17:23:03 +01:00
2021-12-15 16:24:53 +01:00
playerIn.swing(handIn);
2019-02-19 17:23:03 +01:00
}
2021-12-15 16:24:53 +01:00
playerIn.getCooldowns().addCooldown(this, 20 * 30);
return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack);
2019-02-19 17:23:03 +01:00
}
}