More changes

fixed build but wasn't able to fix the colors problem :/
looks like the problem is not the color comparison code anymore, but the way doubleColors is initialized
it works for MapTest but doesn't want to work properly for that array :/
This commit is contained in:
Ivan Pekov 2020-12-26 21:57:15 +02:00
parent cd4b222dcc
commit 02d2ee9fcb
No known key found for this signature in database
GPG Key ID: BC975C392D9CA3A3
3 changed files with 16 additions and 154 deletions

View File

@ -5,7 +5,7 @@ Subject: [PATCH] Optimise Bukkit's MapPalette
diff --git a/src/main/java/org/bukkit/map/MapPalette.java b/src/main/java/org/bukkit/map/MapPalette.java
index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..84daffdda568039423d0f6d18981bb9d9a392e1f 100644
index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..4f015713ef9ee66f8ad53a9570c65c7791ded153 100644
--- a/src/main/java/org/bukkit/map/MapPalette.java
+++ b/src/main/java/org/bukkit/map/MapPalette.java
@@ -22,7 +22,9 @@ public final class MapPalette {
@ -43,7 +43,7 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..84daffdda568039423d0f6d18981bb9d
@NotNull
static final Color[] colors = {
@@ -95,6 +113,52 @@ public final class MapPalette {
@@ -95,6 +113,51 @@ public final class MapPalette {
c(60, 31, 43), c(74, 37, 53), c(86, 44, 62), c(45, 23, 32),
c(14, 127, 93), c(17, 155, 114), c(20, 180, 133), c(10, 95, 70)
};
@ -62,12 +62,11 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..84daffdda568039423d0f6d18981bb9d
+ rgbColors = java.util.Arrays.stream(colors).mapToInt(Color::getRGB).toArray();
+
+ try {
+ // load net.minecraft.server.MaterialMapColor class. using reflection because build fails. maven thinks nms isn't accessible from Yatopia-API
+ Class<?> materialMapColorClass = MapPalette.class.getClassLoader().loadClass("net.minecraft.server.MaterialMapColor");
+ // get vanilla colors
+ Object[] vanillaColorObjects = (Object[]) java.util.Arrays.stream(materialMapColorClass.getFields()).filter(f -> f.getType().isArray() && f.getType().getComponentType().equals(materialMapColorClass)).findAny().get().get(null);
+ // get MaterialMapColor.rgb instance field
+ java.lang.reflect.Field rgbField = materialMapColorClass.getDeclaredField("rgb");
+ rgbField.setAccessible(true);
+ // get vanilla colors array as java.awt.Color instances array
+ vanillaColors = java.util.Arrays.stream(vanillaColorObjects).filter(java.util.Objects::nonNull).mapToInt(objColor -> {
+ try {
@ -82,9 +81,9 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..84daffdda568039423d0f6d18981bb9d
+ Color vanillaColor = vanillaColors[i];
+ for (int j = 0; j < modifiers.length; j++) {
+ int modifier = modifiers[j];
+ int mr = vanillaColor.getRed() * modifier / 255;
+ int mg = vanillaColor.getGreen() * modifier / 255;
+ int mb = vanillaColor.getBlue() * modifier / 255;
+ int mr = (vanillaColor.getRed() * modifier) / 255;
+ int mg = (vanillaColor.getGreen() * modifier) / 255;
+ int mb = (vanillaColor.getBlue() * modifier) / 255;
+ doubleColors[i][j] = c(mr, mg, mb);
+ }
+ }
@ -96,28 +95,27 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..84daffdda568039423d0f6d18981bb9d
// Interface
/**
@@ -235,6 +299,8 @@ public final class MapPalette {
public static byte matchColor(@NotNull Color color) {
@@ -236,6 +299,8 @@ public final class MapPalette {
if (color.getAlpha() < 128) return 0;
int index = 0;
+ // Yatopia start - foreach a maximum of 62 colors instead of 236 per pixel
+ /*
int index = 0;
double best = -1;
@@ -245,9 +311,45 @@ public final class MapPalette {
for (int i = 4; i < colors.length; i++) {
@@ -245,9 +310,44 @@ public final class MapPalette {
index = i;
}
}
+ */
+
+ int index = 0;
+ int best = -1;
+
+ for (int i = 0; i < vanillaColors.length; i++) {
+ int distance = getDistanceSquared(color, vanillaColors[i]);
+ if (distance == 0) {
+ index = i;
+ best = 0;
+ break;
+ }
+ if (distance < best || best == -1) {
@ -125,6 +123,10 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..84daffdda568039423d0f6d18981bb9d
+ index = i;
+ }
+ }
+ if (best == 0) {
+ index = com.google.common.primitives.Ints.indexOf(rgbColors, vanillaColors[index].getRGB());
+ return (byte) (index < 128 ? index : index - 256);
+ }
+ Color[] modColors = doubleColors[index];
+ best = -1;
+ for (int i = 0; i < modColors.length; i++) {
@ -140,10 +142,6 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..84daffdda568039423d0f6d18981bb9d
+ }
+
+ index = com.google.common.primitives.Ints.indexOf(rgbColors, modColors[index].getRGB());
+ // we have already checked if color is transparent so now there aren't any transparent colors
+ // and if (index < 4) then nearest color was black (hex #000000), so we need to return the most black color
+ if (index < 4)
+ index = 119;
+ // Yatopia end
// Minecraft has 143 colors, some of which have negative byte representations
@ -152,7 +150,7 @@ index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..84daffdda568039423d0f6d18981bb9d
}
/**
@@ -260,7 +362,7 @@ public final class MapPalette {
@@ -260,7 +360,7 @@ public final class MapPalette {
@Deprecated
@NotNull
public static Color getColor(byte index) {

View File

@ -1,100 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ivan Pekov <ivan@mrivanplays.com>
Date: Sat, 26 Dec 2020 12:14:12 +0200
Subject: [PATCH] fixup! Optimise Bukkit's MapPalette
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 7b704b5841aa2f212b70719cf9bd79b4fb21fcf0..2e4795be4eadd20c24e058850b6d35b6a6de1f3c 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -1837,5 +1837,6 @@ public final class Bukkit {
public static java.time.Duration getLastTickTime() {
return server.getLastTickTime();
}
+ public static java.awt.Color[] getVanillaMapColors() { return server.getVanillaMapColors(); }
// Yatopia end
}
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index 2f86ada4a487a6d59fa8b173c8339dcdda65cb30..0b5db06d26e69f7bff36ec12c5155634e2c56c37 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -1669,5 +1669,12 @@ public interface Server extends PluginMessageRecipient {
* @return duration
*/
java.time.Duration getLastTickTime();
+
+ /**
+ * Returns the vanilla declared map colors.
+ *
+ * @return vanilla map colors
+ */
+ java.awt.Color[] getVanillaMapColors();
// Yatopia end
}
diff --git a/src/main/java/org/bukkit/map/MapPalette.java b/src/main/java/org/bukkit/map/MapPalette.java
index 84daffdda568039423d0f6d18981bb9d9a392e1f..0bff82448b8c677027bd95d67d46c7526d720dcb 100644
--- a/src/main/java/org/bukkit/map/MapPalette.java
+++ b/src/main/java/org/bukkit/map/MapPalette.java
@@ -127,8 +127,8 @@ public final class MapPalette {
// getting an array of rgb values of each color
rgbColors = java.util.Arrays.stream(colors).mapToInt(Color::getRGB).toArray();
+ /*
try {
- // load net.minecraft.server.MaterialMapColor class. using reflection because build fails. maven thinks nms isn't accessible from Yatopia-API
Class<?> materialMapColorClass = MapPalette.class.getClassLoader().loadClass("net.minecraft.server.MaterialMapColor");
// get vanilla colors
Object[] vanillaColorObjects = (Object[]) java.util.Arrays.stream(materialMapColorClass.getFields()).filter(f -> f.getType().isArray() && f.getType().getComponentType().equals(materialMapColorClass)).findAny().get().get(null);
@@ -157,6 +157,19 @@ public final class MapPalette {
} catch (Exception ex) {
throw new RuntimeException(ex);
}
+ */
+ vanillaColors = org.bukkit.Bukkit.getVanillaMapColors();
+ doubleColors = new Color[vanillaColors.length][modifiers.length];
+ for (int i = 0; i < vanillaColors.length; i++) {
+ Color vanillaColor = vanillaColors[i];
+ for (int j = 0; j < modifiers.length; j++) {
+ int modifier = modifiers[j];
+ int mr = vanillaColor.getRed() * modifier / 255;
+ int mg = vanillaColor.getGreen() * modifier / 255;
+ int mb = vanillaColor.getBlue() * modifier / 255;
+ doubleColors[i][j] = c(mr, mg, mb);
+ }
+ }
}
// Yatopia end
@@ -299,9 +312,9 @@ public final class MapPalette {
public static byte matchColor(@NotNull Color color) {
if (color.getAlpha() < 128) return 0;
+ int index = 0;
// Yatopia start - foreach a maximum of 62 colors instead of 236 per pixel
/*
- int index = 0;
double best = -1;
for (int i = 4; i < colors.length; i++) {
@@ -313,7 +326,6 @@ public final class MapPalette {
}
*/
- int index = 0;
int best = -1;
for (int i = 0; i < vanillaColors.length; i++) {
@@ -342,10 +354,12 @@ public final class MapPalette {
}
index = com.google.common.primitives.Ints.indexOf(rgbColors, modColors[index].getRGB());
+ /*
// we have already checked if color is transparent so now there aren't any transparent colors
// and if (index < 4) then nearest color was black (hex #000000), so we need to return the most black color
if (index < 4)
index = 119;
+ */
// Yatopia end
// Minecraft has 143 colors, some of which have negative byte representations

View File

@ -1,36 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ivan Pekov <ivan@mrivanplays.com>
Date: Sat, 26 Dec 2020 12:17:22 +0200
Subject: [PATCH] Add a way to retrieve vanilla map colors
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
index 201d84401114864ab2f9665bda3fe09c30cc7107..1849b7204f1cbbf25811c9a4efc0a5f02b6507b4 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -269,6 +269,7 @@ public final class CraftServer implements Server {
private final List<CraftPlayer> playerView;
public int reloadCount;
public static Exception excessiveVelEx; // Paper - Velocity warnings
+ private java.awt.Color[] vanillaMapColors = null; // Yatopia
static {
ConfigurationSerialization.registerClass(CraftOfflinePlayer.class);
@@ -2473,4 +2474,17 @@ public final class CraftServer implements Server {
// Purpur end
@Override public java.time.Duration getLastTickTime() { return net.minecraft.server.MinecraftServer.lastTickTime; } // Yatopia
+
+ // Yatopia start
+ @Override
+ public java.awt.Color[] getVanillaMapColors() {
+ if (vanillaMapColors != null) {
+ return vanillaMapColors;
+ }
+ vanillaMapColors = java.util.Arrays.stream(net.minecraft.server.MaterialMapColor.a)
+ .map(c -> new java.awt.Color(c.rgb))
+ .toArray(java.awt.Color[]::new);
+ return vanillaMapColors;
+ }
+ // Yatopia end
}