From e74c8ac9fd80d628cbd0ec867e2b2f226a8f90a7 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Mon, 6 Jul 2020 16:06:31 +0200 Subject: [PATCH] Changed the way to create FakePlayer --- src/main/java/fr/themode/demo/PlayerInit.java | 44 ++++++++++------ .../server/entity/fakeplayer/FakePlayer.java | 52 ++++++++++++++----- .../server/network/ConnectionManager.java | 2 +- 3 files changed, 68 insertions(+), 30 deletions(-) diff --git a/src/main/java/fr/themode/demo/PlayerInit.java b/src/main/java/fr/themode/demo/PlayerInit.java index 767dbd024..bd9b4c236 100644 --- a/src/main/java/fr/themode/demo/PlayerInit.java +++ b/src/main/java/fr/themode/demo/PlayerInit.java @@ -10,13 +10,15 @@ import net.minestom.server.chat.ChatColor; import net.minestom.server.chat.ColoredText; import net.minestom.server.entity.*; import net.minestom.server.entity.damage.DamageType; +import net.minestom.server.entity.fakeplayer.FakePlayer; +import net.minestom.server.entity.fakeplayer.FakePlayerController; import net.minestom.server.entity.type.EntityZombie; import net.minestom.server.event.entity.EntityAttackEvent; +import net.minestom.server.event.entity.EntityDeathEvent; import net.minestom.server.event.item.ItemDropEvent; import net.minestom.server.event.item.ItemUpdateStateEvent; import net.minestom.server.event.item.PickupItemEvent; import net.minestom.server.event.player.*; -import net.minestom.server.gamedata.tags.TagManager; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.instance.InstanceContainer; @@ -29,18 +31,15 @@ import net.minestom.server.item.ItemFlag; import net.minestom.server.item.ItemStack; import net.minestom.server.item.Material; import net.minestom.server.network.ConnectionManager; -import net.minestom.server.network.packet.server.play.TagsPacket; import net.minestom.server.ping.ResponseDataConsumer; import net.minestom.server.timer.TaskRunnable; import net.minestom.server.utils.MathUtils; -import net.minestom.server.utils.NamespaceID; import net.minestom.server.utils.Position; import net.minestom.server.utils.Vector; import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.world.Dimension; -import java.io.FileNotFoundException; import java.util.Map; import java.util.UUID; @@ -65,11 +64,11 @@ public class PlayerInit { // Load some chunks beforehand int loopStart = -2; - int loopEnd = 2; + int loopEnd = 10; for (int x = loopStart; x < loopEnd; x++) for (int z = loopStart; z < loopEnd; z++) { instanceContainer.loadChunk(x, z); - netherTest.loadChunk(x, z); + //netherTest.loadChunk(x, z); } @@ -163,14 +162,18 @@ public class PlayerInit { zombie.setAttribute(Attribute.MOVEMENT_SPEED, 0.25f); zombie.setInstance(player.getInstance()); - /*FakePlayer fakePlayer = new FakePlayer(UUID.randomUUID(), "test"); - fakePlayer.addEventCallback(EntityDeathEvent.class, e -> { - fakePlayer.getController().respawn(); - }); - fakePlayer.setArrowCount(25); - FakePlayerController controller = fakePlayer.getController(); - controller.sendChatMessage("I am a bot!");*/ + FakePlayer.initPlayer(UUID.randomUUID(), "test", false, fakePlayer -> { + //fakePlayer.setInstance(player.getInstance()); + fakePlayer.teleport(player.getPosition()); + fakePlayer.addEventCallback(EntityDeathEvent.class, e -> { + fakePlayer.getController().respawn(); + }); + + fakePlayer.setArrowCount(25); + FakePlayerController controller = fakePlayer.getController(); + controller.sendChatMessage("I am a bot!"); + }); //Hologram hologram = new Hologram(player.getInstance(), player.getPosition(), "Hey guy"); }); @@ -243,9 +246,13 @@ public class PlayerInit { item.setEnchantment(Enchantment.SHARPNESS, (short) 50); player.getInventory().addItemStack(item); - player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 127)); + for (int i = 0; i < 9; i++) { + player.getInventory().setItemStack(i, new ItemStack(Material.STONE, (byte) 127)); + } - //player.setHelmet(new ItemStack(Material.DIAMOND_HELMET, (byte) 1)); + player.setHelmet(new ItemStack(Material.DIAMOND_HELMET, (byte) 1)); + + player.getInventory().setItemStack(41, ItemStack.getAirItem()); inventory.addItemStack(item.clone()); //player.openInventory(inventory); @@ -278,7 +285,12 @@ public class PlayerInit { belowNameScoreboard.updateScore(this, 50);*/ player.sendLegacyMessage("&aIm &bHere", '&'); - player.sendMessage(ColoredText.of("{#ff55ff}test")); + player.sendMessage(ColoredText.of("{#ff55ff}" + ChatColor.RESET + "test")); + + /*RichMessage richMessage = RichMessage.of(ColoredText.of("Hey the item")) + .setHoverEvent(ChatHoverEvent.showItem(new ItemStack(Material.IRON_HELMET, (byte) 1))); + System.out.println(richMessage.toString()); + player.sendMessage(richMessage);*/ }); diff --git a/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java b/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java index f0af18071..6f248a5bc 100644 --- a/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java +++ b/src/main/java/net/minestom/server/entity/fakeplayer/FakePlayer.java @@ -2,24 +2,22 @@ package net.minestom.server.entity.fakeplayer; import net.minestom.server.MinecraftServer; import net.minestom.server.entity.Player; +import net.minestom.server.event.player.PlayerLoginEvent; import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.player.FakePlayerConnection; +import net.minestom.server.timer.TaskRunnable; +import net.minestom.server.utils.time.TimeUnit; +import net.minestom.server.utils.time.UpdateOption; import java.util.UUID; +import java.util.function.Consumer; public class FakePlayer extends Player { private FakePlayerController fakePlayerController; private boolean registered; - /** - * @param uuid the player uuid - * @param username the player username - * @param addInCache should the player be registered internally - * (gettable with {@link ConnectionManager#getOnlinePlayers()} - * and {@link ConnectionManager#getPlayer(String)}) - */ - public FakePlayer(UUID uuid, String username, boolean addInCache) { + private FakePlayer(UUID uuid, String username, boolean addInCache) { super(uuid, username, new FakePlayerConnection()); this.fakePlayerController = new FakePlayerController(this); @@ -32,13 +30,41 @@ public class FakePlayer extends Player { } /** - * Create a FakePlayer not registered in {@link ConnectionManager} + * Init a new FakePlayer * - * @param uuid the player uuid - * @param username the player username + * @param uuid the FakePlayer uuid + * @param username the FakePlayer username + * @param addInCache should the player be registered internally + * (gettable with {@link ConnectionManager#getOnlinePlayers()}) + * @param scheduledCallback the callback called when the FakePlayer is finished logging + * (1 tick after the {@link PlayerLoginEvent}) + * WARNING: it will be called in the + * {@link net.minestom.server.timer.SchedulerManager} thread pool */ - public FakePlayer(UUID uuid, String username) { - this(uuid, username, false); + public static void initPlayer(UUID uuid, String username, boolean addInCache, Consumer scheduledCallback) { + final FakePlayer fakePlayer = new FakePlayer(uuid, username, addInCache); + + fakePlayer.addEventCallback(PlayerLoginEvent.class, event -> { + MinecraftServer.getSchedulerManager().addDelayedTask(new TaskRunnable() { + @Override + public void run() { + scheduledCallback.accept(fakePlayer); + } + }, new UpdateOption(1, TimeUnit.TICK)); + }); + } + + /** + * Init a new FakePlayer without adding him in cache + * + * @param uuid the FakePlayer uuid + * @param username the FakePlayer username + * @param scheduledCallback the callback called when the FakePlayer is finished logging + * WARNING: it will be called in the + * {@link net.minestom.server.timer.SchedulerManager} thread pool + */ + public static void initPlayer(UUID uuid, String username, Consumer scheduledCallback) { + initPlayer(uuid, username, false, scheduledCallback); } public FakePlayerController getController() { diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index 819da365d..103ce67bc 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -186,7 +186,7 @@ public final class ConnectionManager { * Add a new player in the players list * Is currently used at * {@link net.minestom.server.network.packet.client.login.LoginStartPacket#process(PlayerConnection, ConnectionManager)} - * and in {@link net.minestom.server.entity.fakeplayer.FakePlayer#FakePlayer(UUID, String, boolean)} + * and in {@link net.minestom.server.entity.fakeplayer.FakePlayer#initPlayer(UUID, String, boolean, Consumer)} * * @param player the player to add */