Disabling double registering

^ as well as trying to cancel the droping of items.
This commit is contained in:
samo_lego 2019-11-14 22:08:48 +01:00
parent 476bddacc8
commit c51fdc7c0a
11 changed files with 80 additions and 40 deletions

View File

@ -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<ServerPlayerEntity> authenticatedUsers = new HashSet<>();

View File

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

View File

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

View File

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

View File

@ -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<DropItemCallback> EVENT = EventFactory.createArrayBacked(DropItemCallback.class, listeners -> (playerEntity) -> {
for(DropItemCallback callback : listeners) {
if(callback.onDropItem(playerEntity)) {
return true;
}
}
return false;
});
boolean onDropItem(ServerPlayerEntity playerEntity);
}

View File

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

View File

@ -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<ItemStack> info) {
ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;
if(DropItemCallback.EVENT.invoker().onDropItem(player)) {
player.giveItemStack(stack);
info.setReturnValue(ItemStack.EMPTY);
}
}
}

View File

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

View File

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

View File

@ -21,7 +21,7 @@
]
},
"mixins": [
"simpleauth.mixins.json"
"mixins.simpleauth.json"
],
"depends": {

View File

@ -6,7 +6,8 @@
],
"server": [
"MixinServerPlayerInteractionManager",
"MixinPlayerManager"
"MixinPlayerManager",
"MixinPlayerEntity"
],
"injectors": {
"defaultRequire": 1