Skip to content

Commit

Permalink
Rewrite to 0.2.0
Browse files Browse the repository at this point in the history
* No longer uses the Geyser API directly, as this caused classloader problems.
  * Requires `floodgate` to be installed instead.

* Should now work on standalone Spigot servers.
  • Loading branch information
eth-p committed Aug 24, 2022
1 parent c73c495 commit 6b0f0f2
Show file tree
Hide file tree
Showing 36 changed files with 1,279 additions and 643 deletions.
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
# clientcontext
A BungeeCord plugin that adds [LuckPerms](https://luckperms.net/) contexts for the client version and client type.
# ClientContext
A BungeeCord plugin that adds [LuckPerms](https://luckperms.net/) contexts for client info, allowing permissions to be added or removed depending on what kind of device the client is on.

## Installation
There are two ways to install ClientContext: standalone with Spigot, or in a BungeeCord network.

### Standalone
When you have [Geyser](https://geysermc.org/) installed as a plugin on a single Spigot or Paper server, you can add ClientContext alongside it under the `plugins` folder.

For a standalone installation, you must have have [floodgate](https://ci.opencollab.dev/job/GeyserMC/job/Floodgate/job/master/) installed as well.

### Networked
When using [Geyser](https://geysermc.org/) on a BungeeCord proxy (as well as [floodgate](https://ci.opencollab.dev/job/GeyserMC/job/Floodgate/job/master/)), contexts will be resolved on the proxy and sent to each of the Spigot servers through plugin messages. This requires installing ClientContext on both the proxy and each of the Spigot servers.

## Contexts
### client:type
This context specifies the type of Minecraft client being used.
Currently, only two types are supported: `java`, and `bedrock`

### client-version
This context specifies the version of Minecraft client that the player is using.
### client:version
This context specifies the version of Minecraft client that the player is using.
Two values will be assigned: an exact version (e.g. `1.2.1`), and a rough version (e.g. `1.2.x`).

### client-type
This context specifies the type of Minecraft client being used.
Currently, only two types are supported: `java`, and `bedrock` (through [Geyser](https://geysermc.org/) for Spigot)
### client:archetype
A device archetype for a client.
Possible values include: `computer`, `console`, `mobile`.

### client:device
> Bedrock clients only.
The device type of a Bedrock client:
See [here](https://github.com/GeyserMC/Geyser/blob/master/common/src/main/java/org/geysermc/floodgate/util/DeviceOs.java) for a list of values, e.g. `ios`, `nx`, `google`, etc.

### client:controls
> Bedrock clients only.
The control type for a client:
See [here](https://github.com/GeyserMC/Geyser/blob/master/common/src/main/java/org/geysermc/floodgate/util/InputMode.java) for a list of values, e.g
`keyboard_mouse`, `touch`, `controller`, etc.
143 changes: 0 additions & 143 deletions bukkit/src/main/java/dev/ethp/clientcontext/BukkitPlugin.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.ethp.clientcontext.bukkit;

import dev.ethp.clientcontext.PlatformAbstraction;
import dev.ethp.clientcontext.util.ClientVersion;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.UUID;

/**
* Implementation of platform abstraction for a Bukkit/Spigot/Paper server.
*/
@SuppressWarnings("FieldCanBeLocal")
public final class BukkitPlatform implements PlatformAbstraction<Player> {
private final JavaPlugin plugin;
private ClientVersion version;

public BukkitPlatform(@NotNull JavaPlugin plugin) {
this.plugin = plugin;

// Parse the server version string.
String[] versionParts = this.plugin.getServer().getBukkitVersion().split("[-_]")[0].split("\\.");
int versionMajor = Integer.parseInt(versionParts[0]);
int versionMinor = versionParts.length < 2 ? 0 : Integer.parseInt(versionParts[1]);
int versionPatch = versionParts.length < 3 ? 0 : Integer.parseInt(versionParts[2]);
this.version = new ClientVersion(versionMajor, versionMinor, versionPatch);
}

@Override
public @NotNull UUID getUniqueId(@NotNull Player player) {
return player.getUniqueId();
}

@Override
public @NotNull Optional<ClientVersion> getPlayerClientVersion(@NotNull Player player) {
return Optional.of(this.version);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.ethp.clientcontext.bukkit;

import dev.ethp.clientcontext.CommonPlugin;
import dev.ethp.clientcontext.Constants;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

/**
* Main class for the Bukkit plugin.
*/
public class BukkitPlugin extends JavaPlugin {
private BukkitPlatform platform;
private CommonPlugin<Player> common;
private ContextSyncReceiver sync;

@Override
public void onEnable() {
// Initialize everything.
this.platform = new BukkitPlatform(this);
this.common = new CommonPlugin<>(getLogger(), this.platform);

// Register contexts.
if (BungeeUtil.isRunningUnderBungee()) {
this.sync = new ContextSyncReceiver(this);
this.common.register(this.sync);
getLogger().info("Running under a BungeeCord network; contexts will be synced from proxy for accuracy.");
getLogger().info("If you do not have the ClientContext plugin installed on the proxy, please install it.");
} else {
this.common.registerDefaults();
}

// Register plugin messages.
if (this.sync != null) {
getServer().getPluginManager().registerEvents(this.sync, this);
getServer().getMessenger().registerIncomingPluginChannel(this, Constants.CHANNEL, this.sync);
getServer().getMessenger().registerOutgoingPluginChannel(this, Constants.CHANNEL);
}

// Sync all client info.
if (this.sync != null) {
this.sync.requestContext();
}
}

public void onDisable() {
this.common.unregister();

getServer().getMessenger().unregisterIncomingPluginChannel(this);
getServer().getMessenger().unregisterOutgoingPluginChannel(this);
}

}
29 changes: 29 additions & 0 deletions bukkit/src/main/java/dev/ethp/clientcontext/bukkit/BungeeUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.ethp.clientcontext.bukkit;

import java.lang.reflect.Field;

/**
* Utilities for working inside a BungeeCord network.
*/
public class BungeeUtil {

/**
* Check to see if the server is running under BungeeCord.
*
* @return True if the spigot.yml file is configured to run under under BungeeCord.
*/
public static boolean isRunningUnderBungee() {
// Try finding out directly through the class that contains the config:
// https://hub.spigotmc.org/stash/projects/SPIGOT/repos/spigot/browse/CraftBukkit-Patches/0030-BungeeCord-Support.patch?until=450dcaa86efd759674bbdeae0f6a37c97977618e&untilPath=CraftBukkit-Patches%2F0030-BungeeCord-Support.patch#215
try {
Class<?> config = BungeeUtil.class.getClassLoader().loadClass("org.spigotmc.SpigotConfig");
Field bungee = config.getField("bungee");
return (Boolean) bungee.get(null);
} catch (ReflectiveOperationException ignored) {
}

// Not running a Spigot server?
return false;
}

}
Loading

0 comments on commit 6b0f0f2

Please sign in to comment.