HashMap not saving between methods.

Discussion in 'Plugin Development' started by KerchooK, Jul 13, 2018.

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

    KerchooK

    I have this HashMap
    Code:
    HashMap<UUID,Boolean> hash = new HashMap<UUID,Boolean>();
    It is initialized outside of all methods. When I use this hashmap in my onCommand method it works fine.
    Code:
    if(!hash.containsKey(id) || hash.get(id).equals(true)) {
                        hash.put(id, false);
                        p.sendMessage(prefix + "Off.");
                    } else {
                        hash.put(id, true);
                        p.sendMessage(prefix + "On.");
                    }
    When I use this in my onJoin event handler it works fine.
    Code:
    if(!hash.containsKey(id)) {
                hash.put(id, true);
            }
    When I use it in my blockBreak event handler it works fine.
    Code:
    if(hash.get(id).equals(Boolean.FALSE)) {
                                         p.sendMessage("YEET!");
                                         return;
                                     }
    The problem is, inside of each method for some reason it creates a new instance of the HashMap. I am not re-initializing it anywhere, all i am using is get, put, and contains.
     
  2. Online

    timtower Administrator Administrator Moderator

    @KerchooK Please post the full class.
    And if not the main: post the main as well.
     
  3. Offline

    KerchooK

    Alright, but its kind of a mess.
    Code:
    package me.kerchook.prisonrequisites.features;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.UUID;
    
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.Plugin;
    
    import com.sk89q.worldedit.Vector;
    import com.sk89q.worldguard.bukkit.BukkitUtil;
    import com.sk89q.worldguard.bukkit.WGBukkit;
    import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    import com.sk89q.worldguard.protection.ApplicableRegionSet;
    import com.sk89q.worldguard.protection.managers.RegionManager;
    import com.sk89q.worldguard.protection.regions.ProtectedRegion;
    
    import me.kerchook.prisonrequisites.Main;
    import me.kerchook.prisonrequisites.apis.APIVault;
    import me.kerchook.prisonrequisites.apis.APIWorldGuard;
    import me.kerchook.prisonrequisites.files.Lang;
    import me.kerchook.prisonrequisites.files.Price;
    
    public class AutoInv implements Listener, CommandExecutor {
    
        Plugin pl = Main.getPlugin();
        Lang lang = new Lang();
        APIWorldGuard wg = new APIWorldGuard();
        Price price = new Price();
        APIVault vapi = new APIVault();
    
        HashMap<UUID,Boolean> hash = new HashMap<UUID,Boolean>();
       
        boolean inRegion(Location loc) {
            WorldGuardPlugin guard = wg.getWorldGuard();
            Vector v = BukkitUtil.toVector(loc);
            RegionManager manager = guard.getRegionManager(loc.getWorld());
            ApplicableRegionSet set = manager.getApplicableRegions(v);
            return set.size() > 0;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if ((sender instanceof Player)) {
                String prefix = ChatColor.translateAlternateColorCodes('&',
                        pl.getConfig().getString("messages.prefix") + ChatColor.WHITE + " ");
                Player p = (Player) sender;
                UUID id = p.getUniqueId();
    
                    if(!hash.containsKey(id) || hash.get(id).equals(true)) {
                        hash.put(id, false);
                        p.sendMessage(prefix + "Autosell off.");
                    } else {
                        hash.put(id, true);
                        p.sendMessage(prefix + "Autosell on.");
                    }
            }
            return true;
        }
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            UUID id = p.getUniqueId();
            if(!hash.containsKey(id)) {
                hash.put(id, true);
            }
        }
    
        @EventHandler
        public void onBreak(BlockBreakEvent e) {
            if (e.isCancelled()) {
                return;
            }
            Player p = e.getPlayer();
            UUID id = p.getUniqueId();
            PlayerInventory inv = p.getInventory();
            Block b = e.getBlock();
            ItemStack hand = inv.getItemInMainHand();
            if (p.getGameMode() != GameMode.SURVIVAL) {
                return;
            }
            hand.setDurability((short) 0);
            if (!wg.getWorldGuard().canBuild(p, b)) {
                return;
            }
            if (inv.firstEmpty() == -1) {
                return;
            }
            int mult = 1;
            if (hand.hasItemMeta() && hand.getItemMeta().hasEnchant(Enchantment.LOOT_BONUS_BLOCKS)) {
                int level = hand.getItemMeta().getEnchantLevel(Enchantment.LOOT_BONUS_BLOCKS);
                mult = level + 1;
                if (mult < 1) {
                    mult = 1;
                }
            }
            if(!hash.containsKey(id)) {
                hash.put(id, true);
            }
            double prestigeBoost = 1;
            if (APIVault.getPermissions().playerInGroup(p, "P1")) {
                prestigeBoost = 1.1;
            }
            if (APIVault.getPermissions().playerInGroup(p, "P2")) {
                prestigeBoost = 1.2;
            }
            Collection<ItemStack> drops = e.getBlock().getDrops();
            for (ItemStack item : drops) {
                for (ProtectedRegion r : WGBukkit.getRegionManager(b.getWorld()).getApplicableRegions(b.getLocation())) {
                    if (inRegion(b.getLocation())) {
                        if (price.getPrice().isSet("blocks.price." + item.getType().toString())) {
                            /*
                             * if (r.getId().equalsIgnoreCase("donorblock")) { double boost =
                             * price.getPrice().getDouble("boost.donor"); int blo =
                             * price.getPrice().getInt("blocks.price." + item.getType().toString());
                             * item.setAmount(item.getAmount() * mult); int qty = item.getAmount(); double
                             * pr = boost * blo * qty * prestigeBoost;
                             * APIVault.getEconomy().depositPlayer(p, pr);
                             * e.getBlock().getWorld().getBlockAt(e.getBlock().getLocation()).setType(
                             * Material.AIR); return; } else
                             */if (r.getId().equalsIgnoreCase("donor+block")) {
                                double boost = price.getPrice().getDouble("boost.donor+");
                                int blo = price.getPrice().getInt("blocks.price." + item.getType().toString());
                                item.setAmount(item.getAmount() * mult);
                                int qty = item.getAmount();
                                double pr = boost * blo * qty * prestigeBoost;
                                APIVault.getEconomy().depositPlayer(p, pr);
                                e.getBlock().getWorld().getBlockAt(e.getBlock().getLocation()).setType(Material.AIR);
                                return;
                            } else if (r.getId().equalsIgnoreCase("vipblock")) {
                                double boost = price.getPrice().getDouble("boost.vip");
                                int blo = price.getPrice().getInt("blocks.price." + item.getType().toString());
                                item.setAmount(item.getAmount() * mult);
                                int qty = item.getAmount();
                                double pr = boost * blo * qty * prestigeBoost;
                                APIVault.getEconomy().depositPlayer(p, pr);
                                e.getBlock().getWorld().getBlockAt(e.getBlock().getLocation()).setType(Material.AIR);
                                return;
                            } /*
                                 * else if (r.getId().equalsIgnoreCase("vip+block")) { double boost =
                                 * price.getPrice().getDouble("boost.vip+"); int blo =
                                 * price.getPrice().getInt("blocks.price." + item.getType().toString());
                                 * item.setAmount(item.getAmount() * mult); int qty = item.getAmount(); double
                                 * pr = boost * blo * qty * prestigeBoost;
                                 * APIVault.getEconomy().depositPlayer(p, pr);
                                 * e.getBlock().getWorld().getBlockAt(e.getBlock().getLocation()).setType(
                                 * Material.AIR); return; }
                                 */else {
                                     if(hash.get(id).equals(Boolean.FALSE)) {
                                         p.sendMessage("YEET!");
                                         return;
                                     } else {
                                         p.sendMessage(":(");
                                     }
                                int blo = price.getPrice().getInt("blocks.price." + item.getType().toString());
                                item.setAmount(item.getAmount() * mult);
                                int qty = item.getAmount();
                                double pr = blo * qty * prestigeBoost;
                                APIVault.getEconomy().depositPlayer(p, pr);
                                e.getBlock().getWorld().getBlockAt(e.getBlock().getLocation()).setType(Material.AIR);
                                return;
                            }
                        } else {
                            inv.addItem(item);
                            e.getBlock().getWorld().getBlockAt(e.getBlock().getLocation()).setType(Material.AIR);
                            return;
                        }
                    } else {
                        inv.addItem(item);
                        e.getBlock().getWorld().getBlockAt(e.getBlock().getLocation()).setType(Material.AIR);
                        return;
                    }
                }
                inv.addItem(item);
                e.getBlock().getWorld().getBlockAt(e.getBlock().getLocation()).setType(Material.AIR);
                return;
            }
            return;
        }
    
    }
    
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    caderapee

    @KerchooK
    Tim ask for the main class cuz u probably register two differents instance for the command and the event.
     
Thread Status:
Not open for further replies.

Share This Page