Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.craftbukkit.map;
2
3import org.bukkit.map.MapCanvas;
4import org.bukkit.map.MapCursorCollection;
5import org.bukkit.map.MapFont;
6import org.bukkit.map.MapFont.CharacterSprite;
7import org.bukkit.map.MapPalette;
8
9import java.awt.*;
10import java.util.Arrays;
11
12public class CraftMapCanvas implements MapCanvas {
13
14 private final byte[] buffer = new byte[128 * 128];
15 private final CraftMapView mapView;
16 private byte[] base;
17 private MapCursorCollection cursors = new MapCursorCollection();
18
19 protected CraftMapCanvas(CraftMapView mapView) {
20 this.mapView = mapView;
21 Arrays.fill(buffer, (byte) -1);
22 }
23
24 public CraftMapView getMapView() {
25 return mapView;
26 }
27
28 public MapCursorCollection getCursors() {
29 return cursors;
30 }
31
32 public void setCursors(MapCursorCollection cursors) {
33 this.cursors = cursors;
34 }
35
36 public void setPixel(int x, int y, byte color) {
37 if (x < 0 || y < 0 || x >= 128 || y >= 128) return;
38 if (buffer[y * 128 + x] != color) {
39 buffer[y * 128 + x] = color;
40 mapView.worldMap.a(x, y, y);
41 }
42 }
43
44 public byte getPixel(int x, int y) {
45 if (x < 0 || y < 0 || x >= 128 || y >= 128) return 0;
46 return buffer[y * 128 + x];
47 }
48
49 public byte getBasePixel(int x, int y) {
50 if (x < 0 || y < 0 || x >= 128 || y >= 128) return 0;
51 return base[y * 128 + x];
52 }
53
54 protected void setBase(byte[] base) {
55 this.base = base;
56 }
57
58 protected byte[] getBuffer() {
59 return buffer;
60 }
61
62 public void drawImage(int x, int y, Image image) {
63 byte[] bytes = MapPalette.imageToBytes(image);
64 for (int x2 = 0; x2 < image.getWidth(null); ++x2) {
65 for (int y2 = 0; y2 < image.getHeight(null); ++y2) {
66 setPixel(x + x2, y + y2, bytes[y2 * image.getWidth(null) + x2]);
67 }
68 }
69 }
70
71 public void drawText(int x, int y, MapFont font, String text) {
72 int xStart = x;
73 byte color = MapPalette.DARK_GRAY;
74 if (!font.isValid(text)) {
75 throw new IllegalArgumentException("text contains invalid characters");
76 }
77
78 for (int i = 0; i < text.length(); ++i) {
79 char ch = text.charAt(i);
80 if (ch == '\n') {
81 x = xStart;
82 y += font.getHeight() + 1;
83 continue;
84 } else if (ch == '\u00A7') {
85 int j = text.indexOf(';', i);
86 if (j >= 0) {
87 try {
88 color = Byte.parseByte(text.substring(i + 1, j));
89 i = j;
90 continue;
91 } catch (NumberFormatException ex) {
92 }
93 }
94 }
95
96 CharacterSprite sprite = font.getChar(text.charAt(i));
97 for (int r = 0; r < font.getHeight(); ++r) {
98 for (int c = 0; c < sprite.getWidth(); ++c) {
99 if (sprite.get(r, c)) {
100 setPixel(x + c, y + r, color);
101 }
102 }
103 }
104 x += sprite.getWidth() + 1;
105 }
106 }
107
108}