How to check both values from Hashmap?

Discussion in 'Plugin Development' started by recon88, May 31, 2012.

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

    recon88

    I got a simple Code-Redeem System.
    I'm adding static codes with a command: String+Integer
    Everyone should be able to use every code once.
    How can i do that?

    e.G.:
    Code:
    HashMap<Player, String> stplayers = new HashMap<Player, String>();
     
     
                if (commandLabel.equalsIgnoreCase("redeem")) {
              if (args.length == 1) {
                      try {
                  String codest = (args[0].toLowerCase());
                  Integer moneyst = stareward.get(codest);
                  if (stareward.containsKey(codest) &&  MISSING PART HERE)) {
                      EasyCoupons.economy.depositPlayer(player.getDisplayName(), moneyst);
                      stareward.remove(codest);
                      stplayers.put(player, codest);
                      player.sendMessage(ChatColor.RED + "Static-Coupon [" + codest + "] used. You got [" + economy.format(moneyst) + "].");
                  }
                      } catch (Exception e) {
                      }
                 }
     
    
     
  2. Map.keySet()?
    EDIT: wait, you mean stplayers.containsValue()?
     
  3. Offline

    recon88

    stplayers.containsValue() doesn't work.
    It locks the command for all players or all codes (depends on which value I'm checking).
    But I want that everyone is able to use every code once.
     
  4. From what I understand, I belive the easiest way would be:
    Code:
    Map<String, Set<String>> playerCodes = new HashMap<String, Set<String>>();
    Map<String, Double> codes = new HashMap<String, Double>();
    		
    // adding codes
    codes.put("0000", 0.1);
    codes.put("0001", 50.0);
    
    // player redeemed:
    String code = the entered code
    String playerName = player's name
    
    if(codes.containsKey(code))
    {
    	final Set<String> redeemed = playerCodes.get(playerName);
    	
    	if(redeemed == null || !redeemed.contains(code))
    	{
    		// player can redeem code !
    		
    		final double reward = codes.get(code);
    		// give rewards...
    		
    		redeemed.add(code); // add the redeemed code to the player's list
    	}
    }
    With this method you can also find out what codes each player redeemed.

    Also, you should use double instead of integer for money, expecially if you want to release it, if not, it's still good for the future even if you're only using integers.
     
  5. Offline

    recon88

    Missing try/catch? Or what's wrong with it?
    Line 127 marked.

    Code:
        HashMap<String, Double> stareward = new HashMap<String, Double>();
        Map<String, Set<String>> playerCodes = new HashMap<String, Set<String>>();
    Code:
            if (commandLabel.equalsIgnoreCase("redeem")) {
                if (args.length == 1) {
                String codest = (args[0].toLowerCase());
                    if(stareward.containsKey(codest)) {
                    final Set<String> redeemed = playerCodes.get(player.getName());
                        if(redeemed == null || !redeemed.contains(codest)) {                 
                            final double rewardst = stareward.get(codest);
                            TestCoupon.economy.depositPlayer(player.getDisplayName(), rewardst);
                            player.sendMessage(ChatColor.RED + "Static-Coupon [" + codest + "] used. You got [" + economy.format(rewardst) + "].");
    (LINE 127 HERE)      redeemed.add(codest);
                        }
                    }
                }
    Code:
    Unhandled exception executing command 'redeem' in plugin  TestCoupon v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at com.recon.EasyCoupons.EasyCoupons.onCommand(TestCoupon.java:127)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
     
  6. Oh 'duh, I forgot about handling the null part...

    So replace that line with:
    Code:
    if(redeemed == null)
    {
    	redeemed = new HashSet<String>();
    	redeemed.add(code);
    	playerCodes.put(player.getName(), redeemed);
    }
    else
    	redeemed.add(code);
    And remove "final" modifier from redeemed.
     
    recon88 likes this.
  7. Offline

    recon88

    Thanks Sir !
    Finally got that fixed.
     
Thread Status:
Not open for further replies.

Share This Page