First working version
This commit is contained in:
49
src/main/java/ru/bitheaven/donpayinteg/DonPayInteg.java
Normal file
49
src/main/java/ru/bitheaven/donpayinteg/DonPayInteg.java
Normal file
@ -0,0 +1,49 @@
|
||||
package ru.bitheaven.donpayinteg;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import ru.bitheaven.donpayinteg.command.DPI;
|
||||
import ru.bitheaven.donpayinteg.config.ConfigHandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public class DonPayInteg implements ModInitializer {
|
||||
public static final String MOD_ID = "donpayinteg";
|
||||
public static final String NAME = "DonatePayIntegration";
|
||||
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
|
||||
public static List<String> commands = new ArrayList<String>();
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
LOGGER.info("Mod [{}] is loading!", NAME);
|
||||
|
||||
ConfigHandler.register();
|
||||
|
||||
DPI.register();
|
||||
|
||||
AtomicInteger i = new AtomicInteger();
|
||||
AtomicInteger j = new AtomicInteger();
|
||||
AtomicReference<DonateThread> thread = new AtomicReference<>(new DonateThread());
|
||||
|
||||
ServerTickEvents.START_SERVER_TICK.register((world) -> {
|
||||
if(!thread.get().isAlive()) {
|
||||
if(i.getAndIncrement() % (20 * 15) == 0) {
|
||||
thread.set(new DonateThread());
|
||||
thread.get().start();
|
||||
}
|
||||
}
|
||||
|
||||
if(i.getAndIncrement() % (20 * 2) == 0) {
|
||||
if (!commands.isEmpty()) {
|
||||
world.getCommandManager().executeWithPrefix(world.getCommandSource(), commands.getFirst());
|
||||
commands.removeFirst();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
59
src/main/java/ru/bitheaven/donpayinteg/DonateThread.java
Normal file
59
src/main/java/ru/bitheaven/donpayinteg/DonateThread.java
Normal file
@ -0,0 +1,59 @@
|
||||
package ru.bitheaven.donpayinteg;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import ru.bitheaven.donpayinteg.config.Action;
|
||||
import ru.bitheaven.donpayinteg.config.Config;
|
||||
import ru.bitheaven.donpayinteg.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 static ru.bitheaven.donpayinteg.DonPayInteg.LOGGER;
|
||||
|
||||
public class DonateThread extends Thread {
|
||||
private int lastDonate = ConfigHandler.load().getLastDonate();
|
||||
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");
|
||||
|
||||
DonPayInteg.commands.addLast("title @a title \"" + msg.replace("{username}", objects.getJSONObject("vars").getString("name")) + "\"");
|
||||
DonPayInteg.commands.addLast(cmd.replace("{username}", objects.getJSONObject("vars").getString("name")));
|
||||
|
||||
LOGGER.info("Exec donate #{}", lastDonate);
|
||||
Config config = ConfigHandler.load();
|
||||
config.setLastDonate(lastDonate);
|
||||
ConfigHandler.save(config);
|
||||
}
|
||||
}
|
||||
}
|
48
src/main/java/ru/bitheaven/donpayinteg/command/DPI.java
Normal file
48
src/main/java/ru/bitheaven/donpayinteg/command/DPI.java
Normal file
@ -0,0 +1,48 @@
|
||||
package ru.bitheaven.donpayinteg.command;
|
||||
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
||||
import net.minecraft.server.command.CommandManager;
|
||||
import net.minecraft.text.Text;
|
||||
import ru.bitheaven.donpayinteg.config.Config;
|
||||
import ru.bitheaven.donpayinteg.config.ConfigHandler;
|
||||
|
||||
public class DPI {
|
||||
public DPI() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
public static void register() {
|
||||
CommandRegistrationCallback.EVENT.register((((dispatcher, registryAccess, environment) ->
|
||||
{
|
||||
dispatcher.register(CommandManager.literal("dpi")
|
||||
.then(CommandManager.literal("set_token")
|
||||
.then(CommandManager.argument("token", StringArgumentType.string())
|
||||
.executes(context -> {
|
||||
String token = StringArgumentType.getString(context, "token");
|
||||
|
||||
Config config = ConfigHandler.load();
|
||||
config.setDonpayToken(token);
|
||||
ConfigHandler.save(config);
|
||||
|
||||
context.getSource().sendFeedback(() -> Text.literal("DonatePay token set!"), false);
|
||||
|
||||
return 1;
|
||||
})))
|
||||
.then(CommandManager.literal("add")
|
||||
.then(CommandManager.argument("sum", IntegerArgumentType.integer())
|
||||
.then(CommandManager.argument("command", StringArgumentType.string())
|
||||
.then(CommandManager.argument("message", StringArgumentType.string())
|
||||
.executes(context -> {
|
||||
int sum = IntegerArgumentType.getInteger(context, "command");
|
||||
String command = StringArgumentType.getString(context, "command");
|
||||
String message = StringArgumentType.getString(context, "message");
|
||||
Config config = ConfigHandler.load();
|
||||
// config.setDonpayToken(token);
|
||||
// ConfigHandler.save(config);
|
||||
return 1;
|
||||
})))))
|
||||
);
|
||||
})));
|
||||
}
|
||||
}
|
31
src/main/java/ru/bitheaven/donpayinteg/config/Action.java
Normal file
31
src/main/java/ru/bitheaven/donpayinteg/config/Action.java
Normal file
@ -0,0 +1,31 @@
|
||||
package ru.bitheaven.donpayinteg.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;
|
||||
}
|
||||
}
|
36
src/main/java/ru/bitheaven/donpayinteg/config/Config.java
Normal file
36
src/main/java/ru/bitheaven/donpayinteg/config/Config.java
Normal file
@ -0,0 +1,36 @@
|
||||
package ru.bitheaven.donpayinteg.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,58 @@
|
||||
package ru.bitheaven.donpayinteg.config;
|
||||
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import org.yaml.snakeyaml.constructor.Constructor;
|
||||
import org.yaml.snakeyaml.inspector.TagInspector;
|
||||
import ru.bitheaven.donpayinteg.DonPayInteg;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static ru.bitheaven.donpayinteg.DonPayInteg.LOGGER;
|
||||
|
||||
|
||||
public class ConfigHandler {
|
||||
|
||||
private static final String PATH = FabricLoader.getInstance().getConfigDir().resolve(DonPayInteg.MOD_ID + ".yaml").toString();
|
||||
|
||||
public static void register() {
|
||||
if(!new File(PATH).exists())
|
||||
std();
|
||||
}
|
||||
|
||||
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() {
|
||||
save(new Config());
|
||||
LOGGER.info("Created new config file");
|
||||
}
|
||||
}
|
BIN
src/main/resources/assets/donpayinteg/icon.png
Normal file
BIN
src/main/resources/assets/donpayinteg/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 64 KiB |
24
src/main/resources/fabric.mod.json
Normal file
24
src/main/resources/fabric.mod.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"id": "donpayinteg",
|
||||
"version": "${version}",
|
||||
"name": "DonatePay Integration",
|
||||
"description": "Mod add integration with donate system",
|
||||
"authors": [
|
||||
"BitHeaven"
|
||||
],
|
||||
"contact": {},
|
||||
"license": "GPL-3.0",
|
||||
"icon": "assets/donpayinteg/icon.png",
|
||||
"environment": "server",
|
||||
"entrypoints": {
|
||||
"main": [
|
||||
"ru.bitheaven.donpayinteg.DonPayInteg"
|
||||
]
|
||||
},
|
||||
"depends": {
|
||||
"fabricloader": ">=${loader_version}",
|
||||
"fabric": "*",
|
||||
"minecraft": "${minecraft_version}"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user