diff --git a/pom.xml b/pom.xml index ea74716..43c1699 100644 --- a/pom.xml +++ b/pom.xml @@ -77,6 +77,12 @@ false + + + com.bergerkiller.bukkit.common + tech.sbdevelopment.mapreflectionapi.libs.bkcommonlib + + @@ -135,6 +141,9 @@ 11 ${maven.lombok.delombok-target} + **/com/bergerkiller/bukkit/common/io/*.java + **/com/bergerkiller/bukkit/common/map/*.java + **/com/bergerkiller/bukkit/common/map/color/*.java **/tech/sbdevelopment/mapreflectionapi/*.java **/tech/sbdevelopment/mapreflectionapi/utils/*.java **/tech/sbdevelopment/mapreflectionapi/listeners/*.java @@ -172,12 +181,6 @@ 1.19.1-R0.1-SNAPSHOT provided - - com.bergerkiller.bukkit - BKCommonLib - 1.19-v2 - provided - com.comphenix.protocol ProtocolLib @@ -190,5 +193,13 @@ 1.18.24 provided + + + + org.jetbrains + annotations-java5 + 23.0.0 + provided + \ No newline at end of file diff --git a/src/main/java/com/bergerkiller/bukkit/common/LICENSE b/src/main/java/com/bergerkiller/bukkit/common/LICENSE new file mode 100644 index 0000000..8643c4a --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/LICENSE @@ -0,0 +1,56 @@ +MIT License + +Copyright (C) 2013-2015 bergerkiller Copyright (C) 2016-2020 Berger Healer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, and/or sublicense the Software, +and to permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +========================================================================================== + +BKCommonLib utilizes Objensis and Javassist for some required functionality. Licenses are listed below: + +====================================== Objenesis ========================================= +http://objenesis.googlecode.com/svn/docs/index.html + +Copyright (c) 2003-2013, Objenesis Team and all contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +====================================== Javassist ========================================= + +The Source Code version of the Covered Code of Javassist is available under the +MOZILLA PUBLIC LICENSE Version 1.1. The project can be found (oct-2020) here: + +https://github.com/jboss-javassist/javassist + +Javassist was not modified for the purpose of this software. \ No newline at end of file diff --git a/src/main/java/com/bergerkiller/bukkit/common/README.md b/src/main/java/com/bergerkiller/bukkit/common/README.md new file mode 100644 index 0000000..ce9a09c --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/README.md @@ -0,0 +1,6 @@ +# BKCommonLib + +These classes are from [BKCommonLib](https://github.com/bergerhealer/BKCommonLib). Only the required classes and methods +are extracted. + +Current version: master#25ad90b702cabbbf632c40f6ed209241aee38a41 (1.19.1-v1 release) \ No newline at end of file diff --git a/src/main/java/com/bergerkiller/bukkit/common/io/BitInputStream.java b/src/main/java/com/bergerkiller/bukkit/common/io/BitInputStream.java new file mode 100644 index 0000000..706a4c9 --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/io/BitInputStream.java @@ -0,0 +1,104 @@ +/* + * This file is part of MapReflectionAPI. + * Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bergerkiller.bukkit.common.io; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Input Stream that can also read individual bits + */ +public class BitInputStream extends InputStream { + private int bitbuff = 0; + private int bitbuff_len = 0; + private boolean closed = false; + private final InputStream input; + private final boolean closeInput; + + /** + * Initializes a new Bit Input Stream, reading from the Input Stream specified + * + * @param inputStream to read from + */ + public BitInputStream(InputStream inputStream) { + this(inputStream, true); + } + + /** + * Initializes a new Bit Input Stream, reading from the Input Stream specified + * + * @param inputStream to read from + * @param closeInputStream whether to close the underlying input stream when closing this stream + */ + public BitInputStream(InputStream inputStream, boolean closeInputStream) { + this.input = inputStream; + this.closeInput = closeInputStream; + } + + @Override + public int available() throws IOException { + if (this.closed) { + throw new IOException("Stream is closed"); + } + return this.input.available(); + } + + @Override + public int read() throws IOException { + return readBits(8); + } + + /** + * Reads bits from the stream + * + * @param nBits to read + * @return read value, -1 when end of stream is reached + * @throws IOException + */ + public int readBits(int nBits) throws IOException { + if (this.closed) { + throw new IOException("Stream is closed"); + } + while (this.bitbuff_len < nBits) { + int readByte = this.input.read(); + if (readByte == -1) return -1; + this.bitbuff |= (readByte << this.bitbuff_len); + this.bitbuff_len += 8; + } + int result = bitbuff & ((1 << nBits) - 1); + this.bitbuff >>= nBits; + this.bitbuff_len -= nBits; + return result; + } + + @Override + public void close() throws IOException { + if (!this.closed) { + this.closed = true; + if (this.closeInput) { + this.input.close(); + } + } + } +} diff --git a/src/main/java/com/bergerkiller/bukkit/common/io/BitPacket.java b/src/main/java/com/bergerkiller/bukkit/common/io/BitPacket.java new file mode 100644 index 0000000..755779f --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/io/BitPacket.java @@ -0,0 +1,51 @@ +/* + * This file is part of MapReflectionAPI. + * Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bergerkiller.bukkit.common.io; + +import lombok.EqualsAndHashCode; +import lombok.ToString; + +/** + * Simple container for multiple bits of data. + */ +@EqualsAndHashCode +@ToString +public class BitPacket implements Cloneable { + public int data, bits; + + public BitPacket() { + this.data = 0; + this.bits = 0; + } + + public BitPacket(int data, int bits) { + this.data = data; + this.bits = bits; + } + + @Override + public BitPacket clone() { + return new BitPacket(this.data, this.bits); + } +} diff --git a/src/main/java/com/bergerkiller/bukkit/common/map/MapColorPalette.java b/src/main/java/com/bergerkiller/bukkit/common/map/MapColorPalette.java new file mode 100644 index 0000000..f2cde2c --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/map/MapColorPalette.java @@ -0,0 +1,237 @@ +/* + * This file is part of MapReflectionAPI. + * Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bergerkiller.bukkit.common.map; + +import com.bergerkiller.bukkit.common.map.color.MCSDBubbleFormat; +import com.bergerkiller.bukkit.common.map.color.MCSDGenBukkit; +import com.bergerkiller.bukkit.common.map.color.MapColorSpaceData; +import tech.sbdevelopment.mapreflectionapi.MapReflectionAPI; +import tech.sbdevelopment.mapreflectionapi.utils.ReflectionUtil; + +import java.awt.*; +import java.io.InputStream; +import java.util.Arrays; + +/** + * Additional functionality on top of Bukkit's MapPalette + */ +public class MapColorPalette { + private static final MapColorSpaceData COLOR_MAP_DATA; + public static final byte[] COLOR_MAP_AVERAGE = new byte[0x10000]; + public static final byte[] COLOR_MAP_ADD = new byte[0x10000]; + public static final byte[] COLOR_MAP_SUBTRACT = new byte[0x10000]; + public static final byte[] COLOR_MAP_MULTIPLY = new byte[0x10000]; + public static final byte[] COLOR_MAP_SPECULAR = new byte[0x10000]; + + public static final byte COLOR_TRANSPARENT = 0; + + static { + // We NEED java awt or all will just fail to work anyway. + try { + Class.forName("java.awt.Color"); + } catch (ClassNotFoundException e) { + MapReflectionAPI.getInstance().getLogger().severe("The Java AWT runtime library is not available"); + MapReflectionAPI.getInstance().getLogger().severe("This is usually because a headless JVM is used for the server"); + MapReflectionAPI.getInstance().getLogger().severe("Please install and configure a non-headless JVM to have Map Displays work"); + throw new UnsupportedOperationException("Map Displays require a non-headless JVM (Uses AWT)"); + } + + // Now we know java.awt exists we can initialize this one + COLOR_MAP_DATA = new MapColorSpaceData(); + + // Load color map data from the Bubble format file bundled with the library + { + boolean success = false; + MCSDBubbleFormat bubbleData = new MCSDBubbleFormat(); + try { + String bub_path_postfix; + if (ReflectionUtil.supports(17)) { + bub_path_postfix = "map_1_17.bub"; + } else if (ReflectionUtil.supports(16)) { + bub_path_postfix = "map_1_16.bub"; + } else if (ReflectionUtil.supports(12)) { + bub_path_postfix = "map_1_12.bub"; + } else { + bub_path_postfix = "map_1_8_8.bub"; + } + String bub_path = "/tech/sbdevelopment/mapreflectionapi/libs/bkcommonlib/internal/resources/map/" + bub_path_postfix; + InputStream input = MapColorPalette.class.getResourceAsStream(bub_path); + if (input != null) { + bubbleData.readFrom(input); + success = true; + } + } catch (Exception e) { + e.printStackTrace(); + } + + if (success) { + COLOR_MAP_DATA.readFrom(bubbleData); + } else { + MCSDGenBukkit bukkitGen = new MCSDGenBukkit(); + bukkitGen.generate(); + COLOR_MAP_DATA.readFrom(bukkitGen); + } + } + + // Generate 256 lightness values for all colors + for (int a = 0; a < 256; a++) { + int index = (a * 256); + Color color_a = getRealColor((byte) a); + if (color_a.getAlpha() < 128) { + // All specular colors for the transparent color are transparent + Arrays.fill(COLOR_MAP_SPECULAR, index, index + 256, COLOR_TRANSPARENT); + } else { + for (int b = 0; b < 256; b++) { + // 0.0 = black + // 1.0 = natural color + // 2.0 = white + float f = b / 128.0f; + int sr = (int) (color_a.getRed() * f); + int sg = (int) (color_a.getGreen() * f); + int sb = (int) (color_a.getBlue() * f); + COLOR_MAP_SPECULAR[index++] = getColor(sr, sg, sb); + } + } + } + + // Initialize the color map tables for all possible color values + for (int c1 = 0; c1 < 256; c1++) { + for (int c2 = 0; c2 < 256; c2++) { + initTable((byte) c1, (byte) c2); + } + } + } + + private static void initTable(byte color1, byte color2) { + int index = getMapIndex(color1, color2); + if (isTransparent(color1) || isTransparent(color2)) { + initTransparent(index, color2); + } else { + Color c1 = getRealColor(color1); + Color c2 = getRealColor(color2); + initColor( + index, + c1.getRed(), c1.getGreen(), c1.getBlue(), + c2.getRed(), c2.getGreen(), c2.getBlue() + ); + } + } + + private static void initTransparent(int index, byte color2) { + COLOR_MAP_AVERAGE[index] = color2; + COLOR_MAP_ADD[index] = color2; + COLOR_MAP_SUBTRACT[index] = color2; + COLOR_MAP_MULTIPLY[index] = (byte) 0; + } + + private static void initColor(int index, int r1, int g1, int b1, int r2, int g2, int b2) { + initArray(COLOR_MAP_AVERAGE, index, (r1 + r2) >> 1, (g1 + g2) >> 1, (b1 + b2) >> 1); + initArray(COLOR_MAP_ADD, index, (r1 + r2), (g1 + g2), (b1 + b2)); + initArray(COLOR_MAP_SUBTRACT, index, (r2 - r1), (g2 - g1), (b2 - b1)); + initArray(COLOR_MAP_MULTIPLY, index, (r1 * r2) / 255, (g1 * g2) / 255, (b1 * b2) / 255); + } + + private static void initArray(byte[] array, int index, int r, int g, int b) { + if (r < 0x00) r = 0x00; + if (r > 0xFF) r = 0xFF; + if (g < 0x00) g = 0x00; + if (g > 0xFF) g = 0xFF; + if (b < 0x00) b = 0x00; + if (b > 0xFF) b = 0xFF; + array[index] = getColor(r, g, b); + } + + /** + * Gets whether a particular color code is a transparent color. + * There are 4 transparent colors available. Usually value 0 is used. + * + * @param color value + * @return True if transparent + */ + public static boolean isTransparent(byte color) { + return (color & 0xFF) < 0x4; + } + + /** + * Gets the Minecraft map color code for an RGB color + * + * @param color input + * @return minecraft color + */ + public static byte getColor(Color color) { + if ((color.getAlpha() & 0x80) == 0) { + return COLOR_TRANSPARENT; + } else { + return COLOR_MAP_DATA.get(color.getRed(), color.getGreen(), color.getBlue()); + } + } + + /** + * Gets the Minecraft map color code for an RGB color + * + * @param r - red component + * @param g - green component + * @param b - blue component + * @return minecraft color + */ + public static byte getColor(int r, int g, int b) { + // This helps prevent dumb exceptions. + // Nobody likes random exceptions when all you're doing is color calculations + if (r < 0) + r = 0; + else if (r > 255) + r = 255; + if (g < 0) + g = 0; + else if (g > 255) + g = 255; + if (b < 0) + b = 0; + else if (b > 255) + b = 255; + + return COLOR_MAP_DATA.get(r, g, b); + } + + /** + * Gets the index into one of the palette remap arrays + * + * @param color_a first color + * @param color_b second color + * @return index + */ + public static int getMapIndex(byte color_a, byte color_b) { + return (color_a & 0xFF) | ((color_b & 0xFF) << 8); + } + + /** + * Gets the real RGB color belonging to a color code + * + * @param color code input + * @return real RGB color + */ + public static Color getRealColor(byte color) { + return COLOR_MAP_DATA.getColor(color); + } +} diff --git a/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDBubbleFormat.java b/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDBubbleFormat.java new file mode 100644 index 0000000..5928bf5 --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDBubbleFormat.java @@ -0,0 +1,224 @@ +/* + * This file is part of MapReflectionAPI. + * Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bergerkiller.bukkit.common.map.color; + +import com.bergerkiller.bukkit.common.io.BitInputStream; + +import java.awt.*; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.zip.InflaterInputStream; + +/** + * Stores all map color space information in a highly compressed bubble format. + * In this format it is assumed the color data is in cell shapes. It stores the cell + * borders separate from the colors using the {@link MCSDWebbingCodec}. These cells + * are then filled with colors to reproduce the original image. + */ +public class MCSDBubbleFormat extends MapColorSpaceData { + public final boolean[][] strands = new boolean[256][256 * 256]; + public final List bubbles = new ArrayList<>(); + + public void readFrom(InputStream stream) throws IOException { + try (BitInputStream bitStream = new BitInputStream(new InflaterInputStream(stream))) { + // Read all color RGB values + for (int i = 0; i < 256; i++) { + int r = bitStream.read(); + int g = bitStream.read(); + int b = bitStream.read(); + int a = bitStream.read(); + this.setColor((byte) i, new Color(r, g, b, a)); + } + + // Read all bubbles from the stream + while (true) { + Bubble bubble = new Bubble(); + bubble.color = (byte) bitStream.read(); + if (bubble.color == 0) { + break; + } + bubble.x = bitStream.read(); + bubble.y = bitStream.read(); + bubble.z_min = bitStream.read(); + bubble.z_max = bubble.z_min + bitStream.read(); + this.bubbles.add(bubble); + } + + // Read bubble boundary information from the stream + MCSDWebbingCodec codec = new MCSDWebbingCodec(); + for (int z = 0; z < 256; z++) { + Arrays.fill(this.strands[z], false); + codec.reset(strands[z], false); + while (codec.readNext(bitStream)) ; + } + + // Initialize the colors with the bubble colors + this.initColors(); + + // Read color correction data for pixels unset (value = 0) + for (int i = 0; i < (1 << 24); i++) { + if (this.get(i) == 0) { + if (bitStream.readBits(1) == 0) { + this.set(i, this.get(i - 1)); + } else { + int mode = bitStream.readBits(2); + if (mode == 0) { + this.set(i, this.get(i - 256)); + } else if (mode == 1) { + this.set(i, this.get(i + 1)); + } else if (mode == 2) { + this.set(i, this.get(i + 256)); + } else { + this.set(i, (byte) bitStream.readBits(8)); + } + } + } + } + } + } + + private void initColors() { + // Set initial cell colors + this.clearRGBData(); + for (Bubble cell : bubbles) { + for (int z = cell.z_min; z <= cell.z_max; z++) { + this.set(cell.x, cell.y, z, cell.color); + } + } + spreadColors(); + } + + private void spreadColors() { + final boolean[] all_strands = new boolean[1 << 24]; + for (int z = 0; z < 256; z++) { + System.arraycopy(this.strands[z], 0, all_strands, z << 16, 1 << 16); + } + + boolean mode = false; + boolean hasChanges; + do { + hasChanges = false; + + // Alternate the direction in which we process every step + // This prevents really slow filling when the direction is 'wrong' + // The below logic is partially based on the light fixing algorithm in Light Cleaner + final int index_end, index_delta; + int index; + byte color; + if (mode = !mode) { + index_delta = 1; + index = 0; + index_end = (1 << 24); + } else { + index_delta = -1; + index = (1 << 24) - 1; + index_end = 0; + } + do { + if (!all_strands[index]) { + all_strands[index] = true; + + if ((index & 0xFF) < 0xFF) { + if ((color = this.get(index + 1)) != 0) { + this.set(index, color); + hasChanges = true; + } else if ((color = this.get(index)) != 0) { + this.set(index + 1, color); + hasChanges = true; + } else { + all_strands[index] = false; // retry + } + } + + if ((index & 0xFF00) < 0xFF00) { + if ((color = this.get(index + 256)) != 0) { + this.set(index, color); + hasChanges = true; + } else if ((color = this.get(index)) != 0) { + this.set(index + 256, color); + hasChanges = true; + } else { + all_strands[index] = false; // retry + } + } + } + } while ((index += index_delta) != index_end); + } while (hasChanges); + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (o instanceof MCSDBubbleFormat) { + MCSDBubbleFormat other = (MCSDBubbleFormat) o; + for (int i = 0; i < strands.length; i++) { + if (other.strands[i] != this.strands[i]) { + return false; + } + } + if (bubbles.size() != other.bubbles.size()) { + return false; + } + for (int i = 0; i < bubbles.size(); i++) { + if (!bubbles.get(i).equals(other.bubbles.get(i))) { + return false; + } + } + return true; + } else { + return false; + } + } + + public static class Bubble { + public int x, y; + public int z_min; + public int z_max; + public byte color; + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } else if (o instanceof Bubble) { + Bubble other = (Bubble) o; + return other.x == x && other.y == y && + other.z_min == z_min && other.z_max == z_max && + other.color == color; + } else { + return false; + } + } + + @Override + public String toString() { + return "cell{x=" + x + ", y=" + y + ", zmin=" + z_min + ", zmax=" + z_max + ", color=" + (color & 0xFF) + "}"; + } + } + +} diff --git a/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDGenBukkit.java b/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDGenBukkit.java new file mode 100644 index 0000000..77b67c1 --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDGenBukkit.java @@ -0,0 +1,50 @@ +/* + * This file is part of MapReflectionAPI. + * Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bergerkiller.bukkit.common.map.color; + +import org.bukkit.map.MapPalette; + +public class MCSDGenBukkit extends MapColorSpaceData { + + /** + * Generates the color map information by using Bukkit's algorithms. + */ + @SuppressWarnings("deprecation") + public void generate() { + this.clear(); + for (int i = 0; i < 256; i++) { + try { + setColor((byte) i, MapPalette.getColor((byte) i)); + } catch (Exception ignored) { + } + } + for (int r = 0; r < 256; r++) { + for (int g = 0; g < 256; g++) { + for (int b = 0; b < 256; b++) { + set(r, g, b, MapPalette.matchColor(r, g, b)); + } + } + } + } +} diff --git a/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDWebbingCodec.java b/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDWebbingCodec.java new file mode 100644 index 0000000..752cb07 --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/map/color/MCSDWebbingCodec.java @@ -0,0 +1,128 @@ +/* + * This file is part of MapReflectionAPI. + * Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bergerkiller.bukkit.common.map.color; + +import com.bergerkiller.bukkit.common.io.BitInputStream; +import com.bergerkiller.bukkit.common.io.BitPacket; + +import java.io.IOException; + +/** + * Encodes or decodes a 256x256 grid of booleans by walking down the connected lines and encoding them + * using drawing instructions. For example, a diagonal line in the grid may be encoded as follows: + * + *

+ * For encoding the data, the follow bits are written out in sequence: + *

+ */ +public class MCSDWebbingCodec { + private int last_x, last_y; + private int last_dx, last_dy; + public boolean[] strands = new boolean[1 << 16]; + private final BitPacket[] packets = new BitPacket[1024]; + + public MCSDWebbingCodec() { + for (int i = 0; i < this.packets.length; i++) { + this.packets[i] = new BitPacket(); + } + } + + public void reset(boolean[] cells, boolean copyCells) { + if (copyCells) { + System.arraycopy(cells, 0, this.strands, 0, cells.length); + } else { + this.strands = cells; + } + this.last_x = -1000; + this.last_y = -1000; + this.last_dx = 1; + this.last_dy = 1; + } + + public boolean readNext(BitInputStream stream) throws IOException { + int op = stream.readBits(2); + if (op == 0b11) { + if (stream.readBits(1) == 1) { + // Set DX/DY increment/decrement + int sub = stream.readBits(2); + if (sub == 0b00) { + last_dx = -1; + } else if (sub == 0b01) { + last_dx = 1; + } else if (sub == 0b10) { + last_dy = -1; + } else if (sub == 0b11) { + last_dy = 1; + } + } else { + // Command codes + if (stream.readBits(1) == 1) { + // End of slice + return false; + } else { + // Reset position + last_x = stream.readBits(8); + last_y = stream.readBits(8); + strands[last_x | (last_y << 8)] = true; + } + } + } else { + // Write next pixel + if (op == 0b00) { + last_x += last_dx; + } else if (op == 0b01) { + last_y += last_dy; + } else if (op == 0b10) { + last_x += last_dx; + last_y += last_dy; + } else if (op == -1) { + // End of stream + return false; + } + strands[last_x | (last_y << 8)] = true; + } + return true; + } +} diff --git a/src/main/java/com/bergerkiller/bukkit/common/map/color/MapColorSpaceData.java b/src/main/java/com/bergerkiller/bukkit/common/map/color/MapColorSpaceData.java new file mode 100644 index 0000000..3c5e338 --- /dev/null +++ b/src/main/java/com/bergerkiller/bukkit/common/map/color/MapColorSpaceData.java @@ -0,0 +1,160 @@ +/* + * This file is part of MapReflectionAPI. + * Copyright (c) 2022 inventivetalent / SBDevelopment - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.bergerkiller.bukkit.common.map.color; + +import java.awt.*; +import java.util.Arrays; + +/** + * Stores the raw map color space data, enabling transformation between different storage methods. + */ +public class MapColorSpaceData implements Cloneable { + private final Color[] colors = new Color[256]; + private final byte[] data = new byte[1 << 24]; + + public MapColorSpaceData() { + Arrays.fill(this.colors, new Color(0, 0, 0, 0)); + } + + /** + * Clears only the RGB data. Equivalent to using {@link #set(int, byte)} on all RGB colors. + */ + public final void clearRGBData() { + Arrays.fill(this.data, (byte) 0); + } + + /** + * Clears all data, setting all colors to transparent + */ + public final void clear() { + Arrays.fill(this.colors, new Color(0, 0, 0, 0)); + Arrays.fill(this.data, (byte) 0); + } + + /** + * Sets all color data of this color space data to that from the input color space data + * + * @param data to set + */ + public void readFrom(MapColorSpaceData data) { + System.arraycopy(data.data, 0, this.data, 0, this.data.length); + System.arraycopy(data.colors, 0, this.colors, 0, this.colors.length); + } + + /** + * Sets a single map palette color + * + * @param code of the color + * @param color to set to + */ + public final void setColor(byte code, Color color) { + this.colors[code & 0xFF] = color; + } + + /** + * Gets a single map palette color + * + * @param code of the color + * @return map palette color + */ + public final Color getColor(byte code) { + return this.colors[code & 0xFF]; + } + + /** + * Sets the map color code value for an rgb value + * + * @param r component + * @param g component + * @param b component + * @param code to set to + */ + public final void set(int r, int g, int b, byte code) { + this.data[getDataIndex(r, g, b)] = code; + } + + /** + * Gets the map color code value for an rgb value + * + * @param r component + * @param g component + * @param b component + * @return color code + */ + public final byte get(int r, int g, int b) { + return this.data[getDataIndex(r, g, b)]; + } + + /** + * Sets the map color code for an rgb value + * + * @param index rgb compound value + * @param code to set to + */ + public final void set(int index, byte code) { + this.data[index] = code; + } + + /** + * Gets the map color code for an rgb value + * + * @param index rgb compound value + * @return color code + */ + public final byte get(int index) { + return this.data[index]; + } + + @Override + public MapColorSpaceData clone() { + MapColorSpaceData clone = new MapColorSpaceData(); + System.arraycopy(this.colors, 0, clone.colors, 0, this.colors.length); + System.arraycopy(this.data, 0, clone.data, 0, this.data.length); + return clone; + } + + /** + * Gets the mapping index of an rgb value + * + * @param r component + * @param g component + * @param b component + * @return index + */ + private static int getDataIndex(byte r, byte g, byte b) { + return (r & 0xFF) + ((g & 0xFF) << 8) + ((b & 0xFF) << 16); + } + + /** + * Gets the mapping index of an rgb value + * + * @param r component + * @param g component + * @param b component + * @return index + */ + private static int getDataIndex(int r, int g, int b) { + return (r & 0xFF) + ((g & 0xFF) << 8) + ((b & 0xFF) << 16); + } +} diff --git a/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_12.bub b/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_12.bub new file mode 100644 index 0000000..23ee24d Binary files /dev/null and b/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_12.bub differ diff --git a/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_16.bub b/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_16.bub new file mode 100644 index 0000000..3d25c20 Binary files /dev/null and b/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_16.bub differ diff --git a/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_17.bub b/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_17.bub new file mode 100644 index 0000000..63334a9 Binary files /dev/null and b/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_17.bub differ diff --git a/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_8_8.bub b/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_8_8.bub new file mode 100644 index 0000000..1e22e3a Binary files /dev/null and b/src/main/resources/com/bergerkiller/bukkit/common/internal/resources/map/map_1_8_8.bub differ diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 5d3f2fc..9418e75 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -5,7 +5,7 @@ api-version: 1.13 authors: [ inventivetalent, SBDeveloper ] description: This API helps developer with viewing images on maps. website: https://sbdevelopment.tech -softdepend: [ BKCommonLib, ProtocolLib ] +softdepend: [ ProtocolLib ] commands: mapmanager: description: The main command of MapManager.