Skip to content

Commit

Permalink
0.9.3
Browse files Browse the repository at this point in the history
  • Loading branch information
MCUmbrella committed Jul 3, 2022
1 parent e1af6c8 commit 89425b7
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 23 deletions.
2 changes: 1 addition & 1 deletion 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.2</version>
<version>0.9.3</version>
<packaging>jar</packaging>

<name>MGBridge</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@

public interface GuildedCommandExecutor
{
/**
* Gets the name of the subcommand (the first argument when the Guilded command "/mgb" is called).
* For example, if the command you want to implement is "/mgb test", this function should return "test".
* @return The name of the subcommand.
*/
String getCommandName();

/**
* Gets the description of the subcommand.
* @return The description of the subcommand.
*/
String getDescription();

/**
* Gets the usage of the subcommand.
* @return The usage of the subcommand.
*/
String getUsage();
boolean execute(ChatMessage msg, String[] args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
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;
Expand All @@ -19,25 +22,23 @@
public class GuildedEventListener
{
G4JWebSocketClient ws; // used to connect to guilded and receive guilded messages
private final ArrayList<GuildedCommandExecutor> executors = new ArrayList<>(); // list of guilded commands to execute
private final HashMap<String, GuildedCommandExecutor> executors = new HashMap<>(); // subcommands of the guilded command "/mgb"

public HashMap<String, GuildedCommandExecutor> getExecutors()
{
return executors;
}

public GuildedEventListener registerExecutor(GuildedCommandExecutor executor)
{
executors.add(executor);
executors.put(executor.getCommandName(), executor);
return this;
}

public GuildedEventListener unregisterExecutor(String commandName)
{
for(GuildedCommandExecutor executor : executors)
{
if(executor.getCommandName().equals(commandName))
{
executors.remove(executor);
return this;
}
}
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()
Expand Down Expand Up @@ -94,9 +95,8 @@ public void onGuildedMessage(ChatMessageCreatedEvent event)
if(args.length == 1) return; // no subcommand. do nothing
String[] subCommandArgs = new String[args.length - 2]; // arg1 arg2 ...
System.arraycopy(args, 2, subCommandArgs, 0, subCommandArgs.length);
for(GuildedCommandExecutor executor : executors)
if(executor.getCommandName().equals(args[1]))
executor.execute(msg, subCommandArgs);
if(executors.get(args[1]) != null)
executors.get(args[1]).execute(msg, subCommandArgs);
}
else // not a mgb command. consider it as normal message
{ // check if G->M forwarding is enabled, the message is not a command, and the message is from a user who is bound to Minecraft
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/vip/floatationdevice/mgbridge/MGBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import vip.floatationdevice.guilded4j.G4JClient;
import vip.floatationdevice.guilded4j.object.ChatMessage;
import vip.floatationdevice.guilded4j.object.Embed;
import vip.floatationdevice.mgbridge.gce.Command_list;
import vip.floatationdevice.mgbridge.gce.Command_mkbind;
import vip.floatationdevice.mgbridge.gce.Command_ping;
import vip.floatationdevice.mgbridge.gce.Command_rmbind;
import vip.floatationdevice.mgbridge.gce.*;

import java.util.UUID;
import java.util.logging.Logger;
Expand Down Expand Up @@ -47,6 +44,8 @@ public void onEnable()
g4JClient = new G4JClient(token);
getCommand("mgb").setExecutor(new BukkitCommandExecutor());
gEventListener = new GuildedEventListener()
.registerExecutor(new Command_help())
.registerExecutor(new Command_helpof())
.registerExecutor(new Command_mkbind())
.registerExecutor(new Command_rmbind())
.registerExecutor(new Command_ping())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package vip.floatationdevice.mgbridge.event;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import java.util.UUID;

/**
* Event that is fired when a Guilded user is bound to a Minecraft player.
*/
public class UserBoundEvent extends Event
{
private static final HandlerList handlers = new HandlerList();
private final String userId;
private final UUID uuid;

@Override
public HandlerList getHandlers()
{
return handlers;
}

public static HandlerList getHandlerList()
{
return handlers;
}

/**
* Gets the user ID of the user bound to the Minecraft player.
* @return The Guilded user ID.
*/
public String getUserId()
{
return userId;
}

/**
* Gets the UUID of the Minecraft player bound to the user.
* @return The UUID.
*/
public UUID getPlayerUUID()
{
return uuid;
}

public UserBoundEvent(String userId, UUID uuid)
{
this.userId = userId;
this.uuid = uuid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package vip.floatationdevice.mgbridge.event;

import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;

import java.util.UUID;

/**
* Event that is fired when a Guilded user or a Minecraft player is unbound.
*/
public class UserUnboundEvent extends Event
{
private static final HandlerList handlers = new HandlerList();
private final String userId;
private final UUID uuid;

@Override
public HandlerList getHandlers()
{
return handlers;
}

public static HandlerList getHandlerList()
{
return handlers;
}

/**
* Gets the user ID of the user unbound from the Minecraft player.
* @return The Guilded user ID.
*/
public String getUserId()
{
return userId;
}

/**
* Gets the UUID of the Minecraft player unbound from the user.
* @return The UUID.
*/
public UUID getPlayerUUID()
{
return uuid;
}

public UserUnboundEvent(String userId, UUID uuid)
{
this.userId = userId;
this.uuid = uuid;
}
}
46 changes: 46 additions & 0 deletions src/main/java/vip/floatationdevice/mgbridge/gce/Command_help.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package vip.floatationdevice.mgbridge.gce;

import vip.floatationdevice.guilded4j.object.ChatMessage;
import vip.floatationdevice.mgbridge.GuildedCommandExecutor;

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

public class Command_help implements GuildedCommandExecutor
{
@Override
public String getCommandName()
{
return "help";
}

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

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

@Override
public boolean execute(ChatMessage msg, String[] args)
{
StringBuilder sb = new StringBuilder();
for(String gceName : instance.getGEventListener().getExecutors().keySet())
{
sb.append('`').append(gceName).append("`, ");
}
sb.deleteCharAt(sb.length() - 1).deleteCharAt(sb.length() - 1);
instance.sendGuildedMessage(
translate("g-cmd-help-msg").replace("%SUBCOMMANDS%", sb.toString()),
msg.getId(),
null,
null
);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package vip.floatationdevice.mgbridge.gce;

import vip.floatationdevice.guilded4j.object.ChatMessage;
import vip.floatationdevice.mgbridge.GuildedCommandExecutor;

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

public class Command_helpof implements GuildedCommandExecutor
{
@Override
public String getCommandName()
{
return "helpof";
}

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

@Override
public String getUsage()
{
return "/mgb helpof <SUBCOMMAND>";
}

@Override
public boolean execute(ChatMessage msg, String[] args)
{
if(args.length == 1 && instance.getGEventListener().getExecutors().containsKey(args[0]))
{
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();
}
catch(AbstractMethodError e)
{
instance.getLogger().warning("Guilded subcommand '" + args[0] + "' is made for old version of MGBridge. It's recommended to update it");
}
instance.sendGuildedMessage(
translate("g-cmd-helpof-msg")
.replace("%SUBCOMMAND%", gce.getCommandName())
.replace("%DESCRIPTION%", desc != null ? desc : "???")
.replace("%USAGE%", usage != null ? usage : "???"),
msg.getId(),
null,
null
);
return true;
}
else return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@
import java.util.ArrayList;

import static vip.floatationdevice.mgbridge.BindManager.bindMap;
import static vip.floatationdevice.mgbridge.I18nUtil.translate;
import static vip.floatationdevice.mgbridge.MGBridge.instance;

public class Command_list implements GuildedCommandExecutor
{
@Override
public String getCommandName(){return "list";}

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

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

@Override
public boolean execute(ChatMessage msg, String[] args)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import vip.floatationdevice.guilded4j.object.ChatMessage;
import vip.floatationdevice.guilded4j.object.Embed;
import vip.floatationdevice.mgbridge.GuildedCommandExecutor;
import vip.floatationdevice.mgbridge.event.UserBoundEvent;

import static vip.floatationdevice.mgbridge.BindManager.*;
import static vip.floatationdevice.mgbridge.I18nUtil.translate;
Expand All @@ -14,6 +15,12 @@ public class Command_mkbind implements GuildedCommandExecutor
@Override
public String getCommandName(){return "mkbind";}

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

@Override
public String getUsage(){return "/mgb mkbind <CODE>";}

@Override
public boolean execute(ChatMessage msg, String[] args)
{
Expand All @@ -32,18 +39,19 @@ public boolean execute(ChatMessage msg, String[] args)
else// player not bound?
{
if(pendingMap.containsKey(args[0]))// code matched?
{
{// bind!
bindMap.put(msg.getCreatorId(), pendingMap.get(args[0]));
pendingPlayerMap.remove(pendingMap.get(args[0]));
pendingMap.remove(args[0]);
try
{
Bukkit.getPlayer(bindMap.get(msg.getCreatorId())).sendMessage(translate("m-bind-success"));
}
catch(NullPointerException ignored) {}
catch(NullPointerException ignored) {} // player is offline. ignore
instance.sendGuildedEmbed(new Embed().setTitle(translate("g-bind-success").replace("%PLAYER%", getPlayerName(bindMap.get(msg.getCreatorId())))).setColor(0x00ff00), msg.getId(), null, null);
log.info(translate("c-bind-success").replace("%PLAYER%", getPlayerName(bindMap.get(msg.getCreatorId()))));
saveBindMap();
Bukkit.getServer().getPluginManager().callEvent(new UserBoundEvent(msg.getCreatorId(), bindMap.get(msg.getCreatorId())));
return true;
}
else// code not in pending list?
Expand Down
Loading

0 comments on commit 89425b7

Please sign in to comment.