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

Add recipe support #5204

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
73 changes: 73 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondDiscoveredRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
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;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;


@Name("Has Discovered Recipe")
@Description("Checks whether a player has discovered/unlocked a recipe")
@Examples({"discover recipe \"dirty-diamond-boots\" for player"})
@Since("INSERT VERSION")
public class CondDiscoveredRecipe extends Condition {

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

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

@Override
@SuppressWarnings({"unchecked", "null"})
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
players = (Expression<Player>) exprs[0];
setNegated(matchedPattern == 1);
return true;
}

@Override
public boolean check(Event event) {
return players.check(event,
player -> recipes.check(event,
recipe -> player.hasDiscoveredRecipe(Utils.getNamespacedKey(recipe))),
isNegated());
}

@Override
public String toString(@Nullable Event event, final boolean debug) {
return players.toString(event, debug);
}

}
90 changes: 90 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,90 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
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;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;


@Name("Discover Recipe")
@Description("Discovers/unlocks recipes")
@Examples({"discover recipe \"dirty-diamond-boots\" for player"})
@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%",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace is messed up

"(discover|unlock) recipe[s] %strings% for %players%",
"(undiscover|lock) recipe[s] %strings% for %players%");
}

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

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) {
discover = matchedPattern % 2 == 0;
players = (Expression<Player>) exprs[matchedPattern <= 1 ? 0 : 1];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store the condition in a variable

recipes = (Expression<String>) exprs[matchedPattern <= 1 ? 1 : 0];
return true;
}

@Override
protected void execute(Event e) {
for (Player player : players.getArray(e)) {
for (String recipe : recipes.getArray(e)) {
if (discover) {
player.discoverRecipe(getKey(recipe));
} else {
player.undiscoverRecipe(getKey(recipe));
}
}
}

}

@Override
public String toString(@Nullable Event e, boolean debug) {
return "continue";
}

private NamespacedKey getKey(String recipeKey) {
NamespacedKey key = NamespacedKey.fromString(recipeKey);
if (key != null)
return key;
return Utils.createNamespacedKey(recipeKey);
}

}
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/lang/TriggerSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void setTriggerItems(List<TriggerItem> items) {
}
}
}

@Override
public TriggerSection setNext(@Nullable TriggerItem next) {
super.setNext(next);
Expand Down
179 changes: 179 additions & 0 deletions src/main/java/ch/njol/skript/sections/recipe/SecCookingRecipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.sections.recipe;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.config.SectionNode;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.Section;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.TriggerItem;
import org.apache.commons.lang.StringUtils;
import org.skriptlang.skript.lang.entry.EntryContainer;
import org.skriptlang.skript.lang.entry.EntryValidator;
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.util.Timespan;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.event.Event;
import org.bukkit.inventory.BlastingRecipe;
import org.bukkit.inventory.CampfireRecipe;
import org.bukkit.inventory.CookingRecipe;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.SmokingRecipe;
import org.skriptlang.skript.lang.entry.util.ExpressionEntryData;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Name("Cooking Recipe")
@Description("Creates a cooking recipe")
@Examples({
"create a furnace recipe:",
"\tkey: \"dirty-diamond-boots\"",
"\tresult: diamond boots named \"Dirty Diamond Boots\"",
"\tingredient: diamond",
"\tcook time: 1 second",
"\txp: 500"
})
@Since("INSERT VERSION")
public class SecCookingRecipe extends Section {

static {
Skript.registerSection(SecCookingRecipe.class, "(create|add|register) [a] (0:blast furnace|1:campfire|2:furnace|3:smoker) recipe");
}

private static EntryValidator validator = EntryValidator.builder()
.addEntryData(new ExpressionEntryData<>("ingredient", null, false, ItemType.class))
.addEntryData(new ExpressionEntryData<>("result", null, false, ItemType.class))
.addEntryData(new ExpressionEntryData<>("key", null, false, String.class))
.addEntryData(new ExpressionEntryData<>("cook time", null, false, Timespan.class))
.addEntryData(new ExpressionEntryData<>("group", null, true, String.class))
.addEntryData(new ExpressionEntryData<>("xp", null, false, Number.class))
.build();

enum CookingRecipeType {
//TODO: support using more than one of these in the syntax
BLAST_FURNACE, CAMPFIRE, FURNACE, SMOKER
}

private CookingRecipeType type;
private Expression<? extends String> key;
private Expression<? extends ItemType> ingredient;
private Expression<? extends Timespan> cookTime;
private Expression<? extends Number> xp;
private Expression<? extends String> group;
private Expression<? extends ItemType> result;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult, SectionNode sectionNode, List<TriggerItem> triggerItems) {
EntryContainer entryContainer = validator.validate(sectionNode);
if (entryContainer == null)
return false;
type = CookingRecipeType.values()[parseResult.mark];
ingredient = (Expression<? extends ItemType>) entryContainer.get("ingredient", false);
cookTime = (Expression<? extends Timespan>) entryContainer.get("cook time", false);
xp = (Expression<? extends Number>) entryContainer.get("xp", false);
group = (Expression<? extends String>) entryContainer.getOptional("group", false);
result = (Expression<? extends ItemType>) entryContainer.get("result", false);
key = (Expression<? extends String>) entryContainer.get("key", false);
return true;
}

@Override
public String toString(Event event, boolean debug) {
String friendlyName = type.name().replace("_", " ").toLowerCase();
return "create a " + friendlyName + " recipe";
}

@Override
protected TriggerItem walk(Event event) {
execute(event);
return super.walk(event, false);
}

private void execute(Event event) {
String key = this.key.getSingle(event);
if (key == null || StringUtils.isBlank(key))
return;
ItemType result = this.result.getSingle(event);
if (result == null)
return;
ItemType ingredient = this.ingredient.getSingle(event);
if (ingredient == null)
return;
Number xp = this.xp.getSingle(event);
if (xp == null)
return;
Timespan cookTime = this.cookTime.getSingle(event);
if (cookTime == null)
return;

Set<Material> ingredientMaterials = new HashSet<>();
for (ItemStack itemStack : ingredient.getAll()) {
ingredientMaterials.add(itemStack.getType());
}

// the recipe APIs require an int :(
int cookTimeTicks = (int) cookTime.getTicks_i();
NamespacedKey namespacedKey = Utils.createNamespacedKey(key);
RecipeChoice choice = new RecipeChoice.MaterialChoice(ingredientMaterials.toArray(new Material[0]));
CookingRecipe<?> recipe;
switch (type) {
case SMOKER:
recipe = new SmokingRecipe(namespacedKey, result.getRandom(), choice, xp.floatValue(), cookTimeTicks);
break;
case FURNACE:
recipe = new FurnaceRecipe(namespacedKey, result.getRandom(), choice, xp.floatValue(), cookTimeTicks);
break;
case CAMPFIRE:
recipe = new CampfireRecipe(namespacedKey, result.getRandom(), choice, xp.floatValue(), cookTimeTicks);
break;
case BLAST_FURNACE:
recipe = new BlastingRecipe(namespacedKey, result.getRandom(), choice, xp.floatValue(), cookTimeTicks);
break;
default:
throw new IllegalStateException();
}

if (this.group != null) {
String group = this.group.getSingle(event);
if (group != null)
recipe.setGroup(group);
}

try {
Bukkit.getServer().addRecipe(recipe);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check this? it seems to return a boolean not throw an exception now that i look again

} catch (IllegalStateException ignored) {
// Bukkit throws a IllegalStateException if a duplicate recipe is registered
}
Pikachu920 marked this conversation as resolved.
Show resolved Hide resolved
}

}
Loading