From 51892ec52d72a5b9c0fa49e294eac5f7563f879d Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Sat, 23 Nov 2019 17:58:43 +0100 Subject: [PATCH 1/3] 1.15 update Updatede onUseItem event. Making the dropitemcallback work now. --- gradle.properties | 17 ++++----- .../org/samo_lego/simpleauth/SimpleAuth.java | 27 +++++++++----- .../simpleauth/commands/LoginCommand.java | 1 + .../commands/UnregisterCommand.java | 20 ++++++---- .../database/SimpleAuthDatabase.java | 37 +++++++++++-------- .../simpleauth/event/AuthEventHandler.java | 19 +++------- .../event/item/DropItemCallback.java | 2 +- .../simpleauth/mixin/MixinPlayerEntity.java | 21 ++++++++--- .../simpleauth/utils/AuthHelper.java | 2 - 9 files changed, 83 insertions(+), 63 deletions(-) diff --git a/gradle.properties b/gradle.properties index 2ef771c..228331c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,16 +2,15 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties - # check these on https://fabricmc.net/use - minecraft_version=1.14.4 - yarn_mappings=1.14.4+build.12 - loader_version=0.6.1+build.164 + minecraft_version=1.15-pre1 + yarn_mappings=1.15-pre1+build.3 + loader_version=0.7.1+build.173 + +# Dependencies + # Fabric API + fabric_version=0.4.13+build.264-1.15 # Mod Properties mod_version = 1.0.0 maven_group = org.samo_lego - archives_base_name = simpleauth - -# Dependencies - # currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api - fabric_version=0.3.2+build.218-1.14 \ No newline at end of file + archives_base_name = simpleauth \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 71655de..a58baa4 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -2,8 +2,10 @@ package org.samo_lego.simpleauth; import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.fabric.api.event.player.*; +import net.fabricmc.fabric.api.event.server.ServerStopCallback; import net.fabricmc.fabric.api.registry.CommandRegistry; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -20,18 +22,22 @@ import java.util.HashSet; public class SimpleAuth implements DedicatedServerModInitializer { private static final Logger LOGGER = LogManager.getLogger(); public static SimpleAuthDatabase db = new SimpleAuthDatabase(); + public static HashSet authenticatedUsers = new HashSet<>(); + public static boolean isAuthenticated(ServerPlayerEntity player) { return authenticatedUsers.contains(player); } - @Override + @Override public void onInitializeServer() { // Info I guess :D - LOGGER.info("SimpleAuth mod by samo_lego."); + LOGGER.info("[SimpleAuth] SimpleAuth mod by samo_lego."); // The support on discord was great! I really appreciate your help. - LOGGER.info("This mod wouldn't exist without the awesome Fabric Community. TYSM guys!"); + LOGGER.info("[SimpleAuth] This mod wouldn't exist without the awesome Fabric Community. TYSM guys!"); + // Connecting to db + db.openConnection(); // Creating data directory (database is stored there) File file = new File("./mods/SimpleAuth"); if (!file.exists() && !file.mkdir()) - LOGGER.error("Error creating directory"); + LOGGER.error("[SimpleAuth]: Error creating directory!"); // Registering the commands @@ -53,12 +59,13 @@ public class SimpleAuth implements DedicatedServerModInitializer { UseItemCallback.EVENT.register((player, world, hand) -> AuthEventHandler.onUseItem(player)); AttackEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onAttackEntity(player)); UseEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onUseEntity(player)); - - // Making a table in database + ServerStopCallback.EVENT.register(minecraftServer -> SimpleAuth.onStopServer()); + // Making a table in the database db.makeTable(); } - public static HashSet authenticatedUsers = new HashSet<>(); - public static boolean isAuthenticated(ServerPlayerEntity player) { return authenticatedUsers.contains(player); } - -} + private static void onStopServer() { + LOGGER.info("[SimpleAuth] Shutting down SimpleAuth."); + db.close(); + } +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java index 8455c63..9c8c060 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -46,6 +46,7 @@ public class LoginCommand { return 0; } else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) { + SimpleAuth.authenticatedUsers.add(player); player.sendMessage(text); return 1; } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java index 271816a..a117449 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/UnregisterCommand.java @@ -8,20 +8,21 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.TranslatableText; import org.samo_lego.simpleauth.SimpleAuth; +import org.samo_lego.simpleauth.utils.AuthHelper; import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.word; import static net.minecraft.server.command.CommandManager.argument; import static net.minecraft.server.command.CommandManager.literal; -public class UnregisterCommand { // TODO +public class UnregisterCommand { private static TranslatableText enterPassword = new TranslatableText("command.simpleauth.password"); private static TranslatableText wrongPassword = new TranslatableText("command.simpleauth.wrongPassword"); - private static TranslatableText accountDeleted = new TranslatableText("command.simpleauth.passwordUpdated"); + private static TranslatableText accountDeleted = new TranslatableText("command.simpleauth.accountDeleted"); public static void registerCommand(CommandDispatcher dispatcher) { - // Registering the "/changepw" command - dispatcher.register(literal("changepw") + // Registering the "/unregister" command + dispatcher.register(literal("unregister") .executes(ctx -> { ctx.getSource().getPlayer().sendMessage(enterPassword); return 1; @@ -36,11 +37,16 @@ public class UnregisterCommand { // TODO ); } - // Method called for checking the password and then changing it + // Method called for checking the password and then removing user's account from db private static int unregister(ServerCommandSource source, String pass) throws CommandSyntaxException { // Getting the player who send the command ServerPlayerEntity player = source.getPlayer(); - - return 1; + if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) { + SimpleAuth.db.delete(player.getUuidAsString(), null); + player.sendMessage(accountDeleted); + return 1; + } + player.sendMessage(wrongPassword); + return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java b/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java index 74876f0..f38616a 100644 --- a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java +++ b/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java @@ -14,21 +14,32 @@ public class SimpleAuthDatabase { private static final Logger LOGGER = LogManager.getLogger(); // Connects to the DB - private Connection connect() { + private Connection conn; + + public void openConnection() { // SQLite connection string String url = "jdbc:sqlite:mods/SimpleAuth/players.db"; - Connection conn = null; try { conn = DriverManager.getConnection(url); } catch (SQLException e) { LOGGER.error(e.getMessage()); } - return conn; + } + // Cllosing connection + public void close() { + if (conn != null) { + try { + conn.close(); + LOGGER.info("[SimpleAuth] Database connection closed successfully."); + } catch (SQLException e) { + LOGGER.info("[SimpleAuth] Error: " + e); + } + } } // If the mod runs for the first time, we need to create the DB table public void makeTable() { - try (Connection conn = this.connect()) { + try { // Creating database table if it doesn't exist yet String sql = "CREATE TABLE IF NOT EXISTS users (\n" + " `UserID` INTEGER PRIMARY KEY AUTOINCREMENT,\n" + @@ -50,9 +61,8 @@ public class SimpleAuthDatabase { String sql = "INSERT INTO users(uuid, username, password) VALUES(?,?,?)"; String sqlCheck = "SELECT UUID " + "FROM users WHERE UUID = ?"; - try ( - Connection conn = this.connect(); - PreparedStatement pstmt = conn.prepareStatement(sql); + + try (PreparedStatement pstmt = conn.prepareStatement(sql); PreparedStatement pstmtCheck = conn.prepareStatement(sqlCheck)) { pstmtCheck.setString(1, uuid); @@ -81,8 +91,7 @@ public class SimpleAuthDatabase { public void delete(String uuid, String username) { String sql = "DELETE FROM users WHERE uuid = ? OR username = ?"; - try (Connection conn = this.connect(); - PreparedStatement pstmt = conn.prepareStatement(sql)) { + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { // set the corresponding param pstmt.setString(1, uuid); @@ -100,8 +109,7 @@ public class SimpleAuthDatabase { String sql = "UPDATE users SET password = ? " + "WHERE uuid = ? OR username = ?"; - try (Connection conn = this.connect(); - PreparedStatement pstmt = conn.prepareStatement(sql)) { + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { // set the corresponding param pstmt.setString(1, pass); @@ -121,10 +129,9 @@ public class SimpleAuthDatabase { + "FROM users WHERE UUID = ?"; String pass = null; - try (Connection conn = this.connect(); - PreparedStatement pstmt = conn.prepareStatement(sql)) { + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { // Setting statement - pstmt.setString(1,uuid); + pstmt.setString(1, uuid); ResultSet rs = pstmt.executeQuery(); // Getting the password @@ -134,4 +141,4 @@ public class SimpleAuthDatabase { } return pass; } -} +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index e2b0b4a..5625959 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -1,9 +1,11 @@ package org.samo_lego.simpleauth.event; import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; +import net.minecraft.util.TypedActionResult; import org.samo_lego.simpleauth.SimpleAuth; /** @@ -25,15 +27,6 @@ public class AuthEventHandler { SimpleAuth.authenticatedUsers.remove(player); } - // Breaking block - public static boolean onBlockBroken(PlayerEntity player) { - if (!SimpleAuth.isAuthenticated((ServerPlayerEntity) player)) { - player.sendMessage(notAuthenticated); - return true; - } - return false; - } - // Using a block (right-click function) public static ActionResult onUseBlock(PlayerEntity player) { if(!SimpleAuth.authenticatedUsers.contains(player)) { @@ -53,13 +46,13 @@ public class AuthEventHandler { } // Using an item - public static ActionResult onUseItem(PlayerEntity player) { + public static TypedActionResult onUseItem(PlayerEntity player) { if(!SimpleAuth.authenticatedUsers.contains(player)) { player.sendMessage(notAuthenticated); - return ActionResult.FAIL; + return TypedActionResult.fail(ItemStack.EMPTY); } - return ActionResult.PASS; + return TypedActionResult.pass(ItemStack.EMPTY); } // Attacking an entity public static ActionResult onAttackEntity(PlayerEntity player) { @@ -87,4 +80,4 @@ public class AuthEventHandler { } return ActionResult.PASS; } -} +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java b/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java index c26d41a..8a6122f 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java +++ b/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java @@ -18,4 +18,4 @@ public interface DropItemCallback { }); ActionResult onDropItem(PlayerEntity player); -} +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java index 930777a..fb13c31 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java @@ -1,10 +1,13 @@ package org.samo_lego.simpleauth.mixin; +import net.minecraft.container.PlayerContainer; +import net.minecraft.entity.ItemEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.ActionResult; import org.samo_lego.simpleauth.event.item.DropItemCallback; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; @@ -13,14 +16,20 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(PlayerEntity.class) public abstract class MixinPlayerEntity { - // Thanks to PR https://github.com/FabricMC/fabric/pull/260 and AbusedLib - /*@Inject(method = "dropItem(Lnet/minecraft/item/ItemStack;ZZ)Lnet/minecraft/entity/ItemEntity;", at = @At("HEAD")) - private void dropItem(ItemStack stack, boolean boolean_1, boolean boolean_2, CallbackInfoReturnable info) { + + @Shadow @Final public PlayerContainer playerContainer; + + // Thanks to PR https://github.com/FabricMC/fabric/pull/260 and AbusedLib https://github.com/abused/AbusedLib + @Inject(method = "dropItem(Lnet/minecraft/item/ItemStack;ZZ)Lnet/minecraft/entity/ItemEntity;", at = @At("HEAD"), cancellable = true) + private void dropItem(ItemStack stack, boolean dropAtFeet, boolean saveThrower, CallbackInfoReturnable cir) { ServerPlayerEntity player = (ServerPlayerEntity) (Object) this; ActionResult result = DropItemCallback.EVENT.invoker().onDropItem(player); if (result == ActionResult.FAIL) { - info.cancel(); + // Canceling the item drop, as well as giving the items back to player + cir.cancel(); + player.giveItemStack(stack); + playerContainer.sendContentUpdates(); } - }*/ -} + } +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java index 18a4259..0c0b3b4 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java @@ -23,8 +23,6 @@ public class AuthHelper { } finally { // Wipe confidential data argon2.wipeArray(pass); - // Todo del line - System.out.println("Pass data wiped."); } return false; } From beeb1183fca6e302c16552555655461313173139 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Sun, 24 Nov 2019 22:16:02 +0100 Subject: [PATCH 2/3] Added invulnerability & invisibility started with movement and chatting as well. both don't work atm --- .../org/samo_lego/simpleauth/SimpleAuth.java | 8 +++- .../simpleauth/commands/LoginCommand.java | 3 ++ .../simpleauth/commands/RegisterCommand.java | 3 ++ .../simpleauth/event/AuthEventHandler.java | 29 ++++++++++++- .../event/entity/player/OnChatCallback.java | 22 ++++++++++ .../entity/player/OnPlayerMoveCallback.java | 22 ++++++++++ .../player/PlayerJoinServerCallback.java | 3 +- .../event/item/DropItemCallback.java | 1 - .../simpleauth/mixin/MixinEntity.java | 20 --------- .../mixin/MixinServerPlayNetworkHandler.java | 43 +++++++++++++++++++ 10 files changed, 128 insertions(+), 26 deletions(-) create mode 100644 src/main/java/org/samo_lego/simpleauth/event/entity/player/OnChatCallback.java create mode 100644 src/main/java/org/samo_lego/simpleauth/event/entity/player/OnPlayerMoveCallback.java delete mode 100644 src/main/java/org/samo_lego/simpleauth/mixin/MixinEntity.java create mode 100644 src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index a58baa4..29e2b8e 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -5,13 +5,14 @@ import net.fabricmc.fabric.api.event.player.*; import net.fabricmc.fabric.api.event.server.ServerStopCallback; import net.fabricmc.fabric.api.registry.CommandRegistry; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.server.MinecraftServer; import net.minecraft.server.network.ServerPlayerEntity; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.samo_lego.simpleauth.commands.*; import org.samo_lego.simpleauth.database.SimpleAuthDatabase; import org.samo_lego.simpleauth.event.AuthEventHandler; +import org.samo_lego.simpleauth.event.entity.player.OnChatCallback; +import org.samo_lego.simpleauth.event.entity.player.OnPlayerMoveCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerLeaveServerCallback; import org.samo_lego.simpleauth.event.item.DropItemCallback; @@ -37,7 +38,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Creating data directory (database is stored there) File file = new File("./mods/SimpleAuth"); if (!file.exists() && !file.mkdir()) - LOGGER.error("[SimpleAuth]: Error creating directory!"); + LOGGER.error("[SimpleAuth] Error creating directory!"); // Registering the commands @@ -53,6 +54,9 @@ public class SimpleAuth implements DedicatedServerModInitializer { PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin); PlayerLeaveServerCallback.EVENT.register(AuthEventHandler::onPlayerLeave); DropItemCallback.EVENT.register(AuthEventHandler::onDropItem); + //todo + OnChatCallback.EVENT.register(AuthEventHandler::onPlayerChat); + OnPlayerMoveCallback.EVENT.register(AuthEventHandler::onPlayerMove); // From Fabric API AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> AuthEventHandler.onAttackBlock(playerEntity)); UseBlockCallback.EVENT.register((player, world, hand, blockHitResult) -> AuthEventHandler.onUseBlock(player)); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java index 9c8c060..4a48d27 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -47,6 +47,9 @@ public class LoginCommand { } else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) { SimpleAuth.authenticatedUsers.add(player); + // Player no longer needs to be invisible and invulnerable + player.setInvulnerable(false); + player.setInvisible(false); player.sendMessage(text); return 1; } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java index bedc420..df0be6a 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -50,6 +50,9 @@ public class RegisterCommand { String hash = AuthHelper.hashPass(pass1.toCharArray()); if (SimpleAuth.db.registerUser(player.getUuidAsString(), source.getName(), hash)) { SimpleAuth.authenticatedUsers.add(player); + // Player no longer needs to be invisible and invulnerable + player.setInvulnerable(false); + player.setInvisible(false); player.sendMessage(registerSuccess); return 1; } diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index 5625959..ed312f5 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -3,6 +3,7 @@ package org.samo_lego.simpleauth.event; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.network.packet.ChatMessageC2SPacket; import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; import net.minecraft.util.TypedActionResult; @@ -18,8 +19,12 @@ public class AuthEventHandler { // Player joining the server public static void onPlayerJoin(ServerPlayerEntity player) { // Player not authenticated - if (!SimpleAuth.isAuthenticated(player)) + if (!SimpleAuth.isAuthenticated(player)) { player.sendMessage(notAuthenticated); + // Setting the player to be invisible to mobs and also invulnerable + player.setInvulnerable(true); + player.setInvisible(true); + } } // Player leaving the server @@ -27,6 +32,28 @@ public class AuthEventHandler { SimpleAuth.authenticatedUsers.remove(player); } + // todo + public static ActionResult onPlayerChat(PlayerEntity player, ChatMessageC2SPacket chatMessageC2SPacket) { + String msg = chatMessageC2SPacket.getChatMessage(); + System.out.println(msg); + if(!SimpleAuth.authenticatedUsers.contains(player) || !msg.startsWith("/login") || !msg.startsWith("/register")) { + System.out.println("Ok."); + player.sendMessage(notAuthenticated); + return ActionResult.FAIL; + } + System.out.println("Pass "+ msg.startsWith("/login")+msg.startsWith("/register")); + return ActionResult.PASS; + } + //todo + public static ActionResult onPlayerMove(PlayerEntity player) { + if(!SimpleAuth.authenticatedUsers.contains(player)) { + System.out.println("Ok. Moved & should fail. AuthEventHandler"); + player.sendMessage(notAuthenticated); + return ActionResult.FAIL; + } + return ActionResult.PASS; + } + // Using a block (right-click function) public static ActionResult onUseBlock(PlayerEntity player) { if(!SimpleAuth.authenticatedUsers.contains(player)) { diff --git a/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnChatCallback.java b/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnChatCallback.java new file mode 100644 index 0000000..dca8ed1 --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnChatCallback.java @@ -0,0 +1,22 @@ +package org.samo_lego.simpleauth.event.entity.player; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.network.packet.ChatMessageC2SPacket; +import net.minecraft.util.ActionResult; + +public interface OnChatCallback { + Event EVENT = EventFactory.createArrayBacked(OnChatCallback.class, listeners -> (player, chatMessageC2SPacket) -> { + for (OnChatCallback event : listeners) { + ActionResult result = event.onPlayerChat(player, chatMessageC2SPacket); + + if (result != ActionResult.PASS) { + return result; + } + } + return ActionResult.PASS; + }); + + ActionResult onPlayerChat(PlayerEntity player, ChatMessageC2SPacket chatMessageC2SPacket); +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnPlayerMoveCallback.java b/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnPlayerMoveCallback.java new file mode 100644 index 0000000..6ad4e06 --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/event/entity/player/OnPlayerMoveCallback.java @@ -0,0 +1,22 @@ +package org.samo_lego.simpleauth.event.entity.player; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.network.packet.ChatMessageC2SPacket; +import net.minecraft.util.ActionResult; + +public interface OnPlayerMoveCallback { + Event EVENT = EventFactory.createArrayBacked(OnPlayerMoveCallback.class, listeners -> (player) -> { + for (OnPlayerMoveCallback event : listeners) { + ActionResult result = event.onPlayerMove(player); + + if (result != ActionResult.PASS) { + return result; + } + } + return ActionResult.PASS; + }); + + ActionResult onPlayerMove(PlayerEntity player); +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerJoinServerCallback.java b/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerJoinServerCallback.java index c4e2023..adfb59d 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerJoinServerCallback.java +++ b/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerJoinServerCallback.java @@ -5,11 +5,10 @@ import net.fabricmc.fabric.api.event.EventFactory; import net.minecraft.server.network.ServerPlayerEntity; public interface PlayerJoinServerCallback { - Event EVENT = EventFactory.createArrayBacked(PlayerJoinServerCallback.class, listeners -> (player) -> { for (PlayerJoinServerCallback callback : listeners) { callback.onPlayerJoin(player); } }); void onPlayerJoin(ServerPlayerEntity player); -} +} \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java b/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java index 8a6122f..79b59c5 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java +++ b/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java @@ -9,7 +9,6 @@ public interface DropItemCallback { Event EVENT = EventFactory.createArrayBacked(DropItemCallback.class, listeners -> (player) -> { for (DropItemCallback event : listeners) { ActionResult result = event.onDropItem(player); - if (result != ActionResult.PASS) { return result; } diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinEntity.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinEntity.java deleted file mode 100644 index 75f5125..0000000 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinEntity.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.samo_lego.simpleauth.mixin; - -import net.minecraft.entity.Entity; -import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.server.network.ServerPlayerEntity; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Mixin(Entity.class) -public abstract class MixinEntity { - - /*@Inject(method = "canSeePlayer", at = @At("HEAD"), cancellable = true) - private void canSeePlayer(PlayerEntity playerEntity_1, CallbackInfoReturnable info) { - if(!CanSeePlayerCallback.EVENT.invoker().canSeePlayer((ServerPlayerEntity) playerEntity_1)) - info.setReturnValue(false); - - }*/ -} diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java new file mode 100644 index 0000000..a8d3621 --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java @@ -0,0 +1,43 @@ +package org.samo_lego.simpleauth.mixin; + +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.network.packet.ChatMessageC2SPacket; +import net.minecraft.server.network.packet.PlayerMoveC2SPacket; +import net.minecraft.util.ActionResult; +import org.samo_lego.simpleauth.event.entity.player.OnChatCallback; +import org.samo_lego.simpleauth.event.entity.player.OnPlayerMoveCallback; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(ServerPlayNetworkHandler.class) +public class MixinServerPlayNetworkHandler { + @Shadow + public ServerPlayerEntity player; + + // TODO + @Inject(method = "onChatMessage", at = @At(value = "HEAD"), cancellable = true) + private void onChatMessage(ChatMessageC2SPacket chatMessageC2SPacket_1, CallbackInfoReturnable cir) { + ActionResult result = OnChatCallback.EVENT.invoker().onPlayerChat(player, chatMessageC2SPacket_1); + + System.out.println("Mixined"); + if (result == ActionResult.FAIL) { + cir.cancel(); + } + } + + // TODO + @Inject(method="onPlayerMove", at = @At(value = "HEAD"), cancellable = true) + private void onPlayerMove(PlayerMoveC2SPacket playerMoveC2SPacket_1, CallbackInfo cir) { + ActionResult result = OnPlayerMoveCallback.EVENT.invoker().onPlayerMove(player); + + System.out.println("Player move"); + if (result == ActionResult.FAIL) { + cir.cancel(); + } + } +} From 653230e5342bee6d98d50feb74f77212158aedce Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Tue, 26 Nov 2019 22:11:56 +0100 Subject: [PATCH 3/3] Update to 1.15pre2 Added chat protection and movement protection. Mod should now enter polishing state. --- gradle.properties | 13 ++++--- .../simpleauth/event/AuthEventHandler.java | 8 ++--- .../simpleauth/mixin/MixinPlayerEntity.java | 6 ++-- .../mixin/MixinServerPlayNetworkHandler.java | 35 +++++++++++++------ src/main/resources/mixins.simpleauth.json | 2 +- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/gradle.properties b/gradle.properties index 228331c..bdd5fcf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,14 +1,13 @@ # Done to increase the memory available to gradle. org.gradle.jvmargs=-Xmx1G -# Fabric Properties - minecraft_version=1.15-pre1 - yarn_mappings=1.15-pre1+build.3 - loader_version=0.7.1+build.173 +# Fabric properties +minecraft_version=1.15-pre2 +yarn_mappings=1.15-pre2+build.3 +loader_version=0.7.1+build.173 -# Dependencies - # Fabric API - fabric_version=0.4.13+build.264-1.15 +#Fabric api +fabric_version=0.4.16+build.268-1.15 # Mod Properties mod_version = 1.0.0 diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index ed312f5..50394e7 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -35,20 +35,16 @@ public class AuthEventHandler { // todo public static ActionResult onPlayerChat(PlayerEntity player, ChatMessageC2SPacket chatMessageC2SPacket) { String msg = chatMessageC2SPacket.getChatMessage(); - System.out.println(msg); - if(!SimpleAuth.authenticatedUsers.contains(player) || !msg.startsWith("/login") || !msg.startsWith("/register")) { - System.out.println("Ok."); + if(!SimpleAuth.authenticatedUsers.contains(player) && !(msg.startsWith("/login") || msg.startsWith("/register"))) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } - System.out.println("Pass "+ msg.startsWith("/login")+msg.startsWith("/register")); return ActionResult.PASS; } //todo public static ActionResult onPlayerMove(PlayerEntity player) { if(!SimpleAuth.authenticatedUsers.contains(player)) { - System.out.println("Ok. Moved & should fail. AuthEventHandler"); - player.sendMessage(notAuthenticated); + // TP player back? return ActionResult.FAIL; } return ActionResult.PASS; diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java index fb13c31..d1d2de7 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java @@ -26,10 +26,12 @@ public abstract class MixinPlayerEntity { ActionResult result = DropItemCallback.EVENT.invoker().onDropItem(player); if (result == ActionResult.FAIL) { - // Canceling the item drop, as well as giving the items back to player - cir.cancel(); + // Canceling the item drop, as well as giving the items back to player (and updating inv with packet) player.giveItemStack(stack); + + player.inventory.updateItems(); playerContainer.sendContentUpdates(); + cir.cancel(); } } } \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java index a8d3621..22043f3 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java @@ -12,7 +12,6 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ServerPlayNetworkHandler.class) public class MixinServerPlayNetworkHandler { @@ -20,24 +19,38 @@ public class MixinServerPlayNetworkHandler { public ServerPlayerEntity player; // TODO - @Inject(method = "onChatMessage", at = @At(value = "HEAD"), cancellable = true) - private void onChatMessage(ChatMessageC2SPacket chatMessageC2SPacket_1, CallbackInfoReturnable cir) { + @Inject( + method = "onChatMessage(Lnet/minecraft/server/network/packet/ChatMessageC2SPacket;)V", + at = @At( + value = "INVOKE", + // Thanks to Liach for helping me out! + target = "net/minecraft/network/NetworkThreadUtils.forceMainThread(Lnet/minecraft/network/Packet;Lnet/minecraft/network/listener/PacketListener;Lnet/minecraft/server/world/ServerWorld;)V", + shift = At.Shift.AFTER + ), + cancellable = true + ) + private void onChatMessage(ChatMessageC2SPacket chatMessageC2SPacket_1, CallbackInfo ci) { ActionResult result = OnChatCallback.EVENT.invoker().onPlayerChat(player, chatMessageC2SPacket_1); - - System.out.println("Mixined"); if (result == ActionResult.FAIL) { - cir.cancel(); + ci.cancel(); } } // TODO - @Inject(method="onPlayerMove", at = @At(value = "HEAD"), cancellable = true) - private void onPlayerMove(PlayerMoveC2SPacket playerMoveC2SPacket_1, CallbackInfo cir) { + @Inject( + method="onPlayerMove(Lnet/minecraft/server/network/packet/PlayerMoveC2SPacket;)V", + at = @At( + value = "INVOKE", + // Thanks to Liach for helping me out! + target = "net/minecraft/network/NetworkThreadUtils.forceMainThread(Lnet/minecraft/network/Packet;Lnet/minecraft/network/listener/PacketListener;Lnet/minecraft/server/world/ServerWorld;)V", + shift = At.Shift.AFTER + ), + cancellable = true + ) + private void onPlayerMove(PlayerMoveC2SPacket playerMoveC2SPacket_1, CallbackInfo ci) { ActionResult result = OnPlayerMoveCallback.EVENT.invoker().onPlayerMove(player); - - System.out.println("Player move"); if (result == ActionResult.FAIL) { - cir.cancel(); + ci.cancel(); } } } diff --git a/src/main/resources/mixins.simpleauth.json b/src/main/resources/mixins.simpleauth.json index 5d45aef..90ef875 100644 --- a/src/main/resources/mixins.simpleauth.json +++ b/src/main/resources/mixins.simpleauth.json @@ -5,7 +5,7 @@ "mixins": [ ], "server": [ - + "MixinServerPlayNetworkHandler", "MixinPlayerManager", "MixinPlayerEntity" ],