Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe Support #7150

Open
wants to merge 75 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
2b18331
Starter Commit
TheAbsolutionism Oct 8, 2024
5301401
Shapeless Addition
TheAbsolutionism Oct 9, 2024
5aaf2a6
Testing Stuff
TheAbsolutionism Oct 9, 2024
bd2c23f
Testing Fix
TheAbsolutionism Oct 9, 2024
50bb139
More additions
TheAbsolutionism Oct 10, 2024
29bc2e8
Additional Updates + Changes
TheAbsolutionism Oct 11, 2024
d73bad2
Further Additions
TheAbsolutionism Oct 12, 2024
23d4dc0
Fuse Changes
TheAbsolutionism Oct 12, 2024
fbefde4
Tests (Stopping Point)
TheAbsolutionism Oct 12, 2024
a687e0f
Some Fixes
TheAbsolutionism Oct 13, 2024
1f79ed6
Additional Changes + Fixes
TheAbsolutionism Oct 15, 2024
5c3a8c0
Fuzes Wishlist
TheAbsolutionism Oct 15, 2024
78c5411
Almost Done
TheAbsolutionism Oct 16, 2024
ebc9428
Fix Imports
TheAbsolutionism Oct 16, 2024
793c3ee
One more fix
TheAbsolutionism Oct 16, 2024
9a628ea
Fix Test
TheAbsolutionism Oct 16, 2024
9307a02
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Oct 16, 2024
ed8f1db
Change RecipeType class register
TheAbsolutionism Oct 16, 2024
1cc3c24
Module + Changes
TheAbsolutionism Oct 16, 2024
48a6c1f
Fuse's Changes
TheAbsolutionism Oct 16, 2024
db3e501
Partial Changes
TheAbsolutionism Oct 17, 2024
2d611e0
More Partial Changes
TheAbsolutionism Oct 19, 2024
3c37524
RecipeWrapper
TheAbsolutionism Oct 31, 2024
250c69f
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Oct 31, 2024
3bd9c4a
Additional Changes
TheAbsolutionism Nov 1, 2024
06d8346
Remove "runtime"
TheAbsolutionism Nov 5, 2024
4b8ba57
Merge branch 'dev/feature' into dev/Recipes
TheAbsolutionism Nov 6, 2024
530ac32
Merge branch 'dev/feature' into dev/Recipes
TheAbsolutionism Nov 8, 2024
0b712cc
Merge branch 'dev/feature' into dev/Recipes
TheAbsolutionism Nov 11, 2024
76716cb
Merge branch 'dev/feature' into dev/Recipes
Efnilite Nov 12, 2024
1087525
Requested Changes
TheAbsolutionism Nov 13, 2024
e801263
Additional Changes
TheAbsolutionism Nov 13, 2024
ab7edc5
Fix Errors
TheAbsolutionism Nov 13, 2024
99250ad
Fix Docs
TheAbsolutionism Nov 13, 2024
e2193ea
Requested Changes
TheAbsolutionism Nov 16, 2024
2eea7a6
More Changes
TheAbsolutionism Nov 16, 2024
6268513
Whoops
TheAbsolutionism Nov 16, 2024
149b842
Partial Changes
TheAbsolutionism Nov 17, 2024
14972f3
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Nov 23, 2024
0e5e1e4
Secspressions!
TheAbsolutionism Nov 23, 2024
cb52649
Remove Duplicated Module
TheAbsolutionism Nov 24, 2024
f6bdb2f
Merge branch 'dev/feature' into dev/Recipes
TheAbsolutionism Dec 9, 2024
04ba9fc
Changes + Additions
TheAbsolutionism Dec 9, 2024
9f97e4f
Fix Cond
TheAbsolutionism Dec 9, 2024
64834b0
Merge branch 'dev/Recipes' of https://github.com/TheAbsolutionism/Skr…
TheAbsolutionism Dec 9, 2024
b4e809c
Check Lambda
TheAbsolutionism Dec 9, 2024
449148b
Merge branch 'dev/feature' into dev/Recipes
TheAbsolutionism Dec 15, 2024
3788025
Partial Changes
TheAbsolutionism Dec 16, 2024
56ea7a4
Fix Tests
TheAbsolutionism Dec 16, 2024
f4d0e77
Additional Changes
TheAbsolutionism Dec 17, 2024
b36656c
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Dec 19, 2024
b143f26
Update EventValues
TheAbsolutionism Dec 19, 2024
ea18385
Merge branch 'dev/feature' into dev/Recipes
TheAbsolutionism Dec 20, 2024
1679f2d
Fix Comparator Issue
TheAbsolutionism Dec 20, 2024
bbc007a
Remove NamespacedUtils
TheAbsolutionism Dec 20, 2024
e1c2fac
Requested Changes
TheAbsolutionism Dec 20, 2024
a30c03b
Lowercase
TheAbsolutionism Dec 21, 2024
b179275
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Dec 24, 2024
a7175fe
Merge branch 'dev/feature' into dev/Recipes
TheAbsolutionism Dec 24, 2024
22b0ea0
SPE Update
TheAbsolutionism Dec 25, 2024
40a04f9
Changes
TheAbsolutionism Dec 26, 2024
1bcb404
Enum Improvements
TheAbsolutionism Dec 29, 2024
b972c32
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Dec 29, 2024
2172a67
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Dec 29, 2024
07f13ec
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Dec 30, 2024
eff1937
Merge branch 'dev/feature' into dev/Recipes
Moderocky Dec 31, 2024
baa600e
Merge branch 'dev/feature' into dev/Recipes
TheAbsolutionism Dec 31, 2024
8274f7c
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Jan 1, 2025
1829c98
Update skript-aliases
TheAbsolutionism Jan 1, 2025
f6df8f3
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Jan 1, 2025
63d8cb6
Update skript-aliases
TheAbsolutionism Jan 1, 2025
81f6f52
Fixed Everything
TheAbsolutionism Jan 1, 2025
f9c159c
Final Commit
TheAbsolutionism Jan 2, 2025
4647ac1
Merge remote-tracking branch 'upstream/dev/feature' into dev/Recipes
TheAbsolutionism Jan 3, 2025
3897e40
Proper Runtime Errors
TheAbsolutionism Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.junit.runner.notification.Failure;
import org.skriptlang.skript.bukkit.SkriptMetrics;
import org.skriptlang.skript.bukkit.displays.DisplayModule;
import org.skriptlang.skript.bukkit.recipes.RecipeModule;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.converter.Converter;
Expand Down Expand Up @@ -555,6 +556,7 @@ public void onEnable() {
getAddonInstance().loadClasses("org.skriptlang.skript.bukkit", "misc");
// todo: become proper module once registry api is merged
DisplayModule.load();
RecipeModule.load();
} catch (final Exception e) {
exception(e, "Could not load required .class files: " + e.getLocalizedMessage());
setEnabled(false);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.event.player.PlayerRecipeDiscoverEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.bukkit.event.vehicle.VehicleDamageEvent;
import org.bukkit.event.vehicle.VehicleDestroyEvent;
Expand Down Expand Up @@ -1393,6 +1394,12 @@ public Player get(final PrepareItemCraftEvent e) {
return null;
}
}, 0);
EventValues.registerEventValue(PrepareItemCraftEvent.class, Recipe.class, new Getter<Recipe, PrepareItemCraftEvent>() {
@Override
public @Nullable Recipe get(PrepareItemCraftEvent event) {
return event.getRecipe();
}
}, EventValues.TIME_NOW);
// CraftEvents - recipe namespaced key strings
EventValues.registerEventValue(CraftItemEvent.class, String.class, new Getter<String, CraftItemEvent>() {
@Nullable
Expand Down Expand Up @@ -1422,6 +1429,12 @@ public ItemStack get(CraftItemEvent e) {
return e.getRecipe().getResult();
}
}, 0);
EventValues.registerEventValue(CraftItemEvent.class, Recipe.class, new Getter<Recipe, CraftItemEvent>() {
@Override
public @Nullable Recipe get(CraftItemEvent event) {
return event.getRecipe();
}
}, EventValues.TIME_NOW);
//InventoryOpenEvent
EventValues.registerEventValue(InventoryOpenEvent.class, Player.class, new Getter<Player, InventoryOpenEvent>() {
@Override
Expand Down Expand Up @@ -1991,5 +2004,13 @@ public Entity[] get(BlockDropItemEvent event) {
return event.getItems().toArray(Entity[]::new);
}
}, EventValues.TIME_NOW);

// PlayerRecipeDiscoverEvent
EventValues.registerEventValue(PlayerRecipeDiscoverEvent.class, Recipe.class, new Getter<Recipe, PlayerRecipeDiscoverEvent>() {
@Override
public @Nullable Recipe get(PlayerRecipeDiscoverEvent event) {
return Bukkit.getRecipe(event.getRecipe());
}
}, EventValues.TIME_NOW);
}
}
124 changes: 124 additions & 0 deletions src/main/java/ch/njol/skript/util/NamespacedUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package ch.njol.skript.util;

import ch.njol.skript.Skript;
import ch.njol.util.Pair;
import com.google.common.collect.Sets;
import org.apache.commons.lang.ArrayUtils;
import org.bukkit.NamespacedKey;

import java.util.Set;

public class NamespacedUtils {

private static final Set<Character> LEGAL_NAMESPACE_CHARS = Sets.newHashSet(ArrayUtils.toObject("abcdefghijklmnopqrstuvwxyz0123456789._-/".toCharArray()));

/**
* Gets a namespaced key. This method will try to get existing keys first, but if that fails
* it will create the key in Skript's namespace.
* @param key the unparsed key
* @param usingMinecraft whether to have it as Skript or Minecraft namespace
* @return the resulting NamespacedKey
*/
public static NamespacedKey getNamespacedKey(String key, Boolean usingMinecraft) {
NamespacedKey namespacedKey;
if (usingMinecraft)
namespacedKey = NamespacedKey.fromString(key);
else
namespacedKey = NamespacedKey.fromString(key, Skript.getInstance());
if (namespacedKey != null)
return namespacedKey;
NamespacedKey convertedKey = createNamespacedKey(key, usingMinecraft);
if (!Skript.testing())
Skript.info("The key provided '" + key + "' has been converted to '" + convertedKey.toString() + "' due to invalid characters." +
"\n\tValid characters are a-z, 0-9, -, _ and .");
return convertedKey;
}

/**
* Creates a namespaced key in Skript's namespace encoded to avoid the character limitations of a normal key.
* This key will be created in Skript's namespace.
*
* @param key The key to use
* @param usingMinecraft whether to have it as Skript or Minecraft's namespace
* @return a NamespacedKey with the encoded key in corresponding Namespace
*/
public static NamespacedKey createNamespacedKey(String key, Boolean usingMinecraft) {
// TODO: add tests for this
StringBuilder encodedKeyBuilder = new StringBuilder();
// keys must be all lowercase
key = key.toLowerCase();
key = key.replace(' ', '_');
int keyLength = key.length();
for (int i = 0; i < keyLength; i++) {
char currentChar = key.charAt(i);
// if this character is legal to use in a namespace key
if (LEGAL_NAMESPACE_CHARS.contains(currentChar)) {
// if the original string had a ".x" in it, we need to escape it
// so decoding doesn't think it's a hex sequence
if (currentChar == '.' && key.charAt(i + 1) == 'x') {
i += 1; // skip the "x"
encodedKeyBuilder.append(".x");
encodedKeyBuilder.append(Integer.toHexString('.'));
encodedKeyBuilder.append(".x");
encodedKeyBuilder.append(Integer.toHexString('x'));
// if we're not at the end and the next char is a legal char, add the trailing dot
// to represent the end of the hex sequence
if (i != (keyLength - 1) && LEGAL_NAMESPACE_CHARS.contains(key.charAt(i + 1)))
encodedKeyBuilder.append('.');
} else {
// we are dealing with a legal character, so we can just append it
encodedKeyBuilder.append(currentChar);
}
} else {
// add ".x(hex code)" to the encoded key
encodedKeyBuilder.append(".x");
encodedKeyBuilder.append(Integer.toHexString(currentChar));
// only add the trailing dot if the next character is a legal character
if (i != (keyLength - 1) && LEGAL_NAMESPACE_CHARS.contains(key.charAt(i + 1)))
encodedKeyBuilder.append('.');
}
}
if (usingMinecraft)
return NamespacedKey.fromString(encodedKeyBuilder.toString());
return NamespacedKey.fromString(encodedKeyBuilder.toString(), Skript.getInstance());
}

/**
* Decodes a NamespacedKey encoded by #getNamespacedKey
*
* @param namespacedKey the namespaced key to decode
* @return a Pair with the first element as the namespace and the second as the decoded key
*/
public static Pair<String, String> decodeNamespacedKey(NamespacedKey namespacedKey) {
// TODO: add tests for this
String encodedKey = namespacedKey.getKey();
StringBuilder decodedKeyBuilder = new StringBuilder();
int encodedKeyLength = encodedKey.length();
int lastCharIndex = encodedKeyLength - 1;
for (int i = 0; i < encodedKeyLength; i++) {
char currentChar = encodedKey.charAt(i);
// if we are encountering a ".x" hex sequence
if (i != lastCharIndex && currentChar == '.' && encodedKey.charAt(i + 1) == 'x') {
i += 2; // skip the ".x" so it isn't part of our hex string
StringBuilder hexString = new StringBuilder();
// The hex sequence continues until a . is encountered or we reach the end of the string
while (i <= lastCharIndex && encodedKey.charAt(i) != '.') {
hexString.append(encodedKey.charAt(i));
i++;
}
// if the . was the start of another ".x" hex sequence, back up by 1 character
if (i <= lastCharIndex && encodedKey.charAt(i + 1) == 'x')
i--;
// parse the hex sequence to a char
char decodedChar = (char) Long.parseLong(hexString.toString(), 16);
decodedKeyBuilder.append(decodedChar);
} else {
// this is just a normal character, not a hex sequence
// so we can just append it
decodedKeyBuilder.append(currentChar);
}
}
return new Pair<>(namespacedKey.getNamespace(), decodedKeyBuilder.toString());
}

}
Loading