package com.github.steveice10.mc.protocol.data.game.chunk.palette;

import lombok.EqualsAndHashCode;

import java.util.HashMap;
import java.util.Map;

/**
 * A palette backed by a map.
 */
@EqualsAndHashCode
public class MapPalette implements Palette {
    private final int maxId;

    private final Map<Integer, Integer> idToState = new HashMap<>();
    private final Map<Integer, Integer> stateToId = new HashMap<>();
    private int nextId = 1;

    public MapPalette(int bitsPerEntry) {
        this.maxId = (1 << bitsPerEntry) - 1;

        this.idToState.put(0, 0);
        this.stateToId.put(0, 0);
    }

    @Override
    public int size() {
        return this.idToState.size();
    }

    @Override
    public int stateToId(int state) {
        Integer id = this.stateToId.get(state);
        if(id == null && this.size() < this.maxId + 1) {
            id = this.nextId++;
            this.idToState.put(id, state);
            this.stateToId.put(state, id);
        }

        if(id != null) {
            return id;
        } else {
            return -1;
        }
    }

    @Override
    public int idToState(int id) {
        Integer state = this.idToState.get(id);
        if(state != null) {
            return state;
        } else {
            return 0;
        }
    }
}

@@ -298,57 +298,23 @@
 nal 
-Map%3CInteger, Integer%3E idToState = new HashMap%3C%3E()
+int%5B%5D idToState
 ;%0A  
@@ -520,26 +520,42 @@
 dToState
-.put(0, 0)
+ = new int%5Bthis.maxId + 1%5D
 ;%0A      
@@ -651,24 +651,14 @@
 his.
-idToState.size()
+nextId
 ;%0A  
@@ -881,23 +881,20 @@
 tate
-.put(id,
+%5Bid%5D =
  state
-)
 ;%0A  
@@ -1111,72 +1111,38 @@
     
-Integer state = this.idToState.get(id);%0A        if(state != null
+if(id %3E= 0 && id %3C this.size()
 ) %7B%0A
@@ -1160,21 +1160,34 @@
  return 
-state
+this.idToState%5Bid%5D
 ;%0A      

make MapPalette's idToState Map into an array