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 20 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
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
26 changes: 26 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.recipe.CookingBookCategory;
import org.bukkit.inventory.recipe.CraftingBookCategory;
import org.bukkit.inventory.Recipe;
import org.bukkit.metadata.Metadatable;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
Expand Down Expand Up @@ -1549,6 +1552,29 @@ public String toVariableNameString(EnchantmentOffer eo) {
.since("@VERSION")
.requiredPlugins("Minecraft 1.21+")
.documentationId("WolfVariant"));

Classes.registerClass(new ClassInfo<>(Recipe.class, "recipe")
.user("recipes?")
.name("Recipe")
.description("")
.usage("")
.examples("")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
.since("INSERT VERSION")
.defaultExpression(new EventValueExpression<>(Recipe.class)));

Classes.registerClass(new EnumClassInfo<>(CraftingBookCategory.class, "craftingbookcategory", "crafting book categories")
.user("crafting book category")
.name("Crafting Book Category")
.description("Represents a category for crafting recipe types")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
.since("INSERT VERSION")
);

Classes.registerClass(new EnumClassInfo<>(CookingBookCategory.class, "cookingbookcategory", "cooking book categories")
.user("cooking book category")
.name("Cooking Book Category")
.description("Represents a category for cooking recipe types")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
.since("INSERT VERSION")
);
}

}
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>() {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Override
public @Nullable Recipe get(PrepareItemCraftEvent event) {
return event.getRecipe();
}
}, EventValues.TIME_NOW);
// CraftEvents - recipe namespaced key strings
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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>() {
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Override
public @Nullable Recipe get(CraftItemEvent event) {
return event.getRecipe();
}
}, EventValues.TIME_NOW);
//InventoryOpenEvent
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
10 changes: 10 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/DefaultComparators.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import ch.njol.skript.util.Time;
import ch.njol.skript.util.Timeperiod;
import ch.njol.skript.util.Timespan;
import ch.njol.skript.util.RecipeUtils;
import ch.njol.skript.util.WeatherType;
import ch.njol.skript.util.slot.EquipmentSlot;
import ch.njol.skript.util.slot.Slot;
Expand Down Expand Up @@ -670,6 +671,15 @@ public boolean supportsOrdering() {
Comparators.registerComparator(Color.class, Color.class, (one, two) -> Relation.get(one.asBukkitColor().equals(two.asBukkitColor())));
Comparators.registerComparator(Color.class, org.bukkit.Color.class, (one, two) -> Relation.get(one.asBukkitColor().equals(two)));
Comparators.registerComparator(org.bukkit.Color.class, org.bukkit.Color.class, (one, two) -> Relation.get(one.equals(two)));

Comparators.registerComparator(RecipeUtils.RecipeType.class, RecipeUtils.RecipeType.class, new Comparator<RecipeUtils.RecipeType, RecipeUtils.RecipeType>() {
@Override
public Relation compare(RecipeUtils.RecipeType type1, RecipeUtils.RecipeType type2) {
if (type1.getRecipeClass() != null && type2.getRecipeClass() != null)
return Relation.get(type2.getRecipeClass().isAssignableFrom(type1.getRecipeClass()));
return Relation.NOT_EQUAL;
}
});
}

}
9 changes: 9 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ch.njol.skript.bukkitutil.ItemUtils;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.EnumClassInfo;
import ch.njol.skript.classes.EnumSerializer;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.classes.Serializer;
Expand All @@ -25,6 +26,7 @@
import ch.njol.skript.util.EnchantmentType;
import ch.njol.skript.util.Experience;
import ch.njol.skript.util.GameruleValue;
import ch.njol.skript.util.RecipeUtils;
import ch.njol.skript.util.SkriptColor;
import ch.njol.skript.util.StructureType;
import ch.njol.skript.util.Time;
Expand Down Expand Up @@ -668,6 +670,13 @@ public String toVariableNameString(VisualEffect e) {
.since("2.5")
.serializer(new YggdrasilSerializer<GameruleValue>())
);

Classes.registerClass(new EnumClassInfo<>(RecipeUtils.RecipeType.class, "recipetype", "recipe types")
.user("recipe type")
.name("Recipe Type")
.description("Represents recipe types")
.since("INSERT VERSION")
);
}

}
67 changes: 67 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondDiscoveredRecipes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ch.njol.skript.conditions;

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.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.NamespacedUtils;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Has Discovered Recipe")
@Description("Checks whether a player or players have discovered a recipe.")
@Examples({
"if player has discovered recipe \"custom_recipe\":",
"\tgive player 1 diamond",
"",
"if all players have not found recipe \"custom_recipe\":",
"\tkill all players",
})
@Since("INSERT VERSION")
public class CondDiscoveredRecipes extends Condition {

static {
Skript.registerCondition(CondDiscoveredRecipes.class,
"%players% (has|have) (discovered|unlocked) recipe[s] %strings%",
"%players% (hasn't|has not|haven't|have not) (discovered|unlocked) recipe[s] %strings%");
}

private Expression<Player> players;
private Expression<String> recipes;

@Override
@SuppressWarnings("unchecked")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
players = (Expression<Player>) exprs[0];
recipes = (Expression<String>) exprs[1];
setNegated(matchedPattern == 1);
return true;
}

