Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added set and get boolean and added OP requirement for commands. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <database> <set[String/Integer/Double/Float]/get/remove> <key> [<value>]";
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()) {
Expand All @@ -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));
Expand All @@ -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])));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum EnumDatabaseActions {
SET_INTEGER("setInteger"),
SET_DOUBLE("setDouble"),
SET_FLOAT("setFloat"),
SET_BOOLEAN("setBoolean"),
GET("get"),
REMOVE("remove");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down