Skip to content

Commit

Permalink
0.9.4
Browse files Browse the repository at this point in the history
  • Loading branch information
MCUmbrella committed Jul 6, 2022
1 parent 89425b7 commit 24a3374
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 49 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>vip.floatationdevice</groupId>
<artifactId>mgbridge</artifactId>
<version>0.9.3</version>
<version>0.9.4</version>
<packaging>jar</packaging>

<name>MGBridge</name>
Expand Down Expand Up @@ -74,7 +74,7 @@
<dependency>
<groupId>vip.floatationdevice</groupId>
<artifactId>guilded4j</artifactId>
<version>0.9.4</version>
<version>0.9.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.HashMap;
import java.util.UUID;

import static vip.floatationdevice.mgbridge.I18nUtil.translate;
import static vip.floatationdevice.mgbridge.MGBridge.instance;
import static vip.floatationdevice.mgbridge.MGBridge.log;
import static vip.floatationdevice.mgbridge.I18nUtil.translate;

public class BindManager
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,43 @@
import static vip.floatationdevice.mgbridge.ConfigManager.proxy;
import static vip.floatationdevice.mgbridge.ConfigManager.token;
import static vip.floatationdevice.mgbridge.I18nUtil.translate;
import static vip.floatationdevice.mgbridge.MGBridge.*;
import static vip.floatationdevice.mgbridge.MGBridge.instance;
import static vip.floatationdevice.mgbridge.MGBridge.log;

