-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
848 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
common/src/main/java/dev/cerus/maps/api/colormap/ColorMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package dev.cerus.maps.api.colormap; | ||
|
||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
/** | ||
* A container for map colors | ||
*/ | ||
public class ColorMap { | ||
|
||
// Color array, capped at 256 because that's the maximum amount of possible colors at the moment | ||
private final Color[] colors = new Color[256]; | ||
|
||
public void putColor(final Color color) { | ||
this.colors[this.b2i(color.mapColor)] = color; | ||
} | ||
|
||
/** | ||
* Find color by id | ||
* | ||
* @param id Map color id | ||
* | ||
* @return Color or null | ||
*/ | ||
public Color fromId(final int id) { | ||
return this.getById(id); | ||
} | ||
|
||
/** | ||
* Find color by id | ||
* | ||
* @param id Map color id | ||
* | ||
* @return Color or null | ||
*/ | ||
public Color getById(int id) { | ||
id = this.b2i(id); | ||
return (id < 0 || id >= this.colors.length) ? null : this.colors[id]; | ||
} | ||
|
||
/** | ||
* Find java color by id | ||
* | ||
* @param color Map color id | ||
* | ||
* @return Java color or java color id 0 (or null) | ||
*/ | ||
public java.awt.Color mapColorToRgb(final byte color) { | ||
return Optional.ofNullable(this.getById(color)).map(c -> c.javaColor) | ||
.orElse(Optional.ofNullable(getById(0)).map(c -> c.javaColor) | ||
.orElse(null)); | ||
} | ||
|
||
/** | ||
* Find color by RGB | ||
* | ||
* @param r Red | ||
* @param g Green | ||
* @param b Blue | ||
* | ||
* @return Color or transparent color (0) (or null) | ||
*/ | ||
public Color rgbToMapColor(final int r, final int g, final int b) { | ||
return Arrays.stream(this.colors) | ||
.filter(Objects::nonNull) | ||
.filter(color -> color.mapColor() > 3) | ||
.min(Comparator.comparingDouble(value -> this.calcDist(value, r, g, b))) | ||
.orElse(this.getById(0)); | ||
} | ||
|
||
private double calcDist(final Color clr, final int r, final int g, final int b) { | ||
final java.awt.Color color = clr.javaColor(); | ||
return Math.sqrt(Math.pow(r - color.getRed(), 2) + Math.pow(g - color.getGreen(), 2) + Math.pow(b - color.getBlue(), 2)); | ||
} | ||
|
||
private int b2i(int b) { | ||
if (b < 0) { | ||
b += 256; | ||
} | ||
return b; | ||
} | ||
|
||
public Color[] getColors() { | ||
return this.colors; | ||
} | ||
|
||
public record Color(byte mapColor, java.awt.Color javaColor) { | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || this.getClass() != o.getClass()) { | ||
return false; | ||
} | ||
final Color color = (Color) o; | ||
return this.mapColor() == color.mapColor() && Objects.equals(this.javaColor(), color.javaColor()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.mapColor(), this.javaColor()); | ||
} | ||
|
||
} | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
common/src/main/java/dev/cerus/maps/api/colormap/ColorMaps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package dev.cerus.maps.api.colormap; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import java.awt.Color; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.Map; | ||
import org.bukkit.Bukkit; | ||
|
||
/** | ||
* Utility class for color maps | ||
*/ | ||
public class ColorMaps { | ||
|
||
private static final ColorVersion FALLBACK = ColorVersion.MC_1_12; | ||
private static final ColorMap latest = newColorMap(ColorVersion.latest()); | ||
private static final ColorMap current = newColorMap(determineVersion()); | ||
|
||
private ColorMaps() { | ||
} | ||
|
||
/** | ||
* Get a color map for the latest Minecraft version | ||
* | ||
* @return Color map for latest version | ||
*/ | ||
public static ColorMap latest() { | ||
return latest; | ||
} | ||
|
||
/** | ||
* Get a color map for the Minecraft version this server is running | ||
* | ||
* @return Color map for current version | ||
*/ | ||
public static ColorMap current() { | ||
return current; | ||
} | ||
|
||
/** | ||
* Get a new color map for the specified color version | ||
* | ||
* @param version The version | ||
* | ||
* @return A new color map | ||
*/ | ||
public static ColorMap newColorMap(final ColorVersion version) { | ||
final ColorMap colorMap = new ColorMap(); | ||
if (version == ColorVersion.latest()) { | ||
// Load colors from jar | ||
try (final InputStream in = ColorMaps.class.getClassLoader().getResourceAsStream("latest_colors.json"); | ||
final InputStreamReader reader = new InputStreamReader(in)) { | ||
final JsonObject obj = new JsonParser().parse(reader).getAsJsonObject(); | ||
for (final Map.Entry<String, JsonElement> entry : obj.entrySet()) { | ||
final int baseId = Integer.parseInt(entry.getKey()); | ||
final JsonObject item = entry.getValue().getAsJsonObject(); | ||
final JsonArray colors = item.getAsJsonArray("colors"); | ||
|
||
int i = 0; | ||
for (final JsonElement element : colors) { | ||
final int colorRaw = element.getAsInt(); | ||
colorMap.putColor(new ColorMap.Color( | ||
(byte) (baseId * 4 + i++), | ||
new Color(colorRaw, false) | ||
)); | ||
} | ||
} | ||
} catch (final IOException | NullPointerException e) { | ||
System.err.println("Failed to load colors"); | ||
} | ||
} else if (latest != null) { | ||
// Copy the colors from the latest color map | ||
for (final ColorMap.Color color : latest.getColors()) { | ||
if (color != null && ((int) color.mapColor()) / 4 <= version.getLastId()) { | ||
colorMap.putColor(color); | ||
} | ||
} | ||
} | ||
return colorMap; | ||
} | ||
|
||
/** | ||
* Determine an appropriate color version for this server | ||
* | ||
* @return A color version | ||
*/ | ||
public static ColorVersion determineVersion() { | ||
String version = Bukkit.getVersion(); | ||
version = version.substring(version.indexOf("MC:") + 4, version.length() - 1).trim(); | ||
if (!version.matches("\\d+\\.\\d+(\\.\\d+)")) { | ||
return FALLBACK; | ||
} | ||
|
||
final String[] split = version.split("\\."); | ||
final int major = Integer.parseInt(split[0]); | ||
final int minor = Integer.parseInt(split[1]); | ||
if (major != 1) { | ||
// FUTUREEEEE | ||
return FALLBACK; | ||
} | ||
if (minor < 12) { | ||
return ColorVersion.MC_1_8; | ||
} else if (minor < 16) { | ||
return ColorVersion.MC_1_12; | ||
} else if (minor < 17) { | ||
return ColorVersion.MC_1_16; | ||
} else { | ||
return ColorVersion.MC_1_17; | ||
} | ||
} | ||
|
||
|
||
} |
24 changes: 24 additions & 0 deletions
24
common/src/main/java/dev/cerus/maps/api/colormap/ColorVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.cerus.maps.api.colormap; | ||
|
||
public enum ColorVersion { | ||
|
||
MC_1_8(35), | ||
MC_1_12(51), | ||
MC_1_16(58), | ||
MC_1_17(61); | ||
|
||
private final int lastId; | ||
|
||
ColorVersion(final int lastId) { | ||
this.lastId = lastId; | ||
} | ||
|
||
public static ColorVersion latest() { | ||
return MC_1_17; | ||
} | ||
|
||
public int getLastId() { | ||
return this.lastId; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.