mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-19 06:32:03 +01:00
Merge with latest Minestorm
This commit is contained in:
commit
77b99e8223
@ -49,8 +49,10 @@ dependencies {
|
|||||||
api 'net.kyori:text-serializer-gson:3.0.3'
|
api 'net.kyori:text-serializer-gson:3.0.3'
|
||||||
api 'net.kyori:text-serializer-plain:3.0.3'
|
api 'net.kyori:text-serializer-plain:3.0.3'
|
||||||
api 'com.mojang:authlib:1.5.21'
|
api 'com.mojang:authlib:1.5.21'
|
||||||
api 'net.sf.jopt-simple:jopt-simple:5.0.4'
|
|
||||||
api 'org.projectlombok:lombok:1.18.12'
|
api 'org.projectlombok:lombok:1.18.12'
|
||||||
annotationProcessor 'org.projectlombok:lombok:1.18.12'
|
annotationProcessor 'org.projectlombok:lombok:1.18.12'
|
||||||
|
|
||||||
|
// Pathfinding
|
||||||
|
implementation 'com.github.MadMartian:hydrazine-path-finding:1.02'
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import fr.themode.demo.commands.HealthCommand;
|
|||||||
import fr.themode.demo.commands.SimpleCommand;
|
import fr.themode.demo.commands.SimpleCommand;
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.command.CommandManager;
|
import net.minestom.server.command.CommandManager;
|
||||||
|
import net.minestom.server.extras.MojangAuth;
|
||||||
import net.minestom.server.instance.block.BlockManager;
|
import net.minestom.server.instance.block.BlockManager;
|
||||||
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
|
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
|
||||||
import net.minestom.server.storage.StorageManager;
|
import net.minestom.server.storage.StorageManager;
|
||||||
@ -63,6 +64,8 @@ public class Main {
|
|||||||
|
|
||||||
PlayerInit.init();
|
PlayerInit.init();
|
||||||
|
|
||||||
|
//MojangAuth.init();
|
||||||
|
|
||||||
minecraftServer.start("localhost", 55555, PlayerInit.getResponseDataConsumer());
|
minecraftServer.start("localhost", 55555, PlayerInit.getResponseDataConsumer());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package fr.themode.demo.commands;
|
|||||||
|
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.command.CommandProcessor;
|
import net.minestom.server.command.CommandProcessor;
|
||||||
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.world.Dimension;
|
import net.minestom.server.world.Dimension;
|
||||||
@ -20,7 +21,12 @@ public class DimensionCommand implements CommandProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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();
|
Instance instance = player.getInstance();
|
||||||
|
|
||||||
Dimension targetDimension = Dimension.NETHER;
|
Dimension targetDimension = Dimension.NETHER;
|
||||||
|
@ -4,6 +4,7 @@ import fr.themode.command.Arguments;
|
|||||||
import fr.themode.command.Command;
|
import fr.themode.command.Command;
|
||||||
import fr.themode.command.arguments.Argument;
|
import fr.themode.command.arguments.Argument;
|
||||||
import fr.themode.command.arguments.ArgumentType;
|
import fr.themode.command.arguments.ArgumentType;
|
||||||
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.entity.GameMode;
|
import net.minestom.server.entity.GameMode;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ import java.util.Optional;
|
|||||||
/**
|
/**
|
||||||
* Command that make a player change gamemode
|
* Command that make a player change gamemode
|
||||||
*/
|
*/
|
||||||
public class GamemodeCommand extends Command<Player> {
|
public class GamemodeCommand extends Command<CommandSender> {
|
||||||
public GamemodeCommand() {
|
public GamemodeCommand() {
|
||||||
super("gamemode", "g", "gm");
|
super("gamemode", "g", "gm");
|
||||||
|
|
||||||
@ -35,11 +36,13 @@ public class GamemodeCommand extends Command<Player> {
|
|||||||
addSyntax(this::executeOnOther, player, mode);
|
addSyntax(this::executeOnOther, player, mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void usage(Player player, Arguments arguments) {
|
private void usage(CommandSender sender, Arguments arguments) {
|
||||||
player.sendMessage("Usage: /gamemode [player] <gamemode>");
|
sender.sendMessage("Usage: /gamemode [player] <gamemode>");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void executeOnSelf(Player player, Arguments arguments) {
|
private void executeOnSelf(CommandSender sender, Arguments arguments) {
|
||||||
|
Player player = (Player) sender;
|
||||||
|
|
||||||
String gamemodeName = arguments.getWord("mode");
|
String gamemodeName = arguments.getWord("mode");
|
||||||
GameMode mode = GameMode.valueOf(gamemodeName.toUpperCase());
|
GameMode mode = GameMode.valueOf(gamemodeName.toUpperCase());
|
||||||
assert mode != null; // mode is not supposed to be null, because gamemodeName will be valid
|
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> {
|
|||||||
player.sendMessage("You are now playing in " + gamemodeName);
|
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 gamemodeName = arguments.getWord("mode");
|
||||||
String targetName = arguments.getWord("player");
|
String targetName = arguments.getWord("player");
|
||||||
GameMode mode = GameMode.valueOf(gamemodeName.toUpperCase());
|
GameMode mode = GameMode.valueOf(gamemodeName.toUpperCase());
|
||||||
@ -61,11 +66,15 @@ public class GamemodeCommand extends Command<Player> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void gameModeCallback(Player player, String gamemode, int error) {
|
private void gameModeCallback(CommandSender sender, String gamemode, int error) {
|
||||||
player.sendMessage("'" + gamemode + "' is not a valid gamemode!");
|
sender.sendMessage("'" + gamemode + "' is not a valid gamemode!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAllowed(Player player) {
|
private boolean isAllowed(CommandSender sender) {
|
||||||
return true; // TODO: permissions
|
if (!(sender instanceof Player)) {
|
||||||
|
sender.sendMessage("The command is only available for player");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,10 @@ import fr.themode.command.Command;
|
|||||||
import fr.themode.command.arguments.Argument;
|
import fr.themode.command.arguments.Argument;
|
||||||
import fr.themode.command.arguments.ArgumentType;
|
import fr.themode.command.arguments.ArgumentType;
|
||||||
import fr.themode.command.arguments.number.ArgumentNumber;
|
import fr.themode.command.arguments.number.ArgumentNumber;
|
||||||
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
|
|
||||||
public class HealthCommand extends Command<Player> {
|
public class HealthCommand extends Command<CommandSender> {
|
||||||
|
|
||||||
public HealthCommand() {
|
public HealthCommand() {
|
||||||
super("health", "h", "healthbar");
|
super("health", "h", "healthbar");
|
||||||
@ -27,39 +28,39 @@ public class HealthCommand extends Command<Player> {
|
|||||||
addSyntax(this::execute2, arg0, arg1);
|
addSyntax(this::execute2, arg0, arg1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean condition(Player player) {
|
private boolean condition(CommandSender sender) {
|
||||||
boolean hasPerm = true;
|
if (!(sender instanceof Player)) {
|
||||||
if (!hasPerm) {
|
sender.sendMessage("The command is only available for player");
|
||||||
player.sendMessage("You do not have permission !");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void defaultExecutor(Player player, Arguments args) {
|
private void defaultExecutor(CommandSender sender, Arguments args) {
|
||||||
player.sendMessage("Correct usage: health [set/add] [number]");
|
sender.sendMessage("Correct usage: health [set/add] [number]");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void modeCallback(Player player, String value, int error) {
|
private void modeCallback(CommandSender sender, String value, int error) {
|
||||||
player.sendMessage("SYNTAX ERROR: '" + value + "' should be replaced by 'set' or 'add'");
|
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) {
|
switch (error) {
|
||||||
case ArgumentNumber.NOT_NUMBER_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;
|
break;
|
||||||
case ArgumentNumber.RANGE_ERROR:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void execute(Player player, Arguments args) {
|
private void execute(CommandSender sender, Arguments args) {
|
||||||
player.sendMessage("/health " + args.getWord("mode") + " [Integer]");
|
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");
|
String mode = args.getWord("mode");
|
||||||
int value = args.getInteger("value");
|
int value = args.getInteger("value");
|
||||||
|
|
||||||
|
@ -1,7 +1,15 @@
|
|||||||
package fr.themode.demo.commands;
|
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.CommandProcessor;
|
||||||
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
|
import net.minestom.server.instance.Instance;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class SimpleCommand implements CommandProcessor {
|
public class SimpleCommand implements CommandProcessor {
|
||||||
@Override
|
@Override
|
||||||
@ -15,7 +23,11 @@ public class SimpleCommand implements CommandProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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()) {
|
/*for (Player p : MinecraftServer.getConnectionManager().getOnlinePlayers()) {
|
||||||
if (!(p instanceof FakePlayer))
|
if (!(p instanceof FakePlayer))
|
||||||
@ -35,9 +47,33 @@ public class SimpleCommand implements CommandProcessor {
|
|||||||
break;
|
break;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
System.gc();
|
/*for (EntityCreature entityCreature : player.getInstance().getCreatures()) {
|
||||||
player.sendMessage("Garbage collector called");
|
entityCreature.setPathTo(player.getPosition().clone());
|
||||||
|
//entityCreature.jump(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.gc();
|
||||||
|
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<Vec3i> it = path.iterator(); it.hasNext(); ) {
|
||||||
|
Vec3i ite = it.next();
|
||||||
|
|
||||||
|
System.out.println("test: " + ite);
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,19 +9,33 @@ import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
|||||||
import net.minestom.server.utils.ArrayUtils;
|
import net.minestom.server.utils.ArrayUtils;
|
||||||
import net.minestom.server.utils.validate.Check;
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class CommandManager {
|
public class CommandManager {
|
||||||
|
|
||||||
private String commandPrefix = "/";
|
private String commandPrefix = "/";
|
||||||
|
|
||||||
private CommandDispatcher<Player> dispatcher = new CommandDispatcher<>();
|
private ConsoleSender consoleSender = new ConsoleSender();
|
||||||
|
|
||||||
|
private CommandDispatcher<CommandSender> dispatcher = new CommandDispatcher<>();
|
||||||
private Map<String, CommandProcessor> commandProcessorMap = new HashMap<>();
|
private Map<String, CommandProcessor> commandProcessorMap = new HashMap<>();
|
||||||
|
|
||||||
public void register(Command<Player> 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<CommandSender> command) {
|
||||||
this.dispatcher.register(command);
|
this.dispatcher.register(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,20 +43,24 @@ public class CommandManager {
|
|||||||
this.commandProcessorMap.put(commandProcessor.getCommandName().toLowerCase(), commandProcessor);
|
this.commandProcessorMap.put(commandProcessor.getCommandName().toLowerCase(), commandProcessor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean execute(Player source, String command) {
|
public boolean execute(CommandSender sender, String command) {
|
||||||
Check.notNull(source, "Source cannot be null");
|
Check.notNull(sender, "Source cannot be null");
|
||||||
Check.notNull(command, "Command string cannot be null");
|
Check.notNull(command, "Command string cannot be null");
|
||||||
|
|
||||||
PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(source, command);
|
if (sender instanceof Player) {
|
||||||
source.callEvent(PlayerCommandEvent.class, playerCommandEvent);
|
Player player = (Player) sender;
|
||||||
|
|
||||||
|
PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(player, command);
|
||||||
|
player.callEvent(PlayerCommandEvent.class, playerCommandEvent);
|
||||||
|
|
||||||
if (playerCommandEvent.isCancelled())
|
if (playerCommandEvent.isCancelled())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
command = playerCommandEvent.getCommand();
|
command = playerCommandEvent.getCommand();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.dispatcher.execute(source, command);
|
this.dispatcher.execute(sender, command);
|
||||||
return true;
|
return true;
|
||||||
} catch (NullPointerException e) {
|
} catch (NullPointerException e) {
|
||||||
String[] splitted = command.split(" ");
|
String[] splitted = command.split(" ");
|
||||||
@ -53,7 +71,7 @@ public class CommandManager {
|
|||||||
|
|
||||||
String[] args = command.substring(command.indexOf(" ") + 1).split(" ");
|
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;
|
this.commandPrefix = commandPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ConsoleSender getConsoleSender() {
|
||||||
|
return consoleSender;
|
||||||
|
}
|
||||||
|
|
||||||
public DeclareCommandsPacket createDeclareCommandsPacket(Player player) {
|
public DeclareCommandsPacket createDeclareCommandsPacket(Player player) {
|
||||||
return buildPacket(player);
|
return buildPacket(player);
|
||||||
}
|
}
|
||||||
@ -74,7 +96,7 @@ public class CommandManager {
|
|||||||
DeclareCommandsPacket declareCommandsPacket = new DeclareCommandsPacket();
|
DeclareCommandsPacket declareCommandsPacket = new DeclareCommandsPacket();
|
||||||
|
|
||||||
List<String> commands = new ArrayList<>();
|
List<String> commands = new ArrayList<>();
|
||||||
for (Command<Player> command : dispatcher.getCommands()) {
|
for (Command<CommandSender> command : dispatcher.getCommands()) {
|
||||||
CommandCondition<Player> commandCondition = command.getCondition();
|
CommandCondition<Player> commandCondition = command.getCondition();
|
||||||
if (commandCondition != null) {
|
if (commandCondition != null) {
|
||||||
// Do not show command if return false
|
// Do not show command if return false
|
||||||
|
@ -8,7 +8,7 @@ public interface CommandProcessor {
|
|||||||
|
|
||||||
String[] getAliases();
|
String[] getAliases();
|
||||||
|
|
||||||
boolean process(Player player, String command, String[] args);
|
boolean process(CommandSender sender, String command, String[] args);
|
||||||
|
|
||||||
boolean hasAccess(Player player);
|
boolean hasAccess(Player player);
|
||||||
}
|
}
|
||||||
|
13
src/main/java/net/minestom/server/command/CommandSender.java
Normal file
13
src/main/java/net/minestom/server/command/CommandSender.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package net.minestom.server.command;
|
||||||
|
|
||||||
|
public class ConsoleSender implements CommandSender {
|
||||||
|
@Override
|
||||||
|
public void sendMessage(String message) {
|
||||||
|
System.out.println(message);
|
||||||
|
}
|
||||||
|
}
|
@ -539,6 +539,15 @@ public abstract class Entity implements Viewable, EventHandler, DataContainer {
|
|||||||
return uuid;
|
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)}
|
* Return false just after instantiation, set to true after calling {@link #setInstance(Instance)}
|
||||||
*
|
*
|
||||||
|
@ -2,6 +2,7 @@ package net.minestom.server.entity;
|
|||||||
|
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.event.player.PlayerLoginEvent;
|
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.Chunk;
|
||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.InstanceManager;
|
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 net.minestom.server.utils.validate.Check;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public final class EntityManager {
|
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
|
||||||
|
* <p>
|
||||||
|
* Can be considered as a pre-init thing
|
||||||
|
*
|
||||||
|
* @param player the player to add
|
||||||
|
*/
|
||||||
public void addWaitingPlayer(Player player) {
|
public void addWaitingPlayer(Player player) {
|
||||||
|
|
||||||
|
// Init player (register events)
|
||||||
|
for (Consumer<Player> 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);
|
this.waitingPlayers.add(player);
|
||||||
|
|
||||||
|
String username = playerPreLoginEvent.getUsername();
|
||||||
|
UUID uuid = playerPreLoginEvent.getPlayerUuid();
|
||||||
|
|
||||||
|
player.setUsername(username);
|
||||||
|
player.setUuid(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,7 +55,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
|||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class Player extends LivingEntity {
|
public class Player extends LivingEntity implements CommandSender {
|
||||||
|
|
||||||
private long lastKeepAlive;
|
private long lastKeepAlive;
|
||||||
private boolean answerKeepAlive;
|
private boolean answerKeepAlive;
|
||||||
@ -155,7 +155,7 @@ public class Player extends LivingEntity {
|
|||||||
|
|
||||||
this.gameMode = GameMode.SURVIVAL;
|
this.gameMode = GameMode.SURVIVAL;
|
||||||
this.dimension = Dimension.OVERWORLD;
|
this.dimension = Dimension.OVERWORLD;
|
||||||
this.levelType = LevelType.FLAT;
|
this.levelType = LevelType.DEFAULT;
|
||||||
refreshPosition(0, 0, 0);
|
refreshPosition(0, 0, 0);
|
||||||
|
|
||||||
// FakePlayer init its connection there
|
// FakePlayer init its connection there
|
||||||
@ -169,12 +169,6 @@ public class Player extends LivingEntity {
|
|||||||
* Init the player and spawn him
|
* Init the player and spawn him
|
||||||
*/
|
*/
|
||||||
protected void init() {
|
protected void init() {
|
||||||
|
|
||||||
// Init player (register events)
|
|
||||||
for (Consumer<Player> playerInitialization : MinecraftServer.getConnectionManager().getPlayerInitializations()) {
|
|
||||||
playerInitialization.accept(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO complete login sequence with optionals packets
|
// TODO complete login sequence with optionals packets
|
||||||
JoinGamePacket joinGamePacket = new JoinGamePacket();
|
JoinGamePacket joinGamePacket = new JoinGamePacket();
|
||||||
joinGamePacket.entityId = getEntityId();
|
joinGamePacket.entityId = getEntityId();
|
||||||
@ -604,6 +598,7 @@ public class Player extends LivingEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Use legacy color formatting
|
// Use legacy color formatting
|
||||||
|
@Override
|
||||||
public void sendMessage(String message) {
|
public void sendMessage(String message) {
|
||||||
sendMessage(Chat.fromLegacyText(message));
|
sendMessage(Chat.fromLegacyText(message));
|
||||||
}
|
}
|
||||||
@ -956,6 +951,16 @@ public class Player extends LivingEntity {
|
|||||||
return username;
|
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) {
|
private void sendChangeGameStatePacket(ChangeGameStatePacket.Reason reason, float value) {
|
||||||
ChangeGameStatePacket changeGameStatePacket = new ChangeGameStatePacket();
|
ChangeGameStatePacket changeGameStatePacket = new ChangeGameStatePacket();
|
||||||
changeGameStatePacket.reason = reason;
|
changeGameStatePacket.reason = reason;
|
||||||
@ -1295,6 +1300,7 @@ public class Player extends LivingEntity {
|
|||||||
disconnectPacket.message = Chat.toJsonString(textComponent);
|
disconnectPacket.message = Chat.toJsonString(textComponent);
|
||||||
playerConnection.sendPacket(disconnectPacket);
|
playerConnection.sendPacket(disconnectPacket);
|
||||||
playerConnection.disconnect();
|
playerConnection.disconnect();
|
||||||
|
playerConnection.refreshOnline(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1927,6 +1933,7 @@ public class Player extends LivingEntity {
|
|||||||
spawnPlayerPacket.entityId = getEntityId();
|
spawnPlayerPacket.entityId = getEntityId();
|
||||||
spawnPlayerPacket.playerUuid = getUuid();
|
spawnPlayerPacket.playerUuid = getUuid();
|
||||||
spawnPlayerPacket.position = getPosition();
|
spawnPlayerPacket.position = getPosition();
|
||||||
|
|
||||||
connection.sendPacket(getAddPlayerToList());
|
connection.sendPacket(getAddPlayerToList());
|
||||||
|
|
||||||
connection.sendPacket(spawnPlayerPacket);
|
connection.sendPacket(spawnPlayerPacket);
|
||||||
|
@ -7,13 +7,13 @@ import net.minestom.server.event.Event;
|
|||||||
/**
|
/**
|
||||||
* Called when a player interacts (right-click) with an entity
|
* Called when a player interacts (right-click) with an entity
|
||||||
*/
|
*/
|
||||||
public class PlayerInteractEvent extends Event {
|
public class PlayerEntityInteractEvent extends Event {
|
||||||
|
|
||||||
private Player player;
|
private Player player;
|
||||||
private Entity entityTarget;
|
private Entity entityTarget;
|
||||||
private Player.Hand hand;
|
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.player = player;
|
||||||
this.entityTarget = entityTarget;
|
this.entityTarget = entityTarget;
|
||||||
this.hand = hand;
|
this.hand = hand;
|
@ -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 his 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;
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ import net.minestom.server.entity.Entity;
|
|||||||
import net.minestom.server.entity.LivingEntity;
|
import net.minestom.server.entity.LivingEntity;
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.event.entity.EntityAttackEvent;
|
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;
|
import net.minestom.server.network.packet.client.play.ClientInteractEntityPacket;
|
||||||
|
|
||||||
public class UseEntityListener {
|
public class UseEntityListener {
|
||||||
@ -26,12 +26,12 @@ public class UseEntityListener {
|
|||||||
EntityAttackEvent entityAttackEvent = new EntityAttackEvent(player, entity);
|
EntityAttackEvent entityAttackEvent = new EntityAttackEvent(player, entity);
|
||||||
player.callEvent(EntityAttackEvent.class, entityAttackEvent);
|
player.callEvent(EntityAttackEvent.class, entityAttackEvent);
|
||||||
} else if (type == ClientInteractEntityPacket.Type.INTERACT) {
|
} else if (type == ClientInteractEntityPacket.Type.INTERACT) {
|
||||||
PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(player, entity, packet.hand);
|
PlayerEntityInteractEvent playerEntityInteractEvent = new PlayerEntityInteractEvent(player, entity, packet.hand);
|
||||||
player.callEvent(PlayerInteractEvent.class, playerInteractEvent);
|
player.callEvent(PlayerEntityInteractEvent.class, playerEntityInteractEvent);
|
||||||
} else {
|
} else {
|
||||||
// TODO find difference with INTERACT
|
// TODO find difference with INTERACT
|
||||||
PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(player, entity, packet.hand);
|
PlayerEntityInteractEvent playerEntityInteractEvent = new PlayerEntityInteractEvent(player, entity, packet.hand);
|
||||||
player.callEvent(PlayerInteractEvent.class, playerInteractEvent);
|
player.callEvent(PlayerEntityInteractEvent.class, playerEntityInteractEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,10 +110,10 @@ public final class ConnectionManager {
|
|||||||
* @return the uuid based on {@code playerConnection}
|
* @return the uuid based on {@code playerConnection}
|
||||||
* return a random UUID if no UUID provider is defined see {@link #setUuidProvider(UuidProvider)}
|
* 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)
|
if (uuidProvider == null)
|
||||||
return UUID.randomUUID();
|
return UUID.randomUUID();
|
||||||
return uuidProvider.provide(playerConnection);
|
return uuidProvider.provide(playerConnection, username);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,5 +6,5 @@ import java.util.UUID;
|
|||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface UuidProvider {
|
public interface UuidProvider {
|
||||||
UUID provide(PlayerConnection playerConnection);
|
UUID provide(PlayerConnection playerConnection, String username);
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ public class LoginStartPacket implements ClientPreplayPacket {
|
|||||||
EncryptionRequestPacket encryptionRequestPacket = new EncryptionRequestPacket(connection);
|
EncryptionRequestPacket encryptionRequestPacket = new EncryptionRequestPacket(connection);
|
||||||
connection.sendPacket(encryptionRequestPacket);
|
connection.sendPacket(encryptionRequestPacket);
|
||||||
} else {
|
} else {
|
||||||
UUID playerUuid = connectionManager.getPlayerConnectionUuid(connection);
|
UUID playerUuid = connectionManager.getPlayerConnectionUuid(connection, username);
|
||||||
|
|
||||||
LoginSuccessPacket successPacket = new LoginSuccessPacket(playerUuid, username);
|
LoginSuccessPacket successPacket = new LoginSuccessPacket(playerUuid, username);
|
||||||
connection.sendPacket(successPacket);
|
connection.sendPacket(successPacket);
|
||||||
|
Loading…
Reference in New Issue
Block a user