@Override
public boolean check(Event event) {
return players.check(event,
player -> recipes.check(event,
recipe -> {
NamespacedKey key = NamespacedUtils.getNamespacedKey(recipe, false);
if (Bukkit.getRecipe(key) != null)
return player.hasDiscoveredRecipe(key);
return false;
}
)
);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return players.toString(event, debug) + (isNegated() ? "have not" : "have") + " found recipes " + recipes.toString(event, debug);
}
}
51 changes: 51 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondRecipeExists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ch.njol.skript.conditions;

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.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.NamespacedUtils;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Recipe Exists")
@Description("Checks to see if a recipe exists using the name")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"if the recipe \"my_recipe\" exists:",
"\tremove the recipe \"my_recipe\" from the server"
})
@Since("INSERT VERSION")
public class CondRecipeExists extends Condition {

static {
Skript.registerCondition(CondRecipeExists.class,
"[the] recipe[s] %strings% [does] exist[s]",
"[the] recipe[s] %strings% (doesn't|does not) exist[s]");
}

private Expression<String> recipes;

@Override
@SuppressWarnings("unchecked")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
recipes = (Expression<String>) exprs[0];
setNegated(matchedPattern == 1);
return true;
}

@Override
public boolean check(Event event) {
return recipes.check(event, recipe -> Bukkit.getRecipe(NamespacedUtils.getNamespacedKey(recipe, false)) != null, isNegated());
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "recipes " + recipes.toString(event, debug) + (isNegated() ? "does not" : "") + "exists";
}
}
72 changes: 72 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffDiscoverRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ch.njol.skript.effects;

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.ParseResult;
import ch.njol.skript.util.NamespacedUtils;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Discover Recipe")
@Description("Discover or undiscover recipes for players.")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
@Examples({
"make player discover recipe \"my_recipe\"",
"make player undiscover recipe \"my_recipe\"",
"unlock recipe \"my_recipe\" for all players",
"lock recipe \"my_recipe\" for all players"
})
@Since("INSERT VERSION")
public class EffDiscoverRecipe extends Effect {

static {
Skript.registerEffect(EffDiscoverRecipe.class,
"make %players% (discover|unlock) recipe[s] %strings%",
"make %players% (undiscover|lock) recipe[s] %strings%",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"(discover|unlock) recipe[s] %strings% for %players%",
"(undiscover|lock) recipe[s] %strings% for %players%");
}

private Expression<Player> players;
private Expression<String> recipes;
private boolean isDiscover = false;

@Override
@SuppressWarnings("unchecked")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
isDiscover = matchedPattern == 0 || matchedPattern == 2;
players = (Expression<Player>) (matchedPattern <= 1 ? exprs[0] : exprs[1]);
recipes = (Expression<String>) (matchedPattern <= 1 ? exprs[1] : exprs[0]);
return true;
}

@Override
protected void execute(Event event) {
for (Player player : players.getArray(event)) {
for (String recipe : recipes.getArray(event)) {
NamespacedKey key = NamespacedUtils.getNamespacedKey(recipe, false);
if (Bukkit.getRecipe(key) != null) {
if (isDiscover)
player.discoverRecipe(key);
else
player.undiscoverRecipe(key);
} else {
break;
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "make " + players.toString(event, debug) + (isDiscover ? " discover" : " undiscover") + " recipes " + recipes.toString(event, debug);
}
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
}
54 changes: 54 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffRemoveRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ch.njol.skript.effects;

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.ParseResult;
import ch.njol.skript.util.NamespacedUtils;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

@Name("Remove Recipe")
@Description({
"Remove a recipe or multiple recipes from the server",
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
"Removing a recipe from a server will cause all players who have discovered the recipe to be undiscovered."
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
})
@Examples("remove the recipe \"my_recipe\" from the server")
@Since("INSERT VERSION")
public class EffRemoveRecipe extends Effect {

static {
Skript.registerEffect(EffRemoveRecipe.class,
"(remove|delete|clear) [the] recipe[s] [with [the] key] %strings% [from the server]");
}

private Expression<String> recipes;

@Override
@SuppressWarnings("unchecked")
TheAbsolutionism marked this conversation as resolved.
Show resolved Hide resolved
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
recipes = (Expression<String>) exprs[0];
return true;
}

@Override
protected void execute(Event event) {
for (String recipe : recipes.getArray(event)) {
NamespacedKey key = NamespacedUtils.getNamespacedKey(recipe, false);
if (Bukkit.getRecipe(key) != null)
Bukkit.removeRecipe(key);
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "remove recipes " + recipes.toString(event, debug) + " from the server";
}
}
Loading