Files
src
build.gradle
gradle.properties
settings.gradle
Code
19 819
src/main/java/com/example/twofactorauth/TwoFactorAuthMod.java
package com.example.twofactorauth;
import com.example.twofactorauth.command.TwoFactorCommands;
import com.example.twofactorauth.config.ConfigManager;
import com.example.twofactorauth.data.AuthDataManager;
import com.example.twofactorauth.data.PlayerAuthData;
import com.example.twofactorauth.discord.DiscordWebhook;
import com.example.twofactorauth.gui.ModScreenHandlers;
import com.example.twofactorauth.verification.PendingVerificationManager;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackBlockCallback;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.PlayerBlockBreakEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TwoFactorAuthMod implements ModInitializer {
public static final String MOD_ID = "twofactorguard";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
@Override
public void onInitialize() {
ModScreenHandlers.register();
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
ConfigManager.load();
AuthDataManager.load();
});
ServerLifecycleEvents.SERVER_STOPPING.register(server -> AuthDataManager.save());
ServerTickEvents.END_SERVER_TICK.register(PendingVerificationManager::tick);
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
ServerPlayerEntity player = handler.getPlayer();
PlayerAuthData data = AuthDataManager.get(player.getUuid());
if (data != null && data.enabled && data.secret != null) {
PendingVerificationManager.markPending(player);
player.sendMessage(Text.literal("§c§lGiris icin 2FA dogrulamasi gerekli!"), false);
player.sendMessage(Text.literal("§7Google Authenticator uygulamanizdaki kodu §f/2fa verify <kod>§7 ile girin."), false);
String webhook = data.discordWebhookUrl != null && !data.discordWebhookUrl.isBlank()
? data.discordWebhookUrl
: ConfigManager.getConfig().globalDiscordWebhookUrl;
DiscordWebhook.send(webhook, "Guvenlik uyarisi: " + player.getGameProfile().getName()
+ " sunucuya giris yapti ve 2FA dogrulamasi bekliyor.");
}
});
PlayerBlockBreakEvents.BEFORE.register((world, player, pos, state, blockEntity) ->
!PendingVerificationManager.isPending(player.getUuid()));
AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) ->
PendingVerificationManager.isPending(player.getUuid()) ? ActionResult.FAIL : ActionResult.PASS);
UseBlockCallback.EVENT.register((player, world, hand, hitResult) ->
PendingVerificationManager.isPending(player.getUuid()) ? ActionResult.FAIL : ActionResult.PASS);
UseItemCallback.EVENT.register((player, world, hand) ->
PendingVerificationManager.isPending(player.getUuid()) ? ActionResult.FAIL : ActionResult.PASS);
UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) ->
PendingVerificationManager.isPending(player.getUuid()) ? ActionResult.FAIL : ActionResult.PASS);
AttackEntityCallback.EVENT.register((player, world, hand, entity, hitResult) ->
PendingVerificationManager.isPending(player.getUuid()) ? ActionResult.FAIL : ActionResult.PASS);
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
TwoFactorCommands.register(dispatcher));
}
}