From 5d7ce07a65263827136b4d489dde513dfbedec6f Mon Sep 17 00:00:00 2001 From: allenclan23 Date: Tue, 21 Apr 2020 13:00:11 -0400 Subject: [PATCH 1/2] Initial work --- .../catalyst/api/data/key/CatalystKeys.java | 5 +- .../CrossServerTeleportationHelper.java | 35 +++++++ .../api/service/ExecuteCommandService.java | 4 +- .../service/BungeeExecuteCommandService.java | 6 +- .../command/CommonTeleportAcceptCommand.java | 21 ++++ .../common/command/CommonTeleportCommand.java | 79 +++++++++++++++ .../config/CatalystConfigurationService.java | 1 + .../catalyst/common/module/CommonModule.java | 8 ++ .../catalyst/common/plugin/Catalyst.java | 5 + .../common/service/CommonChatFilter.java | 18 ++-- .../CommonCrossServerTeleportationHelper.java | 98 +++++++++++++++++++ .../command/CatalystSpongeCommandNode.java | 21 ++++ .../config/SpongeConfigurationService.java | 1 - .../sponge/listener/SpongeJedisListener.java | 69 +++++++++++++ .../sponge/listener/SpongeListener.java | 57 +++++++++++ .../catalyst/sponge/module/SpongeModule.java | 1 + .../sponge/plugin/CatalystSponge.java | 8 ++ .../service/SpongeExecuteCommandService.java | 13 ++- .../service/SpongeTeleportationService.java | 48 +++++++++ .../command/CatalystCommandManager.java | 6 ++ .../velocity/command/SendCommand.java | 7 +- .../velocity/command/TeleportCommand.java | 56 +++++++++++ .../velocity/listener/VelocityListener.java | 2 + .../VelocityExecuteCommandService.java | 9 +- 24 files changed, 558 insertions(+), 20 deletions(-) create mode 100644 api/src/main/java/org/anvilpowered/catalyst/api/service/CrossServerTeleportationHelper.java create mode 100644 common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportAcceptCommand.java create mode 100644 common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportCommand.java create mode 100644 common/src/main/java/org/anvilpowered/catalyst/common/service/CommonCrossServerTeleportationHelper.java create mode 100644 sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeJedisListener.java create mode 100644 sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeListener.java create mode 100644 sponge/src/main/java/org/anvilpowered/catalyst/sponge/service/SpongeTeleportationService.java create mode 100644 velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportCommand.java diff --git a/api/src/main/java/org/anvilpowered/catalyst/api/data/key/CatalystKeys.java b/api/src/main/java/org/anvilpowered/catalyst/api/data/key/CatalystKeys.java index dd542f57..8510b749 100644 --- a/api/src/main/java/org/anvilpowered/catalyst/api/data/key/CatalystKeys.java +++ b/api/src/main/java/org/anvilpowered/catalyst/api/data/key/CatalystKeys.java @@ -30,7 +30,7 @@ public final class CatalystKeys { public static final Key CHAT_FILTER_ENABLED = new Key("CHAT_FILTER_ENABLED", false) { }; - public static final Key> CHAT_FILTER_SWEARS = new Key>("CHAT_FILTER_SWEARS", Arrays.asList("2g1c", "2girls1cup", "4r5e", "anal", "anus", "arse", "ass", "asses", "assfucker", "assfukka", "asshole", "arsehole", "asswhole", "assmunch", "autoerotic", "autoerotic", "ballsack", "bastard", "beastial", "bestial", "bellend", "bdsm", "beastiality", "bestiality", "bitch", "bitches", "bitchin", "bitching", "bimbo", "bimbos", "blowjob", "blowjob", "blowjobs", "bluewaffle", "boob", "boobs", "booobs", "boooobs", "booooobs", "booooooobs", "breasts", "bootycall", "brownshower", "brownshowers", "boner", "bondage", "buceta", "bukake", "bukkake", "bullshit", "bullshit", "busty", "butthole", "carpetmuncher", "cawk", "chink", "cipa", "clit", "clits", "clitoris", "cnut", "cock", "cocks", "cockface", "cockhead", "cockmunch", "cockmuncher", "cocksuck", "cocksucked", "cocksucking", "cocksucks", "cocksucker", "cokmuncher", "coon", "cowgirl", "cowgirls", "cowgirl", "cowgirls", "crap", "crotch", "cum", "cummer", "cumming", "cuming", "cums", "cumshot", "cunilingus", "cunillingus", "cunnilingus", "cunt", "cuntlicker", "cuntlicking", "cunts", "damn", "dick", "dickhead", "dildo", "dildos", "dink", "dinks", "deepthroat", "deepthroat", "dogstyle", "doggiestyle", "doggiestyle", "doggystyle", "doggystyle", "donkeyribber", "doosh", "douche", "duche", "dyke", "ejaculate", "ejaculated", "ejaculates", "ejaculating", "ejaculatings", "ejaculation", "ejakulate", "erotic", "erotism", "fag", "faggot", "fagging", "faggit", "faggitt", "faggs", "fagot", "fagots", "fags", "fatass", "femdom", "fingering", "footjob", "footjob", "fuck", "fucks", "fucker", "fuckers", "fucked", "fuckhead", "fuckheads", "fuckin", "fucking", "fcuk", "fcuker", "fcuking", "felching", "fellate", "fellatio", "fingerfuck", "fingerfucked", "fingerfucker", "fingerfuckers", "fingerfucking", "fingerfucks", "fistfuck", "fistfucked", "fistfucker", "fistfuckers", "fistfucking", "fistfuckings", "fistfucks", "flange", "fook", "fooker", "fucka", "fuk", "fuks", "fuker", "fukker", "fukkin", "fukking", "futanari", "futanary", "gangbang", "gangbanged", "gangbang", "gokkun", "goldenshower", "goldenshower", "gaysex", "goatse", "handjob", "handjob", "hentai", "hooker", "hoer", "homo", "horny", "incest", "jackoff", "jackoff", "jerkoff", "jerkoff", "jizz", "knob", "kinbaku", "labia", "masturbate", "masochist", "mofo", "mothafuck", "motherfuck", "motherfucker", "mothafucka", "mothafuckas", "mothafuckaz", "mothafucked", "mothafucker", "mothafuckers", "mothafuckin", "mothafucking", "mothafuckings", "mothafucks", "motherfucker", "motherfucked", "motherfucker", "motherfuckers", "motherfuckin", "motherfucking", "motherfuckings", "motherfuckka", "motherfucks", "milf", "muff", "nigga", "nigger", "nigg", "nipple", "nipples", "nob", "nobjokey", "nobhead", "nobjocky", "nobjokey", "numbnuts", "nutsack", "nude", "nudes", "orgy", "orgasm", "orgasms", "panty", "panties", "penis", "playboy", "porn", "porno", "pornography", "pron", "pussy", "pussies", "rape", "raping", "rapist", "rectum", "retard", "rimming", "sadist", "sadism", "schlong", "scrotum", "sex", "semen", "shemale", "shemale", "shibari", "shibary", "shit", "shitdick", "shitfuck", "shitfull", "shithead", "shiting", "shitings", "shits", "shitted", "shitters", "shitting", "shittings", "shitty", "shota", "skank", "slut", "sluts", "smut", "smegma", "spunk", "stripclub", "stripclub", "tit", "tits", "titties", "titty", "titfuck", "tittiefucker", "titties", "tittyfuck", "tittywank", "titwank", "threesome", "threesome", "throating", "twat", "twathead", "twatty", "twunt", "viagra", "vagina", "vulva", "wank", "wanker", "wanky", "whore", "whoar", "xxx", "xx", "yaoi", "yury")) { + public static final Key> CHAT_FILTER_SWEARS = new Key>("CHAT_FILTER_SWEARS", Arrays.asList("fuck", "shit", "ass")) { }; public static final Key> CHAT_FILTER_EXCEPTIONS = new Key>("CHAT_FILTER_EXCEPTIONS", Arrays.asList("assassin", "jkass")) { }; @@ -182,6 +182,8 @@ public final class CatalystKeys { static List advancedServerInfo = new LinkedList<>(); public static final Key> ADVANCED_SERVER_INFO = new Key>("ADVANCED_SERVER_INFO", advancedServerInfo) { }; + public static final Key TELEPORT_PERMISSION = new Key("TELEPORT_PERMISSION", "catalyst.command.teleport"){ + }; static { ChatChannel global = new ChatChannel(); @@ -286,6 +288,7 @@ public final class CatalystKeys { Keys.registerKey(MOTD); Keys.registerKey(ADVANCED_SERVER_INFO_ENABLED); Keys.registerKey(ADVANCED_SERVER_INFO); + Keys.registerKey(TELEPORT_PERMISSION); } private CatalystKeys() { diff --git a/api/src/main/java/org/anvilpowered/catalyst/api/service/CrossServerTeleportationHelper.java b/api/src/main/java/org/anvilpowered/catalyst/api/service/CrossServerTeleportationHelper.java new file mode 100644 index 00000000..73464c41 --- /dev/null +++ b/api/src/main/java/org/anvilpowered/catalyst/api/service/CrossServerTeleportationHelper.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2020 STG_Allen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.anvilpowered.catalyst.api.service; + +public interface CrossServerTeleportationHelper { + + void teleport(String sender, String recipient); + + boolean isOnSameServer(String sender, String recipient); + + void sendTeleportMessage(String serverName, String sender, String recipient); + + void insertIntoTeleportationMap(String sender, String recipient); + + String getSender(String recipient); + + String getRecipient(String sender); + + boolean isPresentInMap(String sender); +} diff --git a/api/src/main/java/org/anvilpowered/catalyst/api/service/ExecuteCommandService.java b/api/src/main/java/org/anvilpowered/catalyst/api/service/ExecuteCommandService.java index 0288c5b0..1739e945 100644 --- a/api/src/main/java/org/anvilpowered/catalyst/api/service/ExecuteCommandService.java +++ b/api/src/main/java/org/anvilpowered/catalyst/api/service/ExecuteCommandService.java @@ -17,11 +17,13 @@ package org.anvilpowered.catalyst.api.service; +import java.util.concurrent.CompletableFuture; + public interface ExecuteCommandService { void executeCommand(TCommandSource source, String command); - void executeAsConsole(String command); + CompletableFuture executeAsConsole(String command); void executeDiscordCommand(String command); } diff --git a/bungeecord/src/main/java/org/anvilpowered/catalyst/bungee/service/BungeeExecuteCommandService.java b/bungeecord/src/main/java/org/anvilpowered/catalyst/bungee/service/BungeeExecuteCommandService.java index c2daaf01..ea77512f 100644 --- a/bungeecord/src/main/java/org/anvilpowered/catalyst/bungee/service/BungeeExecuteCommandService.java +++ b/bungeecord/src/main/java/org/anvilpowered/catalyst/bungee/service/BungeeExecuteCommandService.java @@ -23,6 +23,8 @@ import org.anvilpowered.catalyst.api.service.ExecuteCommandService; import org.anvilpowered.catalyst.bungee.discord.DiscordCommandSource; +import java.util.concurrent.CompletableFuture; + public class BungeeExecuteCommandService implements ExecuteCommandService { @Inject @@ -34,8 +36,8 @@ public void executeCommand(CommandSender commandSender, String command) { } @Override - public void executeAsConsole(String command) { - ProxyServer.getInstance().getPluginManager().dispatchCommand(ProxyServer.getInstance().getConsole(), command); + public CompletableFuture executeAsConsole(String command) { + return CompletableFuture.runAsync(() -> ProxyServer.getInstance().getPluginManager().dispatchCommand(ProxyServer.getInstance().getConsole(), command)); } @Override diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportAcceptCommand.java b/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportAcceptCommand.java new file mode 100644 index 00000000..85797705 --- /dev/null +++ b/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportAcceptCommand.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2020 STG_Allen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.anvilpowered.catalyst.common.command; + +public class CommonTeleportAcceptCommand { +} diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportCommand.java b/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportCommand.java new file mode 100644 index 00000000..a5cc4cb6 --- /dev/null +++ b/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportCommand.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2020 STG_Allen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.anvilpowered.catalyst.common.command; + +import com.google.inject.Inject; +import org.anvilpowered.anvil.api.data.registry.Registry; +import org.anvilpowered.anvil.api.util.PermissionService; +import org.anvilpowered.anvil.api.util.TextService; +import org.anvilpowered.anvil.api.util.UserService; +import org.anvilpowered.catalyst.api.data.key.CatalystKeys; +import org.anvilpowered.catalyst.api.plugin.PluginMessages; +import org.anvilpowered.catalyst.api.service.CrossServerTeleportationHelper; + +import java.util.Optional; + +public class CommonTeleportCommand< + TString, + TUser, + TPlayer, + TCommandSource, + TSubject> { + + @Inject + private TextService textService; + + @Inject + private PluginMessages pluginMessages; + + @Inject + private PermissionService permissionService; + + @Inject + private Registry registry; + + @Inject + private CrossServerTeleportationHelper teleportationHelper; + + @Inject + private UserService userService; + + public void execute(TCommandSource source, TSubject subject, String[] args) { + if (permissionService.hasPermission(subject, registry.getOrDefault(CatalystKeys.TELEPORT_PERMISSION))) { + if (teleportationHelper.isPresentInMap(userService.getUserName((TUser) source)) + && teleportationHelper.getRecipient(userService.getUserName((TUser) source)).equalsIgnoreCase(args[0])) { + //TODO Send + } else { + Optional recipient = userService.get(args[0]); + if (recipient.isPresent()) { + teleportationHelper.insertIntoTeleportationMap(userService.getUserName((TUser) source), userService.getUserName(recipient.get())); + //Send message to target + textService.send( + pluginMessages.getTeleportRequestReceived(userService.getUserName((TUser) source)), + (TCommandSource) recipient.get()); + //Send message to source + textService.send( + pluginMessages.getTeleportRequestSent(userService.getUserName(recipient.get())), + source + ); + } + } + } + } +} + diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/data/config/CatalystConfigurationService.java b/common/src/main/java/org/anvilpowered/catalyst/common/data/config/CatalystConfigurationService.java index a1999c3a..69b12b90 100644 --- a/common/src/main/java/org/anvilpowered/catalyst/common/data/config/CatalystConfigurationService.java +++ b/common/src/main/java/org/anvilpowered/catalyst/common/data/config/CatalystConfigurationService.java @@ -30,6 +30,7 @@ public class CatalystConfigurationService extends BaseConfigurationService { @Inject public CatalystConfigurationService(ConfigurationLoader configLoader) { super(configLoader); + withRedis(); setName(CatalystKeys.CHAT_FILTER_SWEARS, "chat.filter.swears"); setName(CatalystKeys.CHAT_FILTER_EXCEPTIONS, "chat.filter.exceptions"); setName(CatalystKeys.CHAT_FILTER_ENABLED, "chat.filter.enabled"); diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/module/CommonModule.java b/common/src/main/java/org/anvilpowered/catalyst/common/module/CommonModule.java index e648c0d3..19d18054 100644 --- a/common/src/main/java/org/anvilpowered/catalyst/common/module/CommonModule.java +++ b/common/src/main/java/org/anvilpowered/catalyst/common/module/CommonModule.java @@ -45,6 +45,7 @@ import org.anvilpowered.catalyst.api.service.StaffChatService; import org.anvilpowered.catalyst.api.service.StaffListService; import org.anvilpowered.catalyst.api.service.TabService; +import org.anvilpowered.catalyst.api.service.CrossServerTeleportationHelper; import org.anvilpowered.catalyst.common.data.config.CatalystConfigurationService; import org.anvilpowered.catalyst.common.data.registry.CatalystRegistry; import org.anvilpowered.catalyst.common.discord.CommonJDAService; @@ -68,6 +69,7 @@ import org.anvilpowered.catalyst.common.service.CommonPrivateMessageService; import org.anvilpowered.catalyst.common.service.CommonStaffChatService; import org.anvilpowered.catalyst.common.service.CommonTabService; +import org.anvilpowered.catalyst.common.service.CommonCrossServerTeleportationHelper; @SuppressWarnings({"UnstableApiUsage"}) public class CommonModule< @@ -204,6 +206,12 @@ protected void configure() { new TypeToken>(getClass()) { } ); + be.bind( + new TypeToken(getClass()) { + }, + new TypeToken>(getClass()) { + } + ); bind(ConfigurationService.class).to(CatalystConfigurationService.class); bind(ChatFilter.class).to(CommonChatFilter.class); diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/plugin/Catalyst.java b/common/src/main/java/org/anvilpowered/catalyst/common/plugin/Catalyst.java index 50bdcab1..a0586c9d 100644 --- a/common/src/main/java/org/anvilpowered/catalyst/common/plugin/Catalyst.java +++ b/common/src/main/java/org/anvilpowered/catalyst/common/plugin/Catalyst.java @@ -46,4 +46,9 @@ protected void whenLoaded(Environment environment) { protected void whenReady(Environment environment) { super.whenReady(environment); } + + @Override + protected void applyToBuilder(Environment.Builder builder) { + super.applyToBuilder(builder); + } } diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonChatFilter.java b/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonChatFilter.java index af5e2fab..e05404f5 100644 --- a/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonChatFilter.java +++ b/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonChatFilter.java @@ -36,9 +36,9 @@ public class CommonChatFilter implements ChatFilter { @Override public String stripMessage(String checkMessage) { return checkMessage.toLowerCase() - .replaceAll("[*()/.,;'#~^+\\-]", " ").replaceAll("[0@]", "o") - .replaceAll("1", "i").replaceAll("\\$", "s" - ); // Replaces any special characters with their letter equivalent or a space + .replaceAll("[*()/.,;'#~^+\\-]", " ").replaceAll("[0@]", "o") + .replaceAll("1", "i").replaceAll("\\$", "s" + ); // Replaces any special characters with their letter equivalent or a space } // Generates a list with the locations of all the spaces in the original message - for index replacement later @@ -60,7 +60,7 @@ public List findSpacePositions(String message, String noSpaces) { public List findSwears(String message, List spacePositions) { List swearList = new ArrayList<>(); // List of all swear word locations List exceptions = registry.getOrDefault(CatalystKeys.CHAT_FILTER_EXCEPTIONS).stream() - .map(String::toLowerCase).collect(Collectors.toList()); /* Gets exceptions from API, then, via a map, + .map(String::toLowerCase).collect(Collectors.toList()); /* Gets exceptions from API, then, via a map, makes them lowercase. */ for (String bannedWord : registry.getOrDefault(CatalystKeys.CHAT_FILTER_SWEARS)) { /* Iterates through every banned word to see if the message contains it.*/ @@ -76,7 +76,7 @@ public List findSwears(String message, List spacePositions) { int[] wordLocation = new int[]{startIndex + extraStartSpace, endIndex + extraEndSpace + 1}; swearList.add(wordLocation); } else if ((spacePositions.contains(startIndex - 1) || startIndex == 0) && - (endIndex == message.length() - 1|| spacePositions.contains(endIndex))) { + (endIndex == message.length() - 1 || spacePositions.contains(endIndex))) { if (endIndex == message.length() - 1) { extraEndSpace = spacePositions.size(); } @@ -84,7 +84,7 @@ public List findSwears(String message, List spacePositions) { int[] wordLocation = new int[]{startIndex + extraStartSpace, endIndex + extraEndSpace + 1}; swearList.add(wordLocation); } - startIndex = message.indexOf(bannedWord, startIndex+1); // checks for any more instances of the banned word + startIndex = message.indexOf(bannedWord, startIndex + 1); // checks for any more instances of the banned word } } } @@ -106,11 +106,11 @@ public String replaceSwears(String message) { */ if (swearEnd >= message.length()) { // If the swear is at the end of the message, skip adding the rest message = message.substring(0, swearStart) + // This join method adds swearLength '*'s. - String.join("", Collections.nCopies(swearLength, "*")); + String.join("", Collections.nCopies(swearLength, "*")); } else { // Add the beginning & the end of the message around the swear message = message.substring(0, swearStart) + - String.join("", Collections.nCopies(swearLength, "*")) - + message.substring(swearEnd); + String.join("", Collections.nCopies(swearLength, "*")) + + message.substring(swearEnd); } } return message; diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonCrossServerTeleportationHelper.java b/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonCrossServerTeleportationHelper.java new file mode 100644 index 00000000..6f2321f4 --- /dev/null +++ b/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonCrossServerTeleportationHelper.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2020 STG_Allen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.anvilpowered.catalyst.common.service; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.anvilpowered.anvil.api.redis.RedisService; +import org.anvilpowered.anvil.api.util.CurrentServerService; +import org.anvilpowered.catalyst.api.service.CrossServerTeleportationHelper; +import org.anvilpowered.catalyst.api.service.ExecuteCommandService; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; + +@Singleton +public class CommonCrossServerTeleportationHelper implements CrossServerTeleportationHelper { + + @Inject + private CurrentServerService currentServerService; + + @Inject + private RedisService redisService; + + @Inject + private ExecuteCommandService executeCommandService; + + public Map teleportationAcceptMap = new HashMap<>(); + + @Override + public void teleport(String sender, String recipient) { + if (currentServerService.getName(sender).isPresent() && currentServerService.getName(recipient).isPresent()) { + if (isOnSameServer(sender, recipient)) { + sendTeleportMessage(currentServerService.getName(sender).get(), sender, recipient); + } else { + sendTeleportMessage(currentServerService.getName(recipient).get(), sender, recipient); + executeCommandService.executeAsConsole("send " + sender + " " + currentServerService.getName(recipient).get()); + } + } + } + + @Override + public boolean isOnSameServer(String sender, String recipient) { + if (currentServerService.getName(sender).isPresent() && currentServerService.getName(recipient).isPresent()) { + return currentServerService.getName(sender).get().equalsIgnoreCase(currentServerService.getName(recipient).get()); + } + return false; + } + + @Override + public void sendTeleportMessage(String serverName, String sender, String recipient) { + CompletableFuture.runAsync(() -> { + redisService.getJedisPool().getResource().publish(serverName, "Teleport " + sender + " " + recipient); + }); + } + + @Override + public void insertIntoTeleportationMap(String sender, String recipient) { + teleportationAcceptMap.put(sender, recipient); + } + + @Override + public String getSender(String recipient) { + AtomicReference s = new AtomicReference<>("null"); + teleportationAcceptMap.forEach((k, v) -> { + if (v.equalsIgnoreCase(recipient)) { + s.set(v); + } + }); + return s.get(); + } + + @Override + public String getRecipient(String sender) { + return teleportationAcceptMap.get(sender); + } + + @Override + public boolean isPresentInMap(String sender) { + return teleportationAcceptMap.containsKey(sender); + } +} diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/command/CatalystSpongeCommandNode.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/command/CatalystSpongeCommandNode.java index a49d10c9..5026adbf 100644 --- a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/command/CatalystSpongeCommandNode.java +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/command/CatalystSpongeCommandNode.java @@ -19,26 +19,43 @@ import com.google.inject.Inject; import com.google.inject.Singleton; +import org.anvilpowered.anvil.api.Environment; +import org.anvilpowered.anvil.api.data.key.Keys; import org.anvilpowered.anvil.api.data.registry.Registry; import org.anvilpowered.anvil.api.plugin.Plugin; +import org.anvilpowered.anvil.api.redis.RedisService; import org.anvilpowered.catalyst.api.data.key.CatalystKeys; +import org.anvilpowered.catalyst.sponge.listener.SpongeJedisListener; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.args.GenericArguments; import org.spongepowered.api.command.spec.CommandSpec; +import org.spongepowered.api.scheduler.Task; import org.spongepowered.api.text.Text; +import javax.inject.Named; + @Singleton public class CatalystSpongeCommandNode { @Inject private CommandSyncCommand syncCommand; + @Inject + private RedisService redisService; + @Inject private Plugin plugin; + @Inject + private SpongeJedisListener spongeJedisListener; + private Registry registry; private boolean alreadyLoaded = false; + @Inject + @Named("anvil") + Environment environment; + @Inject public CatalystSpongeCommandNode(Registry registry) { this.registry = registry; @@ -48,6 +65,10 @@ public CatalystSpongeCommandNode(Registry registry) { protected void loadCommands() { if (alreadyLoaded) return; alreadyLoaded = true; + Task.builder() + .execute(() -> redisService.getJedisPool().getResource().subscribe(spongeJedisListener, environment.getRegistry().getOrDefault(Keys.SERVER_NAME), "all")) + .async() + .submit(plugin); CommandSpec root = CommandSpec.builder() .description(Text.of("Sync commands to other servers!")) .arguments( diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/data/config/SpongeConfigurationService.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/data/config/SpongeConfigurationService.java index 63381ce8..c9a4baa6 100644 --- a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/data/config/SpongeConfigurationService.java +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/data/config/SpongeConfigurationService.java @@ -28,6 +28,5 @@ public class SpongeConfigurationService extends CatalystConfigurationService { @Inject public SpongeConfigurationService(@DefaultConfig(sharedRoot = false) ConfigurationLoader configLoader) { super(configLoader); - withRedis(); } } diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeJedisListener.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeJedisListener.java new file mode 100644 index 00000000..1080ddf8 --- /dev/null +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeJedisListener.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2020 STG_Allen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.anvilpowered.catalyst.sponge.listener; + +import com.google.inject.Inject; +import org.anvilpowered.anvil.api.Environment; +import org.anvilpowered.anvil.api.data.key.Keys; +import org.anvilpowered.catalyst.api.service.EventService; +import org.anvilpowered.catalyst.sponge.service.SpongeTeleportationService; +import org.slf4j.Logger; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.event.Event; +import redis.clients.jedis.JedisPubSub; + +import javax.inject.Named; + +public class SpongeJedisListener extends JedisPubSub { + + @Inject + @Named("anvil") + private Environment environment; + + @Inject + private SpongeTeleportationService teleportationService; + + @Inject + private EventService eventService; + + @Inject + Logger logger; + + @Override + public void onMessage(String channel, String message) { + logger.warn(channel); + logger.warn(message); + if (environment.getRegistry().getOrDefault(Keys.SERVER_NAME).equalsIgnoreCase(channel)) { + if (message.startsWith("Teleport")) { + message = message.replace("Teleport", ""); + System.out.println(message); + String[] mess = message.split("\\s+"); + System.out.println(mess[1]); + System.out.println(Sponge.getServer().getPlayer(mess[1]).isPresent()); + System.out.println(mess[2]); + System.out.println(Sponge.getServer().getPlayer(mess[2]).isPresent()); + if (Sponge.getServer().getPlayer(mess[1]).isPresent()) { + logger.warn(""); + eventService.run(() -> teleportationService.teleport(mess[1], mess[2], true)); + } else { + eventService.run(() -> teleportationService.teleport(mess[1], mess[2], false)); + } + } + } + } +} diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeListener.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeListener.java new file mode 100644 index 00000000..a2b63289 --- /dev/null +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeListener.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2020 STG_Allen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.anvilpowered.catalyst.sponge.listener; + +import com.google.inject.Inject; +import org.anvilpowered.anvil.api.plugin.Plugin; +import org.anvilpowered.catalyst.sponge.service.SpongeTeleportationService; +import org.slf4j.Logger; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.network.ClientConnectionEvent; +import org.spongepowered.api.scheduler.Task; + +import java.util.concurrent.TimeUnit; + +public class SpongeListener { + + @Inject + private SpongeTeleportationService service; + + @Inject + private Plugin plugin; + + @Listener + public void onPlayerJoin(ClientConnectionEvent.Join event) { + Player player = event.getTargetEntity(); + Task.builder() + .execute(() -> { + if (service.teleportationMap.containsKey(player.getName())) { + service.teleport(player.getName(), service.teleportationMap.get(player.getName()), true); + } + }) + .delay(10, TimeUnit.SECONDS) + .submit(plugin); + } + + @Listener + public void onPlayerLeave(ClientConnectionEvent.Disconnect event) { + Player player = event.getTargetEntity(); + service.teleportationMap.remove(player.getName()); + } +} diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/module/SpongeModule.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/module/SpongeModule.java index 0c40093d..1f084116 100644 --- a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/module/SpongeModule.java +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/module/SpongeModule.java @@ -29,6 +29,7 @@ import org.anvilpowered.catalyst.sponge.service.SpongeEventService; import org.anvilpowered.catalyst.sponge.service.SpongeExecuteCommandService; import org.anvilpowered.catalyst.sponge.service.SpongeLoggerService; +import org.anvilpowered.catalyst.sponge.service.SpongeTeleportationService; import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.entity.living.player.User; diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/plugin/CatalystSponge.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/plugin/CatalystSponge.java index 18f525f7..83bc5286 100644 --- a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/plugin/CatalystSponge.java +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/plugin/CatalystSponge.java @@ -23,7 +23,9 @@ import org.anvilpowered.catalyst.common.plugin.Catalyst; import org.anvilpowered.catalyst.common.plugin.CatalystPluginInfo; import org.anvilpowered.catalyst.sponge.command.CatalystSpongeCommandNode; +import org.anvilpowered.catalyst.sponge.listener.SpongeListener; import org.anvilpowered.catalyst.sponge.module.SpongeModule; +import org.spongepowered.api.Sponge; import org.spongepowered.api.plugin.Dependency; import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; @@ -47,4 +49,10 @@ public CatalystSponge(Injector rootInjector) { protected void whenReady(Environment environment) { super.whenReady(environment); } + + @Override + protected void applyToBuilder(Environment.Builder builder) { + builder.addEarlyServices(SpongeListener.class, t -> + Sponge.getEventManager().registerListeners(this, t)); + } } diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/service/SpongeExecuteCommandService.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/service/SpongeExecuteCommandService.java index fe709c6f..e79a4905 100644 --- a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/service/SpongeExecuteCommandService.java +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/service/SpongeExecuteCommandService.java @@ -19,16 +19,23 @@ import com.google.inject.Inject; import org.anvilpowered.anvil.api.plugin.Plugin; +import org.anvilpowered.catalyst.api.service.EventService; import org.anvilpowered.catalyst.api.service.ExecuteCommandService; import org.spongepowered.api.Sponge; import org.spongepowered.api.command.CommandSource; +import org.spongepowered.api.event.Event; import org.spongepowered.api.scheduler.Task; +import java.util.concurrent.CompletableFuture; + public class SpongeExecuteCommandService implements ExecuteCommandService { @Inject Plugin plugin; + @Inject + private EventService eventService; + @Override public void executeCommand(CommandSource commandSource, String command) { Task.builder() @@ -39,8 +46,10 @@ public void executeCommand(CommandSource commandSource, String command) { } @Override - public void executeAsConsole(String command) { - executeCommand(Sponge.getServer().getConsole(), command); + public CompletableFuture executeAsConsole(String command) { + return CompletableFuture.runAsync(() -> { + eventService.run(() -> executeCommand(Sponge.getServer().getConsole(), command)); + }); } @Override diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/service/SpongeTeleportationService.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/service/SpongeTeleportationService.java new file mode 100644 index 00000000..788e6e02 --- /dev/null +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/service/SpongeTeleportationService.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 STG_Allen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.anvilpowered.catalyst.sponge.service; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.anvilpowered.anvil.api.util.UserService; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.entity.living.player.User; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +@Singleton +public class SpongeTeleportationService { + + public Map teleportationMap = new HashMap<>(); + + @Inject + private UserService userService; + + public void teleport(String sender, String recipient, boolean onSameServer) { + final Optional teleporter = userService.get(sender); + final Optional target = userService.get(recipient); + if (onSameServer && teleporter.isPresent() && target.isPresent()) { + target.flatMap(User::getWorldUniqueId) + .filter(uuid -> teleporter.get().setLocation(target.get().getPosition(), uuid)); + } else { + teleportationMap.put(sender, recipient); + } + } +} diff --git a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/CatalystCommandManager.java b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/CatalystCommandManager.java index b5be6a81..627e3d88 100644 --- a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/CatalystCommandManager.java +++ b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/CatalystCommandManager.java @@ -22,6 +22,7 @@ import com.google.inject.Singleton; import com.velocitypowered.api.proxy.ProxyServer; import org.anvilpowered.anvil.api.data.registry.Registry; +import org.anvilpowered.anvil.api.redis.RedisService; @Singleton public class CatalystCommandManager { @@ -77,6 +78,8 @@ public class CatalystCommandManager { private ExceptionCommand exceptionCommand; @Inject private IgnoreCommand ignoreCommand; + @Inject + private TeleportCommand teleportCommand; private boolean alreadyLoaded = false; @@ -136,5 +139,8 @@ public void registryLoaded() { proxyServer.getCommandManager().register( "ignore", ignoreCommand ); + proxyServer.getCommandManager().register( + "teleport", teleportCommand + ); } } diff --git a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/SendCommand.java b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/SendCommand.java index 9dd70403..ad6deb92 100644 --- a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/SendCommand.java +++ b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/SendCommand.java @@ -75,7 +75,10 @@ public void execute(CommandSource source, @NonNull String[] args) { TextComponent.of(player.get().getUsername() + " is already connected to that server."))); } else { - Player sender = (Player) source; + String sender = "Console"; + if(source instanceof Player) { + sender = ((Player) source).getUsername(); + } source.sendMessage( pluginInfo.getPrefix().append( TextComponent.of("sending " + player.get().getUsername() @@ -84,7 +87,7 @@ public void execute(CommandSource source, @NonNull String[] args) { pluginInfo.getPrefix().append( TextComponent.of("you have been sent to " + server.get().getServerInfo().getName() - + " by " + sender.getUsername()))); + + " by " + sender))); player.get().createConnectionRequest(server.get()).fireAndForget(); } } else { diff --git a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportCommand.java b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportCommand.java new file mode 100644 index 00000000..84bdbf6f --- /dev/null +++ b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportCommand.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2020 STG_Allen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.anvilpowered.catalyst.velocity.command; + +import com.google.inject.Inject; +import com.velocitypowered.api.command.Command; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import org.anvilpowered.catalyst.api.service.CrossServerTeleportationHelper; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class TeleportCommand implements Command { + + @Inject + private CrossServerTeleportationHelper crossServerTeleportationHelper; + + @Inject + private ProxyServer proxyServer; + + @Override + public void execute(CommandSource source, @NonNull String[] args) { + if (args.length != 0) { + Player player = (Player) source; + crossServerTeleportationHelper.teleport(player.getUsername(), args[0]); + } + } + + @Override + public List suggest(CommandSource src, String[] args) { + if (args.length == 1) { + return proxyServer.matchPlayer(args[0]) + .stream().map(Player::getUsername).collect(Collectors.toList()); + } + return Collections.emptyList(); + } +} diff --git a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/listener/VelocityListener.java b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/listener/VelocityListener.java index 469ace41..c82f4e9b 100644 --- a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/listener/VelocityListener.java +++ b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/listener/VelocityListener.java @@ -187,6 +187,8 @@ public void onServerListPing(ProxyPingEvent proxyPingEvent) { if (serverPing.getFavicon().isPresent()) { builder.favicon(serverPing.getFavicon().get()); + } else if (proxyServer.getConfiguration().getFavicon().isPresent()) { + builder.favicon(proxyServer.getConfiguration().getFavicon().get()); } builder.onlinePlayers(proxyServer.getPlayerCount()); diff --git a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/service/VelocityExecuteCommandService.java b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/service/VelocityExecuteCommandService.java index cfcb880f..87edbe37 100644 --- a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/service/VelocityExecuteCommandService.java +++ b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/service/VelocityExecuteCommandService.java @@ -25,6 +25,8 @@ import org.anvilpowered.catalyst.velocity.discord.DiscordCommandSource; import org.slf4j.Logger; +import java.util.concurrent.CompletableFuture; + public class VelocityExecuteCommandService implements ExecuteCommandService { @Inject @@ -45,8 +47,11 @@ public void executeCommand(CommandSource source, String command) { } @Override - public void executeAsConsole(String command) { - proxyServer.getCommandManager().execute(proxyServer.getConsoleCommandSource(), command); + public CompletableFuture executeAsConsole(String command) { + return CompletableFuture.runAsync(() ->{ + System.out.println("Sending Command " + command); + proxyServer.getCommandManager().execute(proxyServer.getConsoleCommandSource(), command); + }); } @Override From 2e07c261c2506c80526a6c7376574319cd341f67 Mon Sep 17 00:00:00 2001 From: STG-Allen Date: Tue, 21 Apr 2020 15:05:27 -0400 Subject: [PATCH 2/2] Got teleportation requesting working --- .../catalyst/api/plugin/PluginMessages.java | 8 +++ .../CrossServerTeleportationHelper.java | 4 +- .../command/CommonTeleportAcceptCommand.java | 62 ++++++++++++++++++- ...java => CommonTeleportRequestCommand.java} | 29 ++++++--- .../common/plugin/CatalystPluginMessages.java | 32 ++++++++++ .../CommonCrossServerTeleportationHelper.java | 13 +++- .../sponge/listener/SpongeListener.java | 1 + .../command/CatalystCommandManager.java | 7 ++- .../command/TeleportAcceptCommand.java | 41 ++++++++++++ .../velocity/command/TeleportCommand.java | 14 ++--- 10 files changed, 189 insertions(+), 22 deletions(-) rename common/src/main/java/org/anvilpowered/catalyst/common/command/{CommonTeleportCommand.java => CommonTeleportRequestCommand.java} (69%) create mode 100644 velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportAcceptCommand.java diff --git a/api/src/main/java/org/anvilpowered/catalyst/api/plugin/PluginMessages.java b/api/src/main/java/org/anvilpowered/catalyst/api/plugin/PluginMessages.java index 91a1451d..fe09b81f 100644 --- a/api/src/main/java/org/anvilpowered/catalyst/api/plugin/PluginMessages.java +++ b/api/src/main/java/org/anvilpowered/catalyst/api/plugin/PluginMessages.java @@ -57,6 +57,14 @@ public interface PluginMessages { TString getTeleportToSelf(); + TString getTeleportRequestPending(String targetUserName); + + TString getTeleportRequestAccepted(String targetUserName); + + TString getSourceAcceptedTeleport(String targetUserName); + + TString getNoPendingRequests(); + TString getIncompatibleServerVersion(); TString getExistingSwear(String swear); diff --git a/api/src/main/java/org/anvilpowered/catalyst/api/service/CrossServerTeleportationHelper.java b/api/src/main/java/org/anvilpowered/catalyst/api/service/CrossServerTeleportationHelper.java index 73464c41..f897a1fe 100644 --- a/api/src/main/java/org/anvilpowered/catalyst/api/service/CrossServerTeleportationHelper.java +++ b/api/src/main/java/org/anvilpowered/catalyst/api/service/CrossServerTeleportationHelper.java @@ -17,6 +17,8 @@ package org.anvilpowered.catalyst.api.service; +import java.util.Optional; + public interface CrossServerTeleportationHelper { void teleport(String sender, String recipient); @@ -27,7 +29,7 @@ public interface CrossServerTeleportationHelper { void insertIntoTeleportationMap(String sender, String recipient); - String getSender(String recipient); + Optional getRequestingPlayerName(String recipient); String getRecipient(String sender); diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportAcceptCommand.java b/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportAcceptCommand.java index 85797705..b4a16ff7 100644 --- a/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportAcceptCommand.java +++ b/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportAcceptCommand.java @@ -17,5 +17,65 @@ package org.anvilpowered.catalyst.common.command; -public class CommonTeleportAcceptCommand { +import com.google.inject.Inject; +import org.anvilpowered.anvil.api.data.registry.Registry; +import org.anvilpowered.anvil.api.util.PermissionService; +import org.anvilpowered.anvil.api.util.TextService; +import org.anvilpowered.anvil.api.util.UserService; +import org.anvilpowered.catalyst.api.data.key.CatalystKeys; +import org.anvilpowered.catalyst.api.plugin.PluginMessages; +import org.anvilpowered.catalyst.api.service.CrossServerTeleportationHelper; + +import java.util.Optional; + +public class CommonTeleportAcceptCommand< + TString, + TUser, + TPlayer, + TCommandSource, + TSubject> { + + @Inject + private TextService textService; + + @Inject + private PluginMessages pluginMessages; + + @Inject + private PermissionService permissionService; + + @Inject + private Registry registry; + + @Inject + private CrossServerTeleportationHelper teleportationHelper; + + @Inject + private UserService userService; + + public void execute(TCommandSource source, TSubject subject, String[] args) { + if (permissionService.hasPermission(subject, registry.getOrDefault(CatalystKeys.TELEPORT_PERMISSION))) { + if (teleportationHelper.getRequestingPlayerName(userService.getUserName((TUser) source)).isPresent()) { + Optional requestingPlayerName = teleportationHelper.getRequestingPlayerName(userService.getUserName((TUser) source)); + if (userService.get(requestingPlayerName.get()).isPresent()) { + System.out.println(requestingPlayerName.get()); + TUser requesting = userService.get(requestingPlayerName.get()).get(); + textService.send( + pluginMessages.getTeleportRequestAccepted(userService.getUserName((TUser) source)), + (TCommandSource) requesting); + textService.send( + pluginMessages.getSourceAcceptedTeleport(requestingPlayerName.get()), + source + ); + teleportationHelper.teleport(requestingPlayerName.get(), userService.getUserName((TUser) source)); + } else { + textService.send(pluginMessages.offlineOrInvalidPlayer(), source); + } + } else { + textService.send(pluginMessages.getNoPendingRequests(), source); + } + } else { + textService.send(pluginMessages.getNoPermission(), source); + } + } } diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportCommand.java b/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportRequestCommand.java similarity index 69% rename from common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportCommand.java rename to common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportRequestCommand.java index a5cc4cb6..2b4cf9fb 100644 --- a/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportCommand.java +++ b/common/src/main/java/org/anvilpowered/catalyst/common/command/CommonTeleportRequestCommand.java @@ -28,7 +28,7 @@ import java.util.Optional; -public class CommonTeleportCommand< +public class CommonTeleportRequestCommand< TString, TUser, TPlayer, @@ -55,12 +55,22 @@ public class CommonTeleportCommand< public void execute(TCommandSource source, TSubject subject, String[] args) { if (permissionService.hasPermission(subject, registry.getOrDefault(CatalystKeys.TELEPORT_PERMISSION))) { - if (teleportationHelper.isPresentInMap(userService.getUserName((TUser) source)) - && teleportationHelper.getRecipient(userService.getUserName((TUser) source)).equalsIgnoreCase(args[0])) { - //TODO Send - } else { - Optional recipient = userService.get(args[0]); - if (recipient.isPresent()) { + if (args.length == 0) { + textService.send(pluginMessages.getNotEnoughArgs(), source); + return; + } + Optional recipient = userService.get(args[0]); + if (recipient.isPresent()) { + if (userService.getUserName(recipient.get()).equalsIgnoreCase(userService.getUserName((TUser) source))) { + textService.send(pluginMessages.getTeleportToSelf(), source); + return; + } + if (teleportationHelper.isPresentInMap(userService.getUserName((TUser) source)) + && teleportationHelper.getRecipient(userService.getUserName((TUser) source)).equalsIgnoreCase(args[0])) { + textService.send(pluginMessages.getTeleportRequestPending(args[0]), source); + } else { + //insert them into the map + //source -> recipient teleportationHelper.insertIntoTeleportationMap(userService.getUserName((TUser) source), userService.getUserName(recipient.get())); //Send message to target textService.send( @@ -72,7 +82,12 @@ public void execute(TCommandSource source, TSubject subject, String[] args) { source ); } + } else { + textService.send(pluginMessages.offlineOrInvalidPlayer(), source); + return; } + } else { + textService.send(pluginMessages.getNoPermission(), source); } } } diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/plugin/CatalystPluginMessages.java b/common/src/main/java/org/anvilpowered/catalyst/common/plugin/CatalystPluginMessages.java index ffdc59b7..76439c21 100644 --- a/common/src/main/java/org/anvilpowered/catalyst/common/plugin/CatalystPluginMessages.java +++ b/common/src/main/java/org/anvilpowered/catalyst/common/plugin/CatalystPluginMessages.java @@ -186,6 +186,38 @@ public TString getTeleportToSelf() { .build(); } + @Override + public TString getTeleportRequestPending(String targetUserName) { + return textService.builder() + .append(pluginInfo.getPrefix()) + .yellow().append("You have a pending request to " + targetUserName) + .build(); + } + + @Override + public TString getTeleportRequestAccepted(String sourceUserNamae) { + return textService.builder() + .append(pluginInfo.getPrefix()) + .green().append(sourceUserNamae + " has accepted your teleportation request.") + .build(); + } + + @Override + public TString getSourceAcceptedTeleport(String targetUserName) { + return textService.builder() + .append(pluginInfo.getPrefix()) + .green().append("Accepted " + targetUserName + "'s teleport request.") + .build(); + } + + @Override + public TString getNoPendingRequests() { + return textService.builder() + .append(pluginInfo.getPrefix()) + .yellow().append("You don't have any pending teleportation requests!") + .build(); + } + @Override public TString getIncompatibleServerVersion() { return textService.builder() diff --git a/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonCrossServerTeleportationHelper.java b/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonCrossServerTeleportationHelper.java index 6f2321f4..3006b2b3 100644 --- a/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonCrossServerTeleportationHelper.java +++ b/common/src/main/java/org/anvilpowered/catalyst/common/service/CommonCrossServerTeleportationHelper.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicReference; @@ -52,6 +53,7 @@ public void teleport(String sender, String recipient) { sendTeleportMessage(currentServerService.getName(recipient).get(), sender, recipient); executeCommandService.executeAsConsole("send " + sender + " " + currentServerService.getName(recipient).get()); } + teleportationAcceptMap.remove(sender); } } @@ -75,19 +77,24 @@ public void insertIntoTeleportationMap(String sender, String recipient) { teleportationAcceptMap.put(sender, recipient); } + //To be run when the player being teleported to does /tpaccept @Override - public String getSender(String recipient) { + public Optional getRequestingPlayerName(String recipient) { AtomicReference s = new AtomicReference<>("null"); teleportationAcceptMap.forEach((k, v) -> { if (v.equalsIgnoreCase(recipient)) { - s.set(v); + s.set(k); } }); - return s.get(); + if(s.get().equalsIgnoreCase("null")) { + return Optional.empty(); + } + return Optional.of(s.get()); } @Override public String getRecipient(String sender) { + System.out.println(sender); return teleportationAcceptMap.get(sender); } diff --git a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeListener.java b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeListener.java index a2b63289..5b0d36b5 100644 --- a/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeListener.java +++ b/sponge/src/main/java/org/anvilpowered/catalyst/sponge/listener/SpongeListener.java @@ -43,6 +43,7 @@ public void onPlayerJoin(ClientConnectionEvent.Join event) { .execute(() -> { if (service.teleportationMap.containsKey(player.getName())) { service.teleport(player.getName(), service.teleportationMap.get(player.getName()), true); + service.teleportationMap.remove(player.getName()); } }) .delay(10, TimeUnit.SECONDS) diff --git a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/CatalystCommandManager.java b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/CatalystCommandManager.java index 627e3d88..160c9010 100644 --- a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/CatalystCommandManager.java +++ b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/CatalystCommandManager.java @@ -22,7 +22,6 @@ import com.google.inject.Singleton; import com.velocitypowered.api.proxy.ProxyServer; import org.anvilpowered.anvil.api.data.registry.Registry; -import org.anvilpowered.anvil.api.redis.RedisService; @Singleton public class CatalystCommandManager { @@ -81,6 +80,9 @@ public class CatalystCommandManager { @Inject private TeleportCommand teleportCommand; + @Inject + private TeleportAcceptCommand teleportAcceptCommand; + private boolean alreadyLoaded = false; @Inject @@ -142,5 +144,8 @@ public void registryLoaded() { proxyServer.getCommandManager().register( "teleport", teleportCommand ); + proxyServer.getCommandManager().register( + "teleportaccept", teleportAcceptCommand + ); } } diff --git a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportAcceptCommand.java b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportAcceptCommand.java new file mode 100644 index 00000000..6a9b1d4d --- /dev/null +++ b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportAcceptCommand.java @@ -0,0 +1,41 @@ +package org.anvilpowered.catalyst.velocity.command; + +import com.google.inject.Inject; +import com.velocitypowered.api.command.Command; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.permission.PermissionSubject; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import net.kyori.text.TextComponent; +import org.anvilpowered.catalyst.common.command.CommonTeleportAcceptCommand; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +public class TeleportAcceptCommand extends CommonTeleportAcceptCommand< + TextComponent, + Player, + Player, + CommandSource, + PermissionSubject> +implements Command { + + @Inject + private ProxyServer proxyServer; + + @Override + public void execute(CommandSource source, @NonNull String[] args) { + execute(source, source, args); + } + + @Override + public List suggest(CommandSource src, String[] args) { + if (args.length == 1) { + return proxyServer.matchPlayer(args[0]) + .stream().map(Player::getUsername).collect(Collectors.toList()); + } + return Collections.emptyList(); + } +} diff --git a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportCommand.java b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportCommand.java index 84bdbf6f..54c6ad4d 100644 --- a/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportCommand.java +++ b/velocity/src/main/java/org/anvilpowered/catalyst/velocity/command/TeleportCommand.java @@ -20,29 +20,25 @@ import com.google.inject.Inject; import com.velocitypowered.api.command.Command; import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.permission.PermissionSubject; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; -import org.anvilpowered.catalyst.api.service.CrossServerTeleportationHelper; +import net.kyori.text.TextComponent; +import org.anvilpowered.catalyst.common.command.CommonTeleportCommand; import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; -public class TeleportCommand implements Command { - - @Inject - private CrossServerTeleportationHelper crossServerTeleportationHelper; +public class TeleportCommand extends CommonTeleportCommand implements Command { @Inject private ProxyServer proxyServer; @Override public void execute(CommandSource source, @NonNull String[] args) { - if (args.length != 0) { - Player player = (Player) source; - crossServerTeleportationHelper.teleport(player.getUsername(), args[0]); - } + execute(source, source, args); } @Override