mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-17 08:27:43 +01:00
Extended OfflinePlayer API
This commit is contained in:
@@ -1,117 +0,0 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: YouHaveTrouble <youhavetrouble@youhavetrouble.me>
|
||||
Date: Sun, 22 Aug 2021 05:11:09 +0200
|
||||
Subject: [PATCH] Extended OfflinePlayer API
|
||||
|
||||
|
||||
diff --git a/src/main/java/org/bukkit/OfflinePlayer.java b/src/main/java/org/bukkit/OfflinePlayer.java
|
||||
index 5622fe3165baad8138c22cfc016ed6c3834cf702..6d31b561d915180fcd473b317721064feed28f37 100644
|
||||
--- a/src/main/java/org/bukkit/OfflinePlayer.java
|
||||
+++ b/src/main/java/org/bukkit/OfflinePlayer.java
|
||||
@@ -573,4 +573,106 @@ public interface OfflinePlayer extends ServerOperator, AnimalTamer, Configuratio
|
||||
@Override
|
||||
io.papermc.paper.persistence.@NotNull PersistentDataContainerView getPersistentDataContainer();
|
||||
// Paper end - add pdc to offline player
|
||||
+
|
||||
+ // Purpur start - OfflinePlayer API
|
||||
+ /**
|
||||
+ * Determines if the OfflinePlayer is allowed to fly via jump key double-tap like
|
||||
+ * in creative mode.
|
||||
+ *
|
||||
+ * @return True if the player is allowed to fly.
|
||||
+ */
|
||||
+ public boolean getAllowFlight();
|
||||
+
|
||||
+ /**
|
||||
+ * Sets if the OfflinePlayer is allowed to fly via jump key double-tap like in
|
||||
+ * creative mode.
|
||||
+ *
|
||||
+ * @param flight If flight should be allowed.
|
||||
+ */
|
||||
+ public void setAllowFlight(boolean flight);
|
||||
+
|
||||
+ /**
|
||||
+ * Checks to see if this player is currently flying or not.
|
||||
+ *
|
||||
+ * @return True if the player is flying, else false.
|
||||
+ */
|
||||
+ public boolean isFlying();
|
||||
+
|
||||
+ /**
|
||||
+ * Makes this player start or stop flying.
|
||||
+ *
|
||||
+ * @param value True to fly.
|
||||
+ */
|
||||
+ public void setFlying(boolean value);
|
||||
+
|
||||
+ /**
|
||||
+ * Sets the speed at which a client will fly. Negative values indicate
|
||||
+ * reverse directions.
|
||||
+ *
|
||||
+ * @param value The new speed, from -1 to 1.
|
||||
+ * @throws IllegalArgumentException If new speed is less than -1 or
|
||||
+ * greater than 1
|
||||
+ */
|
||||
+ public void setFlySpeed(float value) throws IllegalArgumentException;
|
||||
+
|
||||
+ /**
|
||||
+ * Sets the speed at which a client will walk. Negative values indicate
|
||||
+ * reverse directions.
|
||||
+ *
|
||||
+ * @param value The new speed, from -1 to 1.
|
||||
+ * @throws IllegalArgumentException If new speed is less than -1 or
|
||||
+ * greater than 1
|
||||
+ */
|
||||
+ public void setWalkSpeed(float value) throws IllegalArgumentException;
|
||||
+
|
||||
+ /**
|
||||
+ * Gets the current allowed speed that a client can fly.
|
||||
+ *
|
||||
+ * @return The current allowed speed, from -1 to 1
|
||||
+ */
|
||||
+ public float getFlySpeed();
|
||||
+
|
||||
+ /**
|
||||
+ * Gets the current allowed speed that a client can walk.
|
||||
+ *
|
||||
+ * @return The current allowed speed, from -1 to 1
|
||||
+ */
|
||||
+ public float getWalkSpeed();
|
||||
+
|
||||
+ /**
|
||||
+ * Sets OfflinePlayer's location. If player is online, it falls back to the Player#teleport implementation.
|
||||
+ *
|
||||
+ * @param destination
|
||||
+ * @return true if teleportation was successful
|
||||
+ */
|
||||
+ public boolean teleportOffline(@NotNull org.bukkit.Location destination);
|
||||
+
|
||||
+ /**
|
||||
+ * Sets OfflinePlayer's location. If player is online, it falls back to the Player#teleport implementation.
|
||||
+ *
|
||||
+ * @param destination
|
||||
+ * @param cause Teleport cause used if player is online
|
||||
+ * @return true if teleportation was successful
|
||||
+ */
|
||||
+ public boolean teleportOffline(@NotNull org.bukkit.Location destination, @NotNull org.bukkit.event.player.PlayerTeleportEvent.TeleportCause cause);
|
||||
+
|
||||
+ /**
|
||||
+ * Sets OfflinePlayer's location. If player is online, it falls back to the Player#teleportAsync implementation.
|
||||
+ *
|
||||
+ * @param destination
|
||||
+ * @return <code>true</code> if teleportation successful
|
||||
+ */
|
||||
+ @NotNull
|
||||
+ public java.util.concurrent.CompletableFuture<Boolean> teleportOfflineAsync(@NotNull Location destination);
|
||||
+
|
||||
+ /**
|
||||
+ * Sets OfflinePlayer's location. If player is online, it falls back to the Player#teleportAsync implementation.
|
||||
+ *
|
||||
+ * @param destination
|
||||
+ * @param cause Teleport cause used if player is online
|
||||
+ * @return <code>true</code> if teleportation successful
|
||||
+ */
|
||||
+ @NotNull
|
||||
+ public java.util.concurrent.CompletableFuture<Boolean> teleportOfflineAsync(@NotNull Location destination, @NotNull org.bukkit.event.player.PlayerTeleportEvent.TeleportCause cause);
|
||||
+ // Purpur end - OfflinePlayer API
|
||||
}
|
||||
@@ -1,277 +0,0 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: YouHaveTrouble <youhavetrouble@youhavetrouble.me>
|
||||
Date: Sun, 22 Aug 2021 05:12:05 +0200
|
||||
Subject: [PATCH] Extended OfflinePlayer API
|
||||
|
||||
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
|
||||
index 94ca0407303c4493ab4928b12ec6ecc75aaca549..a138e1b6b66d99f2035de054137a607aa6b7f0b9 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java
|
||||
@@ -363,14 +363,26 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
|
||||
|
||||
@Override
|
||||
public Location getLocation() {
|
||||
+ // Purpur start
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.getPlayer().getLocation();
|
||||
+ }
|
||||
+ // Purpur end
|
||||
+
|
||||
CompoundTag data = this.getData();
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
- if (data.contains("Pos") && data.contains("Rotation")) {
|
||||
- ListTag position = (ListTag) data.get("Pos");
|
||||
- ListTag rotation = (ListTag) data.get("Rotation");
|
||||
+ // Purpur start - OfflinePlayer API
|
||||
+ //if (data.contains("Pos") && data.contains("Rotation")) {
|
||||
+ ListTag position = data.getList("Pos", net.minecraft.nbt.Tag.TAG_DOUBLE);
|
||||
+ ListTag rotation = data.getList("Rotation", net.minecraft.nbt.Tag.TAG_FLOAT);
|
||||
+
|
||||
+ if (position.isEmpty() && rotation.isEmpty()) {
|
||||
+ return null;
|
||||
+ }
|
||||
+ // Purpur end - OfflinePlayer API
|
||||
|
||||
UUID uuid = new UUID(data.getLong("WorldUUIDMost"), data.getLong("WorldUUIDLeast"));
|
||||
|
||||
@@ -381,9 +393,9 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
|
||||
rotation.getFloat(0),
|
||||
rotation.getFloat(1)
|
||||
);
|
||||
- }
|
||||
+ //} // Purpur - OfflinePlayer API
|
||||
|
||||
- return null;
|
||||
+ //return null; // Purpur - OfflinePlayer API
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -626,4 +638,191 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
|
||||
manager.save();
|
||||
}
|
||||
}
|
||||
+
|
||||
+ // Purpur start - OfflinePlayer API
|
||||
+ @Override
|
||||
+ public boolean getAllowFlight() {
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.getPlayer().getAllowFlight();
|
||||
+ } else {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return false;
|
||||
+ if (!data.contains("abilities")) return false;
|
||||
+ CompoundTag abilities = data.getCompound("abilities");
|
||||
+ return abilities.getByte("mayfly") == (byte) 1;
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void setAllowFlight(boolean flight) {
|
||||
+ if (this.isOnline()) {
|
||||
+ this.getPlayer().setAllowFlight(flight);
|
||||
+ } else {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return;
|
||||
+ if (!data.contains("abilities")) return;
|
||||
+ CompoundTag abilities = data.getCompound("abilities");
|
||||
+ abilities.putByte("mayfly", (byte) (flight ? 1 : 0));
|
||||
+ data.put("abilities", abilities);
|
||||
+ save(data);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean isFlying() {
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.isFlying();
|
||||
+ } else {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return false;
|
||||
+ if (!data.contains("abilities")) return false;
|
||||
+ CompoundTag abilities = data.getCompound("abilities");
|
||||
+ return abilities.getByte("flying") == (byte) 1;
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void setFlying(boolean value) {
|
||||
+ if (this.isOnline()) {
|
||||
+ this.getPlayer().setFlying(value);
|
||||
+ } else {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return;
|
||||
+ if (!data.contains("abilities")) return;
|
||||
+ CompoundTag abilities = data.getCompound("abilities");
|
||||
+ abilities.putByte("mayfly", (byte) (value ? 1 : 0));
|
||||
+ data.put("abilities", abilities);
|
||||
+ save(data);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void setFlySpeed(float value) throws IllegalArgumentException {
|
||||
+ if (value < -1f || value > 1f) throw new IllegalArgumentException("FlySpeed needs to be between -1 and 1");
|
||||
+ if (this.isOnline()) {
|
||||
+ this.getPlayer().setFlySpeed(value);
|
||||
+ } else {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return;
|
||||
+ if (!data.contains("abilities")) return;
|
||||
+ CompoundTag abilities = data.getCompound("abilities");
|
||||
+ abilities.putFloat("flySpeed", value);
|
||||
+ data.put("abilities", abilities);
|
||||
+ save(data);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public float getFlySpeed() {
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.getPlayer().getFlySpeed();
|
||||
+ } else {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return 0;
|
||||
+ if (!data.contains("abilities")) return 0;
|
||||
+ CompoundTag abilities = data.getCompound("abilities");
|
||||
+ return abilities.getFloat("flySpeed");
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public void setWalkSpeed(float value) throws IllegalArgumentException {
|
||||
+ if (value < -1f || value > 1f) throw new IllegalArgumentException("WalkSpeed needs to be between -1 and 1");
|
||||
+ if (this.isOnline()) {
|
||||
+ this.getPlayer().setWalkSpeed(value);
|
||||
+ } else {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return;
|
||||
+ if (!data.contains("abilities")) return;
|
||||
+ CompoundTag abilities = data.getCompound("abilities");
|
||||
+ abilities.putFloat("walkSpeed", value);
|
||||
+ data.put("abilities", abilities);
|
||||
+ save(data);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public float getWalkSpeed() {
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.getPlayer().getWalkSpeed();
|
||||
+ } else {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return 0;
|
||||
+ if (!data.contains("abilities")) return 0;
|
||||
+ CompoundTag abilities = data.getCompound("abilities");
|
||||
+ return abilities.getFloat("walkSpeed");
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean teleportOffline(Location destination) {
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.getPlayer().teleport(destination);
|
||||
+ } else {
|
||||
+ return setLocation(destination);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean teleportOffline(Location destination, org.bukkit.event.player.PlayerTeleportEvent.TeleportCause cause){
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.getPlayer().teleport(destination, cause);
|
||||
+ } else {
|
||||
+ return setLocation(destination);
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public java.util.concurrent.CompletableFuture<Boolean> teleportOfflineAsync(Location destination) {
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.getPlayer().teleportAsync(destination);
|
||||
+ } else {
|
||||
+ return java.util.concurrent.CompletableFuture.completedFuture(setLocation(destination));
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public java.util.concurrent.CompletableFuture<Boolean> teleportOfflineAsync(Location destination, org.bukkit.event.player.PlayerTeleportEvent.TeleportCause cause) {
|
||||
+ if (this.isOnline()) {
|
||||
+ return this.getPlayer().teleportAsync(destination, cause);
|
||||
+ } else {
|
||||
+ return java.util.concurrent.CompletableFuture.completedFuture(setLocation(destination));
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ private boolean setLocation(Location location) {
|
||||
+ CompoundTag data = this.getData();
|
||||
+ if (data == null) return false;
|
||||
+ data.putLong("WorldUUIDMost", location.getWorld().getUID().getMostSignificantBits());
|
||||
+ data.putLong("WorldUUIDLeast", location.getWorld().getUID().getLeastSignificantBits());
|
||||
+ net.minecraft.nbt.ListTag position = new net.minecraft.nbt.ListTag();
|
||||
+ position.add(net.minecraft.nbt.DoubleTag.valueOf(location.getX()));
|
||||
+ position.add(net.minecraft.nbt.DoubleTag.valueOf(location.getY()));
|
||||
+ position.add(net.minecraft.nbt.DoubleTag.valueOf(location.getZ()));
|
||||
+ data.put("Pos", position);
|
||||
+ net.minecraft.nbt.ListTag rotation = new net.minecraft.nbt.ListTag();
|
||||
+ rotation.add(net.minecraft.nbt.FloatTag.valueOf(location.getYaw()));
|
||||
+ rotation.add(net.minecraft.nbt.FloatTag.valueOf(location.getPitch()));
|
||||
+ data.put("Rotation", rotation);
|
||||
+ save(data);
|
||||
+ return true;
|
||||
+ }
|
||||
+
|
||||
+ /**
|
||||
+ * Safely replaces player's .dat file with provided CompoundTag
|
||||
+ * @param compoundTag
|
||||
+ */
|
||||
+ private void save(CompoundTag compoundTag) {
|
||||
+ File playerDir = server.console.playerDataStorage.getPlayerDir();
|
||||
+ try {
|
||||
+ File tempFile = File.createTempFile(this.getUniqueId()+"-", ".dat", playerDir);
|
||||
+ net.minecraft.nbt.NbtIo.writeCompressed(compoundTag, tempFile.toPath());
|
||||
+ File playerDataFile = new File(playerDir, this.getUniqueId()+".dat");
|
||||
+ File playerDataFileOld = new File(playerDir, this.getUniqueId()+".dat_old");
|
||||
+ net.minecraft.Util.safeReplaceFile(playerDataFile.toPath(), tempFile.toPath(), playerDataFileOld.toPath());
|
||||
+ } catch (java.io.IOException e) {
|
||||
+ e.printStackTrace();
|
||||
+ }
|
||||
+ }
|
||||
+ // Purpur end - OfflinePlayer API
|
||||
}
|
||||
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
||||
index ea8b1f01b2dd626c422649488c1e605a8054dbf2..da20ebe8858b4ab5bf8ac62aeac1942320bfa91c 100644
|
||||
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
||||
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
|
||||
@@ -2769,6 +2769,28 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
||||
return this.getHandle().getAbilities().walkingSpeed * 2f;
|
||||
}
|
||||
|
||||
+ // Purpur start - OfflinePlayer API
|
||||
+ @Override
|
||||
+ public boolean teleportOffline(@NotNull Location destination) {
|
||||
+ return this.teleport(destination);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public boolean teleportOffline(Location destination, PlayerTeleportEvent.TeleportCause cause) {
|
||||
+ return this.teleport(destination, cause);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public java.util.concurrent.CompletableFuture<Boolean> teleportOfflineAsync(@NotNull Location destination) {
|
||||
+ return this.teleportAsync(destination);
|
||||
+ }
|
||||
+
|
||||
+ @Override
|
||||
+ public java.util.concurrent.CompletableFuture<Boolean> teleportOfflineAsync(@NotNull Location destination, PlayerTeleportEvent.TeleportCause cause) {
|
||||
+ return this.teleportAsync(destination, cause);
|
||||
+ }
|
||||
+ // Purpur end - OfflinePlayer API
|
||||
+
|
||||
private void validateSpeed(float value) {
|
||||
Preconditions.checkArgument(value <= 1f && value >= -1f, "Speed value (%s) need to be between -1f and 1f", value);
|
||||
}
|
||||
Reference in New Issue
Block a user