Skip to content

Commit

Permalink
Adds the is_repair context for the recipe formed event (#2496)
Browse files Browse the repository at this point in the history
* Update ItemRecipeFormedScriptEvent.java

* Update ItemRecipeFormedScriptEvent.java

* Update ItemRecipeFormedScriptEvent.java

* Update ItemRecipeFormedScriptEvent.java
  • Loading branch information
heypr committed Jul 15, 2023
1 parent 207edc3 commit 15632dc
Showing 1 changed file with 19 additions and 34 deletions.
Expand Up @@ -16,6 +16,8 @@
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;

public class ItemRecipeFormedScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
Expand All @@ -32,6 +34,7 @@ public class ItemRecipeFormedScriptEvent extends BukkitScriptEvent implements Li
// <context.item> returns the ItemTag to be formed in the result slot.
// <context.recipe> returns a ListTag of ItemTags in the recipe.
// <context.recipe_id> returns the ID of the recipe that was formed.
// <context.is_repair> returns an ElementTag(Boolean) of whether the event was triggered by a tool repair operation rather than a crafting recipe.
//
// @Determine
// ItemTag to change the item that is formed in the result slot.
Expand All @@ -42,6 +45,14 @@ public class ItemRecipeFormedScriptEvent extends BukkitScriptEvent implements Li

public ItemRecipeFormedScriptEvent() {
registerCouldMatcher("<item> recipe formed");
this.<ItemRecipeFormedScriptEvent, ObjectTag>registerOptionalDetermination(null, ObjectTag.class, (evt, context, determination) -> {
if (determination.canBeType(ItemTag.class)) {
ItemTag result = determination.asType(ItemTag.class, context);
evt.event.getInventory().setResult(result.getItemStack());
return true;
}
return false;
});
}


Expand All @@ -56,47 +67,21 @@ public boolean matches(ScriptPath path) {
return super.matches(path);
}

@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj.canBeType(ItemTag.class)) {
ItemTag result = determinationObj.asType(ItemTag.class, getTagContext(path));
event.getInventory().setResult(result.getItemStack());
return true;
}
else {
return super.applyDetermination(path, determinationObj);
}
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(EntityTag.getPlayerFrom(event.getView().getPlayer()), null);
}

@Override
public ObjectTag getContext(String name) {
switch (name) {
case "item": return result;
case "inventory": return InventoryTag.mirrorBukkitInventory(event.getInventory());
case "recipe": {
ListTag recipe = new ListTag();
for (ItemStack itemStack : event.getInventory().getMatrix()) {
if (itemStack != null && itemStack.getType() != Material.AIR) {
recipe.addObject(new ItemTag(itemStack));
}
else {
recipe.addObject(new ItemTag(Material.AIR));
}
}
return recipe;
}
case "recipe_id":
if (event.getRecipe() instanceof Keyed) {
return new ElementTag(((Keyed) event.getRecipe()).getKey().toString());
}
break;
}
return super.getContext(name);
return switch (name) {
case "item" -> result;
case "inventory" -> InventoryTag.mirrorBukkitInventory(event.getInventory());
case "recipe" -> new ListTag(Arrays.asList(event.getInventory().getMatrix()), ItemTag::new);
case "recipe_id" -> event.getRecipe() instanceof Keyed keyed ? new ElementTag(keyed.getKey().toString()): null;
case "is_repair" -> new ElementTag(event.isRepair());
default -> super.getContext(name);
};
}

@Override
Expand Down

0 comments on commit 15632dc

Please sign in to comment.