Not letting multiple people do the tutorial

Discussion in 'Plugin Development' started by Fhbgsdhkfbl, Jun 12, 2016.

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

    Fhbgsdhkfbl

    So I coded this tutorial plugin for my server, but when multiple people try the tutorial, it'll end it for another person, or skip around and randomly remove the player from the tutorial list, and I can't seem to figure it out why

    code:
    Code:
    package me.Fhbgsdhkfbll.Tutorial;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class VillagerListener implements Listener {
    
        private me.Fhbgsdhkfbll.Tutorial.Tutorial plugin;
        public VillagerListener(me.Fhbgsdhkfbll.Tutorial.Tutorial plugin) {
            this.plugin = plugin;
        }
    
        //HogwartsOrigional
       
        int tutorialTask;
       
       
    
        Location[] locations = {
                new Location(Bukkit.getWorld("world"), -320, 15, -930), //spawn locations
                new Location(Bukkit.getWorld("HogwartsOrigional"),  702, 64, 132), //Diagon Alley
                new Location(Bukkit.getWorld("HogwartsOrigional"), 720, 65, 157), //Quidditch Shop
                new Location(Bukkit.getWorld("HogwartsOrigional"), 704, 68, 352), //Gringotts
                new Location(Bukkit.getWorld("HogwartsOrigional"),736, 65, 285), //Spell Shop
                new Location(Bukkit.getWorld("Duelling"), 1, 6, 4), // Duelling Room
                new Location(Bukkit.getWorld("world"), 240, 73, -9), //DADA Room
                new Location(Bukkit.getWorld("world"), -58, 53, 35), //Great Hall
                new Location(Bukkit.getWorld("world"), -58, 44, 43), //Minigames Teleport Room
                new Location(Bukkit.getWorld("Minigames"), 206, 4, 50), //Minigame Example (Might change every once in awhile)
                new Location(Bukkit.getWorld("world"), -320, 15, -930) //spawn locations
        };
        String [][] messages = {
                {"§l§7-------------------------------------------",
                    " ",
                    "§7Welcome to the §aDPHogwarts Tutorial!",
                    "§7Here is where you will learn how everything on DPHogwarts work!",
                    "§7There's so many different things to do on here!",
                    "§7Such as, §6Roleplaying, Minigames, Exploring.",
                    "§7Classes are a fun way to get to know people on here!",
                    "§7You will be teleported around the server and learn",
                    "§7about everything on here! Let's get Started!",
                    "§aI also ask you not to cast spells during this tutorial please!",
                    " ",
                    "§l§7-------------------------------------------",
                },
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7This here is §aDiagon Alley§7! Here is where you can purchase certain neccessities you will need for your",
                    "§7time at Hogwarts! Famous stores such as §6Ollivanders (Wand shop), Eeylops Owl Emporium (Pet Shop) and",
                    "§7Flourish and Blotts (Book shop). §7There is also §6Gringotts Bank §7where you're able to exchange currency",
                    "§7and the §6Daily Prophet §7building where newspaper articles are published for you to read! If you're a",
                    "§7fiendish student and want to play some pranks on friends then stop by §6Weasley Wizard Wheezes §7and get",
                    "§7your firework on! Be careful you don't stray from the road or you might stumble down §6Knockturn Alley",
                    "§7and get into some trouble... Other than that this is the place to be if you're in the mood for a",
                    "§7shopping spree!",
                    " ",
                    "§7-------------------------------------------",
                " "},
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7The §aQuidditch Shop §7is situated in Diagon Alley and is sure to satisfy all of your §6Quidditch needs!",
                    "§7With every broom you could ever wish for, the vendors here sell them all from the family friendly",
                    "§7Bluebottle to every §6Quidditch player’s dream broom the Nimbus 2000!",
                    " ",
                    "§7-------------------------------------------",
                " "},
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7Gringotts bank is the hub for currency - sickles, galleons, knuts - you name it! Just talk with",
                    "§7the goblin at the front desk and your shopping range will expand in the wizarding world!",
                    " ",
                    "§7-------------------------------------------",
                " "},
    
                {" ",
                    "§7-------------------------------------------",
                    "§7I know what you're thinking. When do I get to cast spells and become the greatest wizard/witch of",
                    "§7all time? Well you don't get all the spells straight away, you have to get enough Spell Points",
                    "§7(SP) to buy them!",
                    "§7From §6lumos to Meteolojinx Recanto - You can cast all of your favourite spells with a bit of hard work!",
                    " ",
                    "§7-------------------------------------------"
                },
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7Feeling especially feisty? Then visit the duelling room and start to build up your reputation of",
                    "§7being the best duellist around! Cast spells, make a strategy or if you just want to be entertained",
                    "§7then you can spectate from the stands and watch wizards and witches become aurors in the making!",
                    "§7With different rooms and an arena for the duelling class you can have a private match with your",
                    "§7friend or go full out and stun them all!",
                    " ",
                    "§7-------------------------------------------"
                },
    
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7What's Hogwarts without its §6magical classes§7? This is the §6Defense against the Dark Arts Classroom.",
                    "§7Imagine this. You stroll into the classroom with students sat in all chairs and an expert professor",
                    "§7at the board. You sit down and learn all about §6boggarts, dementors and werewolves!",
                    "§7With practical and lecture classes you can enjoy and sometimes homework afterwards! And after",
                    "§7every lesson you get a §6Spell Point to spend on buying new exciting charms, jinxes and spells!",
                    " ",
                "§7-------------------------------------------"},
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7If you've read the books or films or even both then you know how important the Great Hall is to",
                    "§7the students of Hogwarts. The four grand tables of the houses covered with scrumptious food and",
                    "§7the colossus hourglasses at the very back to show how many points each house have earned by doing",
                    "§7well in classes and handing in good homework! The floating candles and crackling fires make it",
                    "§7feel as homey as Hogwarts can be.",
                    " ",
                "§7-------------------------------------------"},
                {"",
                    "§7-------------------------------------------",
                    "§7As for money, galleons are difficult to get. But are they? Actually here instead of having to do",
                    "§7hard work to get wealthy you just need to have fun! That's right, you play minigames to gain more",
                    "§7galleons to be able to buy more items! This is the minigame portkey room with loads of portkeys",
                    "§7leading to tons of great minigames like replica challenges from the videogames, parkour, droppers",
                    "§7and the famous Triwizard Maze!",
                    " ",
                    "§7-------------------------------------------",
                },
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7Speaking of the Triwizard Maze, here it is in all its glory with its sprawling hedge walls that",
                    "§7make you feel like a teeny ant! If you want to give this a try then go ahead when you've",
                    "§7finished this tutorial and DPHogwarts wishes you the best of luck... ",
                    "§7If you have any questions, please come back to this tutorial or message an online",
                    "§7staff member!",
                    " ",
                    "§7-------------------------------------------",
                },
                {"§7-------------------------------------------",
                    "",
                    "§7You have completed the tutorial of DPHogwarts!",
                    "§7I hope you learned a lot from this experience and can get by without questions",
                    "§7, but if you have any questions, ask any online staff member for your question",
                    "§7to be answer immediately!",
                    "§7Or you can just revisit this tutorial at any time!"
                    ,"§6Have a good time on DPHogwarts!",
                    "",
                    "§7-------------------------------------------"}
        };
        @EventHandler
        public void onTutorial(final PlayerInteractEntityEvent e) {
            final Player p = e.getPlayer();
            PlayerInventory inv = p.getInventory();
            Entity tutorial = e.getRightClicked();
            if(((LivingEntity)tutorial).getCustomName().equalsIgnoreCase(ChatColor.GREEN + "Tutorial")) {
                if(inv.contains(new ItemStack(Material.STICK))
                        || inv.contains(new ItemStack(Material.WOOD_HOE))
                        || inv.contains(new ItemStack(Material.STONE_HOE))
                        || inv.contains(new ItemStack(Material.IRON_HOE))
                        || inv.contains(new ItemStack(Material.GOLD_HOE))
                        || inv.contains(new ItemStack(Material.IRON_SPADE))
                        || inv.contains(new ItemStack(Material.GOLD_SPADE))
                        || inv.contains(new ItemStack(Material.IRON_PICKAXE))
                        || inv.contains(new ItemStack(Material.IRON_AXE))
                        || inv.contains(new ItemStack(Material.WOOD_SPADE))
                        || inv.contains(new ItemStack(Material.WOOD_PICKAXE))
                        || inv.contains(new ItemStack(Material.STONE_SPADE))
                        || inv.contains(new ItemStack(Material.STONE_PICKAXE))
                        || inv.contains(new ItemStack(Material.STONE_AXE))
                        || inv.contains(new ItemStack(Material.DIAMOND_AXE))
                        || inv.contains(new ItemStack(Material.GOLD_SPADE))
                        || inv.contains(new ItemStack(Material.GOLD_AXE))) {
                    p.sendMessage("§4You must not have a broom or a wand in your inventory to do this tutorial!");
                    p.sendMessage("§4Please put it in your enderchest to continue this tutorial!");
                    return;
                }
                plugin.tutorial.add(p.getName());
                p.performCommand("pet hide");
                p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 10000, 1));
                /*
                p.sendMessage("§l§7-------------------------------------------");
                p.sendMessage(" ");
                p.sendMessage("§7Welcome to the §aDPHogwarts Tutorial!");
                p.sendMessage("§7Here is where you will learn how everything on DPHogwarts work!");
                p.sendMessage("§7There's so many different things to do on here!");
                p.sendMessage("§7Such as, §6Roleplaying, Minigames, Exploring.");
                p.sendMessage("§7Classes are a fun way to get to know people on here!");
                p.sendMessage("§7You will be teleported around the server and learn");
                p.sendMessage("§7about everything on here! Let's get Started!");
                p.sendMessage(" ");
                p.sendMessage("§l§7-------------------------------------------");
                 */
                //Location newLocation = locations[0];
                tutorialTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                    int index = 0;
                    public void run() {
                        for(Player p : Bukkit.getOnlinePlayers()) {
                            if(plugin.tutorial.contains(p.getName())) {
                                if(index == locations.length) {
                                    Bukkit.getScheduler().cancelTask(tutorialTask);
                                    plugin.tutorial.remove(p.getName());
                                    p.removePotionEffect(PotionEffectType.INVISIBILITY);
                                }else{
                                    p.teleport(locations[index]);
                                    for(int i = 0; i < 200; i++) {
                                        p.sendMessage(" ");
                                    }
                                    for(int i = 0; i < messages[index].length;i++) {
                                        p.sendMessage(messages[index][i]);
                                    }
                                    index++;
                                }
                            }
                        }
                    }
                }, 0, 10*20);
            }
        }
    }
    
     
  2. Offline

    Zombie_Striker

    Why not just import this tutorial class?

    Main problem: You are only saving one ID at a time. Multiple people can go through the tutorial, so you need multiple task ids to keep track of each one. For that, I would recommend using a Map. The keys will be the Players, and the value with be an Integer, which will represent the Id of their task.
     
  3. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    So instead of saving the tutorial in the arraylist, make a hashmap to save the player, and the taskID??
     
  4. Offline

    Zombie_Striker

    @Fhbgsdhkfbl
    I think you're confusing an Array with Arraylist. An Arraylist is a collection of object, which have no order. Arrays are simple sections that hold a specific amount of object in order.

    For your actual question: no. What I am saying is to replace the field "tutorialTask" with a Hashmap. What you're doing is setting tutorialTask equal to the newest tutorial, instead of saving all the running tasks instead. You should use something like the following:
    Code:
    HashMap<Player,Integer> tasks = new HashMap<Player,Integer>();
    //By giving the hashmap a player, it will give back an ID.
    When you want to add a new tutorial, use the following:
    Code:
    tasks.put( The player, The task ID);
    //What this does is links the player with their tutorial id
    Since I would not want you to put the whole runnable inside that backet (since that is how you get the task's id), I would recommend you create a variable to store the ID first, and then add that variable to the hashmap. This will make your code a lot cleaner.

    Finally, when you want to get the task's id, us the following:
    Code:
    int taskID = tasks.get(the player);
    //This will get the task for that specific player


    [Edit] looking at your code again, it is not optimised correctly. Instead of looping through all the players, and teleporting those inside an Arraylist, Store the player who interacted with an entity inside the runnable instead. Here is a mock-up of what you should have
    Code:
    tutorialTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new [URL='http://www.google.com/search?hl=en&q=allinurl%3Arunnable+java.sun.com&btnI=I%27m%20Feeling%20Lucky']Runnable[/URL]() {
                    int index = 0;
    
                    Player p = e.getPlayer();
                    public void run() {
                      //All I did was add the p variable above, and removed the for loop. This will still work without editing anything else.
                            if(plugin.tutorial.contains(p.getName())) {
                               ....
                            }
                    }
                }, 0, 10*20);
     
  5. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    So, right when the player right clicks the entity, tasks.put(p, tutorialTask);, put them in this task,

    Updated code:
    Code:
    package me.Fhbgsdhkfbll.Tutorial;
    
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class VillagerListener implements Listener {
    
        private me.Fhbgsdhkfbll.Tutorial.Tutorial plugin;
        public VillagerListener(me.Fhbgsdhkfbll.Tutorial.Tutorial plugin) {
            this.plugin = plugin;
        }
    
        //HogwartsOrigional
    
        HashMap<Player,Integer> tasks = new HashMap<Player,Integer>();
        int tutorialTask;
    
    
    
        Location[] locations = {
                new Location(Bukkit.getWorld("world"), -320, 15, -930), //spawn locations
                new Location(Bukkit.getWorld("HogwartsOrigional"),  702, 64, 132), //Diagon Alley
                new Location(Bukkit.getWorld("HogwartsOrigional"), 720, 65, 157), //Quidditch Shop
                new Location(Bukkit.getWorld("HogwartsOrigional"), 704, 68, 352), //Gringotts
                new Location(Bukkit.getWorld("HogwartsOrigional"),736, 65, 285), //Spell Shop
                new Location(Bukkit.getWorld("Duelling"), 1, 6, 4), // Duelling Room
                new Location(Bukkit.getWorld("world"), 240, 73, -9), //DADA Room
                new Location(Bukkit.getWorld("world"), -58, 53, 35), //Great Hall
                new Location(Bukkit.getWorld("world"), -58, 44, 43), //Minigames Teleport Room
                new Location(Bukkit.getWorld("Minigames"), 206, 4, 50), //Minigame Example (Might change every once in awhile)
                new Location(Bukkit.getWorld("world"), -320, 15, -930) //spawn locations
        };
        String [][] messages = {
                {"§l§7-------------------------------------------",
                    " ",
                    "§7Welcome to the §aDPHogwarts Tutorial!",
                    "§7Here is where you will learn how everything on DPHogwarts work!",
                    "§7There's so many different things to do on here!",
                    "§7Such as, §6Roleplaying, Minigames, Exploring.",
                    "§7Classes are a fun way to get to know people on here!",
                    "§7You will be teleported around the server and learn",
                    "§7about everything on here! Let's get Started!",
                    "§aI also ask you not to cast spells during this tutorial please!",
                    " ",
                    "§l§7-------------------------------------------",
                },
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7This here is §aDiagon Alley§7! Here is where you can purchase certain neccessities you will need for your",
                    "§7time at Hogwarts! Famous stores such as §6Ollivanders (Wand shop), Eeylops Owl Emporium (Pet Shop) and",
                    "§7Flourish and Blotts (Book shop). §7There is also §6Gringotts Bank §7where you're able to exchange currency",
                    "§7and the §6Daily Prophet §7building where newspaper articles are published for you to read! If you're a",
                    "§7fiendish student and want to play some pranks on friends then stop by §6Weasley Wizard Wheezes §7and get",
                    "§7your firework on! Be careful you don't stray from the road or you might stumble down §6Knockturn Alley",
                    "§7and get into some trouble... Other than that this is the place to be if you're in the mood for a",
                    "§7shopping spree!",
                    " ",
                    "§7-------------------------------------------",
                " "},
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7The §aQuidditch Shop §7is situated in Diagon Alley and is sure to satisfy all of your §6Quidditch needs!",
                    "§7With every broom you could ever wish for, the vendors here sell them all from the family friendly",
                    "§7Bluebottle to every §6Quidditch player’s dream broom the Nimbus 2000!",
                    " ",
                    "§7-------------------------------------------",
                " "},
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7Gringotts bank is the hub for currency - sickles, galleons, knuts - you name it! Just talk with",
                    "§7the goblin at the front desk and your shopping range will expand in the wizarding world!",
                    " ",
                    "§7-------------------------------------------",
                " "},
    
                {" ",
                    "§7-------------------------------------------",
                    "§7I know what you're thinking. When do I get to cast spells and become the greatest wizard/witch of",
                    "§7all time? Well you don't get all the spells straight away, you have to get enough Spell Points",
                    "§7(SP) to buy them!",
                    "§7From §6lumos to Meteolojinx Recanto - You can cast all of your favourite spells with a bit of hard work!",
                    " ",
                    "§7-------------------------------------------"
                },
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7Feeling especially feisty? Then visit the duelling room and start to build up your reputation of",
                    "§7being the best duellist around! Cast spells, make a strategy or if you just want to be entertained",
                    "§7then you can spectate from the stands and watch wizards and witches become aurors in the making!",
                    "§7With different rooms and an arena for the duelling class you can have a private match with your",
                    "§7friend or go full out and stun them all!",
                    " ",
                    "§7-------------------------------------------"
                },
    
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7What's Hogwarts without its §6magical classes§7? This is the §6Defense against the Dark Arts Classroom.",
                    "§7Imagine this. You stroll into the classroom with students sat in all chairs and an expert professor",
                    "§7at the board. You sit down and learn all about §6boggarts, dementors and werewolves!",
                    "§7With practical and lecture classes you can enjoy and sometimes homework afterwards! And after",
                    "§7every lesson you get a §6Spell Point to spend on buying new exciting charms, jinxes and spells!",
                    " ",
                "§7-------------------------------------------"},
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7If you've read the books or films or even both then you know how important the Great Hall is to",
                    "§7the students of Hogwarts. The four grand tables of the houses covered with scrumptious food and",
                    "§7the colossus hourglasses at the very back to show how many points each house have earned by doing",
                    "§7well in classes and handing in good homework! The floating candles and crackling fires make it",
                    "§7feel as homey as Hogwarts can be.",
                    " ",
                "§7-------------------------------------------"},
                {"",
                    "§7-------------------------------------------",
                    "§7As for money, galleons are difficult to get. But are they? Actually here instead of having to do",
                    "§7hard work to get wealthy you just need to have fun! That's right, you play minigames to gain more",
                    "§7galleons to be able to buy more items! This is the minigame portkey room with loads of portkeys",
                    "§7leading to tons of great minigames like replica challenges from the videogames, parkour, droppers",
                    "§7and the famous Triwizard Maze!",
                    " ",
                    "§7-------------------------------------------",
                },
                {" ",
                    "§7-------------------------------------------",
                    " ",
                    "§7Speaking of the Triwizard Maze, here it is in all its glory with its sprawling hedge walls that",
                    "§7make you feel like a teeny ant! If you want to give this a try then go ahead when you've",
                    "§7finished this tutorial and DPHogwarts wishes you the best of luck... ",
                    "§7If you have any questions, please come back to this tutorial or message an online",
                    "§7staff member!",
                    " ",
                    "§7-------------------------------------------",
                },
                {"§7-------------------------------------------",
                    "",
                    "§7You have completed the tutorial of DPHogwarts!",
                    "§7I hope you learned a lot from this experience and can get by without questions",
                    "§7, but if you have any questions, ask any online staff member for your question",
                    "§7to be answer immediately!",
                    "§7Or you can just revisit this tutorial at any time!"
                    ,"§6Have a good time on DPHogwarts!",
                    "",
                "§7-------------------------------------------"}
        };
        @EventHandler
        public void onTutorial(final PlayerInteractEntityEvent e) {
            final Player p = e.getPlayer();
            PlayerInventory inv = p.getInventory();
            Entity tutorial = e.getRightClicked();
            if(((LivingEntity)tutorial).getCustomName().equalsIgnoreCase(ChatColor.GREEN + "Tutorial")) {
                if(inv.contains(new ItemStack(Material.STICK))
                        || inv.contains(new ItemStack(Material.WOOD_HOE))
                        || inv.contains(new ItemStack(Material.STONE_HOE))
                        || inv.contains(new ItemStack(Material.IRON_HOE))
                        || inv.contains(new ItemStack(Material.GOLD_HOE))
                        || inv.contains(new ItemStack(Material.IRON_SPADE))
                        || inv.contains(new ItemStack(Material.GOLD_SPADE))
                        || inv.contains(new ItemStack(Material.IRON_PICKAXE))
                        || inv.contains(new ItemStack(Material.IRON_AXE))
                        || inv.contains(new ItemStack(Material.WOOD_SPADE))
                        || inv.contains(new ItemStack(Material.WOOD_PICKAXE))
                        || inv.contains(new ItemStack(Material.STONE_SPADE))
                        || inv.contains(new ItemStack(Material.STONE_PICKAXE))
                        || inv.contains(new ItemStack(Material.STONE_AXE))
                        || inv.contains(new ItemStack(Material.DIAMOND_AXE))
                        || inv.contains(new ItemStack(Material.GOLD_SPADE))
                        || inv.contains(new ItemStack(Material.GOLD_AXE))) {
                    p.sendMessage("§4You must not have a broom or a wand in your inventory to do this tutorial!");
                    p.sendMessage("§4Please put it in your enderchest to continue this tutorial!");
                    return;
                }
                plugin.tutorial.add(p.getName());
                p.performCommand("pet hide");
                p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 10000, 1));
                tasks.put(p, tutorialTask);
                /*
                p.sendMessage("§l§7-------------------------------------------");
                p.sendMessage(" ");
                p.sendMessage("§7Welcome to the §aDPHogwarts Tutorial!");
                p.sendMessage("§7Here is where you will learn how everything on DPHogwarts work!");
                p.sendMessage("§7There's so many different things to do on here!");
                p.sendMessage("§7Such as, §6Roleplaying, Minigames, Exploring.");
                p.sendMessage("§7Classes are a fun way to get to know people on here!");
                p.sendMessage("§7You will be teleported around the server and learn");
                p.sendMessage("§7about everything on here! Let's get Started!");
                p.sendMessage(" ");
                p.sendMessage("§l§7-------------------------------------------");
                 */
                //Location newLocation = locations[0];
                tutorialTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                    int index = 0;
                    Player p = e.getPlayer();
                    public void run() {
                        if(plugin.tutorial.contains(p.getName())) {
                            if(index == locations.length) {
                                Bukkit.getScheduler().cancelTask(tutorialTask);
                                plugin.tutorial.remove(p.getName());
                                p.removePotionEffect(PotionEffectType.INVISIBILITY);
                            }else{
                                p.teleport(locations[index]);
                                for(int i = 0; i < 200; i++) {
                                    p.sendMessage(" ");
                                }
                                for(int i = 0; i < messages[index].length;i++) {
                                    p.sendMessage(messages[index][i]);
                                }
                                index++;
                            }
                        }
                    }
                }, 0, 10*20);
            }
        }
    }
    
     
  6. Offline

    Zombie_Striker

    @Fhbgsdhkfbl
    Close, but your order is slightly off. You need to add the player and the task to the map after you create the repeating task. What you are currently doing is storing the taskID of the previous tutorial with the current player. Also, you are still canceling the taskID "tutorialTask". What you should be canceling is the tutorial linked with the player.

    Basically, change this:
    to this:
    Code:
    Bukkit.getScheduler().cancelTask(tasks.get(player));
    and then move the task.put method to be after you create the new task.
     
  7. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    How come I don't add them to the task right when they right click the entity to start the tutorial?
     
  8. Offline

    ipodtouch0218

    The Method is
    Code:
    tutorial.put(p.getName(), int);
    You forgot to add a key


    EDIT: Nevermind. Add is a method too :/
     
  9. Offline

    Zombie_Striker

    @Fhbgsdhkfbl
    Because the field 'turoialTask' has not been set to anything yet. You need to wait the few nanoseconds after 'tutorialTask' has been said to the task has been created (I.e move the line down to below the task) to then add the task to the hashmap.
     
  10. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    So put it after checking if they're in the arraylist, but before checking if the index is past?

    Code:
    public void run() {
                        if(plugin.tutorial.contains(p.getName())) {
                            tasks.put(p, tutorialTask);
                            if(index == locations.length) {
                                Bukkit.getScheduler().cancelTask(tasks.get(p));
                                plugin.tutorial.remove(p.getName());
                                p.removePotionEffect(PotionEffectType.INVISIBILITY);
                            }else{
                                p.teleport(locations[index]);
                                for(int i = 0; i < 200; i++) {
                                    p.sendMessage(" ");
                                }
                                for(int i = 0; i < messages[index].length;i++) {
                                    p.sendMessage(messages[index][i]);
                                }
                                index++;
                            }
                        }
                    }
                }, 0, 10*20);
     
  11. Offline

    Zombie_Striker

    @Fhbgsdhkfbl
    You should put that line below this:
     
  12. Offline

    Fhbgsdhkfbl

  13. Offline

    Zombie_Striker

    @Fhbgsdhkfbl
    Just move that line so that it is like this:
     
  14. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    yeah I did that, but I'm just curious what difference it makes from where i had it before? I'm not questioning it, is it adding them to the task after 15 seconds instead of instantly?
     
  15. Offline

    Zombie_Striker

    @Fhbgsdhkfbl
    The difference is that by putting that line inside the run method, that value will be updated every time it runs and the value it will be updated to will be the the most recent taskID, the the actual taskID.
     
    Fhbgsdhkfbl likes this.
  16. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    Ohhhhh that makes more sense, thanks!

    @Zombie_Striker

    Now I'm having another issue

    Code:
    @EventHandler
        public void onTutorial(final PlayerInteractEntityEvent e) {
            final Player p = e.getPlayer();
            PlayerInventory inv = p.getInventory();
            Entity tutorial = e.getRightClicked();
            if(tutorial == null) {
                return;
            }
            if(((LivingEntity)tutorial).getCustomName().equalsIgnoreCase(ChatColor.GREEN + "Tutorial")) {
                if(inv.contains(new ItemStack(Material.STICK))
                        || inv.contains(new ItemStack(Material.WOOD_HOE))
                        || inv.contains(new ItemStack(Material.STONE_HOE))
                        || inv.contains(new ItemStack(Material.IRON_HOE))
                        || inv.contains(new ItemStack(Material.GOLD_HOE))
                        || inv.contains(new ItemStack(Material.IRON_SPADE))
                        || inv.contains(new ItemStack(Material.GOLD_SPADE))
                        || inv.contains(new ItemStack(Material.IRON_PICKAXE))
                        || inv.contains(new ItemStack(Material.IRON_AXE))
                        || inv.contains(new ItemStack(Material.WOOD_SPADE))
                        || inv.contains(new ItemStack(Material.WOOD_PICKAXE))
                        || inv.contains(new ItemStack(Material.STONE_SPADE))
                        || inv.contains(new ItemStack(Material.STONE_PICKAXE))
                        || inv.contains(new ItemStack(Material.STONE_AXE))
                        || inv.contains(new ItemStack(Material.DIAMOND_AXE))
                        || inv.contains(new ItemStack(Material.GOLD_SPADE))
                        || inv.contains(new ItemStack(Material.GOLD_AXE))) {
                    p.sendMessage("§4You must not have a broom or a wand in your inventory to do this tutorial!");
                    p.sendMessage("§4Please put it in your enderchest to continue this tutorial!");
                    return;
                }
                plugin.tutorial.add(p.getName());
                p.performCommand("pet hide");
                p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 10000, 1));
                //tasks.put(p, tutorialTask);
                /*
                p.sendMessage("§l§7-------------------------------------------");
                p.sendMessage(" ");
                p.sendMessage("§7Welcome to the §aDPHogwarts Tutorial!");
                p.sendMessage("§7Here is where you will learn how everything on DPHogwarts work!");
                p.sendMessage("§7There's so many different things to do on here!");
                p.sendMessage("§7Such as, §6Roleplaying, Minigames, Exploring.");
                p.sendMessage("§7Classes are a fun way to get to know people on here!");
                p.sendMessage("§7You will be teleported around the server and learn");
                p.sendMessage("§7about everything on here! Let's get Started!");
                p.sendMessage(" ");
                p.sendMessage("§l§7-------------------------------------------");
    It's supposed to check their inventory for those items, if they have it, not do anything and send the message, it skips over it entirely, and they can do the tutorial, and whenever another player does the tutorial, anyone that is in the tutorial at the time, it'll skip through some parts of the tutorial.

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

    Zombie_Striker

    This is not how you should do this. Try the following instead:
    1. Create an Arraylist or List if Materials (not itemstacks). This will contain all the materials you're testing for. Since this will always be the same, make this a field instead of a variable inside a method (I.e Put the new arraylist in the same place where the HashMap is).
    2. For loop through all of the inventories contents.
    3. If the item is not null,
    4. If the Arraylist from #1 contains the item's type
    5. Send the message and return.
     
  18. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    Off the top of my head using your steps

    Code:
    List<Material> tutorialItems = Arrays.asList(Material.WOOD_HOE,
                Material.STONE_HOE ,
                Material.IRON_HOE,
                Material.GOLD_HOE,
                Material.IRON_SPADE,
                Material.GOLD_SPADE,
                Material.IRON_PICKAXE,
                Material.IRON_AXE,
                Material.WOOD_SPADE,
                Material.WOOD_PICKAXE,
                Material.STONE_SPADE,
                Material.STONE_PICKAXE,
                Material.STONE_AXE,
                Material.DIAMOND_AXE,
                Material.GOLD_SPADE,
                Material.GOLD_AXE);
    
    for(ItemStack i : p.getInventory().getContents()) {
                    if(!(i == null)) {
                        if(p.getInventory().getContents().equals(tutorialItems)) {
                            p.sendMessage("§4You must not have a broom or a wand in your inventory to do this tutorial!");
                            p.sendMessage("§4Please put it in your enderchest to continue this tutorial!");
                            return;
                        }
    
                    }
                }
            }
     
  19. Offline

    Zombie_Striker

    Use this instead:
    Code:
    if(tutorialItems.contains(i.getType()))
    Besides that, have you tested it? Does it work?
     
  20. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    No I haven't tested, I have another issue, when another player starts the tutorial, it'll start skipping around sometimes and not do it correctly for a different player.
     
  21. Offline

    Zombie_Striker

    @Fhbgsdhkfbl
    Have you tried debugging? Is it a problem with the index? Are you sure the locations and message arrays are in the right order?
     
  22. Offline

    Fhbgsdhkfbl

    @Zombie_Striker
    I'll test it when I get a chance, I'm heading out for a little bit, I'll reply on this forum with what I come up with.
    @Zombie_Striker
    Edit:
    I tested it just now, the item arraylist I set worked good, but I had a friend to the tutorial as well, and after a second try going through, it just removed them from the task and the arraylist, like they never started the tutorial, it happened after I started the tutorial when they started it

    Code:
    HashMap<Player,Integer> tasks = new HashMap<Player,Integer>();
        int tutorialTask;
    
    tutorialTask = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                int index = 0;
                Player p = e.getPlayer();
                public void run() {
                    if(plugin.tutorial.contains(p.getName())) {
                        if(index == locations.length) {
                            Bukkit.getScheduler().cancelTask(tasks.get(p));
                            plugin.tutorial.remove(p.getName());
                            p.removePotionEffect(PotionEffectType.INVISIBILITY);
                        }else{
                            p.teleport(locations[index]);
                            for(int i = 0; i < 200; i++) {
                                p.sendMessage(" ");
                            }
                            for(int i = 0; i < messages[index].length;i++) {
                                p.sendMessage(messages[index][i]);
                            }
                            index++;
                        }
                    }
                }
            }, 0, 10*20);
            tasks.put(p, tutorialTask);
        }
    }
     
    Last edited: Jun 12, 2016
  23. Offline

    Zombie_Striker

    @Fhbgsdhkfbl
    Have you tried debugging? What is 'index' equal to when both of you are doing the tutorial? What are your task ids? Are the Ids the same before and after another player does the tutorial? Are both entries being stored in the hashmap correctly?
     
Thread Status:
Not open for further replies.

Share This Page