Skip to content

Commit 412e607

Browse files
committed
Implement BlueMapAPI 1.5.0
1 parent 658ff70 commit 412e607

File tree

9 files changed

+747
-41
lines changed

9 files changed

+747
-41
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
/*
2+
* This file is part of BlueMap, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package de.bluecolored.bluemap.common.api.marker;
26+
27+
import com.flowpowered.math.vector.Vector2d;
28+
import com.flowpowered.math.vector.Vector3d;
29+
import com.google.common.base.Preconditions;
30+
import de.bluecolored.bluemap.api.BlueMapAPI;
31+
import de.bluecolored.bluemap.api.BlueMapMap;
32+
import de.bluecolored.bluemap.api.marker.ExtrudeMarker;
33+
import de.bluecolored.bluemap.api.marker.Shape;
34+
import ninja.leaping.configurate.ConfigurationNode;
35+
36+
import java.awt.*;
37+
import java.util.List;
38+
39+
public class ExtrudeMarkerImpl extends ObjectMarkerImpl implements ExtrudeMarker {
40+
public static final String MARKER_TYPE = "extrude";
41+
42+
private Shape shape;
43+
private float shapeMinY;
44+
private float shapeMaxY;
45+
private boolean depthTest;
46+
private int lineWidth;
47+
private Color lineColor, fillColor;
48+
49+
private boolean hasUnsavedChanges;
50+
51+
public ExtrudeMarkerImpl(String id, BlueMapMap map, Vector3d position, Shape shape, float shapeMinY, float shapeMaxY) {
52+
super(id, map, position);
53+
54+
Preconditions.checkNotNull(shape);
55+
56+
this.shape = shape;
57+
this.shapeMinY = shapeMinY;
58+
this.shapeMaxY = shapeMaxY;
59+
this.lineWidth = 2;
60+
this.lineColor = new Color(255, 0, 0, 200);
61+
this.fillColor = new Color(200, 0, 0, 100);
62+
63+
this.hasUnsavedChanges = true;
64+
}
65+
66+
@Override
67+
public String getType() {
68+
return MARKER_TYPE;
69+
}
70+
71+
@Override
72+
public Shape getShape() {
73+
return this.shape;
74+
}
75+
76+
@Override
77+
public float getShapeMinY() {
78+
return shapeMinY;
79+
}
80+
81+
@Override
82+
public float getShapeMaxY() {
83+
return shapeMaxY;
84+
}
85+
86+
@Override
87+
public synchronized void setShape(Shape shape, float shapeMinY, float shapeMaxY) {
88+
Preconditions.checkNotNull(shape);
89+
90+
this.shape = shape;
91+
this.shapeMinY = shapeMinY;
92+
this.shapeMaxY = shapeMaxY;
93+
this.hasUnsavedChanges = true;
94+
}
95+
96+
@Override
97+
public boolean isDepthTestEnabled() {
98+
return this.depthTest;
99+
}
100+
101+
@Override
102+
public void setDepthTestEnabled(boolean enabled) {
103+
this.depthTest = enabled;
104+
this.hasUnsavedChanges = true;
105+
}
106+
107+
@Override
108+
public int getLineWidth() {
109+
return lineWidth;
110+
}
111+
112+
@Override
113+
public void setLineWidth(int lineWidth) {
114+
this.lineWidth = lineWidth;
115+
this.hasUnsavedChanges = true;
116+
}
117+
118+
@Override
119+
public Color getLineColor() {
120+
return this.lineColor;
121+
}
122+
123+
@Override
124+
public synchronized void setLineColor(Color color) {
125+
Preconditions.checkNotNull(color);
126+
127+
this.lineColor = color;
128+
this.hasUnsavedChanges = true;
129+
}
130+
131+
@Override
132+
public Color getFillColor() {
133+
return this.fillColor;
134+
}
135+
136+
@Override
137+
public synchronized void setFillColor(Color color) {
138+
Preconditions.checkNotNull(color);
139+
140+
this.fillColor = color;
141+
this.hasUnsavedChanges = true;
142+
}
143+
144+
@Override
145+
public void load(BlueMapAPI api, ConfigurationNode markerNode, boolean overwriteChanges) throws MarkerFileFormatException {
146+
super.load(api, markerNode, overwriteChanges);
147+
148+
if (!overwriteChanges && hasUnsavedChanges) return;
149+
this.hasUnsavedChanges = false;
150+
151+
this.shape = readShape(markerNode.getNode("shape"));
152+
this.shapeMinY = markerNode.getNode("shapeMinY").getFloat(0);
153+
this.shapeMaxY = markerNode.getNode("shapeMaxY").getFloat(255);
154+
this.depthTest = markerNode.getNode("depthTest").getBoolean(true);
155+
this.lineWidth = markerNode.getNode("lineWidth").getInt(2);
156+
this.lineColor = readColor(markerNode.getNode("lineColor"));
157+
this.fillColor = readColor(markerNode.getNode("fillColor"));
158+
}
159+
160+
@Override
161+
public void save(ConfigurationNode markerNode) {
162+
super.save(markerNode);
163+
164+
writeShape(markerNode.getNode("shape"), this.shape);
165+
markerNode.getNode("shapeMinY").setValue(Math.round(shapeMinY * 1000f) / 1000f);
166+
markerNode.getNode("shapeMaxY").setValue(Math.round(shapeMaxY * 1000f) / 1000f);
167+
markerNode.getNode("depthTest").setValue(this.depthTest);
168+
markerNode.getNode("lineWidth").setValue(this.lineWidth);
169+
writeColor(markerNode.getNode("lineColor"), this.lineColor);
170+
writeColor(markerNode.getNode("fillColor"), this.fillColor);
171+
172+
hasUnsavedChanges = false;
173+
}
174+
175+
private Shape readShape(ConfigurationNode node) throws MarkerFileFormatException {
176+
List<? extends ConfigurationNode> posNodes = node.getChildrenList();
177+
178+
if (posNodes.size() < 3) throw new MarkerFileFormatException("Failed to read shape: point-list has fewer than 3 entries!");
179+
180+
Vector2d[] positions = new Vector2d[posNodes.size()];
181+
for (int i = 0; i < positions.length; i++) {
182+
positions[i] = readShapePos(posNodes.get(i));
183+
}
184+
185+
return new Shape(positions);
186+
}
187+
188+
private static Vector2d readShapePos(ConfigurationNode node) throws MarkerFileFormatException {
189+
ConfigurationNode nx, nz;
190+
nx = node.getNode("x");
191+
nz = node.getNode("z");
192+
193+
if (nx.isVirtual() || nz.isVirtual()) throw new MarkerFileFormatException("Failed to read shape position: Node x or z is not set!");
194+
195+
return new Vector2d(
196+
nx.getDouble(),
197+
nz.getDouble()
198+
);
199+
}
200+
201+
private static Color readColor(ConfigurationNode node) throws MarkerFileFormatException {
202+
ConfigurationNode nr, ng, nb, na;
203+
nr = node.getNode("r");
204+
ng = node.getNode("g");
205+
nb = node.getNode("b");
206+
na = node.getNode("a");
207+
208+
if (nr.isVirtual() || ng.isVirtual() || nb.isVirtual()) throw new MarkerFileFormatException("Failed to read color: Node r,g or b is not set!");
209+
210+
float alpha = na.getFloat(1);
211+
if (alpha < 0 || alpha > 1) throw new MarkerFileFormatException("Failed to read color: alpha value out of range (0-1)!");
212+
213+
try {
214+
return new Color(nr.getInt(), ng.getInt(), nb.getInt(), (int)(alpha * 255));
215+
} catch (IllegalArgumentException ex) {
216+
throw new MarkerFileFormatException("Failed to read color: " + ex.getMessage(), ex);
217+
}
218+
}
219+
220+
private static void writeShape(ConfigurationNode node, Shape shape) {
221+
for (int i = 0; i < shape.getPointCount(); i++) {
222+
ConfigurationNode pointNode = node.appendListNode();
223+
Vector2d point = shape.getPoint(i);
224+
pointNode.getNode("x").setValue(Math.round(point.getX() * 1000d) / 1000d);
225+
pointNode.getNode("z").setValue(Math.round(point.getY() * 1000d) / 1000d);
226+
}
227+
}
228+
229+
private static void writeColor(ConfigurationNode node, Color color) {
230+
int r = color.getRed();
231+
int g = color.getGreen();
232+
int b = color.getBlue();
233+
float a = color.getAlpha() / 255f;
234+
235+
node.getNode("r").setValue(r);
236+
node.getNode("g").setValue(g);
237+
node.getNode("b").setValue(b);
238+
node.getNode("a").setValue(a);
239+
}
240+
241+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* This file is part of BlueMap, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package de.bluecolored.bluemap.common.api.marker;
26+
27+
import com.flowpowered.math.vector.Vector2i;
28+
import com.flowpowered.math.vector.Vector3d;
29+
import de.bluecolored.bluemap.api.BlueMapAPI;
30+
import de.bluecolored.bluemap.api.BlueMapMap;
31+
import de.bluecolored.bluemap.api.marker.HtmlMarker;
32+
import ninja.leaping.configurate.ConfigurationNode;
33+
34+
public class HtmlMarkerImpl extends MarkerImpl implements HtmlMarker {
35+
public static final String MARKER_TYPE = "html";
36+
37+
private String html;
38+
private Vector2i anchor;
39+
40+
private boolean hasUnsavedChanges;
41+
42+
public HtmlMarkerImpl(String id, BlueMapMap map, Vector3d position, String html) {
43+
super(id, map, position);
44+
45+
this.html = html;
46+
this.anchor = new Vector2i(25, 45);
47+
48+
this.hasUnsavedChanges = true;
49+
}
50+
51+
@Override
52+
public String getType() {
53+
return MARKER_TYPE;
54+
}
55+
56+
@Override
57+
public Vector2i getAnchor() {
58+
return anchor;
59+
}
60+
61+
@Override
62+
public void setAnchor(Vector2i anchor) {
63+
this.anchor = anchor;
64+
this.hasUnsavedChanges = true;
65+
}
66+
67+
@Override
68+
public String getHtml() {
69+
return html;
70+
}
71+
72+
@Override
73+
public synchronized void setHtml(String html) {
74+
this.html = html;
75+
this.hasUnsavedChanges = true;
76+
}
77+
78+
@Override
79+
public synchronized void load(BlueMapAPI api, ConfigurationNode markerNode, boolean overwriteChanges) throws MarkerFileFormatException {
80+
super.load(api, markerNode, overwriteChanges);
81+
82+
if (!overwriteChanges && hasUnsavedChanges) return;
83+
this.hasUnsavedChanges = false;
84+
85+
this.html = markerNode.getNode("html").getString("");
86+
this.anchor = readAnchor(markerNode.getNode("anchor"));
87+
}
88+
89+
@Override
90+
public synchronized void save(ConfigurationNode markerNode) {
91+
super.save(markerNode);
92+
93+
markerNode.getNode("html").setValue(this.html);
94+
writeAnchor(markerNode.getNode("anchor"), this.anchor);
95+
96+
hasUnsavedChanges = false;
97+
}
98+
99+
private static Vector2i readAnchor(ConfigurationNode node) {
100+
return new Vector2i(
101+
node.getNode("x").getInt(0),
102+
node.getNode("y").getInt(0)
103+
);
104+
}
105+
106+
private static void writeAnchor(ConfigurationNode node, Vector2i anchor) {
107+
node.getNode("x").setValue(anchor.getX());
108+
node.getNode("y").setValue(anchor.getY());
109+
}
110+
111+
}

0 commit comments

Comments
 (0)