mirror of
https://github.com/funkemunky/AntiVPN.git
synced 2026-05-31 09:31:54 +00:00
Starting to add custom messages to AntiVPN
This commit is contained in:
@@ -8,6 +8,7 @@ import dev.brighten.antivpn.command.impl.AntiVPNCommand;
|
||||
import dev.brighten.antivpn.command.impl.LookupCommand;
|
||||
import dev.brighten.antivpn.database.VPNDatabase;
|
||||
import dev.brighten.antivpn.database.sql.MySqlVPN;
|
||||
import dev.brighten.antivpn.message.MessageHandler;
|
||||
import dev.brighten.antivpn.utils.VPNResponse;
|
||||
import dev.brighten.antivpn.utils.json.JSONException;
|
||||
import dev.brighten.antivpn.utils.json.JSONObject;
|
||||
@@ -29,6 +30,7 @@ public class AntiVPN {
|
||||
private VPNExecutor executor;
|
||||
private PlayerExecutor playerExecutor;
|
||||
private VPNDatabase database;
|
||||
private MessageHandler messageHandler;
|
||||
private List<Command> commands = new ArrayList<>();
|
||||
public int detections, checked;
|
||||
|
||||
@@ -44,6 +46,8 @@ public class AntiVPN {
|
||||
INSTANCE.executor.registerListeners();
|
||||
INSTANCE.config.update();
|
||||
|
||||
INSTANCE.messageHandler = new MessageHandler();
|
||||
|
||||
switch(INSTANCE.config.getDatabaseType().toLowerCase()) {
|
||||
case "mysql":
|
||||
case "sql":{
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package dev.brighten.antivpn.command.impl;
|
||||
|
||||
import dev.brighten.antivpn.AntiVPN;
|
||||
import dev.brighten.antivpn.api.APIPlayer;
|
||||
import dev.brighten.antivpn.command.Command;
|
||||
import dev.brighten.antivpn.command.CommandExecutor;
|
||||
import dev.brighten.antivpn.message.VpnString;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -47,17 +49,15 @@ public class AlertsCommand extends Command {
|
||||
@Override
|
||||
public String execute(CommandExecutor executor, String[] args) {
|
||||
Optional<APIPlayer> pgetter = executor.getPlayer();
|
||||
if(!pgetter.isPresent()) return "&cYou must be a player to execute this command!";
|
||||
if(!pgetter.isPresent()) return AntiVPN.getInstance().getMessageHandler()
|
||||
.getString("command-misc-playerRequired").getMessage();
|
||||
|
||||
APIPlayer player = pgetter.get();
|
||||
|
||||
if(player.isAlertsEnabled()) {
|
||||
player.setAlertsEnabled(false);
|
||||
return "&7You have set your AntiVPN alerts to: &coff";
|
||||
} else {
|
||||
player.setAlertsEnabled(true);
|
||||
return "&7You have set your AntiVPN alerts to: &aon";
|
||||
}
|
||||
player.setAlertsEnabled(!player.isAlertsEnabled());
|
||||
|
||||
return AntiVPN.getInstance().getMessageHandler().getString("command-alerts-toggled")
|
||||
.getFormattedMessage(new VpnString.Var<>("state", player.isAlertsEnabled()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package dev.brighten.antivpn.message;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class MessageHandler {
|
||||
private final Map<String, VpnString> messages = new HashMap<>();
|
||||
|
||||
public VpnString getString(String key) {
|
||||
if(!messages.containsKey(key)) {
|
||||
throw new NullPointerException("There is no VpnString with the key \"" + key + "\"");
|
||||
}
|
||||
|
||||
return messages.get(key);
|
||||
}
|
||||
|
||||
public void reloadStrings() {
|
||||
for (VpnString value : messages.values()) {
|
||||
value.updateString();
|
||||
}
|
||||
}
|
||||
|
||||
public void clearStrings() {
|
||||
messages.clear();
|
||||
}
|
||||
|
||||
public void addString(VpnString string, Function<VpnString, String> getter) {
|
||||
string.setConfigStringGetter(getter);
|
||||
messages.put(string.getKey(), string);
|
||||
}
|
||||
|
||||
public void initStrings(Function<VpnString, String> getter) {
|
||||
addString(new VpnString("command-misc-playerRequired",
|
||||
"&cYou must be a player to execute this command!"), getter);
|
||||
addString(new VpnString("command-alerts-toggled",
|
||||
"&7Your player proxy notifications have been set to: &e%state%"), getter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package dev.brighten.antivpn.message;
|
||||
|
||||
import dev.brighten.antivpn.api.APIPlayer;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Getter
|
||||
public class VpnString {
|
||||
private final String key;
|
||||
private final String defaultMessage;
|
||||
private String message;
|
||||
@Setter
|
||||
private Function<VpnString, String> configStringGetter;
|
||||
|
||||
public VpnString(String key, String defaultMessage) {
|
||||
this.key = key;
|
||||
this.defaultMessage = defaultMessage;
|
||||
this.message = defaultMessage;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
public void updateString() {
|
||||
if(configStringGetter == null) throw new Exception("The configStringGetter for string " + key + " is null!");
|
||||
|
||||
message = configStringGetter.apply(this);
|
||||
}
|
||||
|
||||
public String getFormattedMessage(Var<String, Object>... replacements) {
|
||||
String formatted = message;
|
||||
|
||||
for (Var<String, Object> replacement : replacements) {
|
||||
formatted = formatted
|
||||
.replace("%" + replacement.getKey() + "%", replacement.getReplacement().toString());
|
||||
}
|
||||
|
||||
return formatted;
|
||||
}
|
||||
|
||||
public void sendMessage(APIPlayer player, Var<String, Object>... replacements) {
|
||||
String formatted = message;
|
||||
|
||||
for (Var<String, Object> replacement : replacements) {
|
||||
formatted = formatted
|
||||
.replace("%" + replacement.getKey() + "%", replacement.getReplacement().toString());
|
||||
}
|
||||
player.sendMessage(formatted);
|
||||
}
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public static class Var<S, O> {
|
||||
private final String key;
|
||||
private final Object replacement;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user