aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/unapplied/server/0540-Added-PlayerStonecutterRecipeSelectEvent.patch
blob: 9750570e73985ad119a9253b023bdf5ce08b0650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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] Added PlayerStonecutterRecipeSelectEvent

Co-Authored-By: MiniDigger <admin@benndorf.dev>

diff --git a/src/main/java/net/minecraft/world/inventory/StonecutterMenu.java b/src/main/java/net/minecraft/world/inventory/StonecutterMenu.java
index 70ecc3f673ebd56b65ad901e10f40c28368cbfac..aa1d1466d0a7b76967a41d948b7a4114fe06242f 100644
--- a/src/main/java/net/minecraft/world/inventory/StonecutterMenu.java
+++ b/src/main/java/net/minecraft/world/inventory/StonecutterMenu.java
@@ -63,7 +63,7 @@ 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 - allow replication
         this.recipes = Lists.newArrayList();
         this.input = ItemStack.EMPTY;
         this.slotUpdateListener = () -> {
@@ -161,7 +161,29 @@ 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);
+            // Paper start
+            int recipeIndex = id;
+            this.selectedRecipeIndex.set(recipeIndex);
+            this.selectedRecipeIndex.checkAndClearUpdateFlag(); // mark as changed
+            if (this.isValidRecipeIndex(id)) {
+                io.papermc.paper.event.player.PlayerStonecutterRecipeSelectEvent event = new io.papermc.paper.event.player.PlayerStonecutterRecipeSelectEvent((Player) player.getBukkitEntity(), (org.bukkit.inventory.StonecutterInventory) getBukkitView().getTopInventory(), (org.bukkit.inventory.StonecuttingRecipe) this.getRecipes().get(id).toBukkitRecipe());
+                if (!event.callEvent()) {
+                    ((Player) player.getBukkitEntity()).updateInventory();
+                    return false;
+                }
+                int newRecipeIndex;
+                if (!this.getRecipes().get(recipeIndex).id().equals(org.bukkit.craftbukkit.util.CraftNamespacedKey.toMinecraft(event.getStonecuttingRecipe().getKey()))) { // If the recipe did NOT stay the same
+                    for (newRecipeIndex = 0; newRecipeIndex < this.getRecipes().size(); newRecipeIndex++) {
+                        if (this.getRecipes().get(newRecipeIndex).id().equals(org.bukkit.craftbukkit.util.CraftNamespacedKey.toMinecraft(event.getStonecuttingRecipe().getKey()))) {
+                            recipeIndex = newRecipeIndex;
+                            break;
+                        }
+                    }
+                }
+            }
+            ((Player) player.getBukkitEntity()).updateInventory();
+            this.selectedRecipeIndex.set(recipeIndex); // set new index, so that listeners can read it
+            // Paper end
             this.setupResultSlot();
         }