Skip to content

Commit

Permalink
Added cross-platform console interfaces
Browse files Browse the repository at this point in the history
Similar to MCPlayer, MCConsole has been added, along with its respecting SpigotConsole and VelocityConsole variants.
  • Loading branch information
xDec0de committed Sep 7, 2024
1 parent c91b09b commit 450d7a4
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.codersky.mcutils.spigot;

import net.codersky.mcutils.crossplatform.MCConsole;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.command.ConsoleCommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class SpigotConsole implements MCConsole<ConsoleCommandSender> {

private final ConsoleCommandSender handle;

SpigotConsole(@NotNull ConsoleCommandSender handle) {
this.handle = handle;
}

@NotNull
@Override
public ConsoleCommandSender getHandle() {
return handle;
}

@Override
public boolean sendMessage(@Nullable String message) {
if (message != null)
handle.sendMessage(message);
return true;
}

@Override
public boolean sendMessage(@Nullable Component message) {
if (message != null)
handle.sendMessage(LegacyComponentSerializer.legacySection().serialize(message));
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.codersky.mcutils.velocity;

import com.velocitypowered.api.proxy.ConsoleCommandSource;
import net.codersky.mcutils.crossplatform.MCConsole;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class VelocityConsole implements MCConsole<ConsoleCommandSource> {

private final ConsoleCommandSource handle;

VelocityConsole(@NotNull ConsoleCommandSource handle) {
this.handle = handle;
}

@NotNull
@Override
public ConsoleCommandSource getHandle() {
return handle;
}

@Override
public boolean sendMessage(@Nullable String message) {
if (message != null)
handle.sendMessage(Component.text(message));
return true;
}

@Override
public boolean sendMessage(@Nullable Component message) {
if (message != null)
handle.sendMessage(message);
return true;
}
}
13 changes: 12 additions & 1 deletion shared/src/main/java/net/codersky/mcutils/MCUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.codersky.mcutils;

import net.codersky.mcutils.cmd.MCCommand;
import net.codersky.mcutils.crossplatform.MCConsole;
import net.codersky.mcutils.crossplatform.player.PlayerProvider;
import net.codersky.mcutils.files.ConfigFileHolder;
import net.codersky.mcutils.files.FileHolder;
Expand Down Expand Up @@ -36,10 +37,20 @@ public PlayerProvider<?> getPlayerProvider() {
}

@Nullable
public MCPlayer getPlayer(@NotNull UUID uuid) {
public MCPlayer<?> getPlayer(@NotNull UUID uuid) {
return playerProvider.getPlayer(uuid);
}

/**
* Provides a cross-platform {@link MCConsole} instance.
*
* @return A cross-platform {@link MCConsole} instance.
*
* @since MCUtils 1.0.0
*/
@NotNull
public abstract MCConsole<?> getConsole();

/**
* Gets the version of MCUtils being used by this utility
* class.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.codersky.mcutils.crossplatform;

import org.jetbrains.annotations.NotNull;

/**
* Interface used to represent a server or proxy console.
*
* @param <T> The platform-specific console object
*
* @since MCUtils 1.0.0
*
* @author xDec0de_
*/
public interface MCConsole<T> extends MessageReceiver {

/**
* Gets the platform-specific object that is being wrapped by
* this {@link MCConsole} instance. This can be, for example, a
* Bukkit ConsoleCommandSender instance.
*
* @return The platform-specific object that is being wrapped by
* this {@link MCConsole} instance.
*
* @since MCUtils 1.0.0
*/
@NotNull
T getHandle();
}

0 comments on commit 450d7a4

Please sign in to comment.