Skip to content

Commit

Permalink
Added mongo database
Browse files Browse the repository at this point in the history
- Made the placeholder parser more efficient.
- Added mongo database option.
  • Loading branch information
Smudgge committed Aug 8, 2023
1 parent 3bbe0a8 commit c1f2b43
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 43 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.smuddgge</groupId>
<artifactId>Leaf</artifactId>
<name>Leaf</name>
<version>3.4.0</version>
<version>3.5.0</version>
<description>A velocity utility plugin</description>
<build>
<resources>
Expand Down
11 changes: 2 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.smuddgge</groupId>
<artifactId>Leaf</artifactId>
<version>3.4.0</version>
<version>3.5.0</version>
<packaging>jar</packaging>

<name>Leaf</name>
Expand Down Expand Up @@ -69,13 +69,6 @@
<scope>test</scope>
</dependency>

<!-- Sqlite Driver -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.40.0.0</version>
</dependency>

<!-- Protocolize -->
<dependency>
<groupId>dev.simplix</groupId>
Expand All @@ -88,7 +81,7 @@
<dependency>
<groupId>com.github.smuddgge</groupId>
<artifactId>SquishyDatabase</artifactId>
<version>2.6.1</version>
<version>3.0.1</version>
</dependency>

<!-- Discord Webhooks -->
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/com/github/smuddgge/leaf/Leaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.github.smuddgge.leaf.placeholders.standard.*;
import com.github.smuddgge.squishydatabase.DatabaseCredentials;
import com.github.smuddgge.squishydatabase.DatabaseFactory;
import com.github.smuddgge.squishydatabase.console.Console;
import com.github.smuddgge.squishydatabase.interfaces.Database;
import com.google.inject.Inject;
import com.velocitypowered.api.event.Subscribe;
Expand All @@ -30,14 +31,16 @@
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.plugin.annotation.DataDirectory;
import com.velocitypowered.api.proxy.ProxyServer;
import org.checkerframework.checker.units.qual.C;

import java.io.File;
import java.nio.file.Path;
import java.util.Objects;

@Plugin(
id = "leaf",
name = "Leaf",
version = "3.4.0",
version = "3.5.0",
description = "A velocity utility plugin",
authors = {"Smudge"}
)
Expand Down Expand Up @@ -215,11 +218,32 @@ public static void setupDatabase(File folder) {
return;
}

DatabaseFactory databaseFactory = DatabaseFactory.SQLITE;
try {
String type = ConfigDatabase.get().getString("type", "SQLITE");

Leaf.database = databaseFactory.create(new DatabaseCredentials(
folder.getAbsolutePath() + File.separator + "database.sqlite3"
));
// Check if it's an SQLITE database.
if (Objects.equals(type, "SQLITE")) {
DatabaseFactory databaseFactory = DatabaseFactory.SQLITE;
Leaf.database = databaseFactory.create(
DatabaseCredentials.SQLITE(folder.getAbsolutePath() + File.separator + "database.sqlite3")
);
}

// Check if it's a MONGO database.
if (Objects.equals(type, "MONGO")) {
DatabaseFactory databaseFactory = DatabaseFactory.MONGO;
Leaf.database = databaseFactory.create(
DatabaseCredentials.MONGO(
ConfigDatabase.get().getString("connection_string", "none"),
ConfigDatabase.get().getString("database_name", "Database")
)
);
}

} catch (Exception exception) {
exception.printStackTrace();
Console.warn("Connection String : " + ConfigDatabase.get().getString("connection_string"));
}

