Skip to content

Commit bf783b2

Browse files
committed
Added entity id utilities
1 parent f0e45a1 commit bf783b2

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.pianoman911.mapengine.api.clientside;
2+
3+
/**
4+
* Single frame of a map display.
5+
* The main purpose of this is to provide the entity IDs of the item frame and the interaction
6+
* entity for own packet handling and entity modification.
7+
* <p>
8+
* <b>Note: Frames only exist on the network level,
9+
* so the server doesn't know about this frame entity.
10+
*/
11+
public interface IFrame {
12+
13+
/**
14+
* <b>Note: Frames only exist on the network level,
15+
* so the server doesn't know about this frame entity.
16+
*
17+
* @return the entity id of the item frame
18+
*/
19+
int frameEntityId();
20+
21+
/**
22+
* <b>Note: Frames only exist on the network level,
23+
* so the server doesn't know about this frame entity.
24+
*
25+
* @return the entity id of the interaction entity
26+
*/
27+
int interactionEntityId();
28+
}

api/src/main/java/de/pianoman911/mapengine/api/clientside/IMapDisplay.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package de.pianoman911.mapengine.api.clientside;
22

3+
import de.pianoman911.mapengine.api.util.Vec2i;
34
import org.bukkit.block.BlockFace;
45
import org.bukkit.entity.Player;
56
import org.bukkit.util.BoundingBox;
67
import org.jetbrains.annotations.Range;
78

9+
import java.util.function.BiConsumer;
10+
811
/**
912
* A map display which creates packet-level item frame
1013
* displays with a custom width and height.
@@ -174,4 +177,50 @@ default void spawn(Player player) {
174177
* to update the map ids.</strong>
175178
*/
176179
void cutOffCloneGroupIds();
180+
181+
/**
182+
* Returns the frames of this display in a flattened 2d array.
183+
* Frames are ordered from left to right, top to bottom.
184+
* <p>
185+
* x = 0 &and; y = 0 is positioned at the top left.
186+
* The index of this is 0.
187+
* <p>
188+
* Use {@link #frameAt(int, int)} to easily get a specific
189+
* frame of this display.
190+
*
191+
* @return the frames of this display
192+
* @see #frameAt(int, int)
193+
*/
194+
IFrame[] frames();
195+
196+
/**
197+
* Returns the frame at the given x and y position.
198+
*
199+
* @param x the x position
200+
* @param y the y position
201+
* @return the frame at the given position
202+
* @throws ArrayIndexOutOfBoundsException if the x or y position is out of bounds
203+
*/
204+
default IFrame frameAt(int x, int y) {
205+
int width = this.width();
206+
if (x < 0 || x >= width || y < 0 || y >= this.height()) {
207+
throw new ArrayIndexOutOfBoundsException("x or y position is out of bounds");
208+
}
209+
return this.frames()[x + y * width];
210+
}
211+
212+
/**
213+
* Consumes all frames of this display with a consumer.<br>
214+
* The arguments provided to the specified consumer are immutable.
215+
*
216+
* @param consumer the consumer to consume the frames
217+
*/
218+
default void consumeFrames(BiConsumer<IFrame, Vec2i> consumer) {
219+
IFrame[] frames = this.frames();
220+
for (int y = 0, w = this.width(), h = this.height(); y < h; ++y) {
221+
for (int x = 0; x < w; ++x) {
222+
consumer.accept(frames[x + y * w], new Vec2i(x, y));
223+
}
224+
}
225+
}
177226
}

plugin/src/main/java/de/pianoman911/mapengine/core/clientside/Frame.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.pianoman911.mapengine.core.clientside;
22

3+
import de.pianoman911.mapengine.api.clientside.IFrame;
34
import de.pianoman911.mapengine.common.platform.PacketContainer;
45
import de.pianoman911.mapengine.core.MapEnginePlugin;
56
import it.unimi.dsi.fastutil.ints.IntArrayList;
@@ -8,7 +9,7 @@
89
import org.bukkit.util.BlockVector;
910
import org.bukkit.util.Vector;
1011

11-
public class Frame extends FilledMap {
12+
public class Frame extends FilledMap implements IFrame {
1213

1314
public static final double INVISIBLE_MAP_DEPTH = 0.0078125;
1415
public static final double INTERACTION_OFFSET = 0.0626;
@@ -85,4 +86,14 @@ protected PacketContainer<?> rotationPacket(float yaw, float pitch) {
8586
public PacketContainer<?> itemRotationPacket(int rotation) {
8687
return plugin.platform().createItemRotationPacket(entityId, rotation);
8788
}
89+
90+
@Override
91+
public int frameEntityId() {
92+
return this.entityId;
93+
}
94+
95+
@Override
96+
public int interactionEntityId() {
97+
return this.interactionId;
98+
}
8899
}

plugin/src/main/java/de/pianoman911/mapengine/core/clientside/FrameContainer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ public void cutOffCloneGroupIds() {
280280
}
281281
}
282282

283+
@Override
284+
public Frame[] frames() {
285+
return this.frames;
286+
}
287+
283288
private void spawn0(Player player, BlockFace visualDirection, int z) {
284289
for (Frame frame : frames) {
285290
frame.spawnPacket(visualDirection, this.glowing).send(player);

0 commit comments

Comments
 (0)