Need Halp Figuring Out How To Do This

Discussion in 'Plugin Development' started by Agent000037, Jun 28, 2017.

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

    Agent000037

    Code:
        @EventHandler
        public void onMove (PlayerMoveEvent e) {
           
           
            Player p = e.getPlayer();
            p.sendMessage("bool dangle: " + dangle);
           
            if (dangle == true) {
               
                p.sendMessage("new dangle");
                           
                Location loc = p.getLocation().add(0, -4, 0);
               
                ItemStack itemStack = new ItemStack(Material.DIAMOND_SWORD, 1);
                final Hologram hologram = HologramsAPI.createHologram(this, loc);
                hologram.appendTextLine(ChatColor.WHITE + "Diamond Sword");
                ItemLine itemLine = hologram.appendItemLine(itemStack);
               
               
            }//p.sendMessage("moving");
           
        }
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    
    
    if(cmd.getName().equalsIgnoreCase("dangle")) {
    
    
    sender.sendMessage("dangling...");
    
    
    dangle = true;
    
    
    BukkitScheduler scheduler2 = getServer().getScheduler();
    
    
    scheduler2.scheduleSyncDelayedTask(this, new Runnable() {
    
    
    @Override
    
            public void run() {
    
    dangle = false;
    
    sender.sendMessage("end dangle");
    
            }
    
        }, 100L);
    
    
    returntrue;
    
    }
    
    
    returnfalse;
    
    }
        
    So this is my code atm. Obviously, if you look at the move event code, I have it so that whenever you move the hologram is displayed at a new spot... so basically what this does is it makes it be displayed at a different location whenever you move, so that it's always in the same spot in relation to you, of course. Although the problem is I only want it to be displayed once, then deleted when the player moves (otherwise it's just an endless trail of holograms). How could I achieve this?
     
  2. Offline

    Zombie_Striker

    @Agent000037
    Store the instance of the previous hologram. When you want to move, remove the old hologram, add the new one, and store the new hologram in the place as the previous one. If you do this for multiple players, use Hashmaps.
     
  3. Offline

    Agent000037

    I'm not sure how to actually do that, could u write some pseudo code? or modify my code? I get the idea but I'm new to this API, sorry XD
     
  4. Offline

    Zombie_Striker

    @Agent000037
    This has nothing to do with the BukkitAPI. This is a part of base- Java. If you had any Java experience before this (which you should. Do not learn Java through Bukkit) you should have dealt with hashmaps before. If not, read this.
     
  5. Offline

    Agent000037

    Dude I understand hashmaps, I'm just confused about how to delete or make holograms etc. How can I create a hologram then like, replace it? thats what im trying to figure out.
     
  6. Offline

    Zombie_Striker

  7. Offline

    Agent000037

    OH OK
    see, i was trying to do like hologram.move but i couldn't find anything (im totally blind xD)

    ok, gonna experiment, but YES, I KNOW WHAT HASHMAPS ARE XD

    uhhhh... .teleport() isnt working... says it's undefined....?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  8. Offline

    Zombie_Striker

    @Agent000037
    Odd. What version of Holographic displays are you using? Can you post your new code?
     
  9. Offline

    Agent000037

    Code:
    @EventHandler
        public void onMove (PlayerMoveEvent e) {
           
           
            Player p = e.getPlayer();
            p.sendMessage("bool dangle: " + dangle);
           
            if (dangle == true) {
               
                p.sendMessage("new dangle");
                           
                Location loc = p.getLocation().add(0, -4, 0);
               
                hologram.teleport(loc);
               
               
               
               
            }//p.sendMessage("moving");
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if(cmd.getName().equalsIgnoreCase("dangle")) {
               
                Player p = (Player) sender;
               
                sender.sendMessage("dangling...");
               
                Location loc = p.getLocation().add(0, -4, 0);
               
                ItemStack itemStack = new ItemStack(Material.DIAMOND_SWORD, 1);
                final Hologram hologram = HologramsAPI.createHologram(this, loc);
                hologram.appendTextLine(ChatColor.WHITE + "Diamond Sword");
                ItemLine itemLine = hologram.appendItemLine(itemStack);
               
                dangle = true;
               
                BukkitScheduler scheduler2 = getServer().getScheduler();
               
                scheduler2.scheduleSyncDelayedTask(this, new Runnable() {
                               
                    @Override
                    public void run() {
                        dangle = false;
                        hologram.delete();
                        sender.sendMessage("end dangle");
                    }
                }, 100L);
           
                return true;
            }
           
            return false;
        }
    
     
  10. Offline

    Zombie_Striker

    @Agent000037
    From what you posted, it does not seem like you are storing the hologram instance. Using a hashmap, link a hologram with a player and get the hologram from the hashmap when that player moves.
     
  11. Offline

    Agent000037

    Yeah... I understand the concept of hashmaps, but I haven't like, avidly used them before.
     
  12. Offline

    A5H73Y

    This looks like a Java knowledge problem, you're referring to the variable hologram which is undeclared at that point (unless you're using a global variable (which is also bad)). As mentioned, if you use a map to store a player to a hologram then do a lookup at that point, that would solve your issue. This isn't hard Java code so I hope you can do this without pseudo code.
     
  13. Offline

    Agent000037

    I have a Hashmap<String, Hologram> would that be right?
     
  14. Offline

    Zombie_Striker

  15. Offline

    Agent000037

    Alright, great. Now, would I be able to do this?
    Code:
    dangleHolograms.put(p.getName(), Hologram hologram = HologramsAPI.createHologram(this, loc));
    
     
  16. Offline

    Zombie_Striker

    @Agent000037
    I don't think you can nestle a variable creation inside of a method's parameter. I think you have to move that variable out of that line and just reference it in this line.
     
  17. Offline

    A5H73Y

    If you want to do it in a single line just do
    Code:
    dangleHolograms.put(p.getName(), HologramsAPI.createHologram(this, loc));
     
  18. Offline

    Agent000037

    Thanks :)
     
Thread Status:
Not open for further replies.

Share This Page