public class BukkitCommandExecutor implements CommandExecutor
{
public static final Random r = new Random(); // used to generate random bind code
static final Random r = new Random(); // used to generate random bind code

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
{
if(args.length == 1 && args[0].equals("reload") && sender.hasPermission("mgbridge.reload"))
{
try
{
instance.gEventListener.disconnect();
try {instance.gEventListener.disconnect();} catch(IllegalArgumentException ignored) {}
if(ConfigManager.loadConfig())
{
instance.g4JClient = new G4JClient(token);
instance.g4JClient.setProxy(proxy);
instance.gEventListener.connect();
log.info("MGBridge reloaded");
if(sender instanceof Player) sender.sendMessage("[§eMGBridge§f] §aMGBridge reloaded");
}
else
{
log.severe("MGBridge reloaded with errors");
if(sender instanceof Player) sender.sendMessage("[§eMGBridge§f] §cMGBridge reloaded with errors");
}
return true;
}
catch(Throwable e)
{
log.severe("Failed to reload MGBridge");
e.printStackTrace();
if(sender instanceof Player) sender.sendMessage("[§eMGBridge§f] §cFailed to reload MGBridge");
return false;
}
catch(IllegalArgumentException ignored){}
ConfigManager.loadConfig();
instance.g4JClient = new G4JClient(token);
instance.g4JClient.getChatMessageManager().setProxy(proxy);
instance.gEventListener.connect();
sender.sendMessage("MGBridge reloaded");
return true;
}

if(!(sender instanceof Player))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ public class ConfigManager
static Proxy proxy = Proxy.NO_PROXY;
static String toGuildedMessageFormat = "**{PLAYER} ⟫** {MESSAGE}";
static String toMinecraftMessageFormat = "[§eGuilded§r] <{PLAYER}> {MESSAGE}";

/**
* Load the config file of MGBridge.
* @return True if the config file is loaded and valid, false otherwise.
*/
static boolean loadConfig()
{
File cfgFile = new File(instance.getDataFolder(), "config.yml");
if (!cfgFile.exists())
if(!cfgFile.exists())
{ // create default config file if it doesn't exist
log.severe("Config file not found and an empty one will be created. Set the token and channel UUID and RESTART server.");
instance.saveDefaultConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public interface GuildedCommandExecutor
* Gets the description of the subcommand.
* @return The description of the subcommand.
*/
String getDescription();
String getCommandDescription();

/**
* Gets the usage of the subcommand.
* @return The usage of the subcommand.
*/
String getUsage();
String getCommandUsage();
boolean execute(ChatMessage msg, String[] args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,52 @@
import vip.floatationdevice.guilded4j.event.GuildedWebSocketWelcomeEvent;
import vip.floatationdevice.guilded4j.object.ChatMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import static vip.floatationdevice.mgbridge.BindManager.*;
import static vip.floatationdevice.mgbridge.I18nUtil.translate;
import static vip.floatationdevice.mgbridge.MGBridge.*;
import static vip.floatationdevice.mgbridge.BindManager.bindMap;
import static vip.floatationdevice.mgbridge.ConfigManager.*;
import static vip.floatationdevice.mgbridge.I18nUtil.translate;
import static vip.floatationdevice.mgbridge.MGBridge.getPlayerName;
import static vip.floatationdevice.mgbridge.MGBridge.log;

@SuppressWarnings({"unused", "UnstableApiUsage"})
public class GuildedEventListener
{
G4JWebSocketClient ws; // used to connect to guilded and receive guilded messages
private final HashMap<String, GuildedCommandExecutor> executors = new HashMap<>(); // subcommands of the guilded command "/mgb"

/**
* Gets the map of subcommands of the Guilded command "/mgb".
* @return A HashMap object. The key is the name of the subcommand, the value is the GuildedCommandExecutor object of the subcommand.
*/
public HashMap<String, GuildedCommandExecutor> getExecutors()
{
return executors;
}

/**
* Register a subcommand.
* @param executor The GuildedCommandExecutor object of the subcommand.
*/
public GuildedEventListener registerExecutor(GuildedCommandExecutor executor)
{
executors.put(executor.getCommandName(), executor);
return this;
}

/**
* Unregister a subcommand.
* @param commandName The name of the subcommand.
* @throws IllegalArgumentException If the subcommand does not exist.
*/
public GuildedEventListener unregisterExecutor(String commandName)
{
if(executors.remove(commandName) == null) throw new IllegalArgumentException("No executor found for command " + commandName);
if(executors.remove(commandName) == null)
throw new IllegalArgumentException("No executor found for command " + commandName);
return this;
}

public void unregisterAllExecutors()
void unregisterAllExecutors()
{
executors.clear();
}
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/vip/floatationdevice/mgbridge/I18nUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,44 @@
public class I18nUtil
{
private static FileConfiguration l;
private static String lang = "en_US";
private static String lang = null;

/**
* Set the language of the plugin.
* @param language Locale code of the language to use.
* @return The language code.
*/
public static String setLanguage(String language)
{
if(language == null || language.equals("")) return lang;
if(language == null || language.equals(""))
throw new IllegalArgumentException("Language cannot be null or empty");
lang = language;
File langFile = new File(MGBridge.instance.getDataFolder(), "lang_" + lang + ".yml");
if(!langFile.exists()) MGBridge.instance.saveResource("lang_" + lang + ".yml", false);
l = YamlConfiguration.loadConfiguration(langFile);
return lang;
}

/**
* Translate a string.
* @param key The key of the string.
* @return The translated string. If the key does not exist, return "[NO TRANSLATION: key]"
* @throws IllegalArgumentException If the language is not set.
*/
public static String translate(String key)
{
if(l == null) throw new IllegalStateException("Translation engine not initialized");
return l.getString(key) == null ? "[NO TRANSLATION: " + key + "]" : l.getString(key);
}

/**
* Get the language of the plugin.
* @return The language of the plugin.
* @throws IllegalStateException If the language is not set.
*/
public static String getLanguage()
{
if(lang == null) throw new IllegalStateException("Translation engine not initialized");
return lang;
}
}
47 changes: 43 additions & 4 deletions src/main/java/vip/floatationdevice/mgbridge/MGBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
import java.util.logging.Logger;

import static vip.floatationdevice.mgbridge.ConfigManager.*;
import static vip.floatationdevice.mgbridge.ConfigManager.toGuildedMessageFormat;
import static vip.floatationdevice.mgbridge.I18nUtil.translate;

public final class MGBridge extends JavaPlugin implements Listener
{
/**
* The instance of the plugin.
*/
public static MGBridge instance;
public static Logger log;
static Logger log;
G4JClient g4JClient = null;
GuildedEventListener gEventListener = null;

Expand All @@ -50,7 +52,7 @@ public void onEnable()
.registerExecutor(new Command_rmbind())
.registerExecutor(new Command_ping())
.registerExecutor(new Command_list());
g4JClient.getChatMessageManager().setProxy(proxy);
g4JClient.setProxy(proxy);
Bukkit.getPluginManager().registerEvents(this, this);
sendGuildedEmbed(new Embed().setTitle(translate("mgb-started").replace("%VERSION%", getDescription().getVersion())).setColor(0xffffff), null, null, null);
}
Expand All @@ -71,6 +73,7 @@ public void onDisable()
gEventListener.unregisterAllExecutors();
gEventListener = null;
}

if(g4JClient != null)
{
ChatMessage result = null;
Expand Down Expand Up @@ -124,6 +127,13 @@ public void onLeave(PlayerQuitEvent event)
sendGuildedEmbed(new Embed().setTitle(translate("player-disconnected").replace("%PLAYER%", event.getPlayer().getName())).setColor(0xffff00), null, null, null);
}

/**
* Sends a message to Guilded server.
* @param msg The content of the message.
* @param replyTo The ID of the message to reply to. Set to null to not reply.
* @param isPrivate Whether the reply is private or not.
* @param isSilent Whether the reply is silent or not.
*/
public void sendGuildedMessage(String msg, String replyTo, Boolean isPrivate, Boolean isSilent)
{
Bukkit.getScheduler().runTaskAsynchronously(instance, new Runnable()
Expand Down Expand Up @@ -157,6 +167,13 @@ public void run()
});
}

/**
* Sends an embed to Guilded server.
* @param emb The embed to send.
* @param replyTo The ID of the message to reply to. Set to null to not reply.
* @param isPrivate Whether the reply is private or not.
* @param isSilent Whether the reply is silent or not.
*/
public void sendGuildedEmbed(Embed emb, String replyTo, Boolean isPrivate, Boolean isSilent)
{
Bukkit.getScheduler().runTaskAsynchronously(instance, new Runnable()
Expand Down Expand Up @@ -198,14 +215,36 @@ static boolean notSet(String... s)
return false;
}

/**
* Gets the name of a player by UUID.
* @param u The UUID of the player.
* @return The name of the player. If the player is not found, returns "???".
*/
public static String getPlayerName(final UUID u)
{
try {return Bukkit.getPlayer(u).getName();}
catch(NullPointerException e) {return Bukkit.getOfflinePlayer(u).getName();}
catch(NullPointerException e)
{
String s = Bukkit.getOfflinePlayer(u).getName();
return s != null ? s : "???";
}
}

/**
* Gets the Guilded event listener.
* @return The GuildedEventListener object.
*/
public GuildedEventListener getGEventListener()
{
return gEventListener;
}

/**
* Gets the G4JClient used by the plugin.
* @return The G4JClient object.
*/
public G4JClient getG4JClient()
{
return g4JClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public String getCommandName()
}

@Override
public String getDescription()
public String getCommandDescription()
{
return translate("g-cmd-help-desc");
}

@Override
public String getUsage()
public String getCommandUsage()
{
return "/mgb help";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public String getCommandName()
}

@Override
public String getDescription()
public String getCommandDescription()
{
return translate("g-cmd-helpof-desc");
}

@Override
public String getUsage()
public String getCommandUsage()
{
return "/mgb helpof <SUBCOMMAND>";
}
Expand All @@ -34,9 +34,9 @@ public boolean execute(ChatMessage msg, String[] args)
GuildedCommandExecutor gce = instance.getGEventListener().getExecutors().get(args[0]);
String desc = null, usage = null;
try
{// GuildedCommandExecutor from MGB version 0.9.2 and older doesn't implement getDescription() and getUsage()
desc = gce.getDescription();
usage = gce.getUsage();
{// GuildedCommandExecutor from MGB version 0.9.3 and older doesn't implement getCommandDescription() and getCommandUsage()
desc = gce.getCommandDescription();
usage = gce.getCommandUsage();
}
catch(AbstractMethodError e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class Command_list implements GuildedCommandExecutor
public String getCommandName(){return "list";}

@Override
public String getDescription(){return translate("g-cmd-list-desc");}
public String getCommandDescription(){return translate("g-cmd-list-desc");}

@Override
public String getUsage(){return "/mgb list";}
public String getCommandUsage(){return "/mgb list";}

@Override
public boolean execute(ChatMessage msg, String[] args)
Expand Down
Loading

0 comments on commit 24a3374

Please sign in to comment.