From 51b01ea63309b2efe79c7952c9280921a78d5293 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Mon, 4 May 2020 18:36:18 +0200 Subject: [PATCH] Added set and get boolean and added OP requirement for commands. --- .../command/CommandCustomPlayerdata.java | 5 ++ .../command/CommandDatabase.java | 47 +++++++++---------- .../command/EnumDatabaseActions.java | 1 + .../simpledatabasemanager/core/Database.java | 9 ++++ .../core/DatabaseReadOnly.java | 10 ++++ 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/main/java/fr/nathanael2611/simpledatabasemanager/command/CommandCustomPlayerdata.java b/src/main/java/fr/nathanael2611/simpledatabasemanager/command/CommandCustomPlayerdata.java index ae0fc21..71313b3 100644 --- a/src/main/java/fr/nathanael2611/simpledatabasemanager/command/CommandCustomPlayerdata.java +++ b/src/main/java/fr/nathanael2611/simpledatabasemanager/command/CommandCustomPlayerdata.java @@ -27,6 +27,11 @@ public String getUsage(ICommandSender sender) return "HAHA"; } + @Override + public int getRequiredPermissionLevel() { + return 4; + } + @Override public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { diff --git a/src/main/java/fr/nathanael2611/simpledatabasemanager/command/CommandDatabase.java b/src/main/java/fr/nathanael2611/simpledatabasemanager/command/CommandDatabase.java index 08ad467..4f39cd8 100644 --- a/src/main/java/fr/nathanael2611/simpledatabasemanager/command/CommandDatabase.java +++ b/src/main/java/fr/nathanael2611/simpledatabasemanager/command/CommandDatabase.java @@ -1,47 +1,46 @@ package fr.nathanael2611.simpledatabasemanager.command; +import fr.nathanael2611.simpledatabasemanager.core.Database; +import fr.nathanael2611.simpledatabasemanager.core.Databases; +import fr.nathanael2611.simpledatabasemanager.core.StoredData; import net.minecraft.command.*; import net.minecraft.server.MinecraftServer; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.TextComponentString; import org.apache.commons.lang3.ArrayUtils; -import fr.nathanael2611.simpledatabasemanager.core.Database; -import fr.nathanael2611.simpledatabasemanager.core.Databases; -import fr.nathanael2611.simpledatabasemanager.core.StoredData; import javax.annotation.Nullable; import java.util.List; -public class CommandDatabase extends CommandBase -{ +public class CommandDatabase extends CommandBase { @Override - public String getName() - { + public String getName() { return "database"; } @Override - public String getUsage(ICommandSender sender) - { + public String getUsage(ICommandSender sender) { return "HAHA"; } @Override - public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException - { + public int getRequiredPermissionLevel() { + return 4; + } + + @Override + public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException { String correctUsage = "/database []"; - if(args.length > 2) - { + if (args.length > 2) { Database db = Databases.getDatabase(args[0]); - if(args[1].equalsIgnoreCase("get")) - { - if(db.contains(args[2])) sender.sendMessage(new TextComponentString(String.format("§aValue '%s' in database '%s' is '%s'", args[2], args[0], db.get(args[2]).asUnknownObject()))); - else throw new CommandException(String.format("No value named '%s' in database '%s'", args[2], args[0])); - } else if(args[1].startsWith("set")) - { + if (args[1].equalsIgnoreCase("get")) { + if (db.contains(args[2])) + sender.sendMessage(new TextComponentString(String.format("§aValue '%s' in database '%s' is '%s'", args[2], args[0], db.get(args[2]).asUnknownObject()))); + else + throw new CommandException(String.format("No value named '%s' in database '%s'", args[2], args[0])); + } else if (args[1].startsWith("set")) { String valueType = args[1].substring(3); - if(args.length > 3) - { + if (args.length > 3) { Object willBeInsert; try { switch (valueType.toLowerCase()) { @@ -60,8 +59,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args default: throw new WrongUsageException(correctUsage); } - } catch (NumberFormatException ex) - { + } catch (NumberFormatException ex) { throw new NumberInvalidException("commands.generic.num.invalid", args[3]); } db.set(args[2], new StoredData(willBeInsert)); @@ -70,8 +68,7 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args String.format("§a%s '%s' was successful set to '%s' in database '%s'", s1 + valueType.toLowerCase().toLowerCase().substring(1), args[2], args[3], args[0]) )); } else throw new WrongUsageException(correctUsage); - } else if(args[1].equalsIgnoreCase("remove")) - { + } else if (args[1].equalsIgnoreCase("remove")) { db.remove(args[2]); sender.sendMessage(new TextComponentString(String.format("§aValue '%s' was successful removed from database '%s'", args[2], args[0]))); } diff --git a/src/main/java/fr/nathanael2611/simpledatabasemanager/command/EnumDatabaseActions.java b/src/main/java/fr/nathanael2611/simpledatabasemanager/command/EnumDatabaseActions.java index 8ef8a6f..04049f3 100644 --- a/src/main/java/fr/nathanael2611/simpledatabasemanager/command/EnumDatabaseActions.java +++ b/src/main/java/fr/nathanael2611/simpledatabasemanager/command/EnumDatabaseActions.java @@ -9,6 +9,7 @@ public enum EnumDatabaseActions { SET_INTEGER("setInteger"), SET_DOUBLE("setDouble"), SET_FLOAT("setFloat"), + SET_BOOLEAN("setBoolean"), GET("get"), REMOVE("remove"); diff --git a/src/main/java/fr/nathanael2611/simpledatabasemanager/core/Database.java b/src/main/java/fr/nathanael2611/simpledatabasemanager/core/Database.java index f2d946f..389e1e1 100644 --- a/src/main/java/fr/nathanael2611/simpledatabasemanager/core/Database.java +++ b/src/main/java/fr/nathanael2611/simpledatabasemanager/core/Database.java @@ -79,6 +79,15 @@ public void setInteger(String key, int value) set(key, new StoredData(value)); } + /** + * Set a boolean-entry in the database + */ + public void setBoolean(String key, boolean value) + { + Integer integer = value ? 1 : 0; + set(key, new StoredData(integer)); + } + /** * Set a double-entry in the database */ diff --git a/src/main/java/fr/nathanael2611/simpledatabasemanager/core/DatabaseReadOnly.java b/src/main/java/fr/nathanael2611/simpledatabasemanager/core/DatabaseReadOnly.java index 7570f35..e0f33f4 100644 --- a/src/main/java/fr/nathanael2611/simpledatabasemanager/core/DatabaseReadOnly.java +++ b/src/main/java/fr/nathanael2611/simpledatabasemanager/core/DatabaseReadOnly.java @@ -161,6 +161,16 @@ public int getInteger(String key) return this.get(key).asInteger(); } + /** + * Used for directly get a stored-data as Boolean + * @param key the researched key + * @return the stored-data as Boolean + */ + public boolean getBoolean(String key) + { + return this.get(key).asInteger() == 1; + } + /** * Used for directly get a stored-data as Double * @param key the researched key