1 parent
52c904d896
commit
e1e0908ef1
7 files changed
+96
-29
No files matched your search
@@ -2,6 +2,7 @@ package ciomek.loon;
|
||||
|
||||
import ciomek.loon.commands.ChangePasswordCommand;
|
||||
import ciomek.loon.commands.RegisterCommand;
|
||||
import ciomek.loon.commands.WebUsernameCommand;
|
||||
import ciomek.loon.mqtt.MQTTClient;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.minecraft.core.net.command.CommandManager;
|
||||
@@ -25,6 +26,7 @@ public class Loon implements ModInitializer {
|
||||
|
||||
CommandManager.registerCommand(new RegisterCommand());
|
||||
CommandManager.registerCommand(new ChangePasswordCommand());
|
||||
CommandManager.registerCommand(new WebUsernameCommand());
|
||||
|
||||
MQTTClient.getInstance();
|
||||
}
|
||||
|
||||
@@ -33,20 +33,10 @@ public class ChangePasswordCommand implements CommandManager.CommandRegistry {
|
||||
return 0;
|
||||
}
|
||||
|
||||
String password = ArgumentTypeString.getString(context, "password");
|
||||
|
||||
String hashedPassword = PasswordHasher.hash(password);
|
||||
|
||||
ChangePasswordPayload payload = new ChangePasswordPayload(player.uuid.toString(), hashedPassword);
|
||||
|
||||
MQTTClient.getInstance().publish(payload.topics().get(0), payload.toJson());
|
||||
|
||||
source.sendMessage("Sent request to the server! Waiting for the response. ");
|
||||
|
||||
MQTTClient.getInstance().subscribe("loon/register/+/response", (topic, message) -> {
|
||||
MQTTClient.getInstance().subscribe("loon/auth/register/+/response", (topic, message) -> {
|
||||
String[] parts = topic.split("/");
|
||||
|
||||
String playerUuid = parts[2];
|
||||
String playerUuid = parts[3];
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
RegisterResponse response = objectMapper.readValue(
|
||||
@@ -67,6 +57,16 @@ public class ChangePasswordCommand implements CommandManager.CommandRegistry {
|
||||
}
|
||||
});
|
||||
|
||||
String password = ArgumentTypeString.getString(context, "password");
|
||||
|
||||
String hashedPassword = PasswordHasher.hash(password);
|
||||
|
||||
ChangePasswordPayload payload = new ChangePasswordPayload(player.uuid.toString(), hashedPassword);
|
||||
|
||||
MQTTClient.getInstance().publish(payload.topics().get(0), payload.toJson());
|
||||
|
||||
source.sendMessage("Sent request to the server! Waiting for the response. ");
|
||||
|
||||
return 1;
|
||||
})
|
||||
)
|
||||
|
||||
@@ -35,21 +35,10 @@ public class RegisterCommand implements CommandManager.CommandRegistry {
|
||||
return 0;
|
||||
}
|
||||
|
||||
String internalUsername = ArgumentTypeString.getString(context, "internalUsername");
|
||||
String password = ArgumentTypeString.getString(context, "password");
|
||||
|
||||
String hashedPassword = PasswordHasher.hash(password);
|
||||
|
||||
RegisterPayload payload = new RegisterPayload(player.uuid.toString(), hashedPassword, internalUsername);
|
||||
|
||||
MQTTClient.getInstance().publish(payload.topics().get(0), payload.toJson());
|
||||
|
||||
source.sendMessage("Sent request to the server! Waiting for the response. ");
|
||||
|
||||
MQTTClient.getInstance().subscribe("loon/register/+/response", (topic, message) -> {
|
||||
MQTTClient.getInstance().subscribe("loon/auth/register/+/response", (topic, message) -> {
|
||||
String[] parts = topic.split("/");
|
||||
|
||||
String playerUuid = parts[2];
|
||||
String playerUuid = parts[3];
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
RegisterResponse response = objectMapper.readValue(
|
||||
@@ -70,6 +59,17 @@ public class RegisterCommand implements CommandManager.CommandRegistry {
|
||||
}
|
||||
});
|
||||
|
||||
String internalUsername = ArgumentTypeString.getString(context, "internalUsername");
|
||||
String password = ArgumentTypeString.getString(context, "password");
|
||||
|
||||
String hashedPassword = PasswordHasher.hash(password);
|
||||
|
||||
RegisterPayload payload = new RegisterPayload(player.uuid.toString(), hashedPassword, internalUsername);
|
||||
|
||||
MQTTClient.getInstance().publish(payload.topics().get(0), payload.toJson());
|
||||
|
||||
source.sendMessage("Sent request to the server! Waiting for the response. ");
|
||||
|
||||
return 1;
|
||||
})
|
||||
)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package ciomek.loon.commands;
|
||||
|
||||
import ciomek.loon.mqtt.MQTTClient;
|
||||
import ciomek.loon.mqtt.payload.ShowUsernameResponse;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.builder.ArgumentBuilderLiteral;
|
||||
import net.minecraft.core.entity.player.Player;
|
||||
import net.minecraft.core.net.command.CommandManager;
|
||||
import net.minecraft.core.net.command.CommandSource;
|
||||
|
||||
import static com.mojang.brigadier.builder.ArgumentBuilderLiteral.literal;
|
||||
|
||||
public class WebUsernameCommand implements CommandManager.CommandRegistry {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void register(CommandDispatcher<CommandSource> commandDispatcher) {
|
||||
commandDispatcher
|
||||
.register(
|
||||
(ArgumentBuilderLiteral<CommandSource>) (Object) literal("web-username")
|
||||
.executes(context -> {
|
||||
CommandSource source = (CommandSource) context.getSource();
|
||||
Player player = source.getSender();
|
||||
|
||||
if (player == null) {
|
||||
source.sendMessage("This command can only be run by a player!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
String uuid = player.uuid.toString();
|
||||
|
||||
MQTTClient.getInstance().subscribe("loon/auth/show_username/" + uuid + "/response", (topic, message) -> {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
ShowUsernameResponse response = objectMapper.readValue(
|
||||
message.getPayload(),
|
||||
ShowUsernameResponse.class
|
||||
);
|
||||
|
||||
if (response == null)
|
||||
return;
|
||||
|
||||
if (response.success())
|
||||
source.sendMessage(response.internalUsername());
|
||||
else
|
||||
source.sendMessage("You are not registered. Use /register command. ");
|
||||
});
|
||||
|
||||
MQTTClient.getInstance().publish("loon/auth/show_username/" + uuid, "{}");
|
||||
|
||||
return 1;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
package ciomek.loon.mqtt.payload;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record ChangePasswordPayload (
|
||||
String uuid,
|
||||
@JsonIgnore String uuid,
|
||||
String password
|
||||
) implements Payload {
|
||||
@Override
|
||||
public List<String> topics() {
|
||||
return List.of("loon/register/change_password");
|
||||
return List.of("loon/auth/register/" + uuid + "/change_password");
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
package ciomek.loon.mqtt.payload;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record RegisterPayload (
|
||||
String uuid,
|
||||
@JsonIgnore String uuid,
|
||||
String password,
|
||||
String internalUsername
|
||||
) implements Payload {
|
||||
@Override
|
||||
public List<String> topics() {
|
||||
return List.of("loon/register/request");
|
||||
return List.of("loon/auth/register/" + uuid + "/request");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package ciomek.loon.mqtt.payload;
|
||||
|
||||
public record ShowUsernameResponse(
|
||||
boolean success,
|
||||
String error,
|
||||
String internalUsername
|
||||
) {}
|
||||
Reference in new issue
Block a user