SPIGOT-2768: Fully implement map color

This commit is contained in:
md_5 2016-11-17 17:24:20 +11:00
parent c25ddf063a
commit 54450b727e

View File

@ -3,27 +3,30 @@ package org.bukkit.craftbukkit.inventory;
import java.util.Map; import java.util.Map;
import net.minecraft.server.NBTTagCompound; import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.NBTTagInt;
import net.minecraft.server.NBTTagString; import net.minecraft.server.NBTTagString;
import org.bukkit.Color;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta; import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.inventory.meta.MapMeta; import org.bukkit.inventory.meta.MapMeta;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@DelegateDeserialization(SerializableMeta.class) @DelegateDeserialization(SerializableMeta.class)
class CraftMetaMap extends CraftMetaItem implements MapMeta { class CraftMetaMap extends CraftMetaItem implements MapMeta {
static final ItemMetaKey MAP_SCALING = new ItemMetaKey("map_is_scaling", "scaling"); static final ItemMetaKey MAP_SCALING = new ItemMetaKey("map_is_scaling", "scaling");
static final ItemMetaKey MAP_LOC_NAME = new ItemMetaKey("LocName", "display-loc-name"); static final ItemMetaKey MAP_LOC_NAME = new ItemMetaKey("LocName", "display-loc-name");
static final ItemMetaKey MAP_COLOR = new ItemMetaKey("Mapcolor", "display-map-color"); static final ItemMetaKey MAP_COLOR = new ItemMetaKey("MapColor", "display-map-color");
static final byte SCALING_EMPTY = (byte) 0; static final byte SCALING_EMPTY = (byte) 0;
static final byte SCALING_TRUE = (byte) 1; static final byte SCALING_TRUE = (byte) 1;
static final byte SCALING_FALSE = (byte) 2; static final byte SCALING_FALSE = (byte) 2;
private byte scaling = SCALING_EMPTY; private byte scaling = SCALING_EMPTY;
private String locName; private String locName;
private String mapColor; private Color color;
CraftMetaMap(CraftMetaItem meta) { CraftMetaMap(CraftMetaItem meta) {
super(meta); super(meta);
@ -34,6 +37,8 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
CraftMetaMap map = (CraftMetaMap) meta; CraftMetaMap map = (CraftMetaMap) meta;
this.scaling = map.scaling; this.scaling = map.scaling;
this.locName = map.locName;
this.color = map.color;
} }
CraftMetaMap(NBTTagCompound tag) { CraftMetaMap(NBTTagCompound tag) {
@ -51,7 +56,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
} }
if (display.hasKey(MAP_COLOR.NBT)) { if (display.hasKey(MAP_COLOR.NBT)) {
mapColor = display.getString(MAP_COLOR.NBT); color = Color.fromRGB(display.getInt(MAP_COLOR.NBT));
} }
} }
} }
@ -63,6 +68,16 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
if (scaling != null) { if (scaling != null) {
setScaling(scaling); setScaling(scaling);
} }
String locName = SerializableMeta.getString(map, MAP_LOC_NAME.BUKKIT, true);
if (locName != null) {
setLocationName(locName);
}
Color color = SerializableMeta.getObject(Color.class, map, MAP_COLOR.BUKKIT, true);
if (color != null) {
setColor(color);
}
} }
@Override @Override
@ -77,8 +92,8 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
setDisplayTag(tag, MAP_LOC_NAME.NBT, new NBTTagString(getLocationName())); setDisplayTag(tag, MAP_LOC_NAME.NBT, new NBTTagString(getLocationName()));
} }
if (hasMapColor()) { if (hasColor()) {
setDisplayTag(tag, MAP_COLOR.NBT, new NBTTagString(mapColor)); setDisplayTag(tag, MAP_COLOR.NBT, new NBTTagInt(color.asRGB()));
} }
} }
@ -98,7 +113,7 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
} }
boolean isMapEmpty() { boolean isMapEmpty() {
return !hasScaling(); return !(hasScaling() | hasLocationName() || hasColor());
} }
boolean hasScaling() { boolean hasScaling() {
@ -125,11 +140,26 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
@Override @Override
public void setLocationName(String name) { public void setLocationName(String name) {
Preconditions.checkArgument(name != null, "name");
this.locName = name; this.locName = name;
} }
public boolean hasMapColor() { @Override
return this.mapColor != null; public boolean hasColor() {
return this.color != null;
}
@Override
public Color getColor() {
return this.color;
}
@Override
public void setColor(Color color) {
Preconditions.checkArgument(color != null, "color");
this.color = color;
} }
@Override @Override
@ -141,8 +171,8 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
CraftMetaMap that = (CraftMetaMap) meta; CraftMetaMap that = (CraftMetaMap) meta;
return (this.scaling == that.scaling) return (this.scaling == that.scaling)
&& (hasLocationName() ? that.hasLocationName() && this.getLocationName().equals(that.getLocationName()) : !that.hasLocationName()) && (hasLocationName() ? that.hasLocationName() && this.locName.equals(that.locName) : !that.hasLocationName())
&& (hasMapColor() ? that.hasMapColor() && this.mapColor.equals(that.mapColor) : !that.hasMapColor()); && (hasColor() ? that.hasColor() && this.color.equals(that.color) : !that.hasColor());
} }
return true; return true;
} }
@ -160,10 +190,17 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
if (hasScaling()) { if (hasScaling()) {
hash ^= 0x22222222 << (isScaling() ? 1 : -1); hash ^= 0x22222222 << (isScaling() ? 1 : -1);
} }
if (hasLocationName()) {
hash = 61 * hash + locName.hashCode();
}
if (hasColor()) {
hash = 61 * hash + color.hashCode();
}
return original != hash ? CraftMetaMap.class.hashCode() ^ hash : hash; return original != hash ? CraftMetaMap.class.hashCode() ^ hash : hash;
} }
public CraftMetaMap clone() { public CraftMetaMap clone() {
return (CraftMetaMap) super.clone(); return (CraftMetaMap) super.clone();
} }
@ -180,8 +217,8 @@ class CraftMetaMap extends CraftMetaItem implements MapMeta {
builder.put(MAP_LOC_NAME.BUKKIT, getLocationName()); builder.put(MAP_LOC_NAME.BUKKIT, getLocationName());
} }
if (hasMapColor()) { if (hasColor()) {
builder.put(MAP_COLOR.BUKKIT, mapColor); builder.put(MAP_COLOR.BUKKIT, getColor());
} }
return builder; return builder;