Combat Tag Boss Bar

Discussion in 'Plugin Development' started by opd02, Apr 18, 2020.

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

    opd02

    Hello! I have been working on this plugin and I sort of realized that I am grinding to a mental halt and cant figure out how to keep going! If you could, just send suggestions (even if they don't work!) to just keep me going! If you know how to fix this problem, I would LOVE to hear it!
    What I'm going for: I am working on a custom combat tag plugin that crates a bossbar for the tagged players, allowing them to see how much time left of combat tagged they have.
    Where things are hard... At first it sounds easy, set up listeners and add hit players to an array list, then give them a boss bar with a delayed repeating task that removes time from the bar, and when it hits zero, POOF, off the array list. HOWEVER its not that easy. When players are hit multiple times while still counting down from the last attempt, either multiple bars are created or they players leave combat tag too eary!
    What I have so far: Basically when two players hit each other, a random number is generated and stored next to their name in a config. Then after 15 seconds, it checks if the number is still the same as it was 15 seconds ago. If it is, it leaves combat tag. If not, then stop the code and let the NEW runnable finish the job.
    Please help me out! Sort of lost and not sure where to go!
    Code:
    package me.opd02.combattag;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class main extends JavaPlugin implements Listener {
        ArrayList<Player> tagged = new ArrayList<Player>();
        public Random rand = new Random();
        public void onEnable(){
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
        public void tagPlayers(Player p, Player d){
            if(!(tagged.contains(p))){
                tagged.add(p);
                p.sendMessage("§8[§2§lOmega§r§8] §6You have been combat tagged with §r§a§l" + d.getName() + "§r§6!");
                p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1, 4);
            }
            if(!(tagged.contains(d))){
                tagged.add(d);
                d.sendMessage("§8[§2§lOmega§r§8] §6You have been combat tagged with §r§a§l" + p.getName() + "§r§6!");
                d.playSound(d.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1, 4);
            }
            int stamp = rand.nextInt(60000);
            //BossBar pbar = Bukkit.createBossBar("§a§lCombat Tag Coutdown", BarColor.BLUE, BarStyle.SOLID, new BarFlag[0]);
            //pbar.setProgress(1);
            //pbar.addPlayer(p);
            getConfig().set("TagInts." + p.getName(), stamp);
            getConfig().set("TagInts." + d.getName(), stamp);
            saveConfig();
            this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                  public void run() {
                      if(getConfig().getInt("TagInts." + p.getName())==stamp){
                          tagged.remove(p);
                          p.sendMessage("§8[§2§lOmega§r§8] §cYou are no longer combat tagged!");
                          p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1, (float)0.1);
                        //  pbar.removeAll();
                      }
                      if(getConfig().getInt("TagInts." + d.getName())==stamp){
                          tagged.remove(d);
                          d.sendMessage("§8[§2§lOmega§r§8] §cYou are no longer combat tagged!");
                          d.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1, (float)0.1);
                      }
                  }
                }, 300L);
        }
        @EventHandler
        public void onPlayerCommand(PlayerCommandPreprocessEvent e){
            if(e.getMessage().contains("/spawn") || e.getMessage().contains("/tpa") || e.getMessage().contains("/f home") || e.getMessage().contains("/warp")){
                if(tagged.contains(e.getPlayer())){
                    e.setCancelled(true);
                    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                          public void run() {
                                e.getPlayer().closeInventory();
                          }
                        }, 2L);
                    e.getPlayer().sendMessage("§8[§2§lOmega§r§8] §cYou can not use that command while combat tagged!");
                    return;
                }
                return;
            }
        }
        @EventHandler
        public void onPlayerDamage(EntityDamageByEntityEvent e){
            if(e.getDamager() instanceof Arrow){
                Arrow arrow = (Arrow) e.getDamager();
                if(arrow.getShooter() instanceof Player){
                    Player p = (Player) e.getEntity();
                    Player d = (Player) arrow.getShooter();
                    tagPlayers(p, d);
                }
            }
            if(e.getEntityType()==EntityType.PLAYER){
                if(e.getCause()==DamageCause.ENTITY_ATTACK){
                    Player p = (Player) e.getEntity();
                    Player d = (Player) e.getDamager();
                    tagPlayers(p, d);
                }
            }
        }
        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e){
            Player p = e.getPlayer();
            if(tagged.contains(p)){
                p.setHealth(0);
                getConfig().set("BadPlayer." + p.getName(), "bad");
                tagged.remove(p);
                saveConfig();
            }
        }
        @EventHandler
        public void onPlayerLongin(PlayerRespawnEvent e){
            String s = getConfig().getString("BadPlayer." + e.getPlayer().getName());
            if(s=="bad"){
                e.getPlayer().sendMessage("§8[§2§lOmega§r§8] §cYou have been killed because you logged off while in combat!");
            }
            getConfig().set("BadPlayer." + e.getPlayer().getName(), 0);
            saveConfig();
        }
    }
     
  2. Offline

    Niv-Mizzet

    Just create a "global"(You can do this by writing a static function within the class with it to access it) hashmap for the players online, using Player for the id(i forgot the official term for it) and the Bossbar for the value. Then you can edit the Bossbar from the Player, and set it visible or invisible, and change the value during combat. You don't need to create a new Bossbar every time.
    Also, you spelled Login wrong for the PlayerRespawnEvent :)
     
  3. Offline

    opd02

    Thanks for the typo :D But if we aren't creating multiple bossbars, then wouldn't changing the value of the boss bar change it for every other player? With this, wouldn't we only be allowed to have 2 players in combat?
     
  4. Offline

    alant7_

    I think he meant to create boss bars only when player logs in and make it invisible. When player is combat tagged then only show it instead of making another one.
     
  5. Offline

    Niv-Mizzet

    @opd02 I meant what alant7_ said. Create bossbars when player logs in, make it invisible. Add player to hashmap for bossbars. When player is "tagged," make it visible and set the value to full. When player leaves remove from hashmap and bossbar.
     
  6. Offline

    opd02

    @alant7_
    Hi, sorry to keep bugging but I still down really get it. I understand your logic and using the hashmap would be a good idea. However, I cant seem to see how you will be able to create a bossbar specifically for a player when they log in and be able to edit that boss bar from a different part of the code. I mean I could put the Bossbar IN the hashmap and call it forth that way? Maybe I'm being dumb, but can you should me a little bit of what you mean? :D Thanks again! Very helpful
     
  7. Offline

    alant7_

    @opd02
    Hey, no worries.

    So, there is hashmap called tagged. (you used array list in your code, if you are not familiar with hash maps, please read a little bit about them, they are not complicated).

    In PlayerJoinEvent, you create bossbar using Bukkit.createBossBar(...), then simply put that boss bar in hashmap using tagget.put(player, bossbar).

    To access that bossbar later in code, you can just do tagged.get(player).

    This should help.
     
    Niv-Mizzet and opd02 like this.
  8. Offline

    Niv-Mizzet

    @opd02 alant7_ provided a great explanation, just wanted to add on, remove the player and bossbar from HashMap on PlayerQuitEvent and PlayerKickEvent so that there isn't more than one bossbar per player.
     
    opd02 likes this.
  9. Offline

    opd02

    Thanks guys! I get cha now! Ill do that and it seems like it should work! Thanks!
     
Thread Status:
Not open for further replies.

Share This Page