PaperMC/patches/server/Add-PlayerStonecutterRecipeSelectEvent.patch

58 lines
3.6 KiB
Diff
Raw Normal View History

2021-06-14 21:58:32 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Fri, 27 Nov 2020 17:14:27 -0800
Subject: [PATCH] Add PlayerStonecutterRecipeSelectEvent
2021-06-14 21:58:32 +02:00
Co-Authored-By: MiniDigger | Martin <admin@minidigger.dev>
diff --git a/src/main/java/net/minecraft/world/inventory/StonecutterMenu.java b/src/main/java/net/minecraft/world/inventory/StonecutterMenu.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/inventory/StonecutterMenu.java
+++ b/src/main/java/net/minecraft/world/inventory/StonecutterMenu.java
@@ -0,0 +0,0 @@ public class StonecutterMenu extends AbstractContainerMenu {
public StonecutterMenu(int syncId, Inventory playerInventory, final ContainerLevelAccess context) {
super(MenuType.STONECUTTER, syncId);
- this.selectedRecipeIndex = DataSlot.standalone();
+ this.selectedRecipeIndex = DataSlot.shared(new int[1], 0); // Paper - Add PlayerStonecutterRecipeSelectEvent
2024-10-23 19:46:06 +02:00
this.recipesForInput = SelectableRecipe.SingleInputSet.empty();
2021-06-14 21:58:32 +02:00
this.input = ItemStack.EMPTY;
this.slotUpdateListener = () -> {
@@ -0,0 +0,0 @@ public class StonecutterMenu extends AbstractContainerMenu {
@Override
public boolean clickMenuButton(net.minecraft.world.entity.player.Player player, int id) {
if (this.isValidRecipeIndex(id)) {
- this.selectedRecipeIndex.set(id);
2024-10-23 19:46:06 +02:00
- this.setupResultSlot(id);
+ // Paper start - Add PlayerStonecutterRecipeSelectEvent
2021-06-14 21:58:32 +02:00
+ int recipeIndex = id;
+ this.selectedRecipeIndex.set(recipeIndex);
+ this.selectedRecipeIndex.checkAndClearUpdateFlag(); // mark as changed
2024-10-23 19:46:06 +02:00
+ paperEventBlock: if (this.isValidRecipeIndex(id)) {
+ final Optional<RecipeHolder<StonecutterRecipe>> recipe = this.recipesForInput.entries().get(id).recipe().recipe();
+ if (recipe.isEmpty()) break paperEventBlock; // The recipe selected does not have an actual server recipe (presumably its the empty one). Cannot call the event, just break.
+
+ io.papermc.paper.event.player.PlayerStonecutterRecipeSelectEvent event = new io.papermc.paper.event.player.PlayerStonecutterRecipeSelectEvent((Player) player.getBukkitEntity(), getBukkitView().getTopInventory(), (org.bukkit.inventory.StonecuttingRecipe) recipe.get().toBukkitRecipe());
2021-06-14 21:58:32 +02:00
+ if (!event.callEvent()) {
2024-02-01 10:15:57 +01:00
+ player.containerMenu.sendAllDataToRemote();
2021-06-14 21:58:32 +02:00
+ return false;
+ }
2024-02-01 10:15:57 +01:00
+
+ net.minecraft.resources.ResourceLocation key = org.bukkit.craftbukkit.util.CraftNamespacedKey.toMinecraft(event.getStonecuttingRecipe().getKey());
2024-10-23 19:46:06 +02:00
+ if (!recipe.get().id().location().equals(key)) { // If the recipe did NOT stay the same
+ for (int newRecipeIndex = 0; newRecipeIndex < this.recipesForInput.entries().size(); newRecipeIndex++) {
+ if (this.recipesForInput.entries().get(newRecipeIndex).recipe().recipe().filter(r -> r.id().location().equals(key)).isPresent()) {
2021-06-14 21:58:32 +02:00
+ recipeIndex = newRecipeIndex;
+ break;
+ }
+ }
+ }
+ }
2024-02-01 10:15:57 +01:00
+ player.containerMenu.sendAllDataToRemote();
2021-06-14 21:58:32 +02:00
+ this.selectedRecipeIndex.set(recipeIndex); // set new index, so that listeners can read it
2024-10-23 19:46:06 +02:00
+ this.setupResultSlot(recipeIndex);
+ // Paper end - Add PlayerStonecutterRecipeSelectEvent
2021-06-14 21:58:32 +02:00
}
2024-10-23 19:46:06 +02:00
return true;