Skip to content

Commit

Permalink
Fakeplayer resident also save inv (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yh-china committed May 21, 2023
1 parent 16d450d commit 828bfd9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 28 deletions.
79 changes: 61 additions & 18 deletions patches/server/0009-Fakeplayer-support.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Subject: [PATCH] Fakeplayer support


diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 3095f4ef8aea1d47d0a8880fcfdaf9d124d83773..fc27cf0cf98a8640e2a451d3f06ac1216eb9686c 100644
index 3095f4ef8aea1d47d0a8880fcfdaf9d124d83773..d2087c9b06fac0c40a1eee3b5f4dfa167f9073fd 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -126,6 +126,7 @@ import net.minecraft.util.profiling.metrics.profiling.ServerMetricsSamplersProvi
Expand All @@ -25,14 +25,11 @@ index 3095f4ef8aea1d47d0a8880fcfdaf9d124d83773..fc27cf0cf98a8640e2a451d3f06ac121
this.server.enablePlugins(org.bukkit.plugin.PluginLoadOrder.POSTWORLD);
this.server.getPluginManager().callEvent(new ServerLoadEvent(ServerLoadEvent.LoadType.STARTUP));
this.connection.acceptConnections();
@@ -942,6 +945,10 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -942,6 +945,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
MinecraftServer.LOGGER.info("Stopping server");
Commands.COMMAND_SENDING_POOL.shutdownNow(); // Paper - Shutdown and don't bother finishing
MinecraftTimings.stopServer(); // Paper
+ // Leaves start - bot
+ top.leavesmc.leaves.bot.ServerBot.saveAllBot(); // Leaves - save
+ top.leavesmc.leaves.bot.ServerBot.removeAllBot(); // Leaves - remove all bot
+ // Leaves end - bot
+ top.leavesmc.leaves.bot.ServerBot.saveOrRemoveAllBot(); // Leaves - save or remove bot
// CraftBukkit start
if (this.server != null) {
this.server.disablePlugins();
Expand Down Expand Up @@ -332,7 +329,7 @@ index 1fb25e8a21b568864974cc81b452ba062890d593..816d78268db3d0be0a70ff3c78e74b52
public final String worldName;
diff --git a/src/main/java/top/leavesmc/leaves/bot/BotCommand.java b/src/main/java/top/leavesmc/leaves/bot/BotCommand.java
new file mode 100644
index 0000000000000000000000000000000000000000..a572488cf07940fa760e8908973ba76cd1c7111f
index 0000000000000000000000000000000000000000..0507d8bfa1100863b5a448d7d36671b658804d21
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/bot/BotCommand.java
@@ -0,0 +1,222 @@
Expand Down Expand Up @@ -461,7 +458,7 @@ index 0000000000000000000000000000000000000000..a572488cf07940fa760e8908973ba76c
+ sender.sendMessage(ChatColor.RED + "Fakeplayer limit is full");
+ }
+
+ ServerBot.createBot(((Player) sender).getLocation(), args[1], args.length > 2 ? args[2] : null);
+ ServerBot.createBot(((Player) sender).getLocation(), args[1], args.length > 2 ? args[2] : null, null);
+ }
+
+ private void onRemove(CommandSender sender, String @NotNull [] args) {
Expand Down Expand Up @@ -785,21 +782,28 @@ index 0000000000000000000000000000000000000000..07b688d376a4af88305e57519a5ae983
+}
diff --git a/src/main/java/top/leavesmc/leaves/bot/BotUtil.java b/src/main/java/top/leavesmc/leaves/bot/BotUtil.java
new file mode 100644
index 0000000000000000000000000000000000000000..406ca19c5cc1006e043873ffcb0db127f75f0f4c
index 0000000000000000000000000000000000000000..f3138e662d7cb58b083e202cd3c623d6ff3bc33d
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/bot/BotUtil.java
@@ -0,0 +1,102 @@
@@ -0,0 +1,141 @@
+package top.leavesmc.leaves.bot;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import net.minecraft.core.NonNullList;
+import net.minecraft.nbt.CompoundTag;
+import net.minecraft.nbt.ListTag;
+import net.minecraft.nbt.NbtIo;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.world.entity.EquipmentSlot;
+import net.minecraft.world.item.ItemStack;
+import net.minecraft.world.level.storage.LevelResource;
+import org.bukkit.Bukkit;
+import org.bukkit.Location;
+import org.jetbrains.annotations.NotNull;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
+
+public class BotUtil {
Expand Down Expand Up @@ -865,6 +869,8 @@ index 0000000000000000000000000000000000000000..406ca19c5cc1006e043873ffcb0db127
+ float pitch = bot.getXRot();
+ String dimension = bot.getLocation().getWorld().getName();
+ String skin = bot.skinName == null ? "null" : bot.skinName;
+ String uuid = bot.getStringUUID();
+
+ JsonObject fakePlayer = new JsonObject();
+ fakePlayer.addProperty("pos_x", pos_x);
+ fakePlayer.addProperty("pos_y", pos_y);
Expand All @@ -873,6 +879,22 @@ index 0000000000000000000000000000000000000000..406ca19c5cc1006e043873ffcb0db127
+ fakePlayer.addProperty("pitch", pitch);
+ fakePlayer.addProperty("dimension", dimension);
+ fakePlayer.addProperty("skin", skin);
+ fakePlayer.addProperty("invuuid", uuid);
+
+ CompoundTag invnbt = new CompoundTag();
+ invnbt.put("Inventory", bot.getInventory().save(new ListTag()));
+
+ File file = MinecraftServer.getServer().getWorldPath(LevelResource.ROOT).resolve("fakeplayer/" + uuid + ".dat").toFile();
+ if (file.isFile()) {
+ file.delete();
+ }
+ try {
+ file.createNewFile();
+ NbtIo.writeCompressed(invnbt, file);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return fakePlayer;
+ }
+
Expand All @@ -887,8 +909,22 @@ index 0000000000000000000000000000000000000000..406ca19c5cc1006e043873ffcb0db127
+ String dimension = fakePlayer.get("dimension").getAsString();
+ String skin = fakePlayer.get("skin").getAsString();
+ skin = skin.equals("null") ? null : skin;
+ ListTag inv = null;
+
+ if (fakePlayer.has("invuuid")) {
+ String uuid = fakePlayer.get("invuuid").getAsString();
+ File file = MinecraftServer.getServer().getWorldPath(LevelResource.ROOT).resolve("fakeplayer/" + uuid + ".dat").toFile();
+ try {
+ CompoundTag nbt = NbtIo.readCompressed(file);
+ inv = nbt.getList("Inventory", 10);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ file.delete();
+ }
+
+ ServerBot.createBot(new Location(Bukkit.getWorld(dimension), pos_x, pos_y, pos_z, yaw, pitch),
+ username, skin);
+ username, skin, inv);
+ }
+}
diff --git a/src/main/java/top/leavesmc/leaves/bot/MojangAPI.java b/src/main/java/top/leavesmc/leaves/bot/MojangAPI.java
Expand Down Expand Up @@ -940,10 +976,10 @@ index 0000000000000000000000000000000000000000..d6466ee4db637106e1394bb462d875e5
+}
diff --git a/src/main/java/top/leavesmc/leaves/bot/ServerBot.java b/src/main/java/top/leavesmc/leaves/bot/ServerBot.java
new file mode 100644
index 0000000000000000000000000000000000000000..8208efc49c9d6e3395a5ca00995da343e57fa215
index 0000000000000000000000000000000000000000..fbdd0f051d933571c65ed032c679255fb31bb7a2
--- /dev/null
+++ b/src/main/java/top/leavesmc/leaves/bot/ServerBot.java
@@ -0,0 +1,782 @@
@@ -0,0 +1,789 @@
+package top.leavesmc.leaves.bot;
+
+import com.google.common.collect.Lists;
Expand All @@ -954,6 +990,7 @@ index 0000000000000000000000000000000000000000..8208efc49c9d6e3395a5ca00995da343
+import com.mojang.authlib.properties.Property;
+import com.mojang.datafixers.util.Pair;
+import net.minecraft.Util;
+import net.minecraft.nbt.ListTag;
+import net.minecraft.network.Connection;
+import net.minecraft.network.PacketSendListener;
+import net.minecraft.network.protocol.Packet;
Expand Down Expand Up @@ -1077,23 +1114,23 @@ index 0000000000000000000000000000000000000000..8208efc49c9d6e3395a5ca00995da343
+ this.fauxSleeping = LeavesConfig.fakeplayerSkipSleep;
+ }
+
+ public static void createBot(Location loc, String name, String skinName) {
+ public static void createBot(Location loc, String name, String skinName, ListTag inv) {
+ String realName = LeavesConfig.fakeplayerPrefix + name + LeavesConfig.fakeplayerSuffix;
+ if (!isCreateLegal(realName)) {
+ return;
+ }
+ if (skinName != null) {
+ Bukkit.getScheduler().runTaskAsynchronously(MINECRAFT_PLUGIN, () -> {
+ String[] skin = MojangAPI.getSkin(skinName);
+ Bukkit.getScheduler().runTask(MINECRAFT_PLUGIN, () -> createBot(loc, realName, skin, skinName));
+ Bukkit.getScheduler().runTask(MINECRAFT_PLUGIN, () -> createBot(loc, realName, skin, skinName, inv));
+ });
+ } else {
+ createBot(loc, realName, null, null);
+ createBot(loc, realName, null, null, inv);
+ }
+ }
+
+ @Nullable
+ public static ServerBot createBot(@NotNull Location loc, @NotNull String name, String[] skin, String skinName) {
+ public static ServerBot createBot(@NotNull Location loc, @NotNull String name, String[] skin, String skinName, ListTag inv) {
+ if (!isCreateLegal(name)) {
+ return null;
+ }
Expand Down Expand Up @@ -1122,6 +1159,10 @@ index 0000000000000000000000000000000000000000..8208efc49c9d6e3395a5ca00995da343
+ Bukkit.broadcastMessage(event.getJoinMessage());
+ }
+
+ if (inv != null) {
+ bot.getInventory().load(inv);
+ }
+
+ bot.teleportTo(loc.getX(), loc.getY(), loc.getZ());
+ bot.setRot(loc.getYaw(), loc.getPitch());
+ bot.getBukkitEntity().setRotation(loc.getYaw(), loc.getPitch());
Expand Down Expand Up @@ -1656,7 +1697,7 @@ index 0000000000000000000000000000000000000000..8208efc49c9d6e3395a5ca00995da343
+ return bot;
+ }
+
+ public static void saveAllBot() {
+ public static void saveOrRemoveAllBot() {
+ if (LeavesConfig.fakeplayerSupport && LeavesConfig.fakeplayerResident) {
+ JsonObject fakePlayerList = new JsonObject();
+ bots.forEach((bot -> {
Expand All @@ -1676,6 +1717,8 @@ index 0000000000000000000000000000000000000000..8208efc49c9d6e3395a5ca00995da343
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ } else {
+ removeAllBot();
+ }
+ }
+
Expand Down
4 changes: 2 additions & 2 deletions patches/server/0031-Config-to-disable-method-profiler.patch
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Subject: [PATCH] Config to disable method profiler
This patch is Powered by Pufferfish(https://github.com/pufferfish-gg/Pufferfish)

diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index fc27cf0cf98a8640e2a451d3f06ac1216eb9686c..161321c0064f55ba3623d24dfabf6a9bfd55e5ae 100644
index d2087c9b06fac0c40a1eee3b5f4dfa167f9073fd..1e68576474d613d0d8da91098a18b384fcace44f 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -2275,6 +2275,11 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -2272,6 +2272,11 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
}

public ProfilerFiller getProfiler() {
Expand Down
4 changes: 2 additions & 2 deletions patches/server/0046-BBOR-Protocol.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Subject: [PATCH] BBOR Protocol


diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 161321c0064f55ba3623d24dfabf6a9bfd55e5ae..b02c2dbed413a06d01e41a3dbd1701d070c27d5b 100644
index 1e68576474d613d0d8da91098a18b384fcace44f..0d51123a5a2c9f72937481bc42e22ca4258bb00e 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1593,6 +1593,8 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -1590,6 +1590,8 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa

this.profiler.popPush("server gui refresh");

Expand Down
4 changes: 2 additions & 2 deletions patches/server/0057-Appleskin-Protocol.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Subject: [PATCH] Appleskin Protocol


diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index b02c2dbed413a06d01e41a3dbd1701d070c27d5b..7c90cc6ceedd4103c210757b2cc8a6ee1f972a5b 100644
index 0d51123a5a2c9f72937481bc42e22ca4258bb00e..41c942edf5227654ab17414b93812104cc19b764 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1594,6 +1594,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -1591,6 +1591,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
this.profiler.popPush("server gui refresh");

top.leavesmc.leaves.protocol.BBORProtocol.tick(); // Leaves - bbor
Expand Down
4 changes: 2 additions & 2 deletions patches/server/0061-Fix-update-suppression-crash.patch
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ index d2f0a0755317f5fa9a1ccf7db346aa77fd287d80..f013fb11379822c4ea307e233e30e1c2
if (listener.shouldPropagateHandlingExceptions()) {
throw exception;
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 7c90cc6ceedd4103c210757b2cc8a6ee1f972a5b..dd3a4213d978180dfc54a779253081bb34323b9e 100644
index 41c942edf5227654ab17414b93812104cc19b764..3f9c077d7c74b975657945fb0ac6087400b60902 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1551,7 +1551,13 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -1548,7 +1548,13 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa

try {
worldserver.timings.doTick.startTiming(); // Spigot
Expand Down
4 changes: 2 additions & 2 deletions patches/server/0062-Bedrock-break-list.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Subject: [PATCH] Bedrock break list


diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index dd3a4213d978180dfc54a779253081bb34323b9e..dedf518a6ebe9f5fa91d9af8be2d4148bf0f7098 100644
index 3f9c077d7c74b975657945fb0ac6087400b60902..ee6f73f0279c12a51d29b81aaaa0e81b173c39a9 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1601,6 +1601,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -1598,6 +1598,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa

top.leavesmc.leaves.protocol.BBORProtocol.tick(); // Leaves - bbor
top.leavesmc.leaves.protocol.AppleSkinProtocol.tick(); // Leaves - appleskin
Expand Down

0 comments on commit 828bfd9

Please sign in to comment.