This commit is contained in:
unurled 2022-11-21 10:05:00 +01:00
parent 8122ecbe70
commit 21f32349ee
29 changed files with 284 additions and 228 deletions

View file

@ -393,7 +393,7 @@ public class Items {
* @return a string
* @throws IllegalStateException stream errors
*/
private static @NotNull String itemTo64(ItemStack stack) throws IllegalStateException {
public static @NotNull String itemTo64(ItemStack stack) throws IllegalStateException {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
@ -414,7 +414,10 @@ public class Items {
* @return itemstack
* @throws IOException stream error
*/
private static ItemStack itemFrom64(String data) throws IOException {
public static ItemStack itemFrom64(String data) throws IOException {
if (data != null) {
return new ItemStack(Material.AIR);
}
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data));
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream);

View file

@ -112,7 +112,7 @@ public class Utils {
/**
* Strips all color from a string.
* @param string the string that want's to be decolored.
* @param string the string that wants to be decolored.
* @return a decolored string
*/
public static String decolor(String string) {
@ -163,6 +163,21 @@ public class Utils {
}
}
/**
* debug output to console if debug is set to true in config
* @param comps output to console
*/
public static void debug(TextComponent... comps) {
Raxen main = (Raxen) Bukkit.getPluginManager().getPlugin("Raxen");
assert main != null;
FileConfiguration config = main.getConfig();
if(config.getBoolean("debug")) {
for(TextComponent comp : comps) {
main.getLogger().info(textCompToString(comp));
}
}
}
/**
* Log the strings to the console
* @param main a main instance running
@ -366,4 +381,62 @@ public class Utils {
}
return true;
}
/**
* get a string from args which contains ""
* @param args a string list
* @return a string
*/
public static String getStringFromArg(String[] args) {
String result = "";
boolean getFColon = false;
boolean getLColon = false;
for (String arg : args) {
if (arg.contains("\"")) {
if (!getFColon) {
getFColon = true;
} else if (getFColon && !getLColon) {
result += arg.replace("\"", "");
return result;
} else if (getFColon) {
result += arg.replace("\"", "");
}
}
}
return result;
}
/**
* search if there is an args that is null or do not exist
* @param args a list of string
* @param n a number of entries a list should have
* @return a boolean if true or not
*/
public static Boolean validateArgs(String[] args, Integer n) {
for (int i = 0; i < n ; i++) {
if (args.length < n-1) {
return false;
} else if (args[i] == null) {
return false;
}
}
return true;
}
/**
* changes every "null" in list to the object null
* @param args a list of strings
* @return return a new list
*/
public static String[] setNullInArgs(String[] args) {
String[] newList = new String[args.length];
for (int i = 0 ; i < args.length ; i++) {
if (args[i].equalsIgnoreCase("null")) {
newList[i] = null;
} else {
newList[i] = args[i];
}
}
return newList;
}
}

View file

@ -1,6 +1,7 @@
package me.unurled.raxen.utils.libs;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
@ -12,8 +13,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import java.util.Objects;
import static me.unurled.raxen.utils.Utils.colorComp;
import static me.unurled.raxen.utils.Utils.log;
import static me.unurled.raxen.utils.Utils.*;
public class MongoDB {
@Getter
@ -29,7 +29,7 @@ public class MongoDB {
FileConfiguration config = Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("Raxen")).getConfig();
String uri = (String) config.get("url");
assert uri != null;
log("URI" + uri);
debug("URI " + uri);
ConnectionString connectionString = new ConnectionString(uri);
mongoClient = MongoClients.create(connectionString);
mongoDatabase = mongoClient.getDatabase("Raxen");