Skip to content

Commit

Permalink
2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixf1nity committed Apr 23, 2022
1 parent 10d808b commit 6b4906f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 62 deletions.
22 changes: 13 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>xyz.damt</groupId>
<groupId>xyz.refinedev</groupId>
<artifactId>RefineCoins</artifactId>
<version>1.0-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -58,36 +58,40 @@
</repository>
<repository>
<id>placeholderapi</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.7</version>
<version>3.12.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.9.2</version>
<version>2.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.jonahseguin</groupId>
<groupId>com.jonahseguin</groupId>
<artifactId>drink</artifactId>
<version>8a692f7</version>
<version>1.0.3</version> <!-- At time of writing, 1.0.3 is latest version. See the pom.xml for the latest version -->
<scope>compile</scope> <!-- Change scope to 'provided' if you are running drink as a plugin rather than shading it -->
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/xyz/refinedev/coins/RefineCoins.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.jonahseguin.drink.CommandService;
import com.jonahseguin.drink.Drink;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.refinedev.coins.commands.BalanceCommand;
import xyz.refinedev.coins.commands.CoinsCommand;
import xyz.refinedev.coins.commands.PayCommand;
import xyz.refinedev.coins.commands.provider.OfflinePlayerCommandProvider;
import xyz.refinedev.coins.mongo.MongoHandler;
import xyz.refinedev.coins.placeholder.CoinsPlaceholderExtension;
import xyz.refinedev.coins.profile.ProfileHandler;
import xyz.refinedev.coins.profile.ProfileStorage;
Expand All @@ -27,6 +29,7 @@ public class RefineCoins extends JavaPlugin {

private BasicConfigurationFile profilesYML;
private BasicConfigurationFile messagesYML;
private BasicConfigurationFile configYML;

/**
* Loading logic of the plugin
Expand All @@ -36,7 +39,7 @@ public class RefineCoins extends JavaPlugin {
public void onLoad() {
instance = this;

this.saveDefaultConfig();
this.configYML = new BasicConfigurationFile(this, "config");

This comment has been minimized.

Copy link
@kayalust

kayalust Apr 23, 2022

Contributor

This is unnecessary, you should just use saveDefaultConfig instead.

this.profilesYML = new BasicConfigurationFile(this, "profiles");
this.messagesYML = new BasicConfigurationFile(this, "messages");
}
Expand All @@ -55,7 +58,7 @@ public void onEnable() {
profileStorage = new MongoProfileStorage(this);
}

if (getConfig().getBoolean("SETTINGS.PLACEHOLDERAPI")) {
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
new CoinsPlaceholderExtension(this).register();
}

Expand Down
112 changes: 69 additions & 43 deletions src/main/java/xyz/refinedev/coins/mongo/MongoHandler.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,92 @@
package xyz.refinedev.coins.mongo;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import lombok.Getter;
import org.bson.Document;
import org.bukkit.configuration.file.FileConfiguration;
import xyz.refinedev.coins.RefineCoins;
import xyz.refinedev.coins.utils.config.BasicConfigurationFile;

import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* This Project is property of Refine Development © 2021
* Redistribution of this Project is not allowed
*
* @author Drizzy
* Created: 9/12/2021
* Project: Array
*/

@Getter
public class MongoHandler {

private final RefineCoins refineCoins;
private final RefineCoins plugin;
private final BasicConfigurationFile config;

private MongoClient client;
private MongoDatabase database;

private MongoDatabase mongoDatabase;
private MongoCollection<Document> profiles;

/**
* Constructs a new instance of {@link MongoHandler}
*
* @param refineCoins instance of {@link RefineCoins}
*/
public MongoHandler(RefineCoins plugin, BasicConfigurationFile config) {
this.plugin = plugin;
this.config = config;
init();
}

public void init() {
this.disableLogging();

if (config.getBoolean("MONGO.URI-MODE")) {
this.client = MongoClients.create(config.getString("MONGO.URI.CONNECTION_STRING"));
this.database = client.getDatabase(config.getString("MONGO.URI.DATABASE"));

plugin.getLogger().info("&7Initialized MongoDB successfully!");
return;
}

boolean auth = config.getBoolean("MONGO.NORMAL.AUTHENTICATION.ENABLED");
String host = config.getString("MONGO.NORMAL.HOST");
int port = config.getInteger("MONGO.NORMAL.PORT");

public MongoHandler(RefineCoins refineCoins) {
this.refineCoins = refineCoins;
this.connect();
String uri = "mongodb://" + host + ":" + port;

if (auth) {
String username = config.getString("MONGO.NORMAL.AUTHENTICATION.USERNAME");
String password = config.getString("MONGO.NORMAL.AUTHENTICATION.PASSWORD");
uri = "mongodb://" + username + ":" + password + "@" + host + ":" + port;
}


this.client = MongoClients.create(uri);
this.database = client.getDatabase(config.getString("MONGO.URI.DATABASE"));

this.profiles = this.database.getCollection("profiles");

plugin.getLogger().info("&7Initialized MongoDB successfully!");
}

/**
* Connect logic of the mongo database
*/

private void connect() {
FileConfiguration mainConfig = refineCoins.getConfig();

if (mainConfig.getBoolean("STORAGE.MONGO.URI-MODE")) {
MongoClient client = new MongoClient(new MongoClientURI(mainConfig.getString("STORAGE.MONGO.URI.CONNECTION_STRING")));
this.mongoDatabase = client.getDatabase(mainConfig.getString("STORAGE.MONGO.URI.DATABASE"));
} else {
MongoClient client;
if (mainConfig.getBoolean("STORAGE.MONGO.NORMAL.AUTHENTICATION.ENABLED")) {
MongoCredential credential = MongoCredential.createCredential(
mainConfig.getString("STORSTORAGE.MONGO.NORMAL.AUTHENTICATION.USERNAME"),
mainConfig.getString("STORAGE.MONGO.NORMAL.DATABASE"),
mainConfig.getString("STORAGE.MONGO.NORMAL.AUTHENTICATION.PASSWORD").toCharArray()
);

client = new MongoClient(new ServerAddress(mainConfig.getString("STORAGE.MONGO.NORMAL.HOST"),
mainConfig.getInt("STORAGE.MONGO.NORMAL.PORT")), Collections.singletonList(credential));
} else {
client = new MongoClient(mainConfig.getString("STORAGE.MONGO.NORMAL.HOST"),
mainConfig.getInt("STORAGE.MONGO.NORMAL.PORT"));
}
this.mongoDatabase = client.getDatabase(mainConfig.getString("STORAGE.MONGO.NORMAL.DATABASE"));
public void shutdown() {
plugin.getLogger().info("&7Disconnecting &cMongo&7...");
try {
Thread.sleep(50L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

profiles = mongoDatabase.getCollection("profiles");
if (this.client != null) this.client.close();
plugin.getLogger().info("&7Disconnected &cMongo &7Successfully!");
}

public void disableLogging() {
Logger mongoLogger = Logger.getLogger( "com.mongodb" );
mongoLogger.setLevel(Level.SEVERE);

Logger legacyLogger = Logger.getLogger( "org.mongodb" );
legacyLogger.setLevel(Level.SEVERE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public CoinsPlaceholderExtension(RefineCoins refineCoins) {

@Override
public String getIdentifier() {
return "coins";
return "RefineCoins";
}

/**
Expand All @@ -40,7 +40,7 @@ public String getIdentifier() {

@Override
public String getAuthor() {
return "refine";
return "Refine Development";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mongodb.client.model.Filters;
import com.mongodb.client.model.ReplaceOptions;
import lombok.Getter;
import org.bson.Document;
import xyz.refinedev.coins.RefineCoins;
import xyz.refinedev.coins.mongo.MongoHandler;
Expand All @@ -11,6 +12,7 @@
public class MongoProfileStorage implements ProfileStorage {

private final RefineCoins refineCoins;
@Getter
private final MongoHandler mongoHandler;

/**
Expand All @@ -21,7 +23,7 @@ public class MongoProfileStorage implements ProfileStorage {

public MongoProfileStorage(RefineCoins refineCoins) {
this.refineCoins = refineCoins;
this.mongoHandler = new MongoHandler(refineCoins);
this.mongoHandler = new MongoHandler(refineCoins, refineCoins.getConfigYML());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public BasicConfigurationFile(JavaPlugin plugin, String name, boolean overwrite)
super(plugin, name);

this.file = new File(plugin.getDataFolder(), name + ".yml");
plugin.saveResource(name + ".yml", overwrite);

if (!file.exists()) {
plugin.saveResource(name + ".yml", overwrite);
}
this.configuration = YamlConfiguration.loadConfiguration(this.file);
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
############################################
# Coins made by #
# damt & refine #
# RefineCoins made by #
# damt @ refine #
############################################

SETTINGS:
ENABLE-PAY: true
PLACEHOLDERAPI: true

STORAGE:
MONGO-STORAGE: false
Expand Down

1 comment on commit 6b4906f

@Spoony1337
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, check kaya's comment

Please sign in to comment.