Added invulnerability & invisibility

started with movement and chatting as well. both don't work atm
This commit is contained in:
samo_lego 2019-11-24 22:16:02 +01:00
parent 51892ec52d
commit beeb1183fc
10 changed files with 128 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,11 +5,10 @@ import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.network.ServerPlayerEntity;
public interface PlayerJoinServerCallback {
Event<PlayerJoinServerCallback> EVENT = EventFactory.createArrayBacked(PlayerJoinServerCallback.class, listeners -> (player) -> {
for (PlayerJoinServerCallback callback : listeners) {
callback.onPlayerJoin(player);
}
});
void onPlayerJoin(ServerPlayerEntity player);
}
}

View File

@ -9,7 +9,6 @@ public interface DropItemCallback {
Event<DropItemCallback> EVENT = EventFactory.createArrayBacked(DropItemCallback.class, listeners -> (player) -> {
for (DropItemCallback event : listeners) {
ActionResult result = event.onDropItem(player);
if (result != ActionResult.PASS) {
return result;
}

View File

@ -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<Boolean> info) {
if(!CanSeePlayerCallback.EVENT.invoker().canSeePlayer((ServerPlayerEntity) playerEntity_1))
info.setReturnValue(false);
}*/
}

View File

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