Skip to content

Commit

Permalink
add Skript as softDepend
Browse files Browse the repository at this point in the history
  • Loading branch information
Anarchick committed Oct 23, 2024
1 parent 44753aa commit 8f255cb
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 15 deletions.
23 changes: 19 additions & 4 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
plugins {
id("java")
id("java-library")
id("maven-publish")
}

base {
archivesName = "ServerKeyboardBridge_Plugin"
}

group = 'fr.anarchick.skb'
version = '1.0.0'
version = '1.1.0'

def mcVersion = '1.20'
def mcSubVersion = '.4'
def skriptVersion = '2.9.2'

repositories {
maven {
Expand All @@ -21,7 +23,7 @@ repositories {
name = "sonatype"
url = "https://oss.sonatype.org/content/groups/public/"
}
//maven { url = "http://repo.choco.wtf/releases"; allowInsecureProtocol = true }
maven { url 'https://repo.skriptlang.org/releases' } // Skript
mavenCentral()
maven { url "https://jitpack.io" } // Github repositories
}
Expand All @@ -31,6 +33,10 @@ dependencies {
compileOnly group: 'io.papermc.paper', name: 'paper-api', version: mcVersion + mcSubVersion + '-R0.1-SNAPSHOT'
compileOnly group: 'io.netty', name: 'netty-buffer', version: '4.1.114.Final'
compileOnly group: 'io.netty', name: 'netty-handler', version: '4.1.114.Final'
compileOnly (group: 'com.github.SkriptLang', name: 'Skript', version: skriptVersion) {
exclude group: 'com.sk89q.worldguard', module: 'worldguard-legacy'
exclude group: 'net.milkbowl.vault', module: 'Vault'
}
//compileOnly fileTree(include: ['*.jar'], dir: 'libs')
}

Expand All @@ -57,13 +63,22 @@ processResources {

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

publishing {
publications {
myLib(MavenPublication) {
from components.java
}
}
}

tasks.register('export') {
group = "other"
description = "build gradle into external folder"
def userHome = System.getenv('USERPROFILE').replace('\\', '/')
def fromFile = 'build/libs/' + base.archivesName.get() + '-' + version + '.jar'
def destinations = [
userHome + '/Documents/minecraft/Universe/server/FloatingRealms/plugins'
userHome + '/Documents/minecraft/Universe/server/FloatingRealms/plugins',
userHome + '/Documents/minecraft/SkriptPacket/1.21.1/plugins'
]
doLast {
destinations.each {dest ->
Expand Down
2 changes: 1 addition & 1 deletion plugin/settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'ServerKeyboardBridge Plugin'
rootProject.name = 'ServerKeyboardBridge_Plugin'
34 changes: 24 additions & 10 deletions plugin/src/main/java/fr/anarchick/skb/ServerKeyboardBridge.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package fr.anarchick.skb;

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;
import fr.anarchick.skb.core.KeyEntry;
import fr.anarchick.skb.core.PluginChannels;
import fr.anarchick.skb.core.PluginMessageListeners;
import fr.anarchick.skb.event.KeyEvent;
import fr.anarchick.skb.event.SkbJoinEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
Expand All @@ -11,6 +14,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.Messenger;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -53,12 +57,29 @@ public static int getKeyEntriesSize() {
public void onEnable() {
INSTANCE = this;
LOGGER = getLogger();
@NotNull PluginManager pluginManager = Bukkit.getPluginManager();
Messenger messenger = Bukkit.getMessenger();
messenger.registerIncomingPluginChannel(this, PluginChannels.HANDSHAKE.getId(), pluginMessageListeners);
messenger.registerOutgoingPluginChannel(this, PluginChannels.LOAD_KEYS.getId());
messenger.registerIncomingPluginChannel(this, PluginChannels.KEY_EVENT.getId(), pluginMessageListeners);

getServer().getPluginManager().registerEvents(this, this);
pluginManager.registerEvents(this, this);

if (pluginManager.isPluginEnabled("Skript")) {
try {
if (Skript.isAcceptRegistrations()) {
SkriptAddon skriptAddon = Skript.registerAddon(this);
skriptAddon.loadClasses("fr.anarchick.skb", "skript");
info("Skript is detected.");
} else {
LOGGER.warning("Skript is not accepting registrations. SkriptAddon will not be loaded.");
}
} catch ( Exception e ) {
e.printStackTrace();
LOGGER.warning("Failed to load SkriptAddon");
return;
}
}

//test();

Expand Down Expand Up @@ -88,6 +109,7 @@ public boolean registerKey(KeyEntry keyEntry) {
LOGGER.warning("Key entries limit reached. Can't add : " + keyEntry);
return false;
}
KEY_ENTRIES.remove(keyEntry);
return KEY_ENTRIES.add(keyEntry);
}

Expand Down Expand Up @@ -145,16 +167,8 @@ private void test() {
/*
@EventHandler
public void keyPressed(KeyEvent event) {
Player player = event.getPlayer();
NamespacedKey namespacedKey = event.getKey();
boolean isPressed = event.isPressed();
boolean isInGui = event.isInGUI();
System.out.println("player = " + player);
System.out.println("namespacedKey = " + namespacedKey.asString());
System.out.println("isPressed = " + isPressed);
System.out.println("isInGui = " + isInGui);
System.out.println("event = " + event);
}
*/

}
6 changes: 6 additions & 0 deletions plugin/src/main/java/fr/anarchick/skb/event/KeyEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ public boolean isPressed() {
public boolean isInGUI() {
return isInGUI;
}

@Override
public String toString() {
return String.format("KeyEvent{player=%s, key=%s, isPressed=%s, isInGUI=%s}",
getPlayer().getName(), key.asString(), isPressed, isInGUI);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package fr.anarchick.skb.skript;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.util.Kleenean;
import fr.anarchick.skb.ServerKeyboardBridge;
import fr.anarchick.skb.core.KeyEntry;
import org.bukkit.NamespacedKey;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Name("Register Key Entry")
@Description({
"Register a key entry with a unique id, a name, a description, a category and a keycode",
"the id must be unique and is used to identify the key entry",
"the id must be lowercase and contain only letters, numbers and underscores",
"keycode can be found here : https://www.glfw.org/docs/latest/group__keys.html"
})
@Examples({
"on load:",
"\tregister key entry with id \"horse\" named \"spawn your horse\", category \"page 1\" and keycode 72",

})
@Since("1.1.0")
public class EffRegisterKeyEntry extends Effect {

private static final String pattern = "register key entry with id %string% named %string%, [description %-string%,] category %string% and keycode %number%";

static {
Skript.registerEffect(EffRegisterKeyEntry.class, pattern);
}

private Expression<String> exprId;
private Expression<String> exprName;
private Expression<String> exprDescription;
private Expression<String> exprCategory;
private Expression<Number> exprKeycode;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] expr, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {
exprId = (Expression<String>) expr[0];
exprName = (Expression<String>) expr[1];
exprDescription = (Expression<String>) expr[2];
exprCategory = (Expression<String>) expr[3];
exprKeycode = (Expression<Number>) expr[4];
return true;
}

@Override
protected void execute(@NotNull Event event) {
String id = exprId.getSingle(event).toLowerCase();
String name = exprName.getSingle(event);
String description = (exprDescription == null) ? "" : exprDescription.getSingle(event);
String category = exprCategory.getSingle(event);
Number keycodeNumber = exprKeycode.getSingle(event);
short keycode = keycodeNumber.shortValue();

NamespacedKey namespacedKey = new NamespacedKey(ServerKeyboardBridge.getInstance(), id);
KeyEntry keyEntry = new KeyEntry(namespacedKey, name, description, category, keycode);
ServerKeyboardBridge.getInstance().registerKey(keyEntry);
}

@Override
public @NotNull String toString(@Nullable Event event, boolean debug) {
if (event == null || exprId == null || exprName == null || exprCategory == null || exprKeycode == null) {
return pattern;
}

return String.format("register key entry with id %s named %s, description %s, category %s and keycode %s",
exprId.toString(event, debug),
exprName.toString(event, debug),
exprDescription.toString(event, debug),
exprCategory.toString(event, debug),
exprKeycode.toString(event, debug)
);
}

}
84 changes: 84 additions & 0 deletions plugin/src/main/java/fr/anarchick/skb/skript/EvtKeyEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package fr.anarchick.skb.skript;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Literal;
import ch.njol.skript.lang.SkriptEvent;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Getter;
import fr.anarchick.skb.ServerKeyboardBridge;
import fr.anarchick.skb.event.KeyEvent;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class EvtKeyEvent extends SkriptEvent {

private static final String[] patterns = new String[] {
"key pressed with id %string%",
"key release with id %string%"
};

static {
Skript.registerEvent("Key Pressed", EvtKeyEvent.class, KeyEvent.class, patterns)
.description("Called when the player press his keyboard key or mouse key. ServerKeyboardBridge Mod MUST be installed on the client. You can listen every input with a limit of 20 custom key entries.")
.examples("on key pressed with id \"horse\":",
"\tset {_playerIsInGui} to event-boolean\n",
"\tif {_playerIsInGui} is false:\n",
"\t\tspawn an horse at event-player")
.since("1.1.0");
EventValues.registerEventValue(KeyEvent.class, Player.class, new Getter<>() {
@Override
@NotNull
public Player get(KeyEvent event) {
return event.getPlayer();
}
}, 0);
EventValues.registerEventValue(KeyEvent.class, Boolean.class, new Getter<>() {
@Override
@NotNull
public Boolean get(KeyEvent event) {
return event.isInGUI();
}
}, 0);
}

private int matchedPattern;
private Literal<String> litId;

@Override
@SuppressWarnings("unchecked")
public boolean init(Literal<?>[] args, int matchedPattern, SkriptParser.@NotNull ParseResult parseResult) {
this.matchedPattern = matchedPattern;
litId = (Literal<String>) args[0];
return true;
}

@Override
public boolean check(@NotNull Event event) {
if (event instanceof KeyEvent keyEvent) {

if (keyEvent.isPressed() != (matchedPattern == 0)) {
return false;
}

String id = litId.getSingle(event);
NamespacedKey namespacedKey = keyEvent.getKey();
return namespacedKey.namespace().equalsIgnoreCase(ServerKeyboardBridge.getInstance().getName()) && namespacedKey.getKey().equalsIgnoreCase(id);
}
return false;
}

@Override
public @NotNull String toString(@Nullable Event event, boolean debug) {
String pattern = (matchedPattern == 0) ? patterns[0] : patterns[1];

if (event == null) {
return pattern;
}

return String.format(pattern.replace("%string%", "%s"), litId.toString(event, debug));
}
}
37 changes: 37 additions & 0 deletions plugin/src/main/java/fr/anarchick/skb/skript/EvtSkbJoin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fr.anarchick.skb.skript;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.util.SimpleEvent;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.util.Getter;
import fr.anarchick.skb.event.SkbJoinEvent;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class EvtSkbJoin {

private static final String pattern = "(ServerKeyboardBridge|SKB) player (login|logging in|join[ing])";

static {
Skript.registerEvent("ServerKeyboardBridge Join", SimpleEvent.class, SkbJoinEvent.class, pattern)
.description("Called when the player joins the server with the ServerKeyboardBridge Mod installed. You can get the client mod version with event-string.")
.examples("on SKB player join:",
" message \"client version : %event-string%\"")
.since("1.1.0");
EventValues.registerEventValue(SkbJoinEvent.class, Player.class, new Getter<>() {
@Override
@NotNull
public Player get(SkbJoinEvent event) {
return event.getPlayer();
}
}, 0);
EventValues.registerEventValue(SkbJoinEvent.class, String.class, new Getter<>() {
@Override
@NotNull
public String get(SkbJoinEvent event) {
return event.getVersion();
}
}, 0);
}

}
1 change: 1 addition & 0 deletions plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ main: fr.anarchick.skb.ServerKeyboardBridge
description: 'ServerKeyboardBridge allow the server to received client keyboard input if the client has the mod installed'
author: Anarchick
website: 'https://github.com/Anarchick/ServerKeyboardBridge'
softdepend: [Skript]
api-version: '1.20'
permissions:
serverkeyboardbridge.bypass.*:
Expand Down

0 comments on commit 8f255cb

Please sign in to comment.