Just can't seem to figure out why this wont work.

Discussion in 'Plugin Development' started by xXRobbie21Xx, Aug 27, 2014.

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

    xXRobbie21Xx

    So, i made this cool parkour plugin that supports check points, so rather than storing the location in a file every time it was requested i just saved it to a hashmap along with the players uuid. However upon going to said checkpoint it doesnt recognise that the person is in the hashmap (or something along those lines)

    I understand that i shouldnt keep relying on these forums but after hours of trouble shooting (and yes im just stupid enough to do that) i have yet to find the solution.

    So in short after setting a checkpoint it doesnt allow me to retrieve it.

    Here is the one and only class:
    Code:
    package parkourMain;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.FireworkEffect;
    import org.bukkit.FireworkEffect.Type;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Firework;
    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.block.SignChangeEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class parkourMain extends JavaPlugin implements Listener {
     
        public Map<UUID, String> parkour = new HashMap<UUID, String>();
        public Map<UUID, Location> checkPoint = new HashMap<UUID, Location>();
     
        String prefix = ChatColor.translateAlternateColorCodes('&', getConfig()
                .getString("ChatPrefix"));
     
     
        public static Inventory parkourInv;
     
        public void onEnable() {
            parkourInv = Bukkit.createInventory(null, 9, "§6§lParkour");
     
            getServer().getPluginManager().registerEvents(this, this);
     
            saveDefaultConfig();
     
        }
     
        public void onDisable() {
     
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
     
            if (sender instanceof Player) {
                Player p = (Player) sender;
     
                if (commandLabel.equalsIgnoreCase("ParkourOpen")) {
     
                    ItemStack setPoint = new ItemStack(Material.REDSTONE);
                    ItemMeta smd = setPoint.getItemMeta();
                    smd.setDisplayName(ChatColor.GREEN + "§nSet Checkpoint!");
                    setPoint.setItemMeta(smd);
     
                    ItemStack telePoint = new ItemStack(Material.SUGAR);
                    ItemMeta tmd = telePoint.getItemMeta();
                    tmd.setDisplayName(ChatColor.GREEN + "§nTeleport back!");
                    telePoint.setItemMeta(tmd);
     
                    ItemStack leave = new ItemStack(Material.GLOWSTONE_DUST);
                    ItemMeta lmd = leave.getItemMeta();
                    lmd.setDisplayName(ChatColor.GREEN + "§nLeave the Parkour!");
                    leave.setItemMeta(lmd);
                    checkPoint.remove(p.getUniqueId());
     
                    ItemStack filler = new ItemStack(Material.THIN_GLASS);
                    ItemMeta fmd = filler.getItemMeta();
                    fmd.setDisplayName(ChatColor.GREEN + "§n-+-+-+-+-+-+-");
                    filler.setItemMeta(fmd);
     
                    parkourInv.clear();
                    parkourInv.setItem(0, setPoint);
                    parkourInv.setItem(1, filler);
                    parkourInv.setItem(2, filler);
                    parkourInv.setItem(3, filler);
                    parkourInv.setItem(4, telePoint);
                    parkourInv.setItem(5, filler);
                    parkourInv.setItem(6, filler);
                    parkourInv.setItem(7, filler);
                    parkourInv.setItem(8, leave);
     
                    p.openInventory(parkourInv);
                }
            }
     
            return false;
     
        }
     
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Action action = e.getAction();
            Block b = e.getClickedBlock();
            UUID uuid = p.getUniqueId();
     
            if (action == Action.RIGHT_CLICK_BLOCK
                    && e.getClickedBlock().getType() == Material.SIGN_POST) {
                Sign s = (Sign) b.getState();
                if (s.getLine(1).equalsIgnoreCase(ChatColor.BLUE + "[Start]")) {
                    if (!s.getLine(2).isEmpty()) {
     
                        if (!parkour.containsKey(uuid)) {
                            parkour.put(p.getUniqueId(), s.getLine(2));
                            p.sendMessage(prefix + ChatColor.BLUE
                                    + "You have entered the " + s.getLine(2)
                                    + ChatColor.BLUE + " parkour!");
     
                        }
     
                        else if (parkour.containsKey(uuid)) {
                            p.sendMessage(prefix + ChatColor.BLUE
                                    + "You are allready entered in the "
                                    + parkour.get(uuid) + ChatColor.BLUE
                                    + " parkour!");
                        }
     
                    }
     
                }
     
                else if (s.getLine(1).equalsIgnoreCase(ChatColor.BLUE + "[Finish]")) {
                    if (s.getLine(2).equalsIgnoreCase(parkour.get(uuid))) {
     
                        if (parkour.containsKey(uuid)) {
     
                            Firework fw = (Firework) p.getWorld().spawn(
                                    p.getLocation(), Firework.class);
                            FireworkMeta fwm = fw.getFireworkMeta();
                            fwm.addEffect(FireworkEffect.builder().flicker(true)
                                    .trail(true).with(Type.BALL)
                                    .with(Type.BALL_LARGE).withColor(Color.AQUA)
                                    .withColor(Color.BLUE).withColor(Color.PURPLE)
                                    .withFade(Color.TEAL).build());
                            fwm.setPower(1);
                            fw.setFireworkMeta(fwm);
     
                            p.playSound(p.getLocation(), Sound.EXPLODE, 1, 1);
     
                            Bukkit.broadcastMessage(prefix + p.getDisplayName()
                                    + ChatColor.GOLD + ChatColor.BOLD
                                    + " has completed the " + s.getLine(2)
                                    + ChatColor.GOLD + ChatColor.BOLD + " parkour!");
                            parkour.remove(uuid);
                        } else {
                            p.sendMessage(prefix
                                    + ChatColor.BLUE
                                    + "You arent participating in any parkour courses!");
                        }
     
                    }
     
                }
     
            }
     
        }
     
        @EventHandler
        public void onSignChange(SignChangeEvent e) {
            Player p = e.getPlayer();
            String name = e.getLine(1);
            if (e.getLine(0).equalsIgnoreCase("[Start]")) {
                if (!e.getLine(1).isEmpty()) {
                    p.sendMessage(prefix + ChatColor.BLUE + "The " + ChatColor.RED
                            + name + ChatColor.BLUE + " start sign was set!");
                    e.setLine(0, ChatColor.BOLD + "+*+*+*+*+*+*+*+");
                    e.setLine(1, ChatColor.BLUE + "[Start]");
                    e.setLine(2, ChatColor.DARK_PURPLE + name);
                    e.setLine(3, ChatColor.BOLD + "+*+*+*+*+*+*+*+");
                    p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1, 1);
     
                }
     
            }
     
            else if (e.getLine(0).equalsIgnoreCase("[Finish]")) {
                if (!e.getLine(1).isEmpty()) {
                    p.sendMessage(prefix + ChatColor.BLUE + "The " + ChatColor.RED
                            + name + ChatColor.BLUE + " start sign was set! ");
                    e.setLine(0, ChatColor.BOLD + "+*+*+*+*+*+*+*+");
                    e.setLine(1, ChatColor.BLUE + "[Finish]");
                    e.setLine(2, ChatColor.DARK_PURPLE + name);
                    e.setLine(3, ChatColor.BOLD + "+*+*+*+*+*+*+*+");
                    p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1, 1);
                }
            }
     
        }
       
       
       
       
     
        @EventHandler
        public void onInvClickEvent(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            UUID uuid = p.getUniqueId();
     
            if (e.getInventory().equals(parkourInv)) {
                if (e.getCurrentItem().getType().equals(Material.REDSTONE)) {
                    if (parkour.containsKey(uuid)) {
                       
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You have set your checkpoint!");
                        checkPoint.put(uuid, p.getLocation());
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                       
                    } else {
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You must enter a parkour course in order to use this feature!");
                    }
     
                }
     
                else if (e.getCurrentItem().getType().equals(Material.SUGAR)) {
                    if (checkPoint.containsKey(uuid)) {
                       
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.teleport(checkPoint.get(uuid));
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You have teleported to your most recent checkpoint!");
     
                    } else {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You havent set a checkpoint yet!");
                    }
     
                }
     
                else if (e.getCurrentItem().getType()
                        .equals(Material.GLOWSTONE_DUST)) {
                    if (parkour.containsKey(uuid)) {
                       
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You have left the " + parkour.get(uuid)
                                + ChatColor.BLUE + " parkour!");
                        parkour.remove(uuid);
                       
                    } else if (!parkour.containsKey(uuid)) {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You arent participating in any parkour courses!");
                    }
     
                }
     
                e.setCancelled(true);
     
            }
     
        }
     
    }
    

    More specificly here is the area in which the problem is occurring (i would like to note that there isn't a single error)

    Code:
    @EventHandler
        public void onInvClickEvent(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            UUID uuid = p.getUniqueId();
     
            if (e.getInventory().equals(parkourInv)) {
                if (e.getCurrentItem().getType().equals(Material.REDSTONE)) {
                    if (parkour.containsKey(uuid)) {
                       
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You have set your checkpoint!");
                        checkPoint.put(uuid, p.getLocation());
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                       
                    } else {
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You must enter a parkour course in order to use this feature!");
                    }
     
                }
     
                else if (e.getCurrentItem().getType().equals(Material.SUGAR)) {
                    if (checkPoint.containsKey(uuid)) {
                       
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.teleport(checkPoint.get(uuid));
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You have teleported to your most recent checkpoint!");
     
                    } else {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You havent set a checkpoint yet!");
                    }
     
                }
     
                else if (e.getCurrentItem().getType()
                        .equals(Material.GLOWSTONE_DUST)) {
                    if (parkour.containsKey(uuid)) {
                       
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You have left the " + parkour.get(uuid)
                                + ChatColor.BLUE + " parkour!");
                        parkour.remove(uuid);
                       
                    } else if (!parkour.containsKey(uuid)) {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You arent participating in any parkour courses!");
                    }
     
                }
     
                e.setCancelled(true);
     
            }
     
        }
     
    }
     
  2. Offline

    mine-care

    Please! For god's shake! Please read about casting!!! You bypassed it in the inv click event and on command.
     
    xXRobbie21Xx likes this.
  3. Offline

    xXRobbie21Xx

    mine-care My bad, i completely misunderstood your signature, (and yes iv made a point to learn about it now thanks to you) Allthough i am just going to make myself look even dumber but i thought that this was the correct way to do it:
    Code:
    if (sender instanceof Player) {
                Player p = (Player) sender;
    Am i completely off or did i not check before i casted?
    Im going to go read about this but i want to ask for learning purposes before-hand.

    Edit: Yep i definently see how i messed up in the click event, btw. (with casting atleast)
     
  4. Offline

    Necrodoom

    xXRobbie21Xx its mostly fine, however you should add an else clause so a console executor doesn't just get no response at all, and would know its a player command only.

    Also, you should use the command name instead of the label, for alias support.
     
    xXRobbie21Xx likes this.
  5. Offline

    xXRobbie21Xx

    Necrodoom Okay i see where you're coming from, thanks!
     
  6. Offline

    mine-care

    xXRobbie21Xx sorry for my reaction, I was mad because a guy I told to fix the casting in the past has forgotten it again -_- so.. Sorry :-(
     
    xXRobbie21Xx likes this.
  7. Offline

    FerusGrim

    xXRobbie21Xx

    Just jumping in to say that, if you have a larger server, the redundant if/else checks could reduce performance a bit.

    Currently:
    Code:java
    1. if (!parkour.containsKey(uuid)) {
    2. doStuff();
    3. } else if (parkour.containsKey(uuid)) {
    4. doOtherStuff();
    5. }


    Because the if/else is a boolean request, there's only two states. True/False. Therefore, you can simplify this statement to:
    Code:java
    1. if (!parkour.containsKey(uuid)) {
    2. doStuff();
    3. } else {
    4. doOtherStuff();
    5. }


    This way, you're not querying the parkour map twice.
     
    xXRobbie21Xx likes this.
  8. Offline

    xXRobbie21Xx

    FerusGrim Oh really? Thank you so much! Its been a my pet peve trying to accomplish the most efficient code. I really appreciate that.

    mine-care Oh its okay D: Im sorry for being so ignorant about the whole matter. I allways make sure to closely follow the advise you give in all your posts and expecially make sure to give your signature a consideration :p. Kinda felt like shit because with all of the other distractions like school right now has really been making it hard to learn this stuff, thanks again!
     
    mine-care and FerusGrim like this.
Thread Status:
Not open for further replies.

Share This Page