Saving MaterialData to a SortedMap overwriting values

Discussion in 'Plugin Development' started by MyPasswordIsPassword, Jun 25, 2012.

Thread Status:
Not open for further replies.
  1. I've been trying to figure out the problem here for hours, but it must be either some SortedMap behavior I don't know about or a problem with the Comparator (which I also don't really understand) I'm trying to separate an ItemStack's amount from its MaterialData so that I can compare the amounts. I have the problem causing part separated in it's own method for testing:
    Code:
    public void maptest()
        {
            ItemStack[] previous = { new ItemStack( 3 ), new ItemStack( 2 ), new ItemStack( 1 ), new ItemStack( 4 ) };
            SortedMap<MaterialData, Integer> prevAmounts = new TreeMap<MaterialData, Integer>( new MaterialDataComparator() );
         
            for ( ItemStack is : previous )
            {
                if ( is != null )
                {
                    //(debug) Outputs if prevAmounts already contains is.getData(), and what the value is
                    getServer().broadcastMessage( new StringBuilder().append(ChatColor.GREEN).append( is.getData() ).append( " " )
                            .append( prevAmounts.get( is.getData() ) ).append( " " ).append( prevAmounts.containsKey( is.getData() ) ).toString() );
                 
                    if ( prevAmounts.containsKey( is.getData() ) )
                        prevAmounts.put( is.getData(), prevAmounts.get( is.getData() ) + is.getAmount() );
                    else
                        prevAmounts.put( is.getData(), is.getAmount() );
                 
                    //(debug) Outputs if prevAmounts contains is.getData() after saved, and what the value is
                    getServer().broadcastMessage( new StringBuilder().append(ChatColor.RED).append( is.getData() ).append( " " ).append( prevAmounts.get( is.getData() ) )
                            .append( " " ).append( prevAmounts.containsKey( is.getData() ) ).toString() );
                 
                    //(debug) Outputs the elements in prevAmounts after each iteration of previous[]
                    for ( MaterialData d : prevAmounts.keySet() )
                    {
                        getServer().broadcastMessage( new StringBuilder().append( ChatColor.GOLD ).append( d ).append( " " )
                                .append( prevAmounts.get( d ) ).toString() );
                    }
                } 
            } 
            //(debug) Outputs the results after done iterating through previous[]
            for ( MaterialData d : prevAmounts.keySet() )
            {
                getServer().broadcastMessage( new StringBuilder().append( ChatColor.AQUA ).append( d ).append( " " ).append( prevAmounts.get( d ) )
                        .append( " " ).append( prevAmounts.containsKey( d ) ).toString() );
            }
        }
    And the comparator (Makes the SortedMap able to compare the MaterialData)
    Code:
    public class MaterialDataComparator implements Comparator<MaterialData> {
        @Override
        public int compare(MaterialData o1, MaterialData o2) {
         
            if ( o1.equals( o2 ) )
                return 0;
            else return 1;
        }
    }
    The problem is that if there are more than two elements in previous, the first element will always have a null integer for a value, and for the life of me I cannot figure out why.

    Edit: I'm an idiot.. I never figured out what was causing the null, but I realized that you can iterate through hashmap keys as well as sortedmap keys. (So much time wasted)
     
  2. you are not implementing the comparator interface corectly, thats why it give unexcepted results
     
Thread Status:
Not open for further replies.

Share This Page