1.15 update

Updatede onUseItem event. Making the dropitemcallback work now.
This commit is contained in:
samo_lego 2019-11-23 17:58:43 +01:00
parent 47fd0cb0fb
commit 51892ec52d
9 changed files with 83 additions and 63 deletions

View File

@ -2,16 +2,15 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.14.4
yarn_mappings=1.14.4+build.12
loader_version=0.6.1+build.164
minecraft_version=1.15-pre1
yarn_mappings=1.15-pre1+build.3
loader_version=0.7.1+build.173
# Dependencies
# Fabric API
fabric_version=0.4.13+build.264-1.15
# Mod Properties
mod_version = 1.0.0
maven_group = org.samo_lego
archives_base_name = simpleauth
# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.3.2+build.218-1.14
archives_base_name = simpleauth

View File

@ -2,8 +2,10 @@ package org.samo_lego.simpleauth;
import net.fabricmc.api.DedicatedServerModInitializer;
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;
@ -20,18 +22,22 @@ import java.util.HashSet;
public class SimpleAuth implements DedicatedServerModInitializer {
private static final Logger LOGGER = LogManager.getLogger();
public static SimpleAuthDatabase db = new SimpleAuthDatabase();
public static HashSet<PlayerEntity> authenticatedUsers = new HashSet<>();
public static boolean isAuthenticated(ServerPlayerEntity player) { return authenticatedUsers.contains(player); }
@Override
@Override
public void onInitializeServer() {
// Info I guess :D
LOGGER.info("SimpleAuth mod by samo_lego.");
LOGGER.info("[SimpleAuth] SimpleAuth mod by samo_lego.");
// The support on discord was great! I really appreciate your help.
LOGGER.info("This mod wouldn't exist without the awesome Fabric Community. TYSM guys!");
LOGGER.info("[SimpleAuth] This mod wouldn't exist without the awesome Fabric Community. TYSM guys!");
// Connecting to db
db.openConnection();
// Creating data directory (database is stored there)
File file = new File("./mods/SimpleAuth");
if (!file.exists() && !file.mkdir())
LOGGER.error("Error creating directory");
LOGGER.error("[SimpleAuth]: Error creating directory!");
// Registering the commands
@ -53,12 +59,13 @@ public class SimpleAuth implements DedicatedServerModInitializer {
UseItemCallback.EVENT.register((player, world, hand) -> AuthEventHandler.onUseItem(player));
AttackEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onAttackEntity(player));
UseEntityCallback.EVENT.register((player, world, hand, entity, entityHitResult) -> AuthEventHandler.onUseEntity(player));
// Making a table in database
ServerStopCallback.EVENT.register(minecraftServer -> SimpleAuth.onStopServer());
// Making a table in the database
db.makeTable();
}
public static HashSet<PlayerEntity> authenticatedUsers = new HashSet<>();
public static boolean isAuthenticated(ServerPlayerEntity player) { return authenticatedUsers.contains(player); }
}
private static void onStopServer() {
LOGGER.info("[SimpleAuth] Shutting down SimpleAuth.");
db.close();
}
}

View File

@ -46,6 +46,7 @@ public class LoginCommand {
return 0;
}
else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) {
SimpleAuth.authenticatedUsers.add(player);
player.sendMessage(text);
return 1;
}

View File

@ -8,20 +8,21 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.TranslatableText;
import org.samo_lego.simpleauth.SimpleAuth;
import org.samo_lego.simpleauth.utils.AuthHelper;
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.word;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
public class UnregisterCommand { // TODO
public class UnregisterCommand {
private static TranslatableText enterPassword = new TranslatableText("command.simpleauth.password");
private static TranslatableText wrongPassword = new TranslatableText("command.simpleauth.wrongPassword");
private static TranslatableText accountDeleted = new TranslatableText("command.simpleauth.passwordUpdated");
private static TranslatableText accountDeleted = new TranslatableText("command.simpleauth.accountDeleted");
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
// Registering the "/changepw" command
dispatcher.register(literal("changepw")
// Registering the "/unregister" command
dispatcher.register(literal("unregister")
.executes(ctx -> {
ctx.getSource().getPlayer().sendMessage(enterPassword);
return 1;
@ -36,11 +37,16 @@ public class UnregisterCommand { // TODO
);
}
// Method called for checking the password and then changing it
// Method called for checking the password and then removing user's account from db
private static int unregister(ServerCommandSource source, String pass) throws CommandSyntaxException {
// Getting the player who send the command
ServerPlayerEntity player = source.getPlayer();
return 1;
if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) {
SimpleAuth.db.delete(player.getUuidAsString(), null);
player.sendMessage(accountDeleted);
return 1;
}
player.sendMessage(wrongPassword);
return 0;
}
}

View File

@ -14,21 +14,32 @@ public class SimpleAuthDatabase {
private static final Logger LOGGER = LogManager.getLogger();
// Connects to the DB
private Connection connect() {
private Connection conn;
public void openConnection() {
// SQLite connection string
String url = "jdbc:sqlite:mods/SimpleAuth/players.db";
Connection conn = null;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
LOGGER.error(e.getMessage());
}
return conn;
}
// Cllosing connection
public void close() {
if (conn != null) {
try {
conn.close();
LOGGER.info("[SimpleAuth] Database connection closed successfully.");
} catch (SQLException e) {
LOGGER.info("[SimpleAuth] Error: " + e);
}
}
}
// If the mod runs for the first time, we need to create the DB table
public void makeTable() {
try (Connection conn = this.connect()) {
try {
// Creating database table if it doesn't exist yet
String sql = "CREATE TABLE IF NOT EXISTS users (\n" +
" `UserID` INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
@ -50,9 +61,8 @@ public class SimpleAuthDatabase {
String sql = "INSERT INTO users(uuid, username, password) VALUES(?,?,?)";
String sqlCheck = "SELECT UUID "
+ "FROM users WHERE UUID = ?";
try (
Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql);
try (PreparedStatement pstmt = conn.prepareStatement(sql);
PreparedStatement pstmtCheck = conn.prepareStatement(sqlCheck)) {
pstmtCheck.setString(1, uuid);
@ -81,8 +91,7 @@ public class SimpleAuthDatabase {
public void delete(String uuid, String username) {
String sql = "DELETE FROM users WHERE uuid = ? OR username = ?";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
// set the corresponding param
pstmt.setString(1, uuid);
@ -100,8 +109,7 @@ public class SimpleAuthDatabase {
String sql = "UPDATE users SET password = ? "
+ "WHERE uuid = ? OR username = ?";
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
// set the corresponding param
pstmt.setString(1, pass);
@ -121,10 +129,9 @@ public class SimpleAuthDatabase {
+ "FROM users WHERE UUID = ?";
String pass = null;
try (Connection conn = this.connect();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
// Setting statement
pstmt.setString(1,uuid);
pstmt.setString(1, uuid);
ResultSet rs = pstmt.executeQuery();
// Getting the password
@ -134,4 +141,4 @@ public class SimpleAuthDatabase {
}
return pass;
}
}
}

View File

@ -1,9 +1,11 @@
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.TranslatableText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.TypedActionResult;
import org.samo_lego.simpleauth.SimpleAuth;
/**
@ -25,15 +27,6 @@ public class AuthEventHandler {
SimpleAuth.authenticatedUsers.remove(player);
}
// Breaking block
public static boolean onBlockBroken(PlayerEntity player) {
if (!SimpleAuth.isAuthenticated((ServerPlayerEntity) player)) {
player.sendMessage(notAuthenticated);
return true;
}
return false;
}
// Using a block (right-click function)
public static ActionResult onUseBlock(PlayerEntity player) {
if(!SimpleAuth.authenticatedUsers.contains(player)) {
@ -53,13 +46,13 @@ public class AuthEventHandler {
}
// Using an item
public static ActionResult onUseItem(PlayerEntity player) {
public static TypedActionResult<ItemStack> onUseItem(PlayerEntity player) {
if(!SimpleAuth.authenticatedUsers.contains(player)) {
player.sendMessage(notAuthenticated);
return ActionResult.FAIL;
return TypedActionResult.fail(ItemStack.EMPTY);
}
return ActionResult.PASS;
return TypedActionResult.pass(ItemStack.EMPTY);
}
// Attacking an entity
public static ActionResult onAttackEntity(PlayerEntity player) {
@ -87,4 +80,4 @@ public class AuthEventHandler {
}
return ActionResult.PASS;
}
}
}

View File

@ -18,4 +18,4 @@ public interface DropItemCallback {
});
ActionResult onDropItem(PlayerEntity player);
}
}

View File

@ -1,10 +1,13 @@
package org.samo_lego.simpleauth.mixin;
import net.minecraft.container.PlayerContainer;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import org.samo_lego.simpleauth.event.item.DropItemCallback;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
@ -13,14 +16,20 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(PlayerEntity.class)
public abstract class MixinPlayerEntity {
// Thanks to PR https://github.com/FabricMC/fabric/pull/260 and 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) {
@Shadow @Final public PlayerContainer playerContainer;
// Thanks to PR https://github.com/FabricMC/fabric/pull/260 and AbusedLib https://github.com/abused/AbusedLib
@Inject(method = "dropItem(Lnet/minecraft/item/ItemStack;ZZ)Lnet/minecraft/entity/ItemEntity;", at = @At("HEAD"), cancellable = true)
private void dropItem(ItemStack stack, boolean dropAtFeet, boolean saveThrower, CallbackInfoReturnable<ItemEntity> cir) {
ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;
ActionResult result = DropItemCallback.EVENT.invoker().onDropItem(player);
if (result == ActionResult.FAIL) {
info.cancel();
// Canceling the item drop, as well as giving the items back to player
cir.cancel();
player.giveItemStack(stack);
playerContainer.sendContentUpdates();
}
}*/
}
}
}

View File

@ -23,8 +23,6 @@ public class AuthHelper {
} finally {
// Wipe confidential data
argon2.wipeArray(pass);
// Todo del line
System.out.println("Pass data wiped.");
}
return false;
}