First working version

This commit is contained in:
2024-09-29 23:42:26 +05:00
commit d4ddc609b3
14 changed files with 765 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package ru.bitheaven.donpayintegplug;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import ru.bitheaven.donpayintegplug.config.ConfigHandler;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;
public final class DonPayIntegPlug extends JavaPlugin {
public static List<String> commands = new ArrayList<>();
@Override
public void onEnable() {
ConfigHandler.register(this.getLogger(), this.getDataFolder().toString());
BukkitScheduler scheduler = this.getServer().getScheduler();
AtomicReference<DonateThread> thread = new AtomicReference<>();
scheduler.runTaskTimer(this, () -> {
thread.set(new DonateThread(this.getLogger()));
thread.get().start();
}, 1, 20 * 15);
scheduler.runTaskTimer(this, () -> {
if (!commands.isEmpty()) {
this.getServer().dispatchCommand(Bukkit.getConsoleSender(), commands.getFirst());
commands.removeFirst();
}
}, 1, 20 * 2);
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
}

View File

@ -0,0 +1,65 @@
package ru.bitheaven.donpayintegplug;
import org.json.JSONArray;
import org.json.JSONObject;
import ru.bitheaven.donpayintegplug.config.Action;
import ru.bitheaven.donpayintegplug.config.Config;
import ru.bitheaven.donpayintegplug.config.ConfigHandler;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.logging.Logger;
public class DonateThread extends Thread {
private int lastDonate = ConfigHandler.load().getLastDonate();
private final Logger logger;
public DonateThread(Logger logger) {
this.logger = logger;
}
public void run() {
String token = ConfigHandler.load().getDonpayToken();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://donatepay.ru/api/v1/notifications?access_token=" + token + "&type=donation&order=ASC&after=" + lastDonate))
.GET()
.build();
HttpResponse<String> response;
try {
response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
JSONArray array = new JSONObject(response.body()).getJSONArray("data");
for(int i = 0; i < array.length(); i++) {
JSONObject objects = array.getJSONObject(i);
boolean finded = false;
String cmd = "", msg = "";
for(Action element : ConfigHandler.load().getActions()) {
if(element.getSum() == objects.getJSONObject("vars").getFloat("sum")) {
cmd = element.getCommand();
msg = element.getMessage();
finded = true;
break;
}
}
if(!finded) continue;
lastDonate = objects.getInt("id");
DonPayIntegPlug.commands.addLast("title @a title \"" + msg.replace("{username}", objects.getJSONObject("vars").getString("name")) + "\"");
DonPayIntegPlug.commands.addLast(cmd.replace("{username}", objects.getJSONObject("vars").getString("name")));
logger.info(String.format("Exec donate #%d", lastDonate));
Config config = ConfigHandler.load();
config.setLastDonate(lastDonate);
ConfigHandler.save(config);
}
}
}

View File

@ -0,0 +1,31 @@
package ru.bitheaven.donpayintegplug.config;
public class Action {
private int sum = 1;
private String message = "{username} donated";
private String command = "give @a dirt";
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
}

View File

@ -0,0 +1,34 @@
package ru.bitheaven.donpayintegplug.config;
import java.util.List;
public class Config {
private String donpayToken = "<YOUR_TOKEN>";
private int lastDonate = 0;
private List<Action> actions = List.of(new Action());
public String getDonpayToken() {
return donpayToken;
}
public void setDonpayToken(String donpayToken) {
this.donpayToken = donpayToken;
}
public int getLastDonate() {
return lastDonate;
}
public void setLastDonate(int lastDonate) {
this.lastDonate = lastDonate;
}
public List<Action> getActions() {
return actions;
}
public void setActions(List<Action> actions) {
this.actions = actions;
}
}

View File

@ -0,0 +1,55 @@
package ru.bitheaven.donpayintegplug.config;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.*;
import java.util.logging.Logger;
public class ConfigHandler {
private static String PATH;
public static void register(Logger logger, String path) {
new File(path).mkdir();
PATH = path + "/config.yaml";
if(!new File(PATH).exists())
std(logger);
}
public static Config load() {
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setTagInspector(tag -> tag.getClassName().equals(Config.class.getName()));
Yaml yaml = new Yaml(new Constructor(Config.class, loaderOptions));
Config config;
try {
config = yaml.load(new FileInputStream(PATH));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
return config;
}
public static void save(Config config) {
DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setPrettyFlow(true);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
try {
yaml.dump(config, new FileWriter(PATH));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void std(Logger logger) {
save(new Config());
logger.info("Created new config file");
}
}

View File

@ -0,0 +1,7 @@
name: DonatePay-Integration
version: '1.0.0'
main: ru.bitheaven.donpayintegplug.DonPayIntegPlug
api-version: '1.21'
authors: [ BitHeaven ]
description: Mod add integration with donate system
website: http://bitheaven.ru