From c51fdc7c0ae227d85016a9fcac168ee0dfdaec30 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Thu, 14 Nov 2019 22:08:48 +0100 Subject: [PATCH] Disabling double registering ^ as well as trying to cancel the droping of items. --- .../org/samo_lego/simpleauth/SimpleAuth.java | 10 +++++--- .../simpleauth/commands/RegisterCommand.java | 15 +++++++----- .../database/SimpleAuthDatabase.java | 24 ++++++++++++++----- .../simpleauth/event/AuthEventHandler.java | 11 ++++++++- .../event/item/DropItemCallback.java | 18 ++++++++++++++ .../player => item}/InteractItemCallback.java | 2 +- .../simpleauth/mixin/MixinPlayerEntity.java | 16 ++++++++++--- .../mixin/MixinServerPlayNetworkHandler.java | 17 ------------- .../MixinServerPlayerInteractionManager.java | 2 +- src/main/resources/fabric.mod.json | 2 +- ...uth.mixins.json => mixins.simpleauth.json} | 3 ++- 11 files changed, 80 insertions(+), 40 deletions(-) create mode 100644 src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java rename src/main/java/org/samo_lego/simpleauth/event/{entity/player => item}/InteractItemCallback.java (93%) delete mode 100644 src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java rename src/main/resources/{simpleauth.mixins.json => mixins.simpleauth.json} (82%) diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 3d4a116..8e2cedd 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -14,7 +14,8 @@ import org.samo_lego.simpleauth.database.SimpleAuthDatabase; import org.samo_lego.simpleauth.event.AuthEventHandler; import org.samo_lego.simpleauth.event.block.BreakBlockCallback; import org.samo_lego.simpleauth.event.block.InteractBlockCallback; -import org.samo_lego.simpleauth.event.entity.player.InteractItemCallback; +import org.samo_lego.simpleauth.event.item.DropItemCallback; +import org.samo_lego.simpleauth.event.item.InteractItemCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerLeaveServerCallback; @@ -48,11 +49,14 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Registering the events InteractBlockCallback.EVENT.register(AuthEventHandler::onInteractBlock); - AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> AuthEventHandler.interact(playerEntity)); + BreakBlockCallback.EVENT.register((world, pos, state, player) -> AuthEventHandler.onBlockBroken(player)); InteractItemCallback.EVENT.register(AuthEventHandler::onInteractItem); + DropItemCallback.EVENT.register(AuthEventHandler::onDropItem); PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin); PlayerLeaveServerCallback.EVENT.register(AuthEventHandler::onPlayerLeave); - BreakBlockCallback.EVENT.register((world, pos, state, player) -> AuthEventHandler.onBlockBroken(player)); + // From Fabric API + AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> AuthEventHandler.onAttackBlock(playerEntity)); + db.makeTable(); } public static HashSet authenticatedUsers = new HashSet<>(); 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 a4d21dd..ad7bce7 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -55,12 +55,15 @@ public class RegisterCommand { // Hash password String hash = argon2.hash(10, 65536, 1, password); // Writing into database - SimpleAuth.db.insert(Objects.requireNonNull(source.getEntity()).getUuidAsString(), source.getName(), hash); - SimpleAuth.authenticatedUsers.add(player); - // Letting the player know it was successful - player.sendMessage( - new LiteralText(source.getName() + ", you have registered successfully!") - ); + if(SimpleAuth.db.registerUser(Objects.requireNonNull(source.getEntity()).getUuidAsString(), source.getName(), hash)) { + SimpleAuth.authenticatedUsers.add(player); + // Letting the player know it was successful + player.sendMessage( + new LiteralText(source.getName() + ", you have registered successfully!") + ); + } + else + player.sendMessage(alreadyRegistered); } catch (Error e) { player.sendMessage(alreadyRegistered); } finally { 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 14aa11c..173f232 100644 --- a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java +++ b/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java @@ -46,20 +46,32 @@ public class SimpleAuthDatabase { } // When player registers, we insert the data into DB - public void insert(String uuid, String username, String password) { + public boolean registerUser(String uuid, String username, String password) { String sql = "INSERT INTO users(uuid, username, password) VALUES(?,?,?)"; + String sqlCheck = "SELECT UUID, Password " + + "FROM users WHERE UUID = ?"; try ( Connection conn = this.connect(); - PreparedStatement pstmt = conn.prepareStatement(sql)) { + PreparedStatement pstmt = conn.prepareStatement(sql); + PreparedStatement pstmtCheck = conn.prepareStatement(sql)) { - pstmt.setString(1, uuid); - pstmt.setString(2, username); - pstmt.setString(3, password); + pstmtCheck.setString(1,uuid); + ResultSet rs = pstmtCheck.executeQuery(); - pstmt.executeUpdate(); + // Getting the password + String dbUuid = rs.getString("UUID"); + if(dbUuid != null) { + pstmt.setString(1, uuid); + pstmt.setString(2, username); + pstmt.setString(3, password); + + pstmt.executeUpdate(); + return true; + } } catch (SQLException e) { LOGGER.error(e.getMessage()); } + return false; } // Deletes row containing the username provided 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 94e5036..c3cb18e 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -1,6 +1,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.text.LiteralText; import net.minecraft.text.TranslatableText; @@ -45,7 +46,7 @@ public class AuthEventHandler { } // Punching a block - public static ActionResult interact(PlayerEntity playerEntity) { + public static ActionResult onAttackBlock(PlayerEntity playerEntity) { if(!SimpleAuth.authenticatedUsers.contains(playerEntity)) { playerEntity.sendMessage(notAuthenticated); return ActionResult.FAIL; @@ -62,4 +63,12 @@ public class AuthEventHandler { return ActionResult.PASS; } + // Dropping an item + public static boolean onDropItem(ServerPlayerEntity player) { + if(!SimpleAuth.authenticatedUsers.contains(player)) { + player.sendMessage(notAuthenticated); + return true; + } + return false; + } } 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 new file mode 100644 index 0000000..c31570c --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/event/item/DropItemCallback.java @@ -0,0 +1,18 @@ +package org.samo_lego.simpleauth.event.item; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.minecraft.server.network.ServerPlayerEntity; + +public interface DropItemCallback { + Event EVENT = EventFactory.createArrayBacked(DropItemCallback.class, listeners -> (playerEntity) -> { + for(DropItemCallback callback : listeners) { + if(callback.onDropItem(playerEntity)) { + return true; + } + } + return false; + }); + + boolean onDropItem(ServerPlayerEntity playerEntity); +} diff --git a/src/main/java/org/samo_lego/simpleauth/event/entity/player/InteractItemCallback.java b/src/main/java/org/samo_lego/simpleauth/event/item/InteractItemCallback.java similarity index 93% rename from src/main/java/org/samo_lego/simpleauth/event/entity/player/InteractItemCallback.java rename to src/main/java/org/samo_lego/simpleauth/event/item/InteractItemCallback.java index 33acf6f..85b1d37 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/entity/player/InteractItemCallback.java +++ b/src/main/java/org/samo_lego/simpleauth/event/item/InteractItemCallback.java @@ -1,4 +1,4 @@ -package org.samo_lego.simpleauth.event.entity.player; +package org.samo_lego.simpleauth.event.item; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; 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 7230968..29f09a8 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerEntity.java @@ -2,13 +2,23 @@ package org.samo_lego.simpleauth.mixin; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.server.network.ServerPlayerEntity; +import org.samo_lego.simpleauth.event.item.DropItemCallback; 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.CallbackInfoReturnable; @Mixin(PlayerEntity.class) public abstract class MixinPlayerEntity { - /*@Inject(method = "dropItem", at = @At("HEAD")) - private void dropItem(ItemStack itemStack_1, boolean boolean_1) { - }*/ + // Thanks to AbusedLib https://github.com/abused/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) { + ServerPlayerEntity player = (ServerPlayerEntity) (Object) this; + if(DropItemCallback.EVENT.invoker().onDropItem(player)) { + player.giveItemStack(stack); + info.setReturnValue(ItemStack.EMPTY); + } + } } diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java deleted file mode 100644 index 612b734..0000000 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.samo_lego.simpleauth.mixin; - -import net.minecraft.server.network.packet.PlayerMoveC2SPacket; -import net.minecraft.util.PacketByteBuf; -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.CallbackInfo; - -@Mixin(PlayerMoveC2SPacket.class) -public abstract class MixinServerPlayNetworkHandler { - - @Inject(method = "read", at = @At("RETURN")) - private void read(PacketByteBuf packetByteBuf_1, CallbackInfo ci) { - System.out.println("Packet "+packetByteBuf_1); - } -} diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayerInteractionManager.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayerInteractionManager.java index 41fa3fb..be81d8c 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayerInteractionManager.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayerInteractionManager.java @@ -12,7 +12,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.samo_lego.simpleauth.event.block.BreakBlockCallback; import org.samo_lego.simpleauth.event.block.InteractBlockCallback; -import org.samo_lego.simpleauth.event.entity.player.InteractItemCallback; +import org.samo_lego.simpleauth.event.item.InteractItemCallback; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 1f4b7f2..8b24f3f 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -21,7 +21,7 @@ ] }, "mixins": [ - "simpleauth.mixins.json" + "mixins.simpleauth.json" ], "depends": { diff --git a/src/main/resources/simpleauth.mixins.json b/src/main/resources/mixins.simpleauth.json similarity index 82% rename from src/main/resources/simpleauth.mixins.json rename to src/main/resources/mixins.simpleauth.json index c5194c1..ebca85d 100644 --- a/src/main/resources/simpleauth.mixins.json +++ b/src/main/resources/mixins.simpleauth.json @@ -6,7 +6,8 @@ ], "server": [ "MixinServerPlayerInteractionManager", - "MixinPlayerManager" + "MixinPlayerManager", + "MixinPlayerEntity" ], "injectors": { "defaultRequire": 1