chatValue = new HashMap<>();
13 |
14 | public static String BLACK_BAR = Character.toString((char) 0x2588);
15 | private static String bol = (ChatColor.BOLD + "");
16 |
17 | static {
18 |
19 | chatValue.put(ChatColor.BLACK + bol + "#", new RGBValue(new Color(0, 0,
20 | 0)));
21 | addColor("C", new double[]{155, 53, 128, 56}, new double[]{155,
22 | 53, 128, 56}, new double[]{155, 53, 128, 56});
23 | addColor("$", new double[]{108, 94, 121, 94}, new double[]{108,
24 | 94, 121, 94}, new double[]{108, 94, 121, 94});
25 | addColor("X", new double[]{105, 86, 93, 76}, new double[]{105, 86,
26 | 93, 76}, new double[]{105, 86, 93, 76});
27 | addColor("M", new double[]{151, 125, 93, 80}, new double[]{151,
28 | 125, 93, 80}, new double[]{151, 125, 93, 80});
29 | addColor("O", new double[]{138, 107, 104, 93}, new double[]{138,
30 | 107, 104, 93}, new double[]{138, 107, 104, 93});
31 | addColor("0", new double[]{159, 131, 127, 102}, new double[]{159,
32 | 131, 127, 102}, new double[]{159, 131, 127, 102});
33 | addColor("/", new double[]{36, 84, 82, 10}, new double[]{36, 84,
34 | 82, 10}, new double[]{36, 84, 82, 10});
35 | addColor("\\", new double[]{84, 36, 10, 82}, new double[]{84, 36,
36 | 10, 82}, new double[]{84, 36, 10, 82});
37 | addColor("_", new double[]{0, 0, 63, 51}, new double[]{0, 0, 63,
38 | 51}, new double[]{0, 0, 63, 51});
39 | addColor("P", new double[]{201, 84, 94, 0}, new double[]{201, 84,
40 | 94, 0}, new double[]{201, 84, 94, 0});
41 | addColor("a", new double[]{63, 63, 110, 150}, new double[]{63, 63,
42 | 110, 150}, new double[]{63, 63, 110, 150});
43 | addColor("=", new double[]{80, 65, 80, 75}, new double[]{80, 65,
44 | 80, 75}, new double[]{80, 65, 80, 75});
45 | addColor("-", new double[]{44, 33, 48, 43}, new double[]{44, 33,
46 | 48, 43}, new double[]{44, 33, 48, 43});
47 | addColor("J", new double[]{0, 90, 63, 90}, new double[]{0, 90, 63,
48 | 90}, new double[]{0, 90, 63, 90});
49 | addColor("U", new double[]{110, 90, 100, 90}, new double[]{110,
50 | 90, 100, 90}, new double[]{110, 90, 100, 90});
51 | addColor("T", new double[]{115, 95, 40, 30}, new double[]{115, 95,
52 | 40, 30}, new double[]{115, 95, 40, 30});
53 | addColor("E", new double[]{191, 60, 130, 35}, new double[]{191,
54 | 60, 130, 35}, new double[]{191, 60, 130, 35});
55 | addColor("8", new double[]{144, 111, 105, 90}, new double[]{144,
56 | 111, 105, 90}, new double[]{144, 111, 105, 90});
57 | addColor("4", new double[]{90, 147, 70, 105}, new double[]{90,
58 | 147, 70, 105}, new double[]{90, 147, 70, 105});
59 | addColor("2", new double[]{98, 104, 118, 105}, new double[]{98,
60 | 104, 118, 105}, new double[]{98, 104, 118, 105});
61 | addColor("6", new double[]{135, 65, 126, 91}, new double[]{135,
62 | 65, 126, 91}, new double[]{135, 65, 126, 91});
63 | addColor("u", new double[]{66, 48, 111, 124}, new double[]{66, 48,
64 | 111, 124}, new double[]{66, 48, 111, 124});
65 | addColor("Y", new double[]{80, 70, 44, 35}, new double[]{80, 70,
66 | 44, 35}, new double[]{80, 70, 44, 35});
67 | addColor("I!", new double[]{136, 123, 113, 78}, new double[]{136,
68 | 123, 113, 78}, new double[]{136, 123, 113, 78});
69 | addColor("!I", new double[]{156, 116, 102, 104}, new double[]{156,
70 | 116, 102, 104}, new double[]{156, 116, 102, 104});
71 | addColor("h", new double[]{150, 50, 108, 88}, new double[]{150,
72 | 50, 108, 88}, new double[]{150, 50, 108, 88});
73 | addColor("H", new double[]{155, 125, 100, 85}, new double[]{155,
74 | 125, 100, 85}, new double[]{155, 125, 100, 85});
75 | addColor("x", new double[]{50, 50, 90, 71}, new double[]{50, 50,
76 | 90, 71}, new double[]{50, 50, 90, 71});
77 | addColor("q", new double[]{80, 80, 70, 135}, new double[]{80, 80,
78 | 70, 135}, new double[]{80, 80, 70, 135});
79 | addColor("![", new double[]{175, 81, 117, 77}, new double[]{175,
80 | 81, 117, 77}, new double[]{175, 81, 117, 77});
81 | addColor("]!", new double[]{105, 146, 90, 191}, new double[]{105,
82 | 146, 90, 191}, new double[]{105, 146, 90, 191});
83 | addColor("S", new double[]{117, 125, 68, 94}, new double[]{117,
84 | 125, 68, 94}, new double[]{117, 125, 68, 94});
85 | addColor("N", new double[]{146, 116, 100, 90}, new double[]{146,
86 | 116, 100, 90}, new double[]{146, 116, 100, 90});
87 | addColor("n", new double[]{106, 58, 107, 86}, new double[]{106,
88 | 58, 107, 86}, new double[]{106, 58, 107, 86});
89 | addColor("b", new double[]{146, 50, 148, 99}, new double[]{146,
90 | 50, 148, 99}, new double[]{146, 50, 148, 99});
91 | addColor("B", new double[]{197, 109, 133, 91}, new double[]{197,
92 | 109, 133, 91}, new double[]{197, 109, 133, 91});
93 | addColor("w", new double[]{67, 52, 133, 141}, new double[]{67, 52,
94 | 133, 141}, new double[]{67, 52, 133, 141});
95 | addColor("W", new double[]{121, 100, 133, 105}, new double[]{121,
96 | 100, 133, 105}, new double[]{121, 100, 133, 105});
97 | addColor("#", new double[]{178, 136, 132, 116}, new double[]{178,
98 | 136, 132, 116}, new double[]{178, 136, 132, 116});
99 | addColor("f", new double[]{129, 114, 82, 33}, new double[]{129,
100 | 114, 82, 33}, new double[]{129, 114, 82, 33});
101 | addColor("F", new double[]{194, 68, 92, 0}, new double[]{194, 68,
102 | 92, 0}, new double[]{194, 68, 92, 0});
103 | addColor("d", new double[]{80, 122, 112, 124}, new double[]{80,
104 | 122, 112, 124}, new double[]{80, 122, 112, 124});
105 | addColor("D", new double[]{153, 101, 120, 84}, new double[]{153,
106 | 101, 120, 84}, new double[]{153, 101, 120, 84});
107 | addColor("A", new double[]{174, 137, 94, 80}, new double[]{174,
108 | 137, 94, 80}, new double[]{174, 137, 94, 80});
109 | addColor("k", new double[]{162, 32, 146, 55}, new double[]{162,
110 | 32, 146, 55}, new double[]{162, 32, 146, 55});
111 | addColor("K", new double[]{157, 75, 100, 70}, new double[]{157,
112 | 75, 100, 70}, new double[]{157, 75, 100, 70});
113 | addColor("v", new double[]{62, 50, 80, 71}, new double[]{62, 50,
114 | 80, 71}, new double[]{62, 50, 80, 71});
115 | addColor("V", new double[]{119, 98, 79, 70}, new double[]{119, 98,
116 | 79, 70}, new double[]{119, 98, 79, 70});
117 | addColor("L", new double[]{113, 0, 133, 54}, new double[]{113, 0,
118 | 133, 54}, new double[]{113, 0, 133, 54});
119 | addColor("ll", new double[]{144, 82, 101, 75}, new double[]{144,
120 | 82, 101, 75}, new double[]{144, 82, 101, 75});
121 | addColor("1", new double[]{73, 42, 99, 83}, new double[]{73, 42,
122 | 99, 83}, new double[]{73, 42, 99, 83});
123 | }
124 |
125 | private static void addColor(String ch, double[] r, double[] g, double[] b) {
126 | double[] c = new double[4 * 3];
127 | for (int k = 0; k < 4; k++) {
128 | c[(k * 3) + 0] = r[k] / 255;
129 | c[(k * 3) + 1] = g[k] / 255;
130 | c[(k * 3) + 2] = b[k] / 255;
131 | }
132 |
133 | int[][] mods = {{0, 0, 0}, // black 0
134 | {0, 0, 170},// darkblue 1
135 | {0, 170, 0}, // dark green 2
136 | {0, 170, 170}, // cyan 3
137 | {170, 0, 0}, // dark red 4
138 | {170, 0, 170}, // purple 5
139 | {255, 170, 0}, // gold 6
140 | {170, 170, 170}, // light gray 7
141 | {85, 85, 85}, // dark gray 8
142 | {85, 85, 255}, // blue 9
143 | {85, 255, 85},// green a
144 | {85, 255, 255}, // light blue b
145 | {255, 85, 85}, // red c
146 | {255, 85, 255}, // light pink d
147 | {255, 255, 85}, // yellow e
148 | {300, 300, 300} // White +45 F //{ 255, 255, 255 } // white f
149 | };
150 |
151 | int cm = 0;
152 | for (ChatColor cc : ChatColor.values()) {
153 | if (cc == ChatColor.BOLD || cc == ChatColor.STRIKETHROUGH
154 | || cc == ChatColor.RESET || cc == ChatColor.ITALIC
155 | || cc == ChatColor.MAGIC || cc == ChatColor.UNDERLINE)
156 | continue;
157 | int x = 0;
158 | chatValue.put(cc + bol + ch, new RGBValue(new Color((c[x]
159 | * mods[cm][x % 3] > 255) ? 255
160 | : (int) (c[x] * mods[cm][x++ % 3]),
161 | (c[x] * mods[cm][x % 3] > 255) ? 255
162 | : (int) (c[x] * mods[cm][x++ % 3]), (c[x]
163 | * mods[cm][x % 3] > 255) ? 255
164 | : (int) (c[x] * mods[cm][x++ % 3])), new Color(
165 | (c[x] * mods[cm][x % 3] > 255) ? 255
166 | : (int) (c[x] * mods[cm][x++ % 3]), (c[x]
167 | * mods[cm][x % 3] > 255) ? 255
168 | : (int) (c[x] * mods[cm][x++ % 3]), (c[x]
169 | * mods[cm][x % 3] > 255) ? 255
170 | : (int) (c[x] * mods[cm][x++ % 3])), new Color(
171 | (c[x] * mods[cm][x % 3] > 255) ? 255
172 | : (int) (c[x] * mods[cm][x++ % 3]), (c[x]
173 | * mods[cm][x % 3] > 255) ? 255
174 | : (int) (c[x] * mods[cm][x++ % 3]), (c[x]
175 | * mods[cm][x % 3] > 255) ? 255
176 | : (int) (c[x] * mods[cm][x++ % 3])), new Color(
177 | (c[x] * mods[cm][x % 3] > 255) ? 255
178 | : (int) (c[x] * mods[cm][x++ % 3]), (c[x]
179 | * mods[cm][x % 3] > 255) ? 255
180 | : (int) (c[x] * mods[cm][x++ % 3]), (c[x]
181 | * mods[cm][x % 3] > 255) ? 255
182 | : (int) (c[x] * mods[cm][x++ % 3]))));
183 | cm++;
184 | }
185 | }
186 |
187 | /**
188 | * The color value of the four closest colors. Use this if you want to
189 | * preserve hard edges in images. For the array, you need four color values.
190 | * Use the following chart to understand which pixel should be at which
191 | * index:
192 | *
193 | * | 0 | 1 |
194 | *
195 | * |---|---|
196 | *
197 | * | 2 | 3 |
198 | *
199 | * @param c - The color value
200 | * @return The closest material and durability.
201 | */
202 | public static String getClosestBlockValue(Color[] c) {
203 |
204 | int[] r = new int[4];
205 | int[] b = new int[4];
206 | int[] g = new int[4];
207 | for (int i = 0; i < c.length; i++) {
208 | r[i] = c[i].getRed();
209 | b[i] = c[i].getBlue();
210 | g[i] = c[i].getGreen();
211 | }
212 |
213 | double cR = Integer.MAX_VALUE;
214 | double cG = Integer.MAX_VALUE;
215 | double cB = Integer.MAX_VALUE;
216 |
217 | String closest = null;
218 |
219 | double[] tR = new double[4];
220 | double[] tG = new double[4];
221 | double[] tB = new double[4];
222 | for (Entry entry : chatValue.entrySet()) {
223 | for (int i = 0; i < 4; i++) {
224 | tR[i] = entry.getValue().r[i] - r[i];
225 | tG[i] = entry.getValue().g[i] - g[i];
226 | tB[i] = entry.getValue().b[i] - b[i];
227 | if (tR[i] < 0)
228 | tR[i] = -tR[i];
229 | if (tG[i] < 0)
230 | tG[i] = -tG[i];
231 | if (tB[i] < 0)
232 | tB[i] = -tB[i];
233 | }
234 | if ((tR[0] * tR[0]) + (tG[0] * tG[0]) + (tB[0] * tB[0])
235 | + (tR[1] * tR[1]) + (tG[1] * tG[1]) + (tB[1] * tB[1])
236 | + (tR[2] * tR[2]) + (tG[2] * tG[2]) + (tB[2] * tB[2])
237 | + (tR[3] * tR[3]) + (tG[3] * tG[3]) + (tB[3] * tB[3]) < (cR
238 | + cG + cB)) {
239 | cR = 0;
240 | cB = 0;
241 | cG = 0;
242 | for (int i = 0; i < 4; i++) {
243 | cR += (tR[i] * tR[i]);
244 | cG += (tG[i] * tG[i]);
245 | cB += (tB[i] * tB[i]);
246 | }
247 | closest = entry.getKey();
248 | }
249 | }
250 | return closest;
251 | }
252 | }
253 |
--------------------------------------------------------------------------------
/src/me/zombie_striker/pixelprinter/util/ReflectionUtil.java:
--------------------------------------------------------------------------------
1 | package me.zombie_striker.pixelprinter.util;
2 |
3 | import org.bukkit.Bukkit;
4 |
5 | import java.lang.reflect.Constructor;
6 | import java.lang.reflect.Field;
7 | import java.lang.reflect.InvocationTargetException;
8 | import java.lang.reflect.Method;
9 | import java.util.Optional;
10 |
11 | /**
12 | * A small help with reflection
13 | */
14 | public class ReflectionUtil {
15 | private static final String SERVER_VERSION;
16 |
17 |
18 | static {
19 | String name = Bukkit.getServer().getClass().getName();
20 | name = name.substring(name.indexOf("craftbukkit.")
21 | + "craftbukkit.".length());
22 | name = name.substring(0, name.indexOf("."));
23 | SERVER_VERSION = name;
24 | }
25 |
26 | public static boolean isVersionHigherThan(int mainVersion,
27 | int secondVersion) {
28 | String firstChar = SERVER_VERSION.substring(1, 2);
29 | int fInt = Integer.parseInt(firstChar);
30 | if (fInt < mainVersion)
31 | return false;
32 | StringBuilder secondChar = new StringBuilder();
33 | for (int i = 3; i < 10; i++) {
34 | if (SERVER_VERSION.charAt(i) == '_'
35 | || SERVER_VERSION.charAt(i) == '.')
36 | break;
37 | secondChar.append(SERVER_VERSION.charAt(i));
38 | }
39 | int sInt = Integer.parseInt(secondChar.toString());
40 | return sInt >= secondVersion;
41 | }
42 |
43 | /**
44 | * Returns the NMS class.
45 | *
46 | * @param name The name of the class
47 | * @return The NMS class or null if an error occurred
48 | */
49 | public static Class> getNMSClass(String name) {
50 | try {
51 | return Class.forName("net.minecraft.server." + SERVER_VERSION
52 | + "." + name);
53 | } catch (ClassNotFoundException e) {
54 | e.printStackTrace();
55 | return null;
56 | }
57 | }
58 |
59 | /**
60 | * Returns the CraftBukkit class.
61 | *
62 | * @param name The name of the class
63 | * @return The CraftBukkit class or null if an error occurred
64 | */
65 |
66 | public static Class> getCraftbukkitClass(String name,
67 | String packageName) {
68 | try {
69 | return Class.forName("org.bukkit.craftbukkit." + SERVER_VERSION
70 | + "." + packageName + "." + name);
71 | } catch (ClassNotFoundException e) {
72 | e.printStackTrace();
73 | return null;
74 | }
75 | }
76 |
77 | /**
78 | * Returns the CraftBukkit class.
79 | *
80 | * @param name The name of the class
81 | * @return The CraftBukkit class or null if an error occurred
82 | */
83 |
84 | public static Class> getCraftbukkitClass(String name) {
85 | try {
86 | return Class.forName("org.bukkit.craftbukkit." + SERVER_VERSION
87 | + "." + name);
88 | } catch (ClassNotFoundException e) {
89 | e.printStackTrace();
90 | return null;
91 | }
92 | }
93 |
94 | /**
95 | * Returns the mojang.authlib class.
96 | *
97 | * @param name The name of the class
98 | * @return The mojang.authlib class or null if an error occurred
99 | */
100 |
101 | public static Class> getMojangAuthClass(String name) {
102 | try {
103 | return Class.forName("com.mojang.authlib." + name);
104 | } catch (ClassNotFoundException e) {
105 | e.printStackTrace();
106 | return null;
107 | }
108 | }
109 |
110 | /**
111 | * Invokes the method
112 | *
113 | * @param handle The handle to invoke it on
114 | * @param methodName The name of the method
115 | * @param parameterClasses The parameter types
116 | * @param args The arguments
117 | * @return The resulting object or null if an error occurred / the
118 | * method didn't return a thing
119 | */
120 | @SuppressWarnings("rawtypes")
121 | public static Object invokeMethod(Object handle, String methodName,
122 | Class[] parameterClasses, Object... args) {
123 | return invokeMethod(handle.getClass(), handle, methodName,
124 | parameterClasses, args);
125 | }
126 |
127 | /**
128 | * Invokes the method
129 | *
130 | * @param clazz The class to invoke it from
131 | * @param handle The handle to invoke it on
132 | * @param methodName The name of the method
133 | * @param parameterClasses The parameter types
134 | * @param args The arguments
135 | * @return The resulting object or null if an error occurred / the
136 | * method didn't return a thing
137 | */
138 | @SuppressWarnings("rawtypes")
139 | public static Object invokeMethod(Class> clazz, Object handle,
140 | String methodName, Class[] parameterClasses, Object... args) {
141 | Optional methodOptional = getMethod(clazz, methodName,
142 | parameterClasses);
143 |
144 | if (!methodOptional.isPresent()) {
145 | return null;
146 | }
147 |
148 | Method method = methodOptional.get();
149 |
150 | try {
151 | return method.invoke(handle, args);
152 | } catch (IllegalAccessException | InvocationTargetException e) {
153 | e.printStackTrace();
154 | }
155 | return null;
156 | }
157 |
158 | /**
159 | * Sets the value of an instance field
160 | *
161 | * @param handle The handle to invoke it on
162 | * @param name The name of the field
163 | * @param value The new value of the field
164 | */
165 | @SuppressWarnings("deprecation")
166 | public static void setInstanceField(Object handle, String name,
167 | Object value) {
168 | Class> clazz = handle.getClass();
169 | Optional fieldOptional = getField(clazz, name);
170 | if (!fieldOptional.isPresent()) {
171 | return;
172 | }
173 |
174 | Field field = fieldOptional.get();
175 | if (!field.isAccessible()) {
176 | field.setAccessible(true);
177 | }
178 | try {
179 | field.set(handle, value);
180 | } catch (IllegalAccessException e) {
181 | e.printStackTrace();
182 | }
183 | }
184 |
185 | /**
186 | * Sets the value of an instance field
187 | *
188 | * @param handle The handle to invoke it on
189 | * @param name The name of the field
190 | * @return The result
191 | */
192 | @SuppressWarnings("deprecation")
193 | public static Object getInstanceField(Object handle, String name) {
194 | Class> clazz = handle.getClass();
195 | Optional fieldOptional = getField(clazz, name);
196 | if (!fieldOptional.isPresent()) {
197 | return handle;
198 | }
199 | Field field = fieldOptional.get();
200 | if (!field.isAccessible()) {
201 | field.setAccessible(true);
202 | }
203 | try {
204 | return field.get(handle);
205 | } catch (IllegalAccessException e) {
206 | e.printStackTrace();
207 | }
208 | return null;
209 | }
210 |
211 | /**
212 | * Returns an enum constant
213 | *
214 | * @param enumClass The class of the enum
215 | * @param name The name of the enum constant
216 | * @return The enum entry or null
217 | */
218 | public static Object getEnumConstant(Class> enumClass, String name) {
219 | if (!enumClass.isEnum()) {
220 | return null;
221 | }
222 | for (Object o : enumClass.getEnumConstants()) {
223 | if (name.equals(invokeMethod(o, "name", new Class[0]))) {
224 | return o;
225 | }
226 | }
227 | return null;
228 | }
229 |
230 | /**
231 | * Returns the constructor
232 | *
233 | * @param clazz The class
234 | * @param params The Constructor parameters
235 | * @return The Constructor or an empty Optional if there is none with
236 | * these parameters
237 | */
238 | public static Optional> getConstructor(Class> clazz,
239 | Class>... params) {
240 | try {
241 | return Optional.of(clazz.getConstructor(params));
242 | } catch (NoSuchMethodException e) {
243 | try {
244 | return Optional.of(clazz.getDeclaredConstructor(params));
245 | } catch (NoSuchMethodException e2) {
246 | e2.printStackTrace();
247 | }
248 | }
249 | return Optional.empty();
250 | }
251 |
252 | /**
253 | * Instantiates the class. Will print the errors it gets
254 | *
255 | * @param constructor The constructor
256 | * @param arguments The initial arguments
257 | * @return The resulting object, or null if an error occurred.
258 | */
259 | public static Object instantiate(Constructor> constructor,
260 | Object... arguments) {
261 | try {
262 | return constructor.newInstance(arguments);
263 | } catch (InstantiationException | IllegalAccessException
264 | | InvocationTargetException e) {
265 | e.printStackTrace();
266 | }
267 | return null;
268 | }
269 |
270 | public static Optional getMethod(Class> clazz, String name,
271 | Class>... params) {
272 | try {
273 | return Optional.of(clazz.getMethod(name, params));
274 | } catch (NoSuchMethodException e) {
275 | e.printStackTrace();
276 | }
277 | try {
278 | return Optional.of(clazz.getDeclaredMethod(name, params));
279 | } catch (NoSuchMethodException e) {
280 | e.printStackTrace();
281 | }
282 | return Optional.empty();
283 | }
284 |
285 | public static Optional getField(Class> clazz, String name) {
286 | try {
287 | return Optional.of(clazz.getField(name));
288 | } catch (NoSuchFieldException e) {
289 | }
290 |
291 | try {
292 | return Optional.of(clazz.getDeclaredField(name));
293 | } catch (NoSuchFieldException e) {
294 | }
295 | return Optional.empty();
296 | }
297 | }
--------------------------------------------------------------------------------
/src/me/zombie_striker/pixelprinter/util/SimilarMapUtil.java:
--------------------------------------------------------------------------------
1 | package me.zombie_striker.pixelprinter.util;
2 |
3 | import me.zombie_striker.pixelprinter.data.CustomMapView;
4 | import org.bukkit.Bukkit;
5 | import org.bukkit.Material;
6 | import org.bukkit.inventory.ItemStack;
7 | import org.bukkit.inventory.meta.MapMeta;
8 | import org.bukkit.map.MapRenderer;
9 | import org.bukkit.map.MapView;
10 |
11 | import javax.imageio.ImageIO;
12 | import java.awt.image.BufferedImage;
13 | import java.io.File;
14 | import java.io.IOException;
15 |
16 | public class SimilarMapUtil {
17 |
18 | private static File mapFolder;
19 |
20 | public static void registerAllMaps(File folder) {
21 | mapFolder = folder;
22 | for (File f : mapFolder.listFiles()) {
23 | int id = Integer.parseInt(f.getName().split(".png")[0]);
24 | try {
25 | renderMap(id, ImageIO.read(f));
26 | } catch (IOException e) {
27 | e.printStackTrace();
28 | }
29 | }
30 | }
31 |
32 | public static void renderMap(int mapIds, BufferedImage image) {
33 | try {
34 | Material map = Material.MAP;
35 | if (ReflectionUtil.isVersionHigherThan(1, 13))
36 | map = Material.FILLED_MAP;
37 | @SuppressWarnings("deprecation")
38 | ItemStack is = new ItemStack(map, 1, (short) mapIds);
39 |
40 | MapMeta meta = (MapMeta) is.getItemMeta();
41 | meta.setMapView(new CustomMapView(mapIds));
42 | if (!meta.hasMapView()) {
43 | Bukkit.getMap(mapIds);
44 | }
45 | MapView mv = meta.getMapView();
46 | for (MapRenderer mr : mv.getRenderers()) {
47 | mv.removeRenderer(mr);
48 | }
49 | mv.addRenderer(new CustomImageRenderer(image, CustomImageRenderer.TICK_FOR_STILLS));
50 | } catch (Error | Exception e4) {
51 | e4.printStackTrace();
52 | }
53 | }
54 |
55 | public static int findSimilarImage(BufferedImage image) {
56 | for (File f : mapFolder.listFiles()) {
57 | String filename = f.getName();
58 | if (filename.contains("."))
59 | filename = filename.split("\\.")[0];
60 | int id = Integer.parseInt(filename);
61 | try {
62 | if (compareImages(image, ImageIO.read(f))) {
63 | return id;
64 | }
65 | } catch (IOException e) {
66 | e.printStackTrace();
67 | }
68 | }
69 | MapView mv = Bukkit.createMap(Bukkit.getWorlds().get(0));
70 | for (MapRenderer mr : mv.getRenderers()) {
71 | mv.removeRenderer(mr);
72 | }
73 | mv.addRenderer(new CustomImageRenderer(image, CustomImageRenderer.TICK_FOR_STILLS));
74 |
75 | try {
76 | ImageIO.write(image, "png", new File(mapFolder, mv.getId() + ".png"));
77 | } catch (IOException e) {
78 | e.printStackTrace();
79 | }
80 | image.flush();
81 |
82 | return mv.getId();
83 | }
84 |
85 | /**
86 | * Compares two images pixel by pixel.
87 | *
88 | * @param imgA the first image.
89 | * @param imgB the second image.
90 | * @return whether the images are both the same or not.
91 | */
92 | public static boolean compareImages(BufferedImage imgA, BufferedImage imgB) {
93 | // The images must be the same size.
94 | if (imgA.getWidth() != imgB.getWidth() || imgA.getHeight() != imgB.getHeight()) {
95 | return false;
96 | }
97 |
98 | int width = imgA.getWidth();
99 | int height = imgA.getHeight();
100 |
101 | // Loop over every pixel.
102 | for (int y = 0; y < height; y++) {
103 | for (int x = 0; x < width; x++) {
104 | // Compare the pixels for equality.
105 | if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) {
106 | return false;
107 | }
108 | }
109 | }
110 |
111 | return true;
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/src/me/zombie_striker/pixelprinter/util/SkinCreator.java:
--------------------------------------------------------------------------------
1 | package me.zombie_striker.pixelprinter.util;
2 |
3 | import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
4 | import me.zombie_striker.pixelprinter.data.Direction;
5 | import me.zombie_striker.pixelprinter.util.RGBBlockColor.Pixel;
6 | import org.bukkit.Location;
7 |
8 | import javax.imageio.ImageIO;
9 | import java.awt.image.BufferedImage;
10 | import java.io.IOException;
11 | import java.io.InputStreamReader;
12 | import java.lang.reflect.Method;
13 | import java.net.URL;
14 |
15 | public class SkinCreator {
16 |
17 | public static BufferedImage[] getSkin(String uuid)
18 | throws NullPointerException, IOException {
19 | int linecode = 21;
20 | try {
21 | StringBuilder code = new StringBuilder();
22 | InputStreamReader is = new InputStreamReader(new URL(
23 | "https://sessionserver.mojang.com/session/minecraft/profile/"
24 | + uuid.replace("-", "")).openStream());
25 | int charI = 0;
26 | while ((charI = is.read()) != -1) {
27 | code.append(((char) charI));
28 | }
29 | linecode = 31;
30 | String[] aaaa = code.toString().split("\"value\" : \"");
31 | if (aaaa.length == 1) {
32 | System.out.println("The user does not exist- AAAA does not contain value. Response:");
33 | System.out.println(code.toString());
34 | throw new NullPointerException();
35 | }
36 | String decode;
37 | try {
38 | linecode = 39;
39 | Method m = Class.forName("javax.xml.bind.DatatypeConverter").getMethod("parseBase64Binary", String.class);
40 | /*
41 | javax.xml.bind.DatatypeConverter.parseBase64Binary(*/
42 | decode = (String) m.invoke(null, aaaa[1]
43 | .split("\"}],\"legacy\"")[0].split("\"}}}")[0].split("\"")[0]);
44 | } catch (Error | Exception e4) {
45 | linecode = 45;
46 | decode = new String(Base64.decode(aaaa[1]
47 | .split("\"}],\"legacy\"")[0].split("\"}}}")[0].split("\"")[0]));
48 | }
49 | linecode = 49;
50 | System.out.println(decode);
51 | String url = decode.split("url\" : \"")[1].split("\"}")[0].split("\",")[0];
52 | linecode = 52;
53 | System.out.println(url);
54 | BufferedImage[] images = new BufferedImage[2];
55 | images[0] = ImageIO.read(new URL(url));
56 | linecode = 56;
57 | if (decode.contains("CAPE")) {
58 | String urlcape = decode.split("url\" : \"")[2].split("\"}")[0];
59 | linecode = 59;
60 | images[1] = ImageIO.read(new URL(urlcape));
61 | }
62 | return images;
63 | } catch (NullPointerException e) {
64 | System.out
65 | .println("The Mojang servers denied the request. Wait a minute or so until you are allowed to get the texture again." + linecode);
66 | throw new NullPointerException();
67 | } catch (IOException e2) {
68 | System.out.println("The user does not exist- ErrorLineCode=" + linecode + ".");
69 | throw new IOException();
70 | }
71 | }
72 |
73 | @SuppressWarnings("deprecation")
74 | public static void createStatue(BufferedImage[] images, Location center,
75 | Direction dir) {
76 | BufferedImage skin = images[0];
77 | BufferedImage cape = images[1];
78 |
79 | Direction front = dir;
80 | Direction back = null;
81 | Direction left = null;
82 | Direction right = null;
83 | Direction flat = null;
84 |
85 | switch (front) {
86 | case UP_EAST:
87 | back = Direction.UP_WEST;
88 | right = Direction.UP_NORTH;
89 | left = Direction.UP_SOUTH;
90 | flat = Direction.FLAT_SOUTHEAST;
91 | break;
92 | case UP_WEST:
93 | back = Direction.UP_EAST;
94 | right = Direction.UP_SOUTH;
95 | left = Direction.UP_NORTH;
96 | flat = Direction.FLAT_NORTHWEST;
97 | break;
98 | case UP_NORTH:
99 | back = Direction.UP_SOUTH;
100 | right = Direction.UP_EAST;
101 | left = Direction.UP_WEST;
102 | flat = Direction.FLAT_SOUTHEAST;
103 | break;
104 | case UP_SOUTH:
105 | back = Direction.UP_NORTH;
106 | right = Direction.UP_WEST;
107 | left = Direction.UP_EAST;
108 | flat = Direction.FLAT_NORTHWEST;
109 | break;
110 | default:
111 | break;
112 | }
113 | /**
114 | *
115 | * This code would be right, If the direction the block is facing is the
116 | * same as the direction of the wall.
117 | *
118 | * switch (front) {
119 | *
120 | * case UP_SOUTH: back = Direction.UP_NORTH; right = Direction.UP_WEST;
121 | * left = Direction.UP_EAST; flat = Direction.FLAT_SOUTHWEST; break;
122 | * case UP_NORTH: back = Direction.UP_SOUTH; right = Direction.UP_EAST;
123 | * left = Direction.UP_WEST; flat = Direction.FLAT_NORTHEAST;
124 | *
125 | * break; case UP_EAST: back = Direction.UP_WEST; right =
126 | * Direction.UP_SOUTH; left = Direction.UP_NORTH; flat =
127 | * Direction.FLAT_SOUTHEAST; break; case UP_WEST: back =
128 | * Direction.UP_EAST; right = Direction.UP_NORTH; left =
129 | * Direction.UP_SOUTH; flat = Direction.FLAT_NORTHWEST; break; default:
130 | * break; }
131 | */
132 | final Location loc = center.clone();
133 | // legs (left)
134 | {
135 | a(2,
136 | 0,
137 | -3,
138 | loc,
139 | left,
140 | front,
141 | RGBBlockColor.createResizedCopy(
142 | skin.getSubimage(0, 32 - 12, 4, 12), 24, false));
143 | a(-1,
144 | 0,
145 | 0,
146 | loc,
147 | right,
148 | front,
149 | RGBBlockColor.createResizedCopy(
150 | skin.getSubimage(0 + 8, 32 - 12, 4, 12), 24, false));
151 | a(2,
152 | 0,
153 | -3,
154 | loc,
155 | front,
156 | front,
157 | RGBBlockColor.createResizedCopy(
158 | skin.getSubimage(0 + 4, 32 - 12, 4, 12), 24, false));
159 | a(-1, 0, 0, loc, back, front, RGBBlockColor.createResizedCopy(
160 | skin.getSubimage(0 + 12, 32 - 12, 4, 12), 24, false));
161 | }
162 |
163 | // Legs (right)
164 | {
165 | int x = 16;
166 | int y = 64;
167 | if (skin.getHeight() == 32) {
168 | x = 0;
169 | y = 32;
170 | }
171 | a(2,
172 | 0,
173 | 1,
174 | loc,
175 | left,
176 | front,
177 | RGBBlockColor.createResizedCopy(
178 | skin.getSubimage(x, y - 12, 4, 12), 24, false));
179 | a(-1,
180 | 0,
181 | 4,
182 | loc,
183 | right,
184 | front,
185 | RGBBlockColor.createResizedCopy(
186 | skin.getSubimage(x + 8, y - 12, 4, 12), 24, false));
187 | a(2,
188 | 0,
189 | 1,
190 | loc,
191 | front,
192 | front,
193 | RGBBlockColor.createResizedCopy(
194 | skin.getSubimage(x + 4, y - 12, 4, 12), 24, false));
195 | a(-1,
196 | 0,
197 | 4,
198 | loc,
199 | back,
200 | front,
201 | RGBBlockColor.createResizedCopy(
202 | skin.getSubimage(x + 12, y - 12, 4, 12), 24, false));
203 | }
204 |
205 | // arm (left)
206 | {
207 | a(-1,
208 | 23,
209 | -3 + 8,
210 | loc,
211 | flat,
212 | front,
213 | RGBBlockColor.createResizedCopy(
214 | skin.getSubimage(40 + 4, 16, 4, 4), 8, false));
215 | a(-1,
216 | 12,
217 | -3 + 8,
218 | loc,
219 | flat,
220 | front,
221 | RGBBlockColor.createResizedCopy(
222 | skin.getSubimage(40 + 4 + 4, 16, 4, 4), 8, false));
223 |
224 | a(2,
225 | 12,
226 | -3 + 8,
227 | loc,
228 | left,
229 | front,
230 | RGBBlockColor.createResizedCopy(
231 | skin.getSubimage(40, 20, 4, 12), 24, false));
232 | a(-1,
233 | 12,
234 | 8,
235 | loc,
236 | right,
237 | front,
238 | RGBBlockColor.createResizedCopy(
239 | skin.getSubimage(40 + 8, 20, 4, 12), 24, false));
240 | a(2,
241 | 12,
242 | -3 + 8,
243 | loc,
244 | front,
245 | front,
246 | RGBBlockColor.createResizedCopy(
247 | skin.getSubimage(40 + 4, 20, 4, 12), 24, false));
248 | a(-1,
249 | 12,
250 | 8,
251 | loc,
252 | back,
253 | front,
254 | RGBBlockColor.createResizedCopy(
255 | skin.getSubimage(40 + 12, 20, 4, 12), 24, false));
256 | }
257 | // arm (right)
258 | {
259 | int x = 32;
260 | int y = 20;
261 | if (skin.getHeight() == 32) {
262 | x = 32;
263 | y = 20;
264 | }
265 |
266 | // Tops and bottoms
267 |
268 | a(2,
269 | 12,
270 | -3 - 4,
271 | loc,
272 | left,
273 | front,
274 | RGBBlockColor.createResizedCopy(
275 | skin.getSubimage(x, y, 4, 12), 24, false));
276 | a(-1,
277 | 12,
278 | -4,
279 | loc,
280 | right,
281 | front,
282 | RGBBlockColor.createResizedCopy(
283 | skin.getSubimage(x + 8, y, 4, 12), 24, false));
284 | a(-1,
285 | 12,
286 | -3 - 4,
287 | loc,
288 | front,
289 | front,
290 | RGBBlockColor.createResizedCopy(
291 | skin.getSubimage(x + 4, y, 4, 12), 24, false));
292 | a(2,
293 | 12,
294 | -4,
295 | loc,
296 | back,
297 | front,
298 | RGBBlockColor.createResizedCopy(
299 | skin.getSubimage(x + 12, y, 4, 12), 24, false));
300 |
301 | a(-1,
302 | 23,
303 | -3 - 4,
304 | loc,
305 | flat,
306 | front,
307 | RGBBlockColor.createResizedCopy(
308 | skin.getSubimage(x + 4, y - 4, 4, 4), 8, false));
309 | a(-1,
310 | 12,
311 | -3 - 4,
312 | loc,
313 | flat,
314 | front,
315 | RGBBlockColor.createResizedCopy(
316 | skin.getSubimage(x + 4 + 4, y - 4, 4, 4), 8, false));
317 | }
318 | // chest
319 | {
320 | a(2,
321 | 12,
322 | -3,
323 | loc,
324 | left,
325 | front,
326 | RGBBlockColor.createResizedCopy(
327 | skin.getSubimage(16, 20, 4, 12), 24, false));
328 | a(-1,
329 | 12,
330 | 4,
331 | loc,
332 | right,
333 | front,
334 | RGBBlockColor.createResizedCopy(
335 | skin.getSubimage(16 + 12, 20, 4, 12), 24, false));
336 | a(2,
337 | 12,
338 | -3,
339 | loc,
340 | front,
341 | front,
342 | RGBBlockColor.createResizedCopy(
343 | skin.getSubimage(16 + 4, 20, 8, 12), 24, false));
344 | a(-1,
345 | 12,
346 | 4,
347 | loc,
348 | back,
349 | front,
350 | RGBBlockColor.createResizedCopy(
351 | skin.getSubimage(16 + 16, 20, 8, 12), 24, false));
352 | }
353 | //Helmet bottom
354 | {
355 | a(-3,
356 | 23,
357 | -3,
358 | loc,
359 | flat,
360 | front,
361 | RGBBlockColor.createResizedCopy(
362 | skin.getSubimage(32 + 16, 0, 8, 8), 16, true), true);
363 | a(-3,
364 | 32,
365 | -3,
366 | loc,
367 | flat,
368 | front,
369 | RGBBlockColor.createResizedCopy(
370 | skin.getSubimage(32 + 8, 0, 8, 8), 16, true), true);
371 | }
372 |
373 | // head
374 | {
375 | a(-3,
376 | 24,
377 | -3,
378 | loc,
379 | flat,
380 | front,
381 | RGBBlockColor.createResizedCopy(
382 | skin.getSubimage(16, 0, 8, 8), 16, false));
383 | a(-3,
384 | 31,
385 | -3,
386 | loc,
387 | flat,
388 | front,
389 | RGBBlockColor.createResizedCopy(
390 | skin.getSubimage(8, 0, 8, 8), 16, false));
391 |
392 | a(-3,
393 | 24,
394 | -3,
395 | loc,
396 | right,
397 | front,
398 | RGBBlockColor.createResizedCopy(
399 | skin.getSubimage(0, 8, 8, 8), 16, false));
400 | a(4,
401 | 24,
402 | 4,
403 | loc,
404 | left,
405 | front,
406 | RGBBlockColor.createResizedCopy(
407 | skin.getSubimage(16, 8, 8, 8), 16, false));
408 | a(-3,
409 | 24,
410 | 4,
411 | loc,
412 | back,
413 | front,
414 | RGBBlockColor.createResizedCopy(
415 | skin.getSubimage(24, 8, 8, 8), 16, false));
416 | a(4,
417 | 24,
418 | -3,
419 | loc,
420 | front,
421 | front,
422 | RGBBlockColor.createResizedCopy(
423 | skin.getSubimage(8, 8, 8, 8), 16, false));
424 | }
425 | // helmet
426 | {
427 |
428 | a(-3,
429 | 24,
430 | -4,//-3
431 | loc,
432 | right,
433 | front,
434 | RGBBlockColor.createResizedCopy(
435 | skin.getSubimage(32 + 0, 8, 8, 8), 16, true), true);
436 | a(4,
437 | 24,
438 | 5,//4,
439 | loc,
440 | left,
441 | front,
442 | RGBBlockColor.createResizedCopy(
443 | skin.getSubimage(32 + 16, 8, 8, 8), 16, true), true);
444 | a(-4,//-3,
445 | 24,
446 | 4,
447 | loc,
448 | back,
449 | front,
450 | RGBBlockColor.createResizedCopy(
451 | skin.getSubimage(32 + 24, 8, 8, 8), 16, true), true);
452 | a(5,//4,
453 | 24,
454 | -3,
455 | loc,
456 | front,
457 | front,
458 | RGBBlockColor.createResizedCopy(
459 | skin.getSubimage(32 + 8, 8, 8, 8), 16, true), true);
460 | }
461 |
462 | if (cape != null) {
463 | a(-6 + 4,
464 | 6,
465 | -9 + 4,
466 | loc,
467 | front,
468 | front,
469 | RGBBlockColor.createResizedCopy(
470 | cape.getSubimage(0, 0, 12, 18), 18 * 2, true), true);
471 | }
472 |
473 | }
474 |
475 | private static void a(int x, int y, int z, Location loc, Direction d,
476 | Direction f, BufferedImage skin2) {
477 | a(x, y, z, loc, d, f, skin2, false);
478 | }
479 |
480 | private static void a(int x, int y, int z, Location loc, Direction d,
481 | Direction f, BufferedImage skin2, boolean enableTrans) {
482 |
483 | if (d == Direction.UP_EAST) {
484 | d = Direction.UP_SOUTH;
485 | } else if (d == Direction.UP_WEST) {
486 | d = Direction.UP_NORTH;
487 | } else if (d == Direction.UP_NORTH) {
488 | d = Direction.UP_EAST;
489 | /*
490 | * if (f == Direction.UP_EAST || f == Direction.UP_WEST) { d =
491 | * Direction.UP_EAST; } else { d = Direction.UP_WEST; }
492 | */
493 | } else if (d == Direction.UP_SOUTH) {
494 | d = Direction.UP_WEST;
495 | /*
496 | * if (f == Direction.UP_EAST || f == Direction.UP_WEST) { d =
497 | * Direction.UP_WEST; } else { d = Direction.UP_EAST; }
498 | */
499 | }
500 | BufferedImage temp = skin2;
501 | Pixel[][] result = RGBBlockColor.convertTo2DWithoutUsingGetRGB(temp);
502 | new AsyncImageHolder(result, null, getOffset(loc, f, x, y, z), d, temp,
503 | enableTrans).loadImage(false);
504 | }
505 |
506 | public static Location getOffset(Location start, Direction d, double xoff,
507 | int yoff, double zoff) {
508 | if (d == Direction.UP_SOUTH) {
509 | return start.clone().add(-zoff, yoff, -xoff);
510 | }
511 | if (d == Direction.UP_NORTH) {
512 | return start.clone().add(zoff, yoff, xoff);
513 | }
514 | if (d == Direction.UP_EAST) {
515 | return start.clone().add(xoff, yoff, zoff);
516 | }
517 | if (d == Direction.UP_WEST) {
518 | return start.clone().add(-xoff, yoff, -zoff);
519 | }
520 | return start.clone().add(xoff, yoff, zoff);
521 | }
522 |
523 | }
524 |
--------------------------------------------------------------------------------
/src/me/zombie_striker/pixelprinter/util/UndoUtil.java:
--------------------------------------------------------------------------------
1 | package me.zombie_striker.pixelprinter.util;
2 |
3 | import org.bukkit.Location;
4 |
5 | import java.util.HashMap;
6 | import java.util.Set;
7 |
8 | public class UndoUtil {
9 |
10 | static HashMap savedLocs = new HashMap<>();
11 |
12 | public static String verifyNewName(String start) {
13 | String test = start;
14 | int id = 0;
15 | while (savedLocs.containsKey(test)) {
16 | id++;
17 | test = test + "(" + id + ")";
18 | }
19 | return test;
20 | }
21 |
22 | public static void addNewSnapshot(String name, Location start, Location end) {
23 | savedLocs.put(verifyNewName(name), new BlockTypeSnapshot(start, end));
24 | }
25 |
26 | public static void undo(String name) {
27 | BlockTypeSnapshot bts = savedLocs.get(name);
28 | if (bts != null)
29 | bts.undo();
30 | }
31 |
32 | public static void remove(String name) {
33 | savedLocs.remove(name);
34 | }
35 |
36 | public static Set getSnapshots() {
37 | return savedLocs.keySet();
38 | }
39 |
40 | public static boolean snapshotExists(String name) {
41 | for (String s : savedLocs.keySet()) {
42 | if (s.equals(name))
43 | return true;
44 | }
45 | return false;
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/me/zombie_striker/pixelprinter/util/Update13Handler.java:
--------------------------------------------------------------------------------
1 | package me.zombie_striker.pixelprinter.util;
2 |
3 | import org.bukkit.Bukkit;
4 | import org.bukkit.block.BlockFace;
5 | import org.bukkit.block.BlockState;
6 |
7 | public class Update13Handler {
8 |
9 | public static boolean isFacing(BlockState bs, BlockFace bf2) {
10 | try {
11 | return (((org.bukkit.block.data.Directional) bs.getBlock().getBlockData()).getFacing() == bf2);
12 | } catch (Error | Exception e45) {
13 | }
14 | return false;
15 | }
16 |
17 | public static BlockFace getFacing(BlockState bs) {
18 | try {
19 | return (((org.bukkit.block.data.Directional) bs.getBlock().getBlockData()).getFacing());
20 | } catch (Error | Exception e45) {
21 | }
22 | return null;
23 | }
24 |
25 | public static boolean isDirectional(BlockState bs) {
26 | try {
27 | return (bs.getBlock().getBlockData() instanceof org.bukkit.block.data.Directional);
28 | } catch (Error | Exception e45) {
29 | }
30 | return false;
31 | }
32 |
33 | public static void setFacing(BlockState bs, BlockFace bf) {
34 | try {
35 | org.bukkit.block.data.Directional d = ((org.bukkit.block.data.Directional) bs.getBlockData());
36 | d.setFacing(bf);
37 | bs.setBlockData(d);
38 | bs.update(true, false);
39 | } catch (Error | Exception e45) {
40 |
41 | }
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------