|
| 1 | +package dev.letsgoaway.mapxyz; |
| 2 | + |
| 3 | +import org.bukkit.World; |
| 4 | +import org.bukkit.entity.Player; |
| 5 | +import org.bukkit.map.*; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.logging.Level; |
| 9 | + |
| 10 | +@SuppressWarnings("deprecation") |
| 11 | +public class PlayerCursorRenderer { |
| 12 | + |
| 13 | + private static final MapCursor.Type MARKER_TYPE = MapCursor.Type.BLUE_MARKER; |
| 14 | + private static final MapCursor.Type NETHER_MARKER_TYPE = MapCursor.Type.RED_MARKER; |
| 15 | + |
| 16 | + public static void render(Player viewer, Player player, MapCanvas canvas, MapView map) { |
| 17 | + if (viewer.equals(player)) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + int scaleFactor = 1 << map.getScale().getValue(); |
| 22 | + float x = (float)(player.getLocation().getX() - (double)map.getCenterX()) / (float)scaleFactor; |
| 23 | + float z = (float)(player.getLocation().getZ() - (double)map.getCenterZ()) / (float)scaleFactor; |
| 24 | + |
| 25 | + byte d = calculateRotation(player); |
| 26 | + |
| 27 | + // Nether marker type matches Bedrock Edition locator maps |
| 28 | + canvas.getCursors().addCursor(new MapCursor(clampMapCoordinate(x), clampMapCoordinate(z), d, map.getWorld().getEnvironment().equals(World.Environment.NETHER) ? NETHER_MARKER_TYPE : MARKER_TYPE, true, player.getName())); |
| 29 | + } |
| 30 | + |
| 31 | + private static byte clampMapCoordinate(float coordinate) { |
| 32 | + if (coordinate <= -63.0F) { |
| 33 | + return -128; |
| 34 | + } else { |
| 35 | + return coordinate >= 63.0F ? 127 : (byte)((int)((double)(coordinate * 2.0F) + 0.5)); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private static byte calculateRotation(Player player) { |
| 40 | + double yaw = player.getLocation().getYaw(); |
| 41 | + boolean shouldFlip = 0.0 > yaw; |
| 42 | + World world = player.getLocation().getWorld(); |
| 43 | + if (world != null && world.getEnvironment().equals(World.Environment.NETHER)) { |
| 44 | + // use world.getFullTime() if its the same player |
| 45 | + // but im going to use player.getTicksLived() |
| 46 | + // to make sure it looks different from your marker |
| 47 | + int i = (int) (player.getTicksLived() / 10L); |
| 48 | + return (byte) (i * i * 34187121 + i * 121 >> 15 & 15); |
| 49 | + } else { |
| 50 | + double adjusted = yaw < 0.0 ? yaw - 8.0 : yaw + 8.0; |
| 51 | + if (shouldFlip) { |
| 52 | + int i = 16 + ((int) (adjusted * 16.0 / 360.0)); |
| 53 | + // todo figure out better way to fix |
| 54 | + if (i == 16) { |
| 55 | + return (byte) 0; |
| 56 | + } |
| 57 | + return (byte) (i); |
| 58 | + } else { |
| 59 | + return (byte) ((int) (adjusted * 16.0 / 360.0)); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static void renderAll(Player viewer, MapCanvas canvas, MapView map) { |
| 65 | + for (int i = 0; i < canvas.getCursors().size(); i++) { |
| 66 | + MapCursor c = canvas.getCursors().getCursor(i); |
| 67 | + MapCursor.Type t = c.getType(); |
| 68 | + |
| 69 | + if (t.equals(MARKER_TYPE) || t.equals(NETHER_MARKER_TYPE)) { |
| 70 | + canvas.getCursors().removeCursor(c); |
| 71 | + } |
| 72 | + |
| 73 | + if (Config.enableLocatorMaps && (t.equals(MapCursor.Type.PLAYER) || t.equals(MapCursor.Type.PLAYER_OFF_MAP) || t.equals(MapCursor.Type.PLAYER_OFF_LIMITS))){ |
| 74 | + canvas.getCursors().removeCursor(c); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (!Config.enableLocatorMaps) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + World world = map.getWorld(); |
| 83 | + if (world != null) { |
| 84 | + for (Player player : world.getPlayers()) { |
| 85 | + render(viewer, player, canvas, map); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments