[Floating entities! - Frozen entities - Floating items!]

Discussion in 'Resources' started by ChipDev, Sep 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    ChipDev

    Hallo!
    Today we will be making Floating entities with WitherSkulls.​
    (Very easy, in fact!)​
    Please note: Don't TLDR and skip to the bottom and CMD-C CMD-V ; This is to LEARN. (Try!)
    First, We want to create a WitherSkull.​
    Code:java
    1. WitherSkull skull = (WitherSkull) world.spawn(SomeLocation, WitherSkull.class);
    2. //Regular WitherSkull

    If you have not studied World methods, I recommend you to look into it.
    Next, we want to not have our WitherSkull blow up instantly, Or have it be visible... (You definitely don't want a ugly WitherSkull!)​
    Code:java
    1. skull.setDirection(new Vector(0, 0, 0));
    2. skull.setVelocity(new Vector(0, 0, 0));
    3. //Doing this glitches the witherSkull, making it not visible and not to blow up.

    Also, You can make pigs ride them.. Cool.
    Code:java
    1. Piggi.setRiding(skull);

    And thats it! Simple? right? Heres a demonstration of what this does by SethBling.


    Also:
    you can easily make FREEZEABLE mobs, they can move their heads... but yeah!
    Code:java
    1. WitherSkull base = (WitherSkull) world.spawn(loc, WitherSkull.class);
    2. //Base witherSkull
    3. skull.setDirection(0, 0, 0);
    4. skull.setVelocity(0, 0, 0);
    5. //Invisible
    6. Entity e = world.spawn(loc2, Zombie.class);
    7. e.setRiding(skull);
    8. //Ride frozen witherskull
    9.  

    Also, You can make items float on them too!

    Fully, heres a example, Of creating a boatload of boats!
    Meh, more!
    *Challenge accepted, Skionz!*
    Code:java
    1. package com.chip;
    2.  
    3. import org.bukkit.entity.Entity;
    4. import org.bukkit.entity.EntityType;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.entity.WitherSkull;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerMoveEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. import org.bukkit.util.Vector;
    12.  
    13. public class Rolley extends JavaPlugin implements Listener {
    14.  
    15. public void onEnable() {
    16. getServer().getPluginManager().registerEvents(this, this);
    17. }
    18. public void spawnBoat(Player p) {
    19. WitherSkull skull = (WitherSkull) p.getWorld().spawn(p.getLocation(), WitherSkull.class);
    20. skull.setDirection(new Vector(0, 0, 0));
    21. skull.setVelocity(new Vector(0, 0, 0));
    22. Entity boat = p.getWorld().spawnEntity(p.getLocation().add(0, -1, 0), EntityType.BOAT);
    23. skull.setPassenger(boat);
    24. }
    25. @EventHandler
    26. public void onPlayerMoveEvent(PlayerMoveEvent e) {
    27. spawnBoat(e.getPlayer());
    28. }
    29.  
    30. }
    31.  

    Which does
    [​IMG]

    Thanks for reading!!
     
    danichef, mine-care, hintss and 4 others like this.
  2. Offline

    Skionz

    ChipDev Nice I never actually knew how to do this until now! The one I made just spawned client side boats that floated by default :p
     
    GrandmaJam and ChipDev like this.
  3. Offline

    ChipDev

    Wat? Automatically? o.o
     
    GrandmaJam likes this.
  4. Offline

    Skionz

    Yea all client side mobs spawned with PacketPlayOutSpawnLivingEntity (something around that) are spawned client side and they just float there and don't move I guess.
     
    GrandmaJam likes this.
  5. Offline

    zombiekiller753

    Skionz
    Yeah.... because the client doesn't tick entities. The server does.

    ChipDev
    pls. packets. With your use of witherskulls, you'll crash the server because of how many entities you'll end up spawning
     
    megasaad44 and LordVakar like this.
  6. Offline

    ChipDev

    zombiekiller753 Im not pro enough to learn packets yet. lol
    Just saying, for example for the boats listen to when the boat breaks, get what it is riding, and destroy it

    I thought you did the same as I did! lol

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  7. Offline

    Onlineids

    ChipDev This is very inefficient you should probably be using packets
     
  8. Offline

    Gamecube762

    As the skulls are never removed, they are going to be staying there forever. This can end badly.

    Also, if you don't want people to be copy/pasting the code at the end, maybe not include it?
     
  9. Offline

    ChipDev

    Thats for some random test code no one wants..
     
  10. Offline

    ChipDev

    Wish I would of made a reservation; but..
    you can easily make FREEZEABLE mobs, they can move their heads... but yeah!
    Code:java
    1. WitherSkull base = (WitherSkull) world.spawn(loc, WitherSkull.class);
    2. //Base witherSkull
    3. skull.setDirection(0, 0, 0);
    4. skull.setVelocity(0, 0, 0);
    5. //Invisible
    6. Entity e = world.spawn(loc2, Zombie.class);
    7. e.setRiding(skull);
    8. //Ride frozen witherskull
    9.  

    Also, You can make items float on them too!
    edit: edited original post
     
  11. Offline

    MrDplugins

    ChipDev How do we use items? I see the "DroppedItem" but I do not understand how to get it to be a item I want like a sword or a helmet. Also is it possible to give these items names?
     
  12. Offline

    ChipDev

    Yeah!
    Use dropItemNaturally.
     
  13. Offline

    MrDplugins

    ChipDev I understand that but I don't understand how to SET the item!
    Heres my code.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
        if(cmd.getName().equalsIgnoreCase("fe")) {
            player.sendMessage(ChatColor.AQUA + "Item Spawned");
            spawnItem(player.getPlayer());
            }
        return true;
    }
     
    public void spawnItem(Player p) {
    WitherSkull skull = (WitherSkull) p.getWorld().spawn(p.getLocation(), WitherSkull.class);
    skull.setDirection(new Vector(0, 0, 0));
    skull.setVelocity(new Vector(0, 0, 0));
    Entity sword = p.getWorld().spawnEntity(p.getLocation().add(0, -1, 0), EntityType.DROPPED_ITEM);
    skull.setPassenger(sword);
     
     
    }
     
  14. Offline

    ChipDev

    Errors?
     
  15. Offline

    MrDplugins

    ChipDev None what I'm asking is that I want to set the item as a diamond sword, how do I set it to be a diamond sword?
     
  16. Offline

    ChipDev

    Ok here:
    Code:java
    1. Entity e = world.dropItem(loc, new ItemStack(Material.DIAMOND_SWORD);
    2. //use e as an entity
     
  17. Offline

    MrDplugins

    ChipDev ohhhhh, thank you! I thought were saying it was a exstention of the .DROPPEDITEM
     
  18. Offline

    ChipDev

    Welcome :D
     
  19. Offline

    Monkey_Swag

    Something I found out how to do is create a living entity that can have custom displaynames and what not. (For more information look at this)

    For example, setting a custom displayname for a Witch:
    Code:java
    1. WitherSkull skull = (WitherSkull) p.getWorld().spawn(p.getLocation(), WitherSkull.class);
    2. skull.setDirection(new Vector(0, 0, 0));
    3. skull.setVelocity(new Vector(0, 0, 0));
    4. Witch witch = (Witch) p.getWorld().spawnEntity(p.getLocation(), EntityType.WITCH);
    5. witch.setCustomName("ยง6Ultra Witch");
    6. witch.setCustomNameVisible(true);
    7. skull.setPassenger(witch);
     
  20. Offline

    ibWill

    Hi, Ive been playing with this idea and I wanted to see if i could make a dropped block move with a player but the wither skull keeps appearing? is there a fix for that?
     
  21. Offline

    ChipDev

    Sorry, that requries something different Like NMS, If the wither skull moves , it appears!
     
  22. Offline

    ChipDev

    Save all of your wither skulls to a hash map and retrieve them, and remove them.
     
  23. Offline

    xXBeLkAXx

    I made a util, with withers and items, but some code is in NMS:

    Code:
    public class FloatingItem {
      
        private Material mater;
        private String name;
        private Location loc;
        private double dy;
        private String message;
        private EntityWitherSkull skull;
        private int[] ids = new int[3];
      
        public FloatingItem(Location loc, double dy, String message, String name, Material mater) {
            this.loc = loc;
            this.dy = dy;
            this.message = message;
            this.name = name;
            this.mater = mater;
            skull = new EntityWitherSkull(((CraftWorld) loc.getWorld()).getHandle());
            skull.setLocation(loc.getX(), loc.getY() + dy + 55, loc.getZ(), 0, 0);
            ((CraftWorld) loc.getWorld()).getHandle().addEntity(skull);
        }
      
        public FloatingItem(String world, double x, double y, double z, double dy, String message, String name, Material mater) {
            this(new Location(Bukkit.getWorld(world), x, y, z), dy, message, name, mater);
        }
      
        @SuppressWarnings("deprecation")
        public void send() {
                EntityHorse horse = new EntityHorse(((CraftWorld) loc.getWorld()).getHandle());
                horse.setLocation(loc.getX(), loc.getY() + dy + 55.25, loc.getZ(), 0, 0);
                horse.setAge(-1700000);
                horse.setCustomName(message);
                horse.setCustomNameVisible(true);
                PacketPlayOutSpawnEntityLiving spawn = new PacketPlayOutSpawnEntityLiving(horse);
              
                for(Player player : Bukkit.getOnlinePlayers()) {
                sendPacket(player, spawn);
                }
              
                PacketPlayOutAttachEntity pa = new PacketPlayOutAttachEntity(0, horse, skull);
              
                for(Player player : Bukkit.getOnlinePlayers()) {
                sendPacket(player, pa);
                }
              
                WorldServer world = ((CraftWorld) loc.getWorld()).getHandle();
                EntityWitherSkull sk = new EntityWitherSkull(world);
                sk.setLocation(loc.getX(), loc.getY() + 0.4, loc.getZ(), 0, 0);
                ((CraftWorld) loc.getWorld()).getHandle().addEntity(sk);
              
                Item haloItem = loc.getWorld().dropItemNaturally(loc, new ItemStack(mater));
                haloItem.setPickupDelay(Integer.MAX_VALUE);
                sk.getBukkitEntity().setPassenger(haloItem);
              
                ids[0] = skull.getId();
                ids[1] = horse.getId();
                ids[2] = haloItem.getEntityId();
        }
      
        @SuppressWarnings("deprecation")
        public void remove() {
                for(Player player : Bukkit.getOnlinePlayers()) {
                    for(int i = 0; i < ids.length; i++) {
                    PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(ids[i]);
                    sendPacket(player, packet);
                    }
                }
              
                for(Entity ent : loc.getWorld().getEntities()) {
                    if(ent.getEntityId() == ids[2]) {
                        ent.remove();
                        break;
                    }
                }
              
                ids = new int[3];
                mater = null;
                name = null;
                loc = null;
                dy = 0;
                message = null;
                skull = null;
        }
      
        private void sendPacket(Player p, Packet packet) {
            ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
        }
      
        public String getName() {
            return name;
        }
    
        public int[] getIds() {
            return ids;
        }
      
        public Location getLocation() {
            return loc;
        }
    
        public void setLocation(Location loc) {
            this.loc = loc;
        }
      
        public int getSkull() {
            return ids[0];
        }
      
        public int getHorse() {
            return ids[1];
        }
      
        public int getItem() {
            return ids[2];
        }
      
    }
    And a manager for util:

    Code:
    public class FloatItemManager {
    
        public List<FloatingItem> holos = new ArrayList<>();
        private static FloatItemManager hm = new FloatItemManager();
      
        public static FloatItemManager getManager() {
            return hm;
        }
      
        public void makeFloatingItem(Location loc, double dy, String message, String name, Material mat) {
            FloatingItem holo = new FloatingItem(loc, 1.0, message, name, mat);
            holo.send();
            holos.add(holo);
        }
      
        public FloatingItem getByName(String name) {
            for(FloatingItem h : holos) {
                if(h.getName().equalsIgnoreCase(name)) {
                   return h;
                }
            }
            return null;
        }
      
        public FloatingItem getFloatingItem(Location loc) {
            for(FloatingItem h : holos) {
                if(h.getLocation() == loc) {
                   return h;
                }
            }
            return null;
        }
      
        public void removeFloatingItem(FloatingItem holo) {
            if(holos.contains(holo)) {
                holos.remove(holo);
              
                holo.remove();
            }
        }
      
    }
    
    This util can make something like this:

    [​IMG]
     
Thread Status:
Not open for further replies.

Share This Page