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] 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(); + } + } +}