Search in sources :

Example 1 with LightningBoltEntity

use of net.minecraft.entity.effect.LightningBoltEntity in project Overloaded by CJ-MC-Mods.

the class ItemRayGun method handleMessage.

public void handleMessage(ServerPlayerEntity player, RayGunMessage message) {
    ItemStack itemStack = player.getItemInHand(Hand.MAIN_HAND);
    if (itemStack.getItem() != this) {
        return;
    }
    LazyOptional<IEnergyStorage> opEnergy = itemStack.getCapability(ENERGY);
    if (!opEnergy.isPresent()) {
        Overloaded.logger.warn("Railgun has no Energy Capability? NBT: " + itemStack.getTag());
        return;
    }
    IEnergyStorage energy = opEnergy.orElseThrow(() -> new RuntimeException("Impossible Condition"));
    if (energy.getEnergyStored() < OverloadedConfig.INSTANCE.rayGun.energyPerShot) {
        player.displayClientMessage(new StringTextComponent("Not enough power to fire."), true);
        return;
    }
    Vector3d eyePos = player.getEyePosition(1);
    if (eyePos.distanceTo(message.vector) > OverloadedConfig.INSTANCE.rayGun.maxRange) {
        player.displayClientMessage(new StringTextComponent("Target out of range."), true);
        return;
    }
    BlockRayTraceResult sanityCheckVec = player.level.clip(new RayTraceContext(eyePos, message.vector, RayTraceContext.BlockMode.COLLIDER, RayTraceContext.FluidMode.NONE, player));
    if (sanityCheckVec.getType() != RayTraceResult.Type.MISS) {
        player.displayClientMessage(new StringTextComponent("Target no longer in sight."), true);
        return;
    }
    energy.extractEnergy(OverloadedConfig.INSTANCE.rayGun.energyPerShot, false);
    LightningBoltEntity entity = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, player.level);
    entity.moveTo(message.vector.x, message.vector.y, message.vector.z, 0, 0);
    player.level.addFreshEntity(entity);
}
Also used : Vector3d(net.minecraft.util.math.vector.Vector3d) RayTraceContext(net.minecraft.util.math.RayTraceContext) LightningBoltEntity(net.minecraft.entity.effect.LightningBoltEntity) IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) StringTextComponent(net.minecraft.util.text.StringTextComponent) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ItemStack(net.minecraft.item.ItemStack)

Aggregations

LightningBoltEntity (net.minecraft.entity.effect.LightningBoltEntity)1 ItemStack (net.minecraft.item.ItemStack)1 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)1 RayTraceContext (net.minecraft.util.math.RayTraceContext)1 Vector3d (net.minecraft.util.math.vector.Vector3d)1 StringTextComponent (net.minecraft.util.text.StringTextComponent)1 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)1