if (ConfigDatabase.isDebugMode()) {
Leaf.database.setDebugMode(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public CommandStatus onConsoleRun(ConfigurationSection section, String[] argumen

@Override
public CommandStatus onPlayerRun(ConfigurationSection section, String[] arguments, User user) {
// Get message as list.
// If the message is not a list it will return an empty list.
// Get a message as a list.
// If the message is not a list, it will return an empty list.
List<String> listMessage = section.getListString("message", new ArrayList<>());

// The final message to send.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ protected void load() {

this.resetActions();

// Loop though every type of item.
// This will not loop though every slot.
for (InventoryItem inventoryItem : this.getInventoryItems()) {

// Get the instance of the item stack.
ItemStack item = inventoryItem.getItemStack();

// Check if the inventory item contains a function.
Expand Down Expand Up @@ -110,6 +114,7 @@ public ConfigurationSection getInventorySection() {

/**
* Used to get the list of inventory items configured in this inventory.
* The amount of items does not represent the amount of slots.
*
* @return The requested list of items.
*/
Expand Down Expand Up @@ -162,10 +167,6 @@ protected ItemStack onNextPage(InventoryItem inventoryItem, int recordsSize) {

if (recordsSize - 1 < recordIndex) {
if (!inventoryItem.getFunctionSection().getBoolean("always_show", false)) {
for (Integer slot : inventoryItem.getSlots(this.getInventoryType())) {
this.addAction(slot, () -> {
});
}
return null;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public InventoryInterface(User user) {
*/
public InventoryInterface open() {

// Check if inventory interfance is disabled.
// Check if inventory interface is disabled.
if (!ProtocolizeDependency.isInventoryEnabled()) {
MessageManager.warn("Tried to use inventorys when the dependency is not enabled.");
MessageManager.log("&7" + ProtocolizeDependency.getDependencyMessage());
Expand All @@ -47,6 +47,7 @@ public InventoryInterface open() {

this.inventory = new Inventory(this.getInventoryType());
this.inventory.title(MessageManager.convertToLegacy(this.getTitle()));

this.load();

ProtocolizePlayer player = Protocolize.playerProvider().player(this.user.getUniqueId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.simplix.protocolize.api.item.ItemStack;
import dev.simplix.protocolize.data.ItemType;
import dev.simplix.protocolize.data.inventory.InventoryType;
import net.kyori.adventure.text.Component;
import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.ListTag;

Expand Down Expand Up @@ -155,9 +156,16 @@ public ItemStack getItemStack(ItemStack item) {

// Set the display name.
String name = this.section.getString("name", "&7");
item.displayName(MessageManager.convert(MessageManager.convertToLegacy(
this.parsePlaceholders(PlaceholderManager.parse(name, null, this.user))
)));

String x = PlaceholderManager.parse(name, null, this.user);

String a = this.parsePlaceholders(x);

String b = MessageManager.convertToLegacy(a);

Component c = MessageManager.convert(b);

item.displayName(c);

// Set the lore.
for (String line : this.section.getListString("lore", new ArrayList<>())) {
Expand Down Expand Up @@ -214,7 +222,6 @@ public ItemStack getItemStack(ItemStack item) {

// Add the nbt data.
item.nbtData(compoundTag);

return item;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,37 +91,42 @@ public ItemStack onLoadItemWithFunction(InventoryItem inventoryItem) {
* @return The updated item stack.
*/
private ItemStack onLoadPlayer(InventoryItem inventoryItem) {

// Just in case there is multiple inventory items with the same function name.
Map<Integer, String> mockInventory = this.getInventoryOf("player");

int friendsPerPage = this.getInventoryOf("player").size();
// Calculate how many friends per page and the record index based on this page.
int friendsPerPage = mockInventory.size();
int recordIndex = (friendsPerPage * this.page) - friendsPerPage;

for (Integer slot : mockInventory.keySet()) {

// Check if the record exists.
if (this.friendRecords.size() - 1 < recordIndex) {
if ((this.friendRecords.size() - 1) < recordIndex) {
ItemStack item = this.appendNoPlayerItemStack(inventoryItem);
this.inventory.item(slot, item);

} else {

// Get friend record.
FriendRecord record = this.friendRecords.get(recordIndex);

// Get tables.
FriendMailTable friendMailTable = Leaf.getDatabase().getTable(FriendMailTable.class);
PlayerTable playerTable = Leaf.getDatabase().getTable(PlayerTable.class);

// Get records.
// Get friend player record.
PlayerRecord friendPlayerRecord = playerTable.getFirstRecord(new Query().match("uuid", record.friendPlayerUuid));
assert friendPlayerRecord != null;
if (friendPlayerRecord == null) continue;

Optional<Player> optionalPlayer = Leaf.getServer().getPlayer(friendPlayerRecord.name);
User friendUser = null;
if (optionalPlayer.isPresent()) {
friendUser = new User(optionalPlayer.get());
}

// Set user to the friend.
inventoryItem.setUser(friendUser);
// Set user if present.
optionalPlayer.ifPresent(player -> {
inventoryItem.setUser(new User(player));
});

ItemStack item = this.appendPlayerItemStack(inventoryItem);

this.inventory.item(slot, this.parseCustomPlaceholders(item, record));
}

Expand Down Expand Up @@ -151,7 +156,8 @@ private ItemStack appendPlayerItemStack(InventoryItem inventoryItem) {
*/
private ItemStack appendNoPlayerItemStack(InventoryItem inventoryItem) {
if (!this.section.getKeys().contains("no_player")) return inventoryItem.getItemStack();
return inventoryItem.append(this.section.getSection("no_player")).getItemStack();
inventoryItem.append(this.section.getSection("no_player"));
return inventoryItem.getItemStack();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public FriendRequestInventory(ConfigurationSection section, User user) {

// Load all the friend records.
this.requestRecords = Leaf.getDatabase().getTable(FriendRequestTable.class).getRecordList(
new Query().match("playerToUuid", this.user.getUniqueId())
new Query().match("playerToUuid", this.user.getUniqueId().toString())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ public static String parse(String message, PlaceholderType filterType, User user
// If we are filtering and the placeholder is not the filter type, skip.
if (filterType != null && placeholder.getType() != filterType) continue;

String replacer = placeholder.getString();
// Check if the message contains the placeholder.
if (!message.contains(placeholder.getString())) continue;

String replacer = placeholder.getString();
String value = placeholder.getValue(user);

if (value == null) value = "null";

if (placeholder.getType() == PlaceholderType.CUSTOM) {
Expand Down
11 changes: 9 additions & 2 deletions src/main/resources/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
#
# Author : Smudge

# Changing these values requires a proxy restart.
# --- Changing these values requires a proxy restart. ---
enabled: true
debugmode: false

# SQLITE DATABASE
type: SQLITE

# # MONGO DATABASE
# connection_string: "connectionString"
# database_same: "Database"

# The maximum amount of history that will
# be saved in the database per player.
player_history_limit: 20

# The maximum amount of messages that will
# The maximum number of messages that will
# be saved in the database per player.
message_limit: 100

0 comments on commit c1f2b43

Please sign in to comment.