1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package org.bukkit.craftbukkit.map;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.server.WorldMap;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public final class CraftMapView implements MapView {
private final Map<CraftPlayer, RenderData> renderCache = new HashMap<CraftPlayer, RenderData>();
private final List<MapRenderer> renderers = new ArrayList<MapRenderer>();
private final Map<MapRenderer, Map<CraftPlayer, CraftMapCanvas>> canvases = new HashMap<MapRenderer, Map<CraftPlayer, CraftMapCanvas>>();
protected final WorldMap worldMap;
public CraftMapView(WorldMap worldMap) {
this.worldMap = worldMap;
addRenderer(new CraftMapRenderer(this, worldMap));
}
public short getId() {
String text = worldMap.id;
if (text.startsWith("map_")) {
try {
return Short.parseShort(text.substring("map_".length()));
}
catch (NumberFormatException ex) {
throw new IllegalStateException("Map has non-numeric ID");
}
} else {
throw new IllegalStateException("Map has invalid ID");
}
}
public boolean isVirtual() {
return renderers.size() > 0 && !(renderers.get(0) instanceof CraftMapRenderer);
}
public Scale getScale() {
return Scale.valueOf(worldMap.scale);
}
public void setScale(Scale scale) {
worldMap.scale = scale.getValue();
}
public World getWorld() {
byte dimension = worldMap.map;
for (World world : Bukkit.getServer().getWorlds()) {
if (((CraftWorld) world).getHandle().dimension == dimension) {
return world;
}
}
return null;
}
public void setWorld(World world) {
worldMap.map = (byte) ((CraftWorld) world).getHandle().dimension;
}
public int getCenterX() {
return worldMap.centerX;
}
public int getCenterZ() {
return worldMap.centerZ;
}
public void setCenterX(int x) {
worldMap.centerX = x;
}
public void setCenterZ(int z) {
worldMap.centerZ = z;
}
public List<MapRenderer> getRenderers() {
return new ArrayList<MapRenderer>(renderers);
}
public void addRenderer(MapRenderer renderer) {
if (!renderers.contains(renderer)) {
renderers.add(renderer);
canvases.put(renderer, new HashMap<CraftPlayer, CraftMapCanvas>());
renderer.initialize(this);
}
}
public boolean removeRenderer(MapRenderer renderer) {
if (renderers.contains(renderer)) {
renderers.remove(renderer);
for (Map.Entry<CraftPlayer, CraftMapCanvas> entry : canvases.get(renderer).entrySet()) {
for (int x = 0; x < 128; ++x) {
for (int y = 0; y < 128; ++y) {
entry.getValue().setPixel(x, y, (byte) -1);
}
}
}
canvases.remove(renderer);
return true;
} else {
return false;
}
}
private boolean isContextual() {
for (MapRenderer renderer : renderers) {
if (renderer.isContextual()) return true;
}
return false;
}
public RenderData render(CraftPlayer player) {
boolean context = isContextual();
RenderData render = renderCache.get(context ? player : null);
if (render == null) {
render = new RenderData();
renderCache.put(context ? player : null, render);
}
if (context && renderCache.containsKey(null)) {
renderCache.remove(null);
}
Arrays.fill(render.buffer, (byte) 0);
render.cursors.clear();
for (MapRenderer renderer : renderers) {
CraftMapCanvas canvas = canvases.get(renderer).get(renderer.isContextual() ? player : null);
if (canvas == null) {
canvas = new CraftMapCanvas(this);
canvases.get(renderer).put(renderer.isContextual() ? player : null, canvas);
}
canvas.setBase(render.buffer);
renderer.render(this, canvas, player);
byte[] buf = canvas.getBuffer();
for (int i = 0; i < buf.length; ++i) {
if (buf[i] >= 0) render.buffer[i] = buf[i];
}
for (int i = 0; i < canvas.getCursors().size(); ++i) {
render.cursors.add(canvas.getCursors().getCursor(i));
}
}
return render;
}
}
|