Tutorial Create a balloon for a player. (Clay or whatever block)

Discussion in 'Resources' started by Shortie, Nov 20, 2018.

  1. Offline

    Shortie

    Hello Community,

    nearly everyone who's watching this Thread is asking himself how to create a balloon that a Player can hold in its hand.

    I'm really sorry for my bad english.

    Image of it (open)


    I tried this only for 1.8.8

    So, let's begin.

    At first, you have to create a Ballon class with a constructor and the following variables and methods:
    Code:
    public class Ballon {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]
        public Player player;
        public ItemStack block;
        public FallingBlock fallingBlock;
        public Bat bat;
        public boolean active = false;
    
        public static HashMap<Player, Ballon> balloonHashMap = new HashMap<Player, Ballon>();
    
    public Ballon(Player player, ItemStack block) {
            this.player = player;
            this.block = block;
        }


    You can also generate Getter's and Setter's if you want.

    So now create a method called "spawn".
    Code:
     public void spawn() {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]        active = true;
            balloonHashMap.put(player, this);
    }


    Now you have to create the Falling Block and the Bat. The Bat is there that you can leash the block later.
    Code:
    final FallingBlock[] fallingBlock = {player.getWorld().spawnFallingBlock(player.getLocation().add(0.0D, 2.0D, 0.0D), this.block.getType(), this.block.getData().getData())};[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]        Bat bat = (Bat)player.getWorld().spawnEntity(player.getLocation().add(0.0D, 2.0D, 0.0D), EntityType.BAT);


    So, now you have to set some parameters for the Bat and the Falling Block:
    Code:
      fallingBlock[0].setDropItem(false);[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]        bat.setPassenger(fallingBlock[0]);
            bat.setRemoveWhenFarAway(false);
            bat.setLeashHolder(player);
            bat.setCanPickupItems(false);
            bat.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 255));
    NBTTagCompound compound = new NBTTagCompound();
    ((CraftEntity)bat).getHandle().e(compound);
    compound.setInt("Silent", 1);
    ((CraftEntity)bat).getHandle().f(compound);
    NBTTagCompound tag = new NBTTagCompound();
    ((CraftEntity)fallingBlock[0]).getHandle().e(tag);
    tag.setInt("Time", 1);
    ((CraftEntity)fallingBlock[0]).getHandle().f(tag);
    


    When you've got that, you have to initialize the Bat and the Falling Block in the class:
    Code:
     this.bat = bat;[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]        this.fallingBlock = fallingBlock[0];


    Then create a BukkitRunnable(Timer Task) with 2L delay and 2L period.
    Code:
    new BukkitRunnable() {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]            @Override
                public void run() {}
            }.runTaskTimer(Main.getPlugin(Main.class),2L,2L);


    So now you need the "active" boolean:
    Code:
     if(active) {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]} else {
           despawn();
           cancel();
    }

    I'll get later to that despawn thing.

    ** Now: In the "active" if-clause **

    ----------
    Code:
    if(active) {
    ----------

    So now you have to ask if the Bat is(n't) leashed.
    Code:
    if(!bat.isLeashed()) {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]                        despawn();
                            cancel();
                        }


    Then you have to do the same thing as above:
    Code:
    NBTTagCompound tag = new NBTTagCompound();[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]                    ((CraftEntity)fallingBlock[0]).getHandle().e(tag);
                        tag.setInt("Time", 1);
                        ((CraftEntity)fallingBlock[0]).getHandle().f(tag);


    Now check if the Falling Block is on the ground and if it's null:
    Code:
    NBTTagCompound tag = new NBTTagCompound();[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]                    ((CraftEntity)fallingBlock[0]).getHandle().e(tag);
                        tag.setInt("Time", 1);
                        ((CraftEntity)fallingBlock[0]).getHandle().f(tag);
    if(fallingBlock[0] == null) {
        fallingBlock[0] = player.getWorld().spawnFallingBlock(player.getLocation().add(0.0D, 2.0D, 0.0D), block.getType(), block.getData().getData());
        bat.setPassenger(fallingBlock[0]);
        fallingBlock[0].setDropItem(false);
    }
    


    Now check if the Bat' distance to the player is above "3.0":
    Code:
    if(bat.getLocation().distance(player.getLocation()) > 3.0D) {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]                        bat.setVelocity(new Vector(player.getLocation().getX() - bat.getLocation().getX()
                            , player.getLocation().getY() - bat.getLocation().getY(),
                                    player.getLocation().getZ() - bat.getLocation().getZ()).normalize().multiply(0.5D));
                        } else {
                            bat.setVelocity(new Vector(0,0,0));
                        }
                       


    Now check if the Bat's distance to the player is above "6.0":

    Code:
      if(bat.getLocation().distance(player.getLocation()) > 6.0D) {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]                        bat.setVelocity(new Vector(player.getLocation().getX() - bat.getLocation().getX()
                                    , player.getLocation().getY() - bat.getLocation().getY(),
                                    player.getLocation().getZ() - bat.getLocation().getZ()).normalize().multiply(1.25D));
                        }


    Now check players eye location with the bat's y location:
    Code:
    if (player.getEyeLocation().getY() - bat.getLocation().getY() >= 0.125D) {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]                        bat.setVelocity(new Vector(0.0D, 0.5D, 0.0D));
                        }


    And finally do a try catch for the "setPassenger" method:
    Code:
    try {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]                        bat.setPassenger(fallingBlock[0]);
                        } catch(Exception e1) {
                            fallingBlock[0].getLocation().getBlock().setType(Material.AIR);
                            fallingBlock[0] = player.getWorld().spawnFallingBlock(player.getLocation().add(0.0D, 2.0D, 0.0D), block.getType(), block.getData().getData());
                            bat.setPassenger(fallingBlock[0]);
                            fallingBlock[0].setDropItem(false);
                        }



    --------
    Code:
    } else {
    --------

    So now insert "despawn" and "cancel" if active isn't true:
    Code:
    despawn();[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]                    cancel();


    -------
    Code:
    }
    -------


    That was the "spawn" method, now finally to the despawn method:

    Set boolean "active" to false.
    Set the leash holder of bat to null.
    Remove the Falling Block.
    Remove the Bat.
    And remove the player from the balloonHashMap.
    Code:
     public void despawn() {[/COLOR][/LEFT]
    [COLOR=rgb(44, 44, 44)]
    [LEFT]        active = false;
            bat.setLeashHolder(null);
            fallingBlock.remove();
            bat.remove();
            Ballons.ballonHashMap.remove(player);
        }



    Thank you for watching my Thread.

     
    Last edited by a moderator: Nov 20, 2018
  2. Offline

    x9nico

    The image not work.
     

Share This Page