Merge with latest Minestorm

This commit is contained in:
Eoghanmc22 2020-06-21 18:57:53 -04:00
commit 77b99e8223
19 changed files with 2259 additions and 2039 deletions

View File

@ -49,8 +49,10 @@ dependencies {
api 'net.kyori:text-serializer-gson:3.0.3'
api 'net.kyori:text-serializer-plain:3.0.3'
api 'com.mojang:authlib:1.5.21'
api 'net.sf.jopt-simple:jopt-simple:5.0.4'
api 'org.projectlombok:lombok:1.18.12'
annotationProcessor 'org.projectlombok:lombok:1.18.12'
// Pathfinding
implementation 'com.github.MadMartian:hydrazine-path-finding:1.02'
}

View File

@ -9,6 +9,7 @@ import fr.themode.demo.commands.HealthCommand;
import fr.themode.demo.commands.SimpleCommand;
import net.minestom.server.MinecraftServer;
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.rule.vanilla.RedstonePlacementRule;
import net.minestom.server.storage.StorageManager;
@ -63,6 +64,8 @@ public class Main {
PlayerInit.init();
//MojangAuth.init();
minecraftServer.start("localhost", 55555, PlayerInit.getResponseDataConsumer());
}

View File

@ -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<Instance> 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;

View File

@ -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<Player> {
public class GamemodeCommand extends Command<CommandSender> {
public GamemodeCommand() {
super("gamemode", "g", "gm");
@ -35,11 +36,13 @@ public class GamemodeCommand extends Command<Player> {
addSyntax(this::executeOnOther, player, mode);
}
private void usage(Player player, Arguments arguments) {
player.sendMessage("Usage: /gamemode [player] <gamemode>");
private void usage(CommandSender sender, Arguments arguments) {
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");
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> {
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<Player> {
}
}
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;
}
}

View File

@ -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<Player> {
public class HealthCommand extends Command<CommandSender> {
public HealthCommand() {
super("health", "h", "healthbar");
@ -27,39 +28,39 @@ public class HealthCommand extends Command<Player> {
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");

View File

@ -1,7 +1,15 @@
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.instance.Instance;
import java.util.Iterator;
public class SimpleCommand implements CommandProcessor {
@Override
@ -15,7 +23,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,9 +47,33 @@ public class SimpleCommand implements CommandProcessor {
break;
}*/
System.gc();
player.sendMessage("Garbage collector called");
/*for (EntityCreature entityCreature : player.getInstance().getCreatures()) {
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;
}

View File

@ -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<Player> dispatcher = new CommandDispatcher<>();
private ConsoleSender consoleSender = new ConsoleSender();
private CommandDispatcher<CommandSender> dispatcher = new CommandDispatcher<>();
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);
}
@ -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<String> commands = new ArrayList<>();
for (Command<Player> command : dispatcher.getCommands()) {
for (Command<CommandSender> command : dispatcher.getCommands()) {
CommandCondition<Player> commandCondition = command.getCondition();
if (commandCondition != null) {
// Do not show command if return false

View File

@ -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);
}

View 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);
}
}
}

View File

@ -0,0 +1,8 @@
package net.minestom.server.command;
public class ConsoleSender implements CommandSender {
@Override
public void sendMessage(String message) {
System.out.println(message);
}
}

View File

@ -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)}
*

View File

@ -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
* <p>
* 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<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);
String username = playerPreLoginEvent.getUsername();
UUID uuid = playerPreLoginEvent.getPlayerUuid();
player.setUsername(username);
player.setUuid(uuid);
}
/**

File diff suppressed because it is too large Load Diff

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
/**

View File

@ -6,5 +6,5 @@ import java.util.UUID;
@FunctionalInterface
public interface UuidProvider {
UUID provide(PlayerConnection playerConnection);
UUID provide(PlayerConnection playerConnection, String username);
}

View File

@ -24,7 +24,7 @@ public class LoginStartPacket implements ClientPreplayPacket {
EncryptionRequestPacket encryptionRequestPacket = new EncryptionRequestPacket(connection);
connection.sendPacket(encryptionRequestPacket);
} else {
UUID playerUuid = connectionManager.getPlayerConnectionUuid(connection);
UUID playerUuid = connectionManager.getPlayerConnectionUuid(connection, username);
LoginSuccessPacket successPacket = new LoginSuccessPacket(playerUuid, username);
connection.sendPacket(successPacket);