Skip to content

Commit

Permalink
Merge pull request #31 from JakeGBLP/dev/feature
Browse files Browse the repository at this point in the history
Dev/feature -> main
  • Loading branch information
JakeGBLP authored Jan 26, 2025
2 parents 57f011f + e2812f9 commit e6f8011
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ jobs:
uses: SkriptLang/[email protected]
with:
test_script_directory: src/test/scripts
skript_repo_ref: dev/patch
skript_repo_ref: dev/feature
extra_plugins_directory: extra-plugins/
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ compileJava {
options.encoding = "UTF-8"
}

version = "1.3.3"

version = "1.4"

def java16 = 16
def java17 = 17
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package it.jakegblp.lusk.elements.minecraft.namespacedkey.types;

import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.yggdrasil.Fields;
import it.jakegblp.lusk.utils.LuskUtils;
import org.bukkit.NamespacedKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.StreamCorruptedException;

public class NamespacedKeyClassInfos {
static {
if (Classes.getExactClassInfo(NamespacedKey.class) == null) {
Classes.registerClass(new ClassInfo<>(NamespacedKey.class, "namespacedkey")
.user("namespaced ?keys?")
.name("Namespaced Key")
.description("""
A String based key which consists of a namespace and a key; used to declare and specify game objects in Minecraft without without potential ambiguity or conflicts.
Namespaces may only contain lowercase alphanumeric characters, periods, underscores, and hyphens.
Keys may only contain lowercase alphanumeric characters, periods, underscores, hyphens, and forward slashes.
More Info: [**Resource Location**](https://minecraft.wiki/w/Resource_location)
""")
.since("1.4")
.parser(new Parser<>() {
@Override
public @Nullable NamespacedKey parse(String s, ParseContext context) {
return LuskUtils.getNamespacedKey(s);
}

@Override
public String toString(NamespacedKey o, int flags) {
return o.toString();
}

@Override
public String toVariableNameString(NamespacedKey o) {
return o.toString();
}
})
.serializer(new Serializer<>() {
@Override
public @NotNull Fields serialize(NamespacedKey namespacedKey) {
Fields fields = new Fields();
fields.putObject("key", namespacedKey.toString());
return fields;
}

@Override
public void deserialize(NamespacedKey o, Fields f) {
}

@Override
protected NamespacedKey deserialize(Fields fields) throws StreamCorruptedException {
String key = fields.getObject("key", String.class);
if (key == null) {
throw new StreamCorruptedException("NamespacedKey string is null");
}
return NamespacedKey.fromString(key);
}

@Override
public boolean mustSyncDeserialization() {
return true;
}

@Override
protected boolean canBeInstantiated() {
return false;
}
}));
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/it/jakegblp/lusk/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class Constants {

public static final double EPSILON = 1e-7;

public static final short MAX_NAMESPACED_KEY_LENGTH = isPaper() ? Short.MAX_VALUE : 255;

public static final String[] LUSK_COLORS = new String[]{"&7", "&9"};

public static final Pattern
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/it/jakegblp/lusk/utils/LuskUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.text.MessageFormat;
import java.util.List;
Expand All @@ -17,6 +19,7 @@
import java.util.stream.IntStream;

import static it.jakegblp.lusk.utils.Constants.*;
import static it.jakegblp.lusk.utils.Constants.MAX_NAMESPACED_KEY_LENGTH;

public class LuskUtils {

Expand Down Expand Up @@ -125,4 +128,18 @@ public static ColorRGB getColorAsRGB(@NotNull Color color) {
return new ColorRGB(color.getRed(), color.getGreen(), color.getBlue());
}


@Nullable
public static NamespacedKey getNamespacedKey(@NotNull String key) {
if (key.isEmpty()) return null;
if (!key.contains(":")) key = "minecraft:" + key;
if (key.length() > MAX_NAMESPACED_KEY_LENGTH) {
warning("Namespacedkey {0} with length {1} exceeds the max length of {2}!", key, key.length(), MAX_NAMESPACED_KEY_LENGTH);
return null;
}
key = key.toLowerCase();
if (key.contains(" ")) key = key.replace(" ", "_");
return NamespacedKey.fromString(key);
}

}
1 change: 1 addition & 0 deletions src/main/resources/lang/default.lang
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ types:
ignitecause: ignition cause¦s @an
entitysnapshot: entity snapshot¦s @an
blockaction: block action¦s @a
namespacedkey: namespaced key¦s @a

# -- Rotations --
rotations:
Expand Down

0 comments on commit e6f8011

Please sign in to comment.