Skip to content

Commit

Permalink
Feat/cross server message (#127)
Browse files Browse the repository at this point in the history
* Expose processTransaction in MoneyCommandClass.

* Add account delete/transfer permission.

* Move Balance Currency Config to per-currency.

* Update Version.

* Add message handler, next is implementing into commands.

* Implement backlog parameter to proxy messaging.

* Add message support for money commands.

* Fix issues with cross-server messaging.

* Fix issue with insufficient message being incorrect for other players.
  • Loading branch information
creatorfromhell authored Aug 19, 2024
1 parent 10ddc2e commit 06a0527
Show file tree
Hide file tree
Showing 25 changed files with 242 additions and 47 deletions.
7 changes: 7 additions & 0 deletions .changelog/0.1.3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@
- Added message that is sent when MyECO is closed, but currencies weren't saved.
- Added configuration to currencies that will determine if the currency is shown in balance commands.
- Added new permissions to shared accounts; TRANSFER_OWNERSHIP and DELETE
- Added ability for messages from some money commands to be sent to players on different servers through bungee/redis/velocity

## Internals
- Various improvements to the module system
- Added the ability to require a specific TNE version
- Made it so commands are actually loaded properly
- Made it so callbacks are actually loaded/registered properly

## Fixes
- Fixed issue with Folia build that got messed up when implementing Components
- Removed luck perms context class completely since it was unused.
- Fixed issue with bundles not removing item currencies properly
- Fixed issue with PotionData on 1.21 servers since spigot changed an API method from NotNull to Nullable...
- Fixed issue with Banners on 1.21 servers, same as above.
- Fixed issue with callbacks not being called.
- Fixed issue with towny/factions accounts not being added.
- Fixed incorrect message being sent for Insufficient funds.

## Notes
- Bad API Design from Spigot/Paper: "changing an API method from NotNull to Nullable..."
Expand Down
6 changes: 6 additions & 0 deletions Bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@
<version>4.3.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-plain</artifactId>
<version>4.17.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>towny</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion Bungee/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: TNEBungeeCore
main: net.tnemc.bungee.BungeeCore
version: 1.2.9
version: 0.1.3.2
author: creatorfromhell
1 change: 1 addition & 0 deletions Bungee/src/net/tnemc/bungee/BungeeCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void onEnable() {

getProxy().registerChannel("tne:balance");
getProxy().registerChannel("tne:sync");
getProxy().registerChannel("tne:message");
getProxy().getPluginManager().registerListener(this, new MessageListener());
getProxy().getPluginManager().registerListener(this, new PlayerConnectListener());
}
Expand Down
13 changes: 8 additions & 5 deletions Bungee/src/net/tnemc/bungee/BungeeProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ public class BungeeProxy implements ProxyProvider {
* @param out The data to send.
*/
@Override
public void sendToAll(String channel, byte[] out) {
public void sendToAll(String channel, byte[] out, boolean backlog) {
BungeeCore.instance().getProxy().getServers().values().forEach(server->{
if(!server.getPlayers().isEmpty()) {
server.sendData(channel, out, false);
server.sendData(channel, out, true);
} else {
MessageManager.instance().addData(server.getSocketAddress().toString(), new BacklogEntry(channel, out));

if(backlog) {
MessageManager.instance().addData(server.getSocketAddress().toString(), new BacklogEntry(channel, out));
}
}
});
}
Expand All @@ -59,7 +62,7 @@ public void sendToAll(String channel, byte[] out) {
public void sendTo(String serverName, String channel, byte[] out) {
BungeeCore.instance().getProxy().getServers().values().forEach(server->{
if(server.getName().equalsIgnoreCase(serverName)) {
server.sendData(channel, out, false);
server.sendData(channel, out, true);
}
});
}
Expand All @@ -74,7 +77,7 @@ public void sendBacklog(@NotNull MessageData data) {
BungeeCore.instance().getProxy().getServers().values().forEach(server->{
if(server.getSocketAddress().toString().equalsIgnoreCase(data.getServerName())) {
for(BacklogEntry entry : data.getBacklog()) {
server.sendData(entry.channel(), entry.out(), false);
server.sendData(entry.channel(), entry.out(), true);
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion Bungee/src/net/tnemc/bungee/ProxyProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface ProxyProvider {
* @param channel The channel to use for sending the data.
* @param out The data to send.
*/
void sendToAll(final String channel, byte[] out);
void sendToAll(final String channel, byte[] out, boolean backlog);

/**
* Used to send data to a specific server.
Expand Down
8 changes: 4 additions & 4 deletions Bungee/src/net/tnemc/bungee/message/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public MessageHandler(String tag) {
this.tag = tag;
}

public static void sendToAll(final String channel, ByteArrayDataOutput out) {
MessageManager.instance().proxy().sendToAll(channel, out.toByteArray());
public static void sendToAll(final String channel, ByteArrayDataOutput out, boolean backlog) {
MessageManager.instance().proxy().sendToAll(channel, out.toByteArray(), backlog);
}

public static void sendToAll(final String channel, byte[] out) {
MessageManager.instance().proxy().sendToAll(channel, out);
public static void sendToAll(final String channel, byte[] out, boolean backlog) {
MessageManager.instance().proxy().sendToAll(channel, out, backlog);
}

public static void sendTo(final String serverName, final String channel, ByteArrayDataOutput out) {
Expand Down
3 changes: 3 additions & 0 deletions Bungee/src/net/tnemc/bungee/message/MessageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.tnemc.bungee.message.backlog.ConfigEntry;
import net.tnemc.bungee.message.backlog.MessageData;
import net.tnemc.bungee.message.handlers.BalanceMessageHandler;
import net.tnemc.bungee.message.handlers.MessageMessageHandler;
import net.tnemc.bungee.message.handlers.SyncAllMessageHandler;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -53,12 +54,14 @@ public MessageManager(final ProxyProvider proxy) {
handlers.put("balance", new BalanceMessageHandler());
//handlers.put("config", new ConfigMessageHandler());
handlers.put("sync", new SyncAllMessageHandler());
handlers.put("message", new MessageMessageHandler());
}

public void onMessage(final String channel, final byte[] data) {

final String tag = channel.replace("tne:", "").split(" ")[0];


if(handlers.containsKey(tag)) {
try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ public static void send(UUID server, String account, String accountName, String
out.writeUTF(currency);
out.writeUTF(handler);
out.writeUTF(amount);
sendToAll("tne:balance", out);
sendToAll("tne:balance", out, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public static void send(UUID server, byte[] left) {
out.write(left);


sendToAll("tne:config", out);
sendToAll("tne:config", out, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package net.tnemc.bungee.message.handlers;
/*
* The New Economy
* Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.tnemc.bungee.message.MessageHandler;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.UUID;

/**
* MessageMessageHandler
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class MessageMessageHandler extends MessageHandler {
public MessageMessageHandler() {
super("message");
}

@Override
public void handle(UUID server, DataInputStream stream) {

try {
final String identifier = stream.readUTF();
final String message = stream.readUTF();

send(server, identifier, message);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void send(UUID server, String identifier, String message) {
final ByteArrayDataOutput out = ByteStreams.newDataOutput();


out.writeUTF(server.toString());
out.writeUTF(identifier);
out.writeUTF(message);


sendToAll("tne:message", out, false);
}
}
1 change: 1 addition & 0 deletions Core/src/net/tnemc/core/TNECore.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public void registerConfigs() {
public void registerPluginChannels() {
PluginCore.instance().getChannelMessageManager().register(new BalanceHandler());
PluginCore.instance().getChannelMessageManager().register(new SyncHandler());
PluginCore.instance().getChannelMessageManager().register(new net.tnemc.core.channel.MessageHandler());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import net.tnemc.core.currency.Currency;
import net.tnemc.core.currency.CurrencyType;
import net.tnemc.core.currency.type.ExperienceLevelType;
import net.tnemc.core.currency.type.ExperienceType;
import net.tnemc.core.utils.Identifier;
import net.tnemc.plugincore.core.utils.Experience;

Expand Down
17 changes: 17 additions & 0 deletions Core/src/net/tnemc/core/actions/response/HoldingsResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ public boolean success() {
return false;
}

@Override
public String response() {
return "Messages.Money.Insufficient";
}
},

/**
* The action was unsuccessful due to the account not having enough funds.
*
* @since 0.1.2.0
*/
INSUFFICIENT_OTHER {
@Override
public boolean success() {
return false;
}

@Override
public String response() {
return "Messages.Money.InsufficientOther";
Expand Down
79 changes: 79 additions & 0 deletions Core/src/net/tnemc/core/channel/MessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.tnemc.core.channel;
/*
* The New Economy
* Copyright (C) 2022 - 2024 Daniel "creatorfromhell" Vidmar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.tnemc.core.TNECore;
import net.tnemc.core.account.PlayerAccount;
import net.tnemc.plugincore.PluginCore;
import net.tnemc.plugincore.core.channel.ChannelBytesWrapper;
import net.tnemc.plugincore.core.channel.ChannelMessageHandler;
import net.tnemc.plugincore.core.compatibility.log.DebugLevel;

import java.util.Optional;
import java.util.UUID;

/**
* MessageHandler
*
* @author creatorfromhell
* @since 0.1.2.0
*/
public class MessageHandler extends ChannelMessageHandler {

public MessageHandler() {
super("message");
}

public static void send(final UUID identifier, final Component component) {
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(PluginCore.instance().getServerID().toString());
out.writeUTF(identifier.toString());
out.writeUTF(MiniMessage.miniMessage().serialize(component));
PluginCore.log().debug("Sending message: " + MiniMessage.miniMessage().serialize(component));

TNECore.instance().storage().sendProxyMessage("tne:message", out.toByteArray());
}

@Override
public void handle(ChannelBytesWrapper wrapper) {

try {

final String uuid = wrapper.readUTF();
final String message = wrapper.readUTF();
PluginCore.log().debug("Received message: " + message);

if(uuid != null && message != null) {

final UUID identifier = UUID.fromString(uuid);

final Optional<PlayerAccount> player = TNECore.eco().account().findPlayerAccount(identifier);
if(player.isPresent() && player.get().getPlayer().isPresent()) {
player.get().getPlayer().get().message(message);
}
}

} catch(Exception e) {
PluginCore.log().error("Issue with message plugin message", e, DebugLevel.STANDARD);
}
}
}
Loading

0 comments on commit 06a0527

Please sign in to comment.