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

68 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;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
2019-11-04 19:08:49 +01:00
import net.minecraft.entity.EntitySpawnPlacementRegistry;
2021-12-04 15:40:09 +01:00
import net.minecraft.entity.player.Player;
2019-02-19 17:23:03 +01:00
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
2021-12-04 15:40:09 +01:00
import net.minecraft.util.InteractionResult;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Hand;
2019-02-19 17:23:03 +01:00
import net.minecraft.util.math.BlockPos;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.LightType;
import net.minecraft.level.Level;
2019-02-19 17:23:03 +01:00
2020-01-26 01:41:49 +01:00
public class ItemCaveFinder extends ItemImpl {
public ItemCaveFinder() {
2020-02-07 23:50:16 +01:00
super("cave_finder", new Properties().maxStackSize(1));
2019-02-19 17:23:03 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public ActionResult<ItemStack> onItemRightClick(Level levelIn, Player playerIn, Hand handIn) {
2019-02-19 17:23:03 +01:00
ItemStack stack = playerIn.getHeldItem(handIn);
NaturesAuraAPI.IInternalHooks inst = NaturesAuraAPI.instance();
2021-12-04 15:40:09 +01:00
if (!inst.extractAuraFromPlayer(playerIn, 20000, levelIn.isClientSide))
return new ActionResult<>(InteractionResult.FAIL, stack);
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);
2019-02-19 17:23:03 +01:00
BlockPos pos = playerIn.getPosition();
int range = 30;
for (int x = -range; x <= range; x++)
for (int y = -range; y <= range; y++)
for (int z = -range; z <= range; z++) {
BlockPos offset = pos.add(x, y, z);
2021-12-04 15:40:09 +01:00
BlockState state = levelIn.getBlockState(offset);
try {
2021-12-04 15:40:09 +01:00
if (!state.getBlock().canCreatureSpawn(state, levelIn, offset, EntitySpawnPlacementRegistry.PlacementType.ON_GROUND, null))
continue;
} catch (Exception e) {
2019-02-19 17:23:03 +01:00
continue;
}
2019-02-19 17:23:03 +01:00
BlockPos offUp = offset.up();
2021-12-04 15:40:09 +01:00
BlockState stateUp = levelIn.getBlockState(offUp);
if (stateUp.isNormalCube(levelIn, offUp) || stateUp.getMaterial().isLiquid())
2019-02-19 17:23:03 +01:00
continue;
2021-12-04 15:40:09 +01:00
int sky = levelIn.getLightFor(LightType.SKY, offUp);
int block = levelIn.getLightFor(LightType.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
playerIn.swingArm(handIn);
}
playerIn.getCooldownTracker().setCooldown(this, 20 * 30);
2021-12-04 15:40:09 +01:00
return new ActionResult<>(InteractionResult.SUCCESS, stack);
2019-02-19 17:23:03 +01:00
}
}