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

Commit

Permalink
feat: Add channels API (#183)
Browse files Browse the repository at this point in the history
* Updated API

* Singleton API access

* Remove unnecessary cast
  • Loading branch information
TrueWinter authored Feb 7, 2024
1 parent b169dc7 commit 444851e
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.william278.desertwell.util.Version;
import net.william278.huskchat.HuskChat;
import net.william278.huskchat.HuskChatAPI;
import net.william278.huskchat.bukkit.command.BukkitCommand;
import net.william278.huskchat.bukkit.event.BukkitEventDispatcher;
import net.william278.huskchat.bukkit.listener.BukkitListener;
Expand Down Expand Up @@ -72,6 +73,7 @@ public static BukkitHuskChat getInstance() {
public void onLoad() {
// Set instance for easy cross-class referencing
instance = this;
BukkitHuskChatAPI.register(this);
}

@Override
Expand Down Expand Up @@ -243,6 +245,11 @@ public void log(@NotNull Level level, @NotNull String message, @NotNull Throwabl
getLogger().log(level, message);
}

@Override
public BukkitHuskChatAPI getAPI() {
return BukkitHuskChatAPI.getInstance();
}

@NotNull
public BukkitAudiences getAudience() {
return audiences;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file is part of HuskChat, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <will27528@gmail.com>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.william278.huskchat.bukkit;

import net.william278.huskchat.HuskChat;
import net.william278.huskchat.HuskChatAPI;
import net.william278.huskchat.bukkit.player.BukkitPlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

public class BukkitHuskChatAPI extends HuskChatAPI {
public BukkitHuskChatAPI(HuskChat plugin) {
super(plugin);
}

public static BukkitHuskChatAPI getInstance() {
return (BukkitHuskChatAPI) instance;
}

/**
* @hidden
*/
@ApiStatus.Internal
public static void register(@NotNull BukkitHuskChat plugin) {
HuskChatAPI.instance = new BukkitHuskChatAPI(plugin);
}

/**
* Adapts a platform-specific Player object to a cross-platform Player object
* @param player Must be a platform-specific Player object, e.g. a Velocity Player
* @return {@link BukkitPlayer}
*/
public BukkitPlayer adaptPlayer(@NotNull Player player) {
return BukkitPlayer.adapt(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.md_5.bungee.api.plugin.Plugin;
import net.william278.desertwell.util.Version;
import net.william278.huskchat.HuskChat;
import net.william278.huskchat.HuskChatAPI;
import net.william278.huskchat.bungeecord.command.BungeeCommand;
import net.william278.huskchat.bungeecord.event.BungeeEventDispatcher;
import net.william278.huskchat.bungeecord.getter.BungeePermsDataGetter;
Expand Down Expand Up @@ -72,6 +73,7 @@ public static BungeeHuskChat getInstance() {
private PlayerCache playerCache;
private List<PlaceholderReplacer> placeholders;


@Override
public void onLoad() {
// Set instance for easy cross-class referencing
Expand All @@ -80,6 +82,7 @@ public void onLoad() {
// Create the event dispatcher, register audiences
eventDispatcher = new BungeeEventDispatcher(getProxy());
audiences = BungeeAudiences.create(this);
BungeeHuskChatAPI.register(this);
}

@Override
Expand Down Expand Up @@ -283,4 +286,8 @@ public Optional<Player> findPlayer(@NotNull String username) {
return optionalPlayer;
}

@Override
public BungeeHuskChatAPI getAPI() {
return BungeeHuskChatAPI.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file is part of HuskChat, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <will27528@gmail.com>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.william278.huskchat.bungeecord;

import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.william278.huskchat.HuskChat;
import net.william278.huskchat.HuskChatAPI;
import net.william278.huskchat.bungeecord.player.BungeePlayer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

public class BungeeHuskChatAPI extends HuskChatAPI {
public BungeeHuskChatAPI(HuskChat plugin) {
super(plugin);
}

public static BungeeHuskChatAPI getInstance() {
return (BungeeHuskChatAPI) instance;
}

/**
* @hidden
*/
@ApiStatus.Internal
public static void register(@NotNull BungeeHuskChat plugin) {
HuskChatAPI.instance = new BungeeHuskChatAPI(plugin);
}

/**
* Adapts a platform-specific Player object to a cross-platform Player object
* @param player Must be a platform-specific Player object, e.g. a Velocity Player
* @return {@link BungeePlayer}
*/
public BungeePlayer adaptPlayer(@NotNull ProxiedPlayer player) {
return BungeePlayer.adapt(player);
}
}
1 change: 1 addition & 0 deletions common/src/main/java/net/william278/huskchat/HuskChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,5 @@ default void checkForUpdates() {

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

HuskChatAPI getAPI();
}
77 changes: 77 additions & 0 deletions common/src/main/java/net/william278/huskchat/HuskChatAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* This file is part of HuskChat, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <will27528@gmail.com>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.william278.huskchat;

import net.william278.huskchat.message.BroadcastMessage;
import net.william278.huskchat.message.ChatMessage;
import net.william278.huskchat.message.PrivateMessage;
import net.william278.huskchat.player.Player;
import org.jetbrains.annotations.NotNull;

import java.util.List;

@SuppressWarnings("unused")
public class HuskChatAPI {
protected static HuskChatAPI instance;
protected final HuskChat plugin;

protected HuskChatAPI(@NotNull HuskChat plugin) {
this.plugin = plugin;
}

public static HuskChatAPI getInstance() {
return instance;
}

/**
* Returns the player's current channel
*/
public String getPlayerChannel(@NotNull Player player) {
return plugin.getPlayerCache().getPlayerChannel(player.getUuid());
}

/**
* Sets the player's channel
*/
public void setPlayerChannel(@NotNull Player player, @NotNull String channel) {
plugin.getPlayerCache().setPlayerChannel(player.getUuid(), channel);
}

/**
* Sends a chat message on behalf of a player
*/
public void sendChatMessage(@NotNull String targetChannelId, @NotNull Player sender, @NotNull String message) {
new ChatMessage(targetChannelId, sender, message, plugin).dispatch();
}

/**
* Sends a broadcast message
*/
public void sendBroadcastMessage(@NotNull Player sender, @NotNull String message) {
new BroadcastMessage(sender, message, plugin).dispatch();
}

/**
* Sends a private message on behalf of a player
*/
public void sendPrivateMessage(@NotNull Player sender, @NotNull List<String> targetUsernames, @NotNull String message) {
new PrivateMessage(sender, targetUsernames, message, plugin).dispatch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.kyori.adventure.audience.Audience;
import net.william278.desertwell.util.Version;
import net.william278.huskchat.HuskChat;
import net.william278.huskchat.HuskChatAPI;
import net.william278.huskchat.command.ShortcutCommand;
import net.william278.huskchat.config.Locales;
import net.william278.huskchat.config.Settings;
Expand Down Expand Up @@ -95,6 +96,8 @@ public VelocityHuskChat(@NotNull ProxyServer server, @NotNull org.slf4j.Logger l
this.dataDirectory = dataDirectory;
this.metrics = metrics;
this.container = pluginContainer;

VelocityHuskChatAPI.register(this);
}

@Subscribe
Expand Down Expand Up @@ -343,4 +346,8 @@ public void log(@NotNull Level level, @NotNull String message, @NotNull Throwabl
}
}

@Override
public VelocityHuskChatAPI getAPI() {
return VelocityHuskChatAPI.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file is part of HuskChat, licensed under the Apache License 2.0.
*
* Copyright (c) William278 <will27528@gmail.com>
* Copyright (c) contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.william278.huskchat.velocity;

import com.velocitypowered.api.proxy.Player;
import net.william278.huskchat.HuskChat;
import net.william278.huskchat.HuskChatAPI;
import net.william278.huskchat.velocity.player.VelocityPlayer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

public class VelocityHuskChatAPI extends HuskChatAPI {
public VelocityHuskChatAPI(HuskChat plugin) {
super(plugin);
}

public static VelocityHuskChatAPI getInstance() {
return (VelocityHuskChatAPI) instance;
}

/**
* @hidden
*/
@ApiStatus.Internal
public static void register(@NotNull VelocityHuskChat plugin) {
HuskChatAPI.instance = new VelocityHuskChatAPI(plugin);
}

/**
* Adapts a platform-specific Player object to a cross-platform Player object
* @param player Must be a platform-specific Player object, e.g. a Velocity Player
* @return {@link VelocityPlayer}
*/
public VelocityPlayer adaptPlayer(@NotNull Player player) {
return VelocityPlayer.adapt(player);
}
}

0 comments on commit 444851e

Please sign in to comment.