Get Block From Hash Map?

Discussion in 'Plugin Development' started by Gamesareme, Jul 22, 2014.

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

    PandazNWafflez

    Yes, but the OP was using a HashMap, not a WeakHashMap.. What I initially said was:
    It changes completely if it's a WeakHashMap
     
  2. Offline

    Gamesareme


    Code:java
    1. Block b = (Block) loc.getBlock().getState();
    2. blocks.put(player.getUniqueId(), (Block) b.getState().update(true));

    So you are saying like this? note this code is buged, and I can not work out why :(
    Also will I need to make another hashmap?
     
  3. Offline

    fireblast709

    Gamesareme you put the BlockState in the Map, later retrieve it and call .update(true)
    PandazNWafflez which is why I started by suggesting a WeakHashMap. Both to suggest improvement to the OP's code and to argue why you can use Player keys
     
  4. Offline

    PandazNWafflez

    fireblast709 Lol whoops, yeah if you are gonna use a WeakHashMap then it's all good. I was going off the OP's code xD
     
  5. Offline

    Gamesareme

    All right this is my code now.
    Code:
        public HashMap<UUID, BlockState> blocks = new HashMap<UUID, BlockState>();
    
    And this is the rest.
    Code:
        @EventHandler
        public void onSnowBallEvent(ProjectileHitEvent event){
            Projectile pj = event.getEntity();
            //if(!(event.getEntity() instanceof Player)){
            //    return;
            //}
            if(pj instanceof Snowball){
                @SuppressWarnings("deprecation")
                Player player = (Player)event.getEntity().getShooter();
                Location loc = pj.getLocation().subtract(0, 1, 0);
                Block b = (Block) loc.getBlock();
                blocks.put(player.getUniqueId(), b.getState());
                if(b.equals(Material.AIR)){
                    return;
                }
                b.setType(Material.WOOL);
                shot.add(player);
                Countdown(player, b);
            }
        }
    and this is the count down.
    Code:
        public void Countdown(final Player player, final Block b) {
            new BukkitRunnable() {
                @Override
                public void run() {
                    //try {
                        shot.remove(player.getUniqueId());
                        player.sendMessage(ChatColor.GRAY + "[" + ChatColor.GOLD + "PXHub" + ChatColor.GRAY + "] " + ChatColor.GREEN + "You Can Use Bat Gun Again!");
                        b.setType(blocks.get(player).getBlock().getType());
    blocks.get(player).update(true);
                    //} catch (Exception e) {
                        player.sendMessage(ChatColor.RED + "Did not work!");
                    //}
                }
            }.runTaskLater(this.plugin, 20);
        }
    This generates I error in the terminal. :(
    Code:
    [11:55:29 WARN]: [PXHub] Task #81 for PXHub v0.1.2 generated an exception
    java.lang.NullPointerException
        at me.joxboyz.Listerners.HubGajets.paintgun$1.run(paintgun.java:69) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:53) ~[minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:600) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
        at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [minecraft_server.jar:git-Bukkit-1.7.2-R0.3-66-g43d8943-b3078jnks]
    I have never seen a error like this before, so I do not know what is wrong.

    Ahhhhh I keep getting NullPointerException, can someone see what I am doing wrong?

    Hang on I found the problem. I was calling player, while I should have been calling their unique code. :)

    Ok, I have this now working for one snow ball, but I want to add more. Can someone point me in the right direction. I tried a for loop, but it did now work. :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page