Skip to content

Examples

Jiří Apjár edited this page Aug 4, 2022 · 1 revision

Example usage

In this example, I'll show you how cross-server private messages can be made. This is just showcase of the ForestRedisAPI features, so DO NOT use it as template for your projects. Example code is intentionally in one class, so you don't need to look into many files for details.

PrivateMessage object

This object will be used to store data about sender, target and the message itself.

public class PrivateMessage {

    private String from;
    private String to;
    private String message;

    public PrivateMessage() {
    }

    public PrivateMessage(String from, String to, String message) {
        this.from = from;
        this.to = to;
        this.message = message;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Plugin itself

Inside the command execution, we want to send the message to Redis server. We can do it quite simply.

public class ForestRedisPM extends JavaPlugin implements Listener, CommandExecutor {

    @Override
    public void onEnable() {
        if (RedisManager.getAPI() == null) {
            //TODO: Handle not-existing ForestRedisAPI instance
            return;
        }

        // Subscribe to channel "pms"
        RedisManager.getAPI().subscribe("pms");

        // Register listener and commands
        this.getCommand("rpm").setExecutor(this);
        this.getServer().getPluginManager().registerEvents(this, this);
    }

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) {
        if (!(commandSender instanceof Player)) {
            return true;
        }

        Player player = (Player) commandSender;

        if (args.length == 0) {
            return true;
        }

        String targetPlayerName = args[0];
        String message = Stream.of(args).skip(1).collect(Collectors.joining(" "));

        // Send player the message
        player.sendMessage("§6§lTO: §7§l" + targetPlayerName + " §8§l>> §f" + message);

        // Create PrivateMessage instance with obtained data
        PrivateMessage privateMessage = new PrivateMessage(player.getName(), targetPlayerName, message);

        // Publish the message to Redis server (done async, so don't worry about lags)
        RedisManager.getAPI().publishObject("pms", privateMessage);

        return true;
    }

    @EventHandler
    public void onReceived(RedisMessageReceivedEvent event) {

        // Skip non-related channels
        if (!event.getChannel().equals("pms")) {
            return;
        }
 
        // Obtain the received object
        PrivateMessage privateMessage = event.getMessageObject(PrivateMessage.class);

        if (privateMessage == null) {
            return;
        }

        Player player = Bukkit.getPlayer(privateMessage.getTo());
        if (player == null) {
            return;
        }

        player.sendMessage("§6§lFROM: §7§l" + privateMessage.getFrom() + " §8§l>> §f§l" + privateMessage.getMessage());
        return;
        
    }
}
Clone this wiki locally