From 5837b59a417bc5ddc4c29d329db9998ad92b9239 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sat, 20 Jun 2020 19:07:34 +0200 Subject: [PATCH 1/5] Give the player username to the UUID provider --- .../java/net/minestom/server/network/ConnectionManager.java | 4 ++-- src/main/java/net/minestom/server/network/UuidProvider.java | 2 +- .../server/network/packet/client/login/LoginStartPacket.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index cd864c555..390d842e6 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -110,10 +110,10 @@ public final class ConnectionManager { * @return the uuid based on {@code playerConnection} * return a random UUID if no UUID provider is defined see {@link #setUuidProvider(UuidProvider)} */ - public UUID getPlayerConnectionUuid(PlayerConnection playerConnection) { + public UUID getPlayerConnectionUuid(PlayerConnection playerConnection, String username) { if (uuidProvider == null) return UUID.randomUUID(); - return uuidProvider.provide(playerConnection); + return uuidProvider.provide(playerConnection, username); } /** diff --git a/src/main/java/net/minestom/server/network/UuidProvider.java b/src/main/java/net/minestom/server/network/UuidProvider.java index 23d928215..5dbd05249 100644 --- a/src/main/java/net/minestom/server/network/UuidProvider.java +++ b/src/main/java/net/minestom/server/network/UuidProvider.java @@ -6,5 +6,5 @@ import java.util.UUID; @FunctionalInterface public interface UuidProvider { - UUID provide(PlayerConnection playerConnection); + UUID provide(PlayerConnection playerConnection, String username); } diff --git a/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java b/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java index 619a69ba2..851e694bc 100644 --- a/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java @@ -17,7 +17,7 @@ public class LoginStartPacket implements ClientPreplayPacket { public void process(PlayerConnection connection, ConnectionManager connectionManager) { // TODO send encryption request OR directly login success - UUID playerUuid = connectionManager.getPlayerConnectionUuid(connection); + UUID playerUuid = connectionManager.getPlayerConnectionUuid(connection, username); LoginSuccessPacket successPacket = new LoginSuccessPacket(playerUuid, username); connection.sendPacket(successPacket); From eba5c5ae3a1a681c37e5b116eef7bffa6a88f0bf Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sun, 21 Jun 2020 14:01:03 +0200 Subject: [PATCH 2/5] Added CommandSender and ConsoleSender --- build.gradle | 3 ++ .../demo/commands/DimensionCommand.java | 18 ++++--- .../demo/commands/GamemodeCommand.java | 27 ++++++---- .../themode/demo/commands/HealthCommand.java | 31 +++++------ .../themode/demo/commands/SimpleCommand.java | 42 ++++++++++++++- .../server/command/CommandManager.java | 54 +++++++++++++------ .../server/command/CommandProcessor.java | 2 +- .../server/command/CommandSender.java | 13 +++++ .../server/command/ConsoleSender.java | 8 +++ .../net/minestom/server/entity/Player.java | 4 +- 10 files changed, 152 insertions(+), 50 deletions(-) create mode 100644 src/main/java/net/minestom/server/command/CommandSender.java create mode 100644 src/main/java/net/minestom/server/command/ConsoleSender.java diff --git a/build.gradle b/build.gradle index 21c034e18..dbdf8df28 100644 --- a/build.gradle +++ b/build.gradle @@ -47,5 +47,8 @@ dependencies { api 'net.kyori:text-serializer-legacy:3.0.3' api 'net.kyori:text-serializer-gson:3.0.3' api 'net.kyori:text-serializer-plain:3.0.3' + + // Pathfinding + implementation 'com.github.MadMartian:hydrazine-path-finding:1.02' } diff --git a/src/main/java/fr/themode/demo/commands/DimensionCommand.java b/src/main/java/fr/themode/demo/commands/DimensionCommand.java index 2cd0e973b..7eca98c89 100644 --- a/src/main/java/fr/themode/demo/commands/DimensionCommand.java +++ b/src/main/java/fr/themode/demo/commands/DimensionCommand.java @@ -2,6 +2,7 @@ package fr.themode.demo.commands; import net.minestom.server.MinecraftServer; import net.minestom.server.command.CommandProcessor; +import net.minestom.server.command.CommandSender; import net.minestom.server.entity.Player; import net.minestom.server.instance.Instance; import net.minestom.server.world.Dimension; @@ -20,22 +21,27 @@ public class DimensionCommand implements CommandProcessor { } @Override - public boolean process(Player player, String command, String[] args) { + public boolean process(CommandSender sender, String command, String[] args) { + + if (!(sender instanceof Player)) + return false; + Player player = (Player) sender; + Instance instance = player.getInstance(); Dimension targetDimension = Dimension.NETHER; - if(instance.getDimension() == Dimension.NETHER) { + if (instance.getDimension() == Dimension.NETHER) { targetDimension = Dimension.OVERWORLD; } Dimension finalTargetDimension = targetDimension; Optional targetInstance = MinecraftServer.getInstanceManager().getInstances().stream().filter(in -> in.getDimension() == finalTargetDimension).findFirst(); - if(targetInstance.isPresent()) { - player.sendMessage("You were in "+instance.getDimension()); + if (targetInstance.isPresent()) { + player.sendMessage("You were in " + instance.getDimension()); player.setInstance(targetInstance.get()); - player.sendMessage("You are now in "+targetDimension); + player.sendMessage("You are now in " + targetDimension); } else { - player.sendMessage("Could not find instance with dimension "+targetDimension); + player.sendMessage("Could not find instance with dimension " + targetDimension); } return true; diff --git a/src/main/java/fr/themode/demo/commands/GamemodeCommand.java b/src/main/java/fr/themode/demo/commands/GamemodeCommand.java index d90eb186f..77d8f4df4 100644 --- a/src/main/java/fr/themode/demo/commands/GamemodeCommand.java +++ b/src/main/java/fr/themode/demo/commands/GamemodeCommand.java @@ -4,6 +4,7 @@ import fr.themode.command.Arguments; import fr.themode.command.Command; import fr.themode.command.arguments.Argument; import fr.themode.command.arguments.ArgumentType; +import net.minestom.server.command.CommandSender; import net.minestom.server.entity.GameMode; import net.minestom.server.entity.Player; @@ -12,7 +13,7 @@ import java.util.Optional; /** * Command that make a player change gamemode */ -public class GamemodeCommand extends Command { +public class GamemodeCommand extends Command { public GamemodeCommand() { super("gamemode", "g", "gm"); @@ -35,11 +36,13 @@ public class GamemodeCommand extends Command { addSyntax(this::executeOnOther, player, mode); } - private void usage(Player player, Arguments arguments) { - player.sendMessage("Usage: /gamemode [player] "); + private void usage(CommandSender sender, Arguments arguments) { + sender.sendMessage("Usage: /gamemode [player] "); } - private void executeOnSelf(Player player, Arguments arguments) { + private void executeOnSelf(CommandSender sender, Arguments arguments) { + Player player = (Player) sender; + String gamemodeName = arguments.getWord("mode"); GameMode mode = GameMode.valueOf(gamemodeName.toUpperCase()); assert mode != null; // mode is not supposed to be null, because gamemodeName will be valid @@ -47,7 +50,9 @@ public class GamemodeCommand extends Command { player.sendMessage("You are now playing in " + gamemodeName); } - private void executeOnOther(Player player, Arguments arguments) { + private void executeOnOther(CommandSender sender, Arguments arguments) { + Player player = (Player) sender; + String gamemodeName = arguments.getWord("mode"); String targetName = arguments.getWord("player"); GameMode mode = GameMode.valueOf(gamemodeName.toUpperCase()); @@ -61,11 +66,15 @@ public class GamemodeCommand extends Command { } } - private void gameModeCallback(Player player, String gamemode, int error) { - player.sendMessage("'" + gamemode + "' is not a valid gamemode!"); + private void gameModeCallback(CommandSender sender, String gamemode, int error) { + sender.sendMessage("'" + gamemode + "' is not a valid gamemode!"); } - private boolean isAllowed(Player player) { - return true; // TODO: permissions + private boolean isAllowed(CommandSender sender) { + if (!(sender instanceof Player)) { + sender.sendMessage("The command is only available for player"); + return false; + } + return true; } } diff --git a/src/main/java/fr/themode/demo/commands/HealthCommand.java b/src/main/java/fr/themode/demo/commands/HealthCommand.java index be97c41a3..52dc7a26a 100644 --- a/src/main/java/fr/themode/demo/commands/HealthCommand.java +++ b/src/main/java/fr/themode/demo/commands/HealthCommand.java @@ -5,9 +5,10 @@ import fr.themode.command.Command; import fr.themode.command.arguments.Argument; import fr.themode.command.arguments.ArgumentType; import fr.themode.command.arguments.number.ArgumentNumber; +import net.minestom.server.command.CommandSender; import net.minestom.server.entity.Player; -public class HealthCommand extends Command { +public class HealthCommand extends Command { public HealthCommand() { super("health", "h", "healthbar"); @@ -27,39 +28,39 @@ public class HealthCommand extends Command { addSyntax(this::execute2, arg0, arg1); } - private boolean condition(Player player) { - boolean hasPerm = true; - if (!hasPerm) { - player.sendMessage("You do not have permission !"); + private boolean condition(CommandSender sender) { + if (!(sender instanceof Player)) { + sender.sendMessage("The command is only available for player"); return false; } return true; } - private void defaultExecutor(Player player, Arguments args) { - player.sendMessage("Correct usage: health [set/add] [number]"); + private void defaultExecutor(CommandSender sender, Arguments args) { + sender.sendMessage("Correct usage: health [set/add] [number]"); } - private void modeCallback(Player player, String value, int error) { - player.sendMessage("SYNTAX ERROR: '" + value + "' should be replaced by 'set' or 'add'"); + private void modeCallback(CommandSender sender, String value, int error) { + sender.sendMessage("SYNTAX ERROR: '" + value + "' should be replaced by 'set' or 'add'"); } - private void valueCallback(Player player, String value, int error) { + private void valueCallback(CommandSender sender, String value, int error) { switch (error) { case ArgumentNumber.NOT_NUMBER_ERROR: - player.sendMessage("SYNTAX ERROR: '" + value + "' isn't a number!"); + sender.sendMessage("SYNTAX ERROR: '" + value + "' isn't a number!"); break; case ArgumentNumber.RANGE_ERROR: - player.sendMessage("SYNTAX ERROR: " + value + " is not between 0 and 100"); + sender.sendMessage("SYNTAX ERROR: " + value + " is not between 0 and 100"); break; } } - private void execute(Player player, Arguments args) { - player.sendMessage("/health " + args.getWord("mode") + " [Integer]"); + private void execute(CommandSender sender, Arguments args) { + sender.sendMessage("/health " + args.getWord("mode") + " [Integer]"); } - private void execute2(Player player, Arguments args) { + private void execute2(CommandSender sender, Arguments args) { + Player player = (Player) sender; String mode = args.getWord("mode"); int value = args.getInteger("value"); diff --git a/src/main/java/fr/themode/demo/commands/SimpleCommand.java b/src/main/java/fr/themode/demo/commands/SimpleCommand.java index 4ff31d581..a262b866f 100644 --- a/src/main/java/fr/themode/demo/commands/SimpleCommand.java +++ b/src/main/java/fr/themode/demo/commands/SimpleCommand.java @@ -1,7 +1,17 @@ package fr.themode.demo.commands; +import com.extollit.gaming.ai.path.HydrazinePathFinder; +import com.extollit.gaming.ai.path.model.PathObject; +import com.extollit.linalg.immutable.Vec3i; +import fr.themode.demo.entity.ChickenCreature; import net.minestom.server.command.CommandProcessor; +import net.minestom.server.command.CommandSender; import net.minestom.server.entity.Player; +import net.minestom.server.entity.pathfinding.hydrazine.PFInstanceSpace; +import net.minestom.server.entity.pathfinding.hydrazine.PFPathingEntity; +import net.minestom.server.instance.Instance; + +import java.util.Iterator; public class SimpleCommand implements CommandProcessor { @Override @@ -15,7 +25,11 @@ public class SimpleCommand implements CommandProcessor { } @Override - public boolean process(Player player, String command, String[] args) { + public boolean process(CommandSender sender, String command, String[] args) { + + if (!(sender instanceof Player)) + return false; + Player player = (Player) sender; /*for (Player p : MinecraftServer.getConnectionManager().getOnlinePlayers()) { if (!(p instanceof FakePlayer)) @@ -35,8 +49,32 @@ public class SimpleCommand implements CommandProcessor { break; }*/ + /*for (EntityCreature entityCreature : player.getInstance().getCreatures()) { + entityCreature.setPathTo(player.getPosition().clone()); + //entityCreature.jump(1); + } + System.gc(); - player.sendMessage("Garbage collector called"); + player.sendMessage("Garbage collector called");*/ + + Instance instance = player.getInstance(); + + ChickenCreature chickenCreature = new ChickenCreature(player.getPosition()); + chickenCreature.setInstance(instance); + + PFPathingEntity pathingEntity = new PFPathingEntity(chickenCreature); + PFInstanceSpace instanceSpace = new PFInstanceSpace(instance); + + final HydrazinePathFinder pathFinder = new HydrazinePathFinder(pathingEntity, instanceSpace); + + final PathObject path = pathFinder.initiatePathTo(-10, 42, -10); + + for (Iterator it = path.iterator(); it.hasNext(); ) { + Vec3i ite = it.next(); + + System.out.println("test: " + ite); + + } return true; } diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index 420d161a7..f74b5ea9f 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -9,19 +9,33 @@ import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; import net.minestom.server.utils.ArrayUtils; import net.minestom.server.utils.validate.Check; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class CommandManager { private String commandPrefix = "/"; - private CommandDispatcher dispatcher = new CommandDispatcher<>(); + private ConsoleSender consoleSender = new ConsoleSender(); + + private CommandDispatcher dispatcher = new CommandDispatcher<>(); private Map commandProcessorMap = new HashMap<>(); - public void register(Command command) { + public CommandManager() { + // Setup console thread + new Thread(() -> { + Scanner scanner = new Scanner(System.in); + while (true) { + String command = scanner.nextLine(); + if (!command.startsWith(commandPrefix)) + continue; + command = command.replaceFirst(commandPrefix, ""); + execute(consoleSender, command); + + } + }, "ConsoleCommand-Thread").start(); + } + + public void register(Command command) { this.dispatcher.register(command); } @@ -29,20 +43,24 @@ public class CommandManager { this.commandProcessorMap.put(commandProcessor.getCommandName().toLowerCase(), commandProcessor); } - public boolean execute(Player source, String command) { - Check.notNull(source, "Source cannot be null"); + public boolean execute(CommandSender sender, String command) { + Check.notNull(sender, "Source cannot be null"); Check.notNull(command, "Command string cannot be null"); - PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(source, command); - source.callEvent(PlayerCommandEvent.class, playerCommandEvent); + if (sender instanceof Player) { + Player player = (Player) sender; - if (playerCommandEvent.isCancelled()) - return false; + PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(player, command); + player.callEvent(PlayerCommandEvent.class, playerCommandEvent); - command = playerCommandEvent.getCommand(); + if (playerCommandEvent.isCancelled()) + return false; + + command = playerCommandEvent.getCommand(); + } try { - this.dispatcher.execute(source, command); + this.dispatcher.execute(sender, command); return true; } catch (NullPointerException e) { String[] splitted = command.split(" "); @@ -53,7 +71,7 @@ public class CommandManager { String[] args = command.substring(command.indexOf(" ") + 1).split(" "); - return commandProcessor.process(source, commandName, args); + return commandProcessor.process(sender, commandName, args); } } @@ -66,6 +84,10 @@ public class CommandManager { this.commandPrefix = commandPrefix; } + public ConsoleSender getConsoleSender() { + return consoleSender; + } + public DeclareCommandsPacket createDeclareCommandsPacket(Player player) { return buildPacket(player); } @@ -74,7 +96,7 @@ public class CommandManager { DeclareCommandsPacket declareCommandsPacket = new DeclareCommandsPacket(); List commands = new ArrayList<>(); - for (Command command : dispatcher.getCommands()) { + for (Command command : dispatcher.getCommands()) { CommandCondition commandCondition = command.getCondition(); if (commandCondition != null) { // Do not show command if return false diff --git a/src/main/java/net/minestom/server/command/CommandProcessor.java b/src/main/java/net/minestom/server/command/CommandProcessor.java index 8c500313d..a899bab0c 100644 --- a/src/main/java/net/minestom/server/command/CommandProcessor.java +++ b/src/main/java/net/minestom/server/command/CommandProcessor.java @@ -8,7 +8,7 @@ public interface CommandProcessor { String[] getAliases(); - boolean process(Player player, String command, String[] args); + boolean process(CommandSender sender, String command, String[] args); boolean hasAccess(Player player); } diff --git a/src/main/java/net/minestom/server/command/CommandSender.java b/src/main/java/net/minestom/server/command/CommandSender.java new file mode 100644 index 000000000..50f485262 --- /dev/null +++ b/src/main/java/net/minestom/server/command/CommandSender.java @@ -0,0 +1,13 @@ +package net.minestom.server.command; + +public interface CommandSender { + + void sendMessage(String message); + + default void sendMessage(String[] messages) { + for (String message : messages) { + sendMessage(message); + } + } + +} diff --git a/src/main/java/net/minestom/server/command/ConsoleSender.java b/src/main/java/net/minestom/server/command/ConsoleSender.java new file mode 100644 index 000000000..8d92332f6 --- /dev/null +++ b/src/main/java/net/minestom/server/command/ConsoleSender.java @@ -0,0 +1,8 @@ +package net.minestom.server.command; + +public class ConsoleSender implements CommandSender { + @Override + public void sendMessage(String message) { + System.out.println(message); + } +} diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index b806a2f43..e2ec8adb9 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -8,6 +8,7 @@ import net.minestom.server.bossbar.BossBar; import net.minestom.server.chat.Chat; import net.minestom.server.collision.BoundingBox; import net.minestom.server.command.CommandManager; +import net.minestom.server.command.CommandSender; import net.minestom.server.effects.Effects; import net.minestom.server.entity.damage.DamageType; import net.minestom.server.entity.vehicle.PlayerVehicleInformation; @@ -52,7 +53,7 @@ import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; -public class Player extends LivingEntity { +public class Player extends LivingEntity implements CommandSender { private long lastKeepAlive; private boolean answerKeepAlive; @@ -596,6 +597,7 @@ public class Player extends LivingEntity { } // Use legacy color formatting + @Override public void sendMessage(String message) { sendMessage(Chat.fromLegacyText(message)); } From 754568d3366730b1d959b2ef1762ecec9401e837 Mon Sep 17 00:00:00 2001 From: jglrxavpok Date: Sun, 21 Jun 2020 22:05:30 +0200 Subject: [PATCH 3/5] Comment out unused code in SimpleCommand --- src/main/java/fr/themode/demo/commands/SimpleCommand.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/themode/demo/commands/SimpleCommand.java b/src/main/java/fr/themode/demo/commands/SimpleCommand.java index a262b866f..bc456c150 100644 --- a/src/main/java/fr/themode/demo/commands/SimpleCommand.java +++ b/src/main/java/fr/themode/demo/commands/SimpleCommand.java @@ -7,8 +7,6 @@ import fr.themode.demo.entity.ChickenCreature; import net.minestom.server.command.CommandProcessor; import net.minestom.server.command.CommandSender; import net.minestom.server.entity.Player; -import net.minestom.server.entity.pathfinding.hydrazine.PFInstanceSpace; -import net.minestom.server.entity.pathfinding.hydrazine.PFPathingEntity; import net.minestom.server.instance.Instance; import java.util.Iterator; @@ -61,7 +59,7 @@ public class SimpleCommand implements CommandProcessor { ChickenCreature chickenCreature = new ChickenCreature(player.getPosition()); chickenCreature.setInstance(instance); - +/* PFPathingEntity pathingEntity = new PFPathingEntity(chickenCreature); PFInstanceSpace instanceSpace = new PFInstanceSpace(instance); @@ -75,7 +73,7 @@ public class SimpleCommand implements CommandProcessor { System.out.println("test: " + ite); } - +*/ return true; } From 72234ac706d79986a778d461cf6848cbd21a3c4d Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sun, 21 Jun 2020 22:11:56 +0200 Subject: [PATCH 4/5] Added PlayerPreLoginEvent --- .../net/minestom/server/entity/Entity.java | 9 +++ .../minestom/server/entity/EntityManager.java | 33 +++++++++ .../net/minestom/server/entity/Player.java | 17 +++-- .../event/player/PlayerPreLoginEvent.java | 71 +++++++++++++++++++ 4 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index a0a83f32a..7a778aec0 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -539,6 +539,15 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer { return uuid; } + /** + * Change the internal entity UUID, mostly unsafe + * + * @param uuid the new entity uuid + */ + protected void setUuid(UUID uuid) { + this.uuid = uuid; + } + /** * Return false just after instantiation, set to true after calling {@link #setInstance(Instance)} * diff --git a/src/main/java/net/minestom/server/entity/EntityManager.java b/src/main/java/net/minestom/server/entity/EntityManager.java index 82d813e5b..0146f3e45 100644 --- a/src/main/java/net/minestom/server/entity/EntityManager.java +++ b/src/main/java/net/minestom/server/entity/EntityManager.java @@ -2,6 +2,7 @@ package net.minestom.server.entity; import net.minestom.server.MinecraftServer; import net.minestom.server.event.player.PlayerLoginEvent; +import net.minestom.server.event.player.PlayerPreLoginEvent; import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Instance; import net.minestom.server.instance.InstanceManager; @@ -9,8 +10,10 @@ import net.minestom.server.utils.thread.MinestomThread; import net.minestom.server.utils.validate.Check; import java.util.Set; +import java.util.UUID; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; +import java.util.function.Consumer; public final class EntityManager { @@ -178,8 +181,38 @@ public final class EntityManager { } } + /** + * Call the player initialization callbacks and the event {@link PlayerPreLoginEvent} + * If the player hasn't been kicked, add him to the waiting list + *

+ * Can be considered as a pre-init thing + * + * @param player the player to add + */ public void addWaitingPlayer(Player player) { + + // Init player (register events) + for (Consumer playerInitialization : MinecraftServer.getConnectionManager().getPlayerInitializations()) { + playerInitialization.accept(player); + } + + // Call pre login event + PlayerPreLoginEvent playerPreLoginEvent = new PlayerPreLoginEvent(player, player.getUsername(), player.getUuid()); + player.callEvent(PlayerPreLoginEvent.class, playerPreLoginEvent); + + // Ignore the player if he has been disconnected (kick) + final boolean online = player.isOnline(); + if (!online) + return; + + // Add him to the list and change his username/uuid if changed this.waitingPlayers.add(player); + + String username = playerPreLoginEvent.getUsername(); + UUID uuid = playerPreLoginEvent.getPlayerUuid(); + + player.setUsername(username); + player.setUuid(uuid); } /** diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index e2ec8adb9..347cd5ee1 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -167,12 +167,6 @@ public class Player extends LivingEntity implements CommandSender { * Init the player and spawn him */ protected void init() { - - // Init player (register events) - for (Consumer playerInitialization : MinecraftServer.getConnectionManager().getPlayerInitializations()) { - playerInitialization.accept(this); - } - // TODO complete login sequence with optionals packets JoinGamePacket joinGamePacket = new JoinGamePacket(); joinGamePacket.entityId = getEntityId(); @@ -950,6 +944,16 @@ public class Player extends LivingEntity implements CommandSender { return username; } + /** + * Change the internal player name, used for the {@link PlayerPreLoginEvent} + * mostly unsafe outside of it + * + * @param username the new player name + */ + protected void setUsername(String username) { + this.username = username; + } + private void sendChangeGameStatePacket(ChangeGameStatePacket.Reason reason, float value) { ChangeGameStatePacket changeGameStatePacket = new ChangeGameStatePacket(); changeGameStatePacket.reason = reason; @@ -1289,6 +1293,7 @@ public class Player extends LivingEntity implements CommandSender { disconnectPacket.message = Chat.toJsonString(textComponent); playerConnection.sendPacket(disconnectPacket); playerConnection.disconnect(); + playerConnection.refreshOnline(false); } /** diff --git a/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java b/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java new file mode 100644 index 000000000..59f98a6fe --- /dev/null +++ b/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java @@ -0,0 +1,71 @@ +package net.minestom.server.event.player; + +import net.minestom.server.entity.Player; +import net.minestom.server.event.Event; +import net.minestom.server.utils.validate.Check; + +import java.util.UUID; + +/** + * Called before the player initialization, it can be used to kick the player before any connection + * or to change its final username/uuid + */ +public class PlayerPreLoginEvent extends Event { + + private Player player; + private String username; + private UUID playerUuid; + + public PlayerPreLoginEvent(Player player, String username, UUID playerUuid) { + this.player = player; + this.username = username; + this.playerUuid = playerUuid; + } + + /** + * Get the player who is trying to connect + * + * @return the player + */ + public Player getPlayer() { + return player; + } + + /** + * Get the player username + * + * @return the player username + */ + public String getUsername() { + return username; + } + + /** + * Change the player username + * + * @param username the new player username + */ + public void setUsername(String username) { + Check.notNull(username, "The player username cannot be null"); + this.username = username; + } + + /** + * Get the player uuid + * + * @return the player uuid + */ + public UUID getPlayerUuid() { + return playerUuid; + } + + /** + * Change the player uuid + * + * @param playerUuid the new player uuid + */ + public void setPlayerUuid(UUID playerUuid) { + Check.notNull(playerUuid, "The player uuid cannot be null"); + this.playerUuid = playerUuid; + } +} From 889b1fe095a6a470e944c4de0c392dde9018989f Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sun, 21 Jun 2020 23:44:20 +0200 Subject: [PATCH 5/5] Renamed PlayerInteractEvent to PlayerEntityInteractEvent --- ...teractEvent.java => PlayerEntityInteractEvent.java} | 4 ++-- .../server/event/player/PlayerPreLoginEvent.java | 2 +- .../minestom/server/listener/UseEntityListener.java | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) rename src/main/java/net/minestom/server/event/player/{PlayerInteractEvent.java => PlayerEntityInteractEvent.java} (86%) diff --git a/src/main/java/net/minestom/server/event/player/PlayerInteractEvent.java b/src/main/java/net/minestom/server/event/player/PlayerEntityInteractEvent.java similarity index 86% rename from src/main/java/net/minestom/server/event/player/PlayerInteractEvent.java rename to src/main/java/net/minestom/server/event/player/PlayerEntityInteractEvent.java index 98a31c4a6..15399a0a6 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerInteractEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerEntityInteractEvent.java @@ -7,13 +7,13 @@ import net.minestom.server.event.Event; /** * Called when a player interacts (right-click) with an entity */ -public class PlayerInteractEvent extends Event { +public class PlayerEntityInteractEvent extends Event { private Player player; private Entity entityTarget; private Player.Hand hand; - public PlayerInteractEvent(Player player, Entity entityTarget, Player.Hand hand) { + public PlayerEntityInteractEvent(Player player, Entity entityTarget, Player.Hand hand) { this.player = player; this.entityTarget = entityTarget; this.hand = hand; diff --git a/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java b/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java index 59f98a6fe..69bcf41d5 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java +++ b/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java @@ -8,7 +8,7 @@ import java.util.UUID; /** * Called before the player initialization, it can be used to kick the player before any connection - * or to change its final username/uuid + * or to change his final username/uuid */ public class PlayerPreLoginEvent extends Event { diff --git a/src/main/java/net/minestom/server/listener/UseEntityListener.java b/src/main/java/net/minestom/server/listener/UseEntityListener.java index 1a40de675..a6fe9b79b 100644 --- a/src/main/java/net/minestom/server/listener/UseEntityListener.java +++ b/src/main/java/net/minestom/server/listener/UseEntityListener.java @@ -4,7 +4,7 @@ import net.minestom.server.entity.Entity; import net.minestom.server.entity.LivingEntity; import net.minestom.server.entity.Player; import net.minestom.server.event.entity.EntityAttackEvent; -import net.minestom.server.event.player.PlayerInteractEvent; +import net.minestom.server.event.player.PlayerEntityInteractEvent; import net.minestom.server.network.packet.client.play.ClientInteractEntityPacket; public class UseEntityListener { @@ -26,12 +26,12 @@ public class UseEntityListener { EntityAttackEvent entityAttackEvent = new EntityAttackEvent(player, entity); player.callEvent(EntityAttackEvent.class, entityAttackEvent); } else if (type == ClientInteractEntityPacket.Type.INTERACT) { - PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(player, entity, packet.hand); - player.callEvent(PlayerInteractEvent.class, playerInteractEvent); + PlayerEntityInteractEvent playerEntityInteractEvent = new PlayerEntityInteractEvent(player, entity, packet.hand); + player.callEvent(PlayerEntityInteractEvent.class, playerEntityInteractEvent); } else { // TODO find difference with INTERACT - PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(player, entity, packet.hand); - player.callEvent(PlayerInteractEvent.class, playerInteractEvent); + PlayerEntityInteractEvent playerEntityInteractEvent = new PlayerEntityInteractEvent(player, entity, packet.hand); + player.callEvent(PlayerEntityInteractEvent.class, playerEntityInteractEvent); } }