Solved Priortites

Discussion in 'Plugin Development' started by BurnerDiamond, Dec 26, 2014.

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

    BurnerDiamond

    If I have a code which for example goes like this:

    Code:
    if (is.getType() == Material.DIAMOND_CHESTPLATE) {
                                    player.closeInventory();
                                    player.getInventory().setArmorContents(new ItemStack[4]);
                                    player.getInventory().clear();
                                    for(PotionEffect effect : player.getActivePotionEffects())
                                    {
                                        player.removePotionEffect(effect.getType());
                                    }
                                    Thread.sleep(100);
                                    player.getInventory().addItem(new ItemStack(Material.IRON_SWORD));
                                    player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
                                    player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
                                    player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
                                    player.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 2147000, 1));
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST,2147000, 9));
                                    player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 4, 100));
    How do I make it so the remove potion is a high priority, therefore it does it first? I just simply added a thread.sleep(100) but it screws up the the potion duration!

    It works but I'm trying to make the plugin cleaner

    I might be wrong, maybe priorities can only be given to events?

    What is the name of these little things here?

    Code:
    player.sendMessage("Hi");
    
    
    I know constructors and others but what name does that code there have?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  2. Offline

    fireblast709

    @BurnerDiamond never, ever invoke Thread.sleep(...) in the main thread. You cause the server to sleep for 100ms, basically skipping two ticks each time the code is ran. Anyway, what do you mean with 'priority'. Your PotionEffects are first removed, and then new ones are added.
     
  3. Offline

    BurnerDiamond

    It's not the main @fireblast709

    It's a listener sorry for the mis-information

    I know this thing called priority=PRIORITY.HIGH

    How would I change my Thread.sleep? I though it would wait a couple seconds and then go again?

    Whole code is this

    Code:
    package me.bd.kitpvp;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class NetherStarOpen implements Listener {
        
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
           
            ItemStack tank = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta tankim = tank.getItemMeta();
            tankim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "TANK");
            ArrayList<String> tanklore = new ArrayList<String>();
            tanklore.add(ChatColor.YELLOW + "Take multiple hits as you bash ");
            tanklore.add(ChatColor.YELLOW + "your opposing players without ");
            tanklore.add(ChatColor.YELLOW + "taking a heart of damage");
            tankim.setLore(tanklore);
            tank.setItemMeta(tankim);
           
            ItemStack striker = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta strikerim = striker.getItemMeta();
            strikerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "STRIKER");
            ArrayList<String> strikerlore = new ArrayList<String>();
            strikerlore.add(ChatColor.YELLOW + "Strike fear with your sword ");
            strikerlore.add(ChatColor.YELLOW + "but be careful since you're ");
            strikerlore.add(ChatColor.YELLOW + "not as well armoAQUA");
            strikerim.setLore(strikerlore);
            striker.setItemMeta(strikerim);
           
            ItemStack archer = new ItemStack(Material.BOW);
            ItemMeta archerim = archer.getItemMeta();
            archerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "ARCHER");
            ArrayList<String> archerlore = new ArrayList<String>();
            archerlore.add(ChatColor.YELLOW + "You may not have the best ");
            archerlore.add(ChatColor.YELLOW + "melee combat but ranged will ");
            archerlore.add(ChatColor.YELLOW + "easily keep you safe and make ");
            archerlore.add(ChatColor.YELLOW + "it easier to kill");
            archerim.setLore(archerlore);
            archer.setItemMeta(archerim);
           
            ItemStack jumper = new ItemStack(Material.PISTON_BASE);
            ItemMeta jumperim = jumper.getItemMeta();
            jumperim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "JUMPER");
            ArrayList<String> jumperlore = new ArrayList<String>();
            jumperlore.add(ChatColor.YELLOW + "They say that a rabbit's foot ");
            jumperlore.add(ChatColor.YELLOW + "brings good luck, you jump like ");
            jumperlore.add(ChatColor.YELLOW + "a rabbit and you're only using ");
            jumperlore.add(ChatColor.YELLOW + "gold, any luckier than that?");
            jumperim.setLore(jumperlore);
            jumper.setItemMeta(jumperim);
           
            ItemStack runner = new ItemStack(Material.SUGAR);
            ItemMeta runnerim = runner.getItemMeta();
            runnerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "RUNNER");
            ArrayList<String> runnerlore = new ArrayList<String>();
            runnerlore.add(ChatColor.YELLOW + "Who needs good armor? Use your speed ");
            runnerlore.add(ChatColor.YELLOW + "to quickly run and attack. It's a ");
            runnerlore.add(ChatColor.YELLOW + "great tactic since you have a  ");
            runnerlore.add(ChatColor.YELLOW + "great sword");
            runnerim.setLore(runnerlore);
            runner.setItemMeta(runnerim);
           
            Inventory chest = Bukkit.createInventory(null, 27, "Kit Selector");                  
            chest.setItem(4, new ItemStack(tank));
            chest.setItem(1, new ItemStack(striker));
            chest.setItem(7, new ItemStack(archer));
            chest.setItem(19, new ItemStack(jumper));
            chest.setItem(22, new ItemStack(runner));
           
            Player player = event.getPlayer();
           
                if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                  if (player.getItemInHand().getItemMeta().getDisplayName() != null) {
                    if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Kit Selector")) {
                        player.openInventory(chest);
               
               
                    
                       
                       
                                    
                }
        }
                }
        }
       
    
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  4. Offline

    fireblast709

    I never said "Don't use Thread.sleep in the main class", I said "Don't use Thread.sleep in the main thread" (which is where 95% of the Bukkit API runs on - aside prelogin, chat and any async tasks you will ever spawn)
    That is for events, and has nothing to do with the order Java executes it's code
    Why do you need to do that in the first place? The code should work fine without the Thread.sleep.
     
  5. Offline

    BurnerDiamond

    Okay. So the order I put it in is the order the works in?
     
  6. Offline

    fireblast709

    @BurnerDiamond to reuse the code you posted earlier, this should work fine (and note, I only removed the Thread.sleep)
    Code:Java
    1.  
    2. if (is.getType() == Material.DIAMOND_CHESTPLATE) {
    3. player.closeInventory();
    4. player.getInventory().setArmorContents(new ItemStack[4]);
    5. player.getInventory().clear();
    6. for(PotionEffect effect : player.getActivePotionEffects())
    7. {
    8. player.removePotionEffect(effect.getType());
    9. }
    10. player.getInventory().addItem(new ItemStack(Material.IRON_SWORD));
    11. player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE));
    12. player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS));
    13. player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS));
    14. player.getInventory().setHelmet(new ItemStack(Material.IRON_HELMET));
    15. player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 2147000, 1));
    16. player.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST,2147000, 9));
    17. player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 4, 100));
     
  7. Offline

    BurnerDiamond

    Yep, so it was just a nuisance to have Thread.sleep(100), removed it and it seems to work. Thanks

    SOLVED
     
Thread Status:
Not open for further replies.

Share This Page