mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-23 11:06:29 +01:00
132 lines
3.6 KiB
Diff
132 lines
3.6 KiB
Diff
From 2bce99d815e516c5ea740c9f161933d7f7f5bcdd Mon Sep 17 00:00:00 2001
|
|
From: Aikar <aikar@aikar.co>
|
|
Date: Mon, 28 Mar 2016 20:55:47 -0400
|
|
Subject: [PATCH] MC Utils
|
|
|
|
Collection of utils to help reduce NMS diff
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java
|
|
new file mode 100644
|
|
index 0000000..abe7e9f
|
|
--- /dev/null
|
|
+++ b/src/main/java/net/minecraft/server/MCUtil.java
|
|
@@ -0,0 +1,115 @@
|
|
+package net.minecraft.server;
|
|
+
|
|
+import org.bukkit.Location;
|
|
+
|
|
+import java.util.regex.Pattern;
|
|
+
|
|
+public class MCUtil {
|
|
+ private static final Pattern REPLACE_QUOTES = Pattern.compile("\"");
|
|
+
|
|
+ private MCUtil() {}
|
|
+
|
|
+
|
|
+ /**
|
|
+ * Builds a chat componenent from a string.
|
|
+ * @param str
|
|
+ * @return
|
|
+ */
|
|
+ public static IChatBaseComponent cmpFromMessage(String str) {
|
|
+ return IChatBaseComponent.ChatSerializer.a("{text:\"" + REPLACE_QUOTES.matcher(str).replaceAll("\\\"") + "\"}");
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Calculates distance between 2 entities
|
|
+ * @param e1
|
|
+ * @param e2
|
|
+ * @return
|
|
+ */
|
|
+ public static double distance(Entity e1, Entity e2) {
|
|
+ return Math.sqrt(distanceSq(e1, e2));
|
|
+ }
|
|
+
|
|
+
|
|
+ /**
|
|
+ * Calculates distance between 2 block positions
|
|
+ * @param e1
|
|
+ * @param e2
|
|
+ * @return
|
|
+ */
|
|
+ public static double distance(BlockPosition e1, BlockPosition e2) {
|
|
+ return Math.sqrt(distanceSq(e1, e2));
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Gets the distance between 2 positions
|
|
+ * @param x1
|
|
+ * @param y1
|
|
+ * @param z1
|
|
+ * @param x2
|
|
+ * @param y2
|
|
+ * @param z2
|
|
+ * @return
|
|
+ */
|
|
+ public static double distance(double x1, double y1, double z1, double x2, double y2, double z2) {
|
|
+ return Math.sqrt(distanceSq(x1, y1, z1, x2, y2, z2));
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Get's the distance squared between 2 entities
|
|
+ * @param e1
|
|
+ * @param e2
|
|
+ * @return
|
|
+ */
|
|
+ public static double distanceSq(Entity e1, Entity e2) {
|
|
+ return distanceSq(e1.locX,e1.locY,e1.locZ, e2.locX,e2.locY,e2.locZ);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Gets the distance sqaured between 2 block positions
|
|
+ * @param pos1
|
|
+ * @param pos2
|
|
+ * @return
|
|
+ */
|
|
+ public static double distanceSq(BlockPosition pos1, BlockPosition pos2) {
|
|
+ return distanceSq(pos1.getX(), pos1.getY(), pos1.getZ(), pos2.getX(), pos2.getY(), pos2.getZ());
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Gets the distance squared between 2 positions
|
|
+ * @param x1
|
|
+ * @param y1
|
|
+ * @param z1
|
|
+ * @param x2
|
|
+ * @param y2
|
|
+ * @param z2
|
|
+ * @return
|
|
+ */
|
|
+ public static double distanceSq(double x1, double y1, double z1, double x2, double y2, double z2) {
|
|
+ return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2);
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Converts a NMS World/BlockPosition to Bukkit Location
|
|
+ * @param world
|
|
+ * @param pos
|
|
+ * @return
|
|
+ */
|
|
+ public static Location toLocation(World world, BlockPosition pos) {
|
|
+ return new Location(world.getWorld(), pos.getX(), pos.getY(), pos.getZ());
|
|
+ }
|
|
+
|
|
+ /**
|
|
+ * Converts an NMS entity's current location to a Bukkit Location
|
|
+ * @param entity
|
|
+ * @return
|
|
+ */
|
|
+ public static Location toLocation(Entity entity) {
|
|
+ return new Location(entity.getWorld().getWorld(), entity.locX, entity.locY, entity.locZ);
|
|
+ }
|
|
+
|
|
+ public static boolean isEdgeOfChunk(BlockPosition pos) {
|
|
+ final int modX = pos.getX() & 15;
|
|
+ final int modZ = pos.getZ() & 15;
|
|
+ return (modX == 0 || modX == 15 || modZ == 0 || modZ == 15);
|
|
+ }
|
|
+}
|
|
--
|
|
2.8.0
|
|
|