Search in sources :

Example 1 with BlockSourceImpl

use of net.minecraft.block.BlockSourceImpl in project SpongeCommon by SpongePowered.

the class DispenserSourceLogic method launch.

@SuppressWarnings("unchecked")
private <P extends Projectile> Optional<P> launch(TileEntityDispenser dispenser, Class<P> projectileClass, Item item) {
    BehaviorDefaultDispenseItem behavior = (BehaviorDefaultDispenseItem) BlockDispenser.DISPENSE_BEHAVIOR_REGISTRY.getObject(item);
    List<Entity> entityList = dispenser.getWorld().loadedEntityList;
    int numEntities = entityList.size();
    behavior.dispense(new BlockSourceImpl(dispenser.getWorld(), dispenser.getPos()), new ItemStack(item));
    // Hack - get the projectile that was spawned from dispense()
    for (int i = entityList.size() - 1; i >= numEntities; i--) {
        if (projectileClass.isInstance(entityList.get(i))) {
            return Optional.of((P) entityList.get(i));
        }
    }
    return Optional.empty();
}
Also used : BehaviorDefaultDispenseItem(net.minecraft.dispenser.BehaviorDefaultDispenseItem) Entity(net.minecraft.entity.Entity) BlockSourceImpl(net.minecraft.block.BlockSourceImpl) ItemStack(net.minecraft.item.ItemStack)

Example 2 with BlockSourceImpl

use of net.minecraft.block.BlockSourceImpl in project Magma by magmafoundation.

the class CraftBlockProjectileSource method launchProjectile.

@Override
public <T extends Projectile> T launchProjectile(Class<? extends T> projectile, Vector velocity) {
    Validate.isTrue(getBlock().getType() == Material.DISPENSER, "Block is no longer dispenser");
    // Copied from BlockDispenser.dispense()
    IBlockSource isourceblock = new BlockSourceImpl(dispenserBlock.getWorld(), dispenserBlock.getPos());
    // Copied from DispenseBehaviorProjectile
    IPosition iposition = BlockDispenser.getDispensePosition(isourceblock);
    EnumFacing enumdirection = (EnumFacing) isourceblock.getBlockState().getValue(BlockDispenser.FACING);
    net.minecraft.world.World world = dispenserBlock.getWorld();
    net.minecraft.entity.Entity launch = null;
    if (Snowball.class.isAssignableFrom(projectile)) {
        launch = new EntitySnowball(world, iposition.getX(), iposition.getY(), iposition.getZ());
    } else if (Egg.class.isAssignableFrom(projectile)) {
        launch = new EntityEgg(world, iposition.getX(), iposition.getY(), iposition.getZ());
    } else if (EnderPearl.class.isAssignableFrom(projectile)) {
        launch = new EntityEnderPearl(world, null);
        launch.setPosition(iposition.getX(), iposition.getY(), iposition.getZ());
    } else if (ThrownExpBottle.class.isAssignableFrom(projectile)) {
        launch = new EntityExpBottle(world, iposition.getX(), iposition.getY(), iposition.getZ());
    } else if (ThrownPotion.class.isAssignableFrom(projectile)) {
        if (LingeringPotion.class.isAssignableFrom(projectile)) {
            launch = new EntityPotion(world, iposition.getX(), iposition.getY(), iposition.getZ(), CraftItemStack.asNMSCopy(new ItemStack(Material.LINGERING_POTION, 1)));
        } else {
            launch = new EntityPotion(world, iposition.getX(), iposition.getY(), iposition.getZ(), CraftItemStack.asNMSCopy(new ItemStack(Material.SPLASH_POTION, 1)));
        }
    } else if (Arrow.class.isAssignableFrom(projectile)) {
        if (TippedArrow.class.isAssignableFrom(projectile)) {
            launch = new EntityTippedArrow(world, iposition.getX(), iposition.getY(), iposition.getZ());
            ((EntityTippedArrow) launch).setType(CraftPotionUtil.fromBukkit(new PotionData(PotionType.WATER, false, false)));
        } else if (SpectralArrow.class.isAssignableFrom(projectile)) {
            launch = new EntitySpectralArrow(world, iposition.getX(), iposition.getY(), iposition.getZ());
        } else {
            launch = new EntityTippedArrow(world, iposition.getX(), iposition.getY(), iposition.getZ());
        }
        ((EntityArrow) launch).pickupStatus = EntityArrow.PickupStatus.ALLOWED;
        ((EntityArrow) launch).projectileSource = this;
    } else if (Fireball.class.isAssignableFrom(projectile)) {
        double d0 = iposition.getX() + (double) ((float) enumdirection.getFrontOffsetX() * 0.3F);
        double d1 = iposition.getY() + (double) ((float) enumdirection.getFrontOffsetY() * 0.3F);
        double d2 = iposition.getZ() + (double) ((float) enumdirection.getFrontOffsetZ() * 0.3F);
        Random random = world.rand;
        double d3 = random.nextGaussian() * 0.05D + (double) enumdirection.getFrontOffsetX();
        double d4 = random.nextGaussian() * 0.05D + (double) enumdirection.getFrontOffsetY();
        double d5 = random.nextGaussian() * 0.05D + (double) enumdirection.getFrontOffsetZ();
        if (SmallFireball.class.isAssignableFrom(projectile)) {
            launch = new EntitySmallFireball(world, null, d0, d1, d2);
        } else if (WitherSkull.class.isAssignableFrom(projectile)) {
            launch = new EntityWitherSkull(world);
            launch.setPosition(d0, d1, d2);
            double d6 = (double) MathHelper.sqrt(d3 * d3 + d4 * d4 + d5 * d5);
            ((EntityFireball) launch).accelerationX = d3 / d6 * 0.1D;
            ((EntityFireball) launch).accelerationY = d4 / d6 * 0.1D;
            ((EntityFireball) launch).accelerationZ = d5 / d6 * 0.1D;
        } else {
            launch = new EntityLargeFireball(world);
            launch.setPosition(d0, d1, d2);
            double d6 = (double) MathHelper.sqrt(d3 * d3 + d4 * d4 + d5 * d5);
            ((EntityFireball) launch).accelerationX = d3 / d6 * 0.1D;
            ((EntityFireball) launch).accelerationY = d4 / d6 * 0.1D;
            ((EntityFireball) launch).accelerationZ = d5 / d6 * 0.1D;
        }
        ((EntityFireball) launch).projectileSource = this;
    }
    Validate.notNull(launch, "Projectile not supported");
    if (launch instanceof IProjectile) {
        if (launch instanceof EntityThrowable) {
            ((EntityThrowable) launch).projectileSource = this;
        }
        // Values from DispenseBehaviorProjectile
        float a = 6.0F;
        float b = 1.1F;
        if (launch instanceof EntityPotion || launch instanceof ThrownExpBottle) {
            // Values from respective DispenseBehavior classes
            a *= 0.5F;
            b *= 1.25F;
        }
        // Copied from DispenseBehaviorProjectile
        ((IProjectile) launch).shoot((double) enumdirection.getFrontOffsetX(), (double) ((float) enumdirection.getFrontOffsetY() + 0.1F), (double) enumdirection.getFrontOffsetZ(), b, a);
    }
    if (velocity != null) {
        ((T) launch.getBukkitEntity()).setVelocity(velocity);
    }
    world.spawnEntity(launch);
    return (T) launch.getBukkitEntity();
}
Also used : EntityWitherSkull(net.minecraft.entity.projectile.EntityWitherSkull) EntityTippedArrow(net.minecraft.entity.projectile.EntityTippedArrow) EntityLargeFireball(net.minecraft.entity.projectile.EntityLargeFireball) EnumFacing(net.minecraft.util.EnumFacing) EntityEnderPearl(net.minecraft.entity.item.EntityEnderPearl) EntityEgg(net.minecraft.entity.projectile.EntityEgg) IProjectile(net.minecraft.entity.IProjectile) PotionData(org.bukkit.potion.PotionData) EntityTippedArrow(net.minecraft.entity.projectile.EntityTippedArrow) TippedArrow(org.bukkit.entity.TippedArrow) EntitySpectralArrow(net.minecraft.entity.projectile.EntitySpectralArrow) Random(java.util.Random) EntitySmallFireball(net.minecraft.entity.projectile.EntitySmallFireball) EntityPotion(net.minecraft.entity.projectile.EntityPotion) EntityFireball(net.minecraft.entity.projectile.EntityFireball) EntitySpectralArrow(net.minecraft.entity.projectile.EntitySpectralArrow) EntityTippedArrow(net.minecraft.entity.projectile.EntityTippedArrow) Arrow(org.bukkit.entity.Arrow) EntityArrow(net.minecraft.entity.projectile.EntityArrow) SpectralArrow(org.bukkit.entity.SpectralArrow) TippedArrow(org.bukkit.entity.TippedArrow) BlockSourceImpl(net.minecraft.block.BlockSourceImpl) EntityArrow(net.minecraft.entity.projectile.EntityArrow) EntityThrowable(net.minecraft.entity.projectile.EntityThrowable) IPosition(net.minecraft.dispenser.IPosition) EntityEgg(net.minecraft.entity.projectile.EntityEgg) Egg(org.bukkit.entity.Egg) EntityExpBottle(net.minecraft.entity.item.EntityExpBottle) IBlockSource(net.minecraft.dispenser.IBlockSource) EntitySnowball(net.minecraft.entity.projectile.EntitySnowball) ThrownExpBottle(org.bukkit.entity.ThrownExpBottle) CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) EntityWitherSkull(net.minecraft.entity.projectile.EntityWitherSkull) WitherSkull(org.bukkit.entity.WitherSkull)

Aggregations

BlockSourceImpl (net.minecraft.block.BlockSourceImpl)2 Random (java.util.Random)1 BehaviorDefaultDispenseItem (net.minecraft.dispenser.BehaviorDefaultDispenseItem)1 IBlockSource (net.minecraft.dispenser.IBlockSource)1 IPosition (net.minecraft.dispenser.IPosition)1 Entity (net.minecraft.entity.Entity)1 IProjectile (net.minecraft.entity.IProjectile)1 EntityEnderPearl (net.minecraft.entity.item.EntityEnderPearl)1 EntityExpBottle (net.minecraft.entity.item.EntityExpBottle)1 EntityArrow (net.minecraft.entity.projectile.EntityArrow)1 EntityEgg (net.minecraft.entity.projectile.EntityEgg)1 EntityFireball (net.minecraft.entity.projectile.EntityFireball)1 EntityLargeFireball (net.minecraft.entity.projectile.EntityLargeFireball)1 EntityPotion (net.minecraft.entity.projectile.EntityPotion)1 EntitySmallFireball (net.minecraft.entity.projectile.EntitySmallFireball)1 EntitySnowball (net.minecraft.entity.projectile.EntitySnowball)1 EntitySpectralArrow (net.minecraft.entity.projectile.EntitySpectralArrow)1 EntityThrowable (net.minecraft.entity.projectile.EntityThrowable)1 EntityTippedArrow (net.minecraft.entity.projectile.EntityTippedArrow)1 EntityWitherSkull (net.minecraft.entity.projectile.EntityWitherSkull)1