Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit 444851e

Browse files
authored
feat: Add channels API (#183)
* Updated API * Singleton API access * Remove unnecessary cast
1 parent b169dc7 commit 444851e

File tree

8 files changed

+261
-0
lines changed

8 files changed

+261
-0
lines changed

bukkit/src/main/java/net/william278/huskchat/bukkit/BukkitHuskChat.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2424
import net.william278.desertwell.util.Version;
2525
import net.william278.huskchat.HuskChat;
26+
import net.william278.huskchat.HuskChatAPI;
2627
import net.william278.huskchat.bukkit.command.BukkitCommand;
2728
import net.william278.huskchat.bukkit.event.BukkitEventDispatcher;
2829
import net.william278.huskchat.bukkit.listener.BukkitListener;
@@ -72,6 +73,7 @@ public static BukkitHuskChat getInstance() {
7273
public void onLoad() {
7374
// Set instance for easy cross-class referencing
7475
instance = this;
76+
BukkitHuskChatAPI.register(this);
7577
}
7678

7779
@Override
@@ -243,6 +245,11 @@ public void log(@NotNull Level level, @NotNull String message, @NotNull Throwabl
243245
getLogger().log(level, message);
244246
}
245247

248+
@Override
249+
public BukkitHuskChatAPI getAPI() {
250+
return BukkitHuskChatAPI.getInstance();
251+
}
252+
246253
@NotNull
247254
public BukkitAudiences getAudience() {
248255
return audiences;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of HuskChat, licensed under the Apache License 2.0.
3+
*
4+
* Copyright (c) William278 <will27528@gmail.com>
5+
* Copyright (c) contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package net.william278.huskchat.bukkit;
21+
22+
import net.william278.huskchat.HuskChat;
23+
import net.william278.huskchat.HuskChatAPI;
24+
import net.william278.huskchat.bukkit.player.BukkitPlayer;
25+
import org.bukkit.entity.Player;
26+
import org.jetbrains.annotations.ApiStatus;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
public class BukkitHuskChatAPI extends HuskChatAPI {
30+
public BukkitHuskChatAPI(HuskChat plugin) {
31+
super(plugin);
32+
}
33+
34+
public static BukkitHuskChatAPI getInstance() {
35+
return (BukkitHuskChatAPI) instance;
36+
}
37+
38+
/**
39+
* @hidden
40+
*/
41+
@ApiStatus.Internal
42+
public static void register(@NotNull BukkitHuskChat plugin) {
43+
HuskChatAPI.instance = new BukkitHuskChatAPI(plugin);
44+
}
45+
46+
/**
47+
* Adapts a platform-specific Player object to a cross-platform Player object
48+
* @param player Must be a platform-specific Player object, e.g. a Velocity Player
49+
* @return {@link BukkitPlayer}
50+
*/
51+
public BukkitPlayer adaptPlayer(@NotNull Player player) {
52+
return BukkitPlayer.adapt(player);
53+
}
54+
}

bungee/src/main/java/net/william278/huskchat/bungeecord/BungeeHuskChat.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import net.md_5.bungee.api.plugin.Plugin;
2727
import net.william278.desertwell.util.Version;
2828
import net.william278.huskchat.HuskChat;
29+
import net.william278.huskchat.HuskChatAPI;
2930
import net.william278.huskchat.bungeecord.command.BungeeCommand;
3031
import net.william278.huskchat.bungeecord.event.BungeeEventDispatcher;
3132
import net.william278.huskchat.bungeecord.getter.BungeePermsDataGetter;
@@ -72,6 +73,7 @@ public static BungeeHuskChat getInstance() {
7273
private PlayerCache playerCache;
7374
private List<PlaceholderReplacer> placeholders;
7475

76+
7577
@Override
7678
public void onLoad() {
7779
// Set instance for easy cross-class referencing
@@ -80,6 +82,7 @@ public void onLoad() {
8082
// Create the event dispatcher, register audiences
8183
eventDispatcher = new BungeeEventDispatcher(getProxy());
8284
audiences = BungeeAudiences.create(this);
85+
BungeeHuskChatAPI.register(this);
8386
}
8487

8588
@Override
@@ -283,4 +286,8 @@ public Optional<Player> findPlayer(@NotNull String username) {
283286
return optionalPlayer;
284287
}
285288

289+
@Override
290+
public BungeeHuskChatAPI getAPI() {
291+
return BungeeHuskChatAPI.getInstance();
292+
}
286293
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of HuskChat, licensed under the Apache License 2.0.
3+
*
4+
* Copyright (c) William278 <will27528@gmail.com>
5+
* Copyright (c) contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package net.william278.huskchat.bungeecord;
21+
22+
import net.md_5.bungee.api.connection.ProxiedPlayer;
23+
import net.william278.huskchat.HuskChat;
24+
import net.william278.huskchat.HuskChatAPI;
25+
import net.william278.huskchat.bungeecord.player.BungeePlayer;
26+
import org.jetbrains.annotations.ApiStatus;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
public class BungeeHuskChatAPI extends HuskChatAPI {
30+
public BungeeHuskChatAPI(HuskChat plugin) {
31+
super(plugin);
32+
}
33+
34+
public static BungeeHuskChatAPI getInstance() {
35+
return (BungeeHuskChatAPI) instance;
36+
}
37+
38+
/**
39+
* @hidden
40+
*/
41+
@ApiStatus.Internal
42+
public static void register(@NotNull BungeeHuskChat plugin) {
43+
HuskChatAPI.instance = new BungeeHuskChatAPI(plugin);
44+
}
45+
46+
/**
47+
* Adapts a platform-specific Player object to a cross-platform Player object
48+
* @param player Must be a platform-specific Player object, e.g. a Velocity Player
49+
* @return {@link BungeePlayer}
50+
*/
51+
public BungeePlayer adaptPlayer(@NotNull ProxiedPlayer player) {
52+
return BungeePlayer.adapt(player);
53+
}
54+
}

common/src/main/java/net/william278/huskchat/HuskChat.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,5 @@ default void checkForUpdates() {
157157

158158
void log(@NotNull Level level, @NotNull String message, @NotNull Throwable... throwable);
159159

160+
HuskChatAPI getAPI();
160161
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* This file is part of HuskChat, licensed under the Apache License 2.0.
3+
*
4+
* Copyright (c) William278 <will27528@gmail.com>
5+
* Copyright (c) contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package net.william278.huskchat;
21+
22+
import net.william278.huskchat.message.BroadcastMessage;
23+
import net.william278.huskchat.message.ChatMessage;
24+
import net.william278.huskchat.message.PrivateMessage;
25+
import net.william278.huskchat.player.Player;
26+
import org.jetbrains.annotations.NotNull;
27+
28+
import java.util.List;
29+
30+
@SuppressWarnings("unused")
31+
public class HuskChatAPI {
32+
protected static HuskChatAPI instance;
33+
protected final HuskChat plugin;
34+
35+
protected HuskChatAPI(@NotNull HuskChat plugin) {
36+
this.plugin = plugin;
37+
}
38+
39+
public static HuskChatAPI getInstance() {
40+
return instance;
41+
}
42+
43+
/**
44+
* Returns the player's current channel
45+
*/
46+
public String getPlayerChannel(@NotNull Player player) {
47+
return plugin.getPlayerCache().getPlayerChannel(player.getUuid());
48+
}
49+
50+
/**
51+
* Sets the player's channel
52+
*/
53+
public void setPlayerChannel(@NotNull Player player, @NotNull String channel) {
54+
plugin.getPlayerCache().setPlayerChannel(player.getUuid(), channel);
55+
}
56+
57+
/**
58+
* Sends a chat message on behalf of a player
59+
*/
60+
public void sendChatMessage(@NotNull String targetChannelId, @NotNull Player sender, @NotNull String message) {
61+
new ChatMessage(targetChannelId, sender, message, plugin).dispatch();
62+
}
63+
64+
/**
65+
* Sends a broadcast message
66+
*/
67+
public void sendBroadcastMessage(@NotNull Player sender, @NotNull String message) {
68+
new BroadcastMessage(sender, message, plugin).dispatch();
69+
}
70+
71+
/**
72+
* Sends a private message on behalf of a player
73+
*/
74+
public void sendPrivateMessage(@NotNull Player sender, @NotNull List<String> targetUsernames, @NotNull String message) {
75+
new PrivateMessage(sender, targetUsernames, message, plugin).dispatch();
76+
}
77+
}

velocity/src/main/java/net/william278/huskchat/velocity/VelocityHuskChat.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.kyori.adventure.audience.Audience;
3030
import net.william278.desertwell.util.Version;
3131
import net.william278.huskchat.HuskChat;
32+
import net.william278.huskchat.HuskChatAPI;
3233
import net.william278.huskchat.command.ShortcutCommand;
3334
import net.william278.huskchat.config.Locales;
3435
import net.william278.huskchat.config.Settings;
@@ -95,6 +96,8 @@ public VelocityHuskChat(@NotNull ProxyServer server, @NotNull org.slf4j.Logger l
9596
this.dataDirectory = dataDirectory;
9697
this.metrics = metrics;
9798
this.container = pluginContainer;
99+
100+
VelocityHuskChatAPI.register(this);
98101
}
99102

100103
@Subscribe
@@ -343,4 +346,8 @@ public void log(@NotNull Level level, @NotNull String message, @NotNull Throwabl
343346
}
344347
}
345348

349+
@Override
350+
public VelocityHuskChatAPI getAPI() {
351+
return VelocityHuskChatAPI.getInstance();
352+
}
346353
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of HuskChat, licensed under the Apache License 2.0.
3+
*
4+
* Copyright (c) William278 <will27528@gmail.com>
5+
* Copyright (c) contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package net.william278.huskchat.velocity;
21+
22+
import com.velocitypowered.api.proxy.Player;
23+
import net.william278.huskchat.HuskChat;
24+
import net.william278.huskchat.HuskChatAPI;
25+
import net.william278.huskchat.velocity.player.VelocityPlayer;
26+
import org.jetbrains.annotations.ApiStatus;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
public class VelocityHuskChatAPI extends HuskChatAPI {
30+
public VelocityHuskChatAPI(HuskChat plugin) {
31+
super(plugin);
32+
}
33+
34+
public static VelocityHuskChatAPI getInstance() {
35+
return (VelocityHuskChatAPI) instance;
36+
}
37+
38+
/**
39+
* @hidden
40+
*/
41+
@ApiStatus.Internal
42+
public static void register(@NotNull VelocityHuskChat plugin) {
43+
HuskChatAPI.instance = new VelocityHuskChatAPI(plugin);
44+
}
45+
46+
/**
47+
* Adapts a platform-specific Player object to a cross-platform Player object
48+
* @param player Must be a platform-specific Player object, e.g. a Velocity Player
49+
* @return {@link VelocityPlayer}
50+
*/
51+
public VelocityPlayer adaptPlayer(@NotNull Player player) {
52+
return VelocityPlayer.adapt(player);
53+
}
54+
}

0 commit comments

Comments
 (0)