Skip to content

Initial implementation of ChatService #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions anvil-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ plugins {

repositories {
mavenCentral()
maven { url 'https://repo.spongepowered.org/maven' }
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
implementation reflections
implementation javasisst
implementation guice
implementation configurate_core
implementation configurate_hocon
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Anvil - AnvilPowered
* Copyright (C) 2020 Cableguy20
*
* 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 <https://www.gnu.org/licenses/>.
*/

package org.anvilpowered.anvil.api.data.config;

import java.util.List;

public class Channel {

public String id;

public List<String> aliases;

public String prefix;

public boolean alwaysVisible;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Anvil - AnvilPowered
* Copyright (C) 2020 Cableguy20
*
* 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 <https://www.gnu.org/licenses/>.
*/

package org.anvilpowered.anvil.api.util;

import org.anvilpowered.anvil.api.data.config.Channel;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public interface ChatService<TString> {

TString createChannel(String channelId, String prefix, List<String> aliases, Boolean alwaysVisible);

//Return the prefix for a specified channel
Optional<String> getPrefixForChannel(String channelId);

//Return a channel for the specified channelId
Optional<Channel> getChannelFromId(String channelId);

//Change channels for a user
TString switchChannel(UUID userUUID, String channelId);

//return the current channel for a user
String getChannelIdForUser(UUID userUUID);

//Return a total count of users in a specified channel
int getUserCountFromChannel(String channelId);

//Return a list of users in a specified channel
TString getUsersInChannel(String channelId);

//Return a list of online players
String getPlayerList();

//Send a message to a specified channel
CompletableFuture<Void> sendMessageToChannel(String channelId, TString message);

//Send a message globally
CompletableFuture<Void> sendGlobalMessage(TString message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Anvil - AnvilPowered
* Copyright (C) 2020 Cableguy20
*
* 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 <https://www.gnu.org/licenses/>.
*/

package org.anvilpowered.anvil.common.util;

import org.anvilpowered.anvil.api.data.config.Channel;
import org.anvilpowered.anvil.api.data.key.Keys;
import org.anvilpowered.anvil.api.data.registry.Registry;
import org.anvilpowered.anvil.api.util.ChatService;
import org.anvilpowered.anvil.api.util.StringResult;
import org.anvilpowered.anvil.api.util.UserService;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Singleton
public class CommonChatService<TPlayer extends TCommandSource, TString, TCommandSource> implements ChatService<TString> {

@Inject
protected StringResult<TString, TCommandSource> stringResult;

@Inject
protected Registry registry;

@Inject
protected UserService<TPlayer, TPlayer> userService;

Map<UUID, String> channelMap = new HashMap<>();

@Override
public TString createChannel(String channelId, String prefix, List<String> aliases, Boolean alwaysVisible) {
Channel channel = new Channel();
channel.id = channelId;
channel.prefix = prefix;
channel.aliases = aliases;
channel.alwaysVisible = alwaysVisible;

return stringResult.success("Successfully created channel " + channelId);
}

@Override
public Optional<String> getPrefixForChannel(String channelId) {
return getChannelFromId(channelId).map(c -> c.prefix);
}

@Override
public Optional<Channel> getChannelFromId(String channelId) {
return registry.get(Keys.<List<Channel>>resolveUnsafe("CHANNEL_ID")).flatMap((List<Channel> channel) ->
channel.stream()
.filter(c -> c.id.equals(channelId))
.findAny());
}

@Override
public TString switchChannel(UUID userUUID, String channelId) {
if (getChannelFromId(channelId).isPresent()) {
channelMap.put(userUUID, channelId);
return stringResult.success("Connected to channel " + channelId);
}
return stringResult.fail("Failed to connect to channel " + channelId);
}

@Override
public String getChannelIdForUser(UUID userUUID) {
return channelMap.get(userUUID) == null ? registry.getOrDefault(Keys.resolveUnsafe("CHANNEL_DEFAULT")) : channelMap.get(userUUID);
}

@Override
public int getUserCountFromChannel(String channelId) {
return (int) userService.getOnlinePlayers()
.stream()
.filter(p -> getChannelIdForUser(userService.getUUID(p))
.equals(channelId)).count();
}

@Override
public TString getUsersInChannel(String channelId) {
List<String> channelUsersList = userService.getOnlinePlayers()
.stream()
.filter(p -> getChannelIdForUser(userService.getUUID(p)).equals(channelId))
.map(p -> userService.getUserName(p))
.collect(Collectors.toList());

return stringResult.builder()
.green().append("------------------- ")
.gold().append(channelId)
.green().append(" --------------------\n")
.append(String.join(", ", channelUsersList))
.build();
}

@Override
public String getPlayerList() {
return userService.getOnlinePlayers().stream().map(userService::getUserName).collect(Collectors.joining(", \n"));
}

@Override
public CompletableFuture<Void> sendMessageToChannel(String channelId, TString message) {
return CompletableFuture.runAsync(() -> userService.getOnlinePlayers().forEach(p -> {
if (getChannelIdForUser(userService.getUUID(p)).equals(channelId))
stringResult.send(message, p);
}));
}

@Override
public CompletableFuture<Void> sendGlobalMessage(TString message) {
return CompletableFuture.runAsync(() -> userService.getOnlinePlayers().forEach(p -> stringResult.send(message, p)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class AnvilCorePluginInfo<TString, TCommandSource> implements PluginInfo<
public static final String version = "$modVersion";
public static final String description = "A cross platform Minecraft plugin framework";
public static final String url = "https://github.com/AnvilPowered/Anvil";
public static final String[] authors = {"Cableguy20"};
public static final String[] authors = {"Cableguy20, STG_Allen"};
public static final String organizationName = "AnvilPowered";
public static final String buildDate = "$buildDate";
public TString pluginPrefix;
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
beanutils=commons-beanutils:commons-beanutils:1.9.2
bson=org.mongodb:bson:3.10.1
configurate_core=org.spongepowered:configurate-core:3.6
configurate_hocon=org.spongepowered:configurate-hocon:3.6
guava=com.google.guava:guava:28.1-jre
guice=com.google.inject:guice:4.1.0
h2=com.h2database:h2-mvstore:1.4.200
Expand Down
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Sat Feb 08 13:34:47 EST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME