aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0661-Added-PlayerStonecutterRecipeSelectEvent.patch
blob: 6444ee5cbef84beb247b17e69de2c5083de6b0de (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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@minidigger.me>

diff --git a/src/main/java/net/minecraft/world/inventory/Container.java b/src/main/java/net/minecraft/world/inventory/Container.java
index bdd00608a72dd81003731ff5fbe774dfdc5220e5..b58ec4abff2840556eb06e08b241a2eaa85c2c7f 100644
--- a/src/main/java/net/minecraft/world/inventory/Container.java
+++ b/src/main/java/net/minecraft/world/inventory/Container.java
@@ -118,7 +118,7 @@ public abstract class Container {
         return slot;
     }
 
-    protected ContainerProperty a(ContainerProperty containerproperty) {
+    protected ContainerProperty addDataSlot(ContainerProperty containerproperty) { return a(containerproperty); } protected ContainerProperty a(ContainerProperty containerproperty) { // Paper - OBFHELPER
         this.d.add(containerproperty);
         return containerproperty;
     }
diff --git a/src/main/java/net/minecraft/world/inventory/ContainerProperty.java b/src/main/java/net/minecraft/world/inventory/ContainerProperty.java
index 67c3b7ddb0b0f10c82577cbea7506c9d80d41368..b9c82e68a811c2ef6bf2d5ce3ae9f858bcb263d5 100644
--- a/src/main/java/net/minecraft/world/inventory/ContainerProperty.java
+++ b/src/main/java/net/minecraft/world/inventory/ContainerProperty.java
@@ -20,7 +20,7 @@ public abstract class ContainerProperty {
         };
     }
 
-    public static ContainerProperty a(final int[] aint, final int i) {
+    public static ContainerProperty shared(final int[] aint, final int i) { return a(aint, i); } public static ContainerProperty a(final int[] aint, final int i) { // Paper - OBFHELPER
         return new ContainerProperty() {
             @Override
             public int get() {
@@ -54,7 +54,7 @@ public abstract class ContainerProperty {
 
     public abstract void set(int i);
 
-    public boolean c() {
+    public boolean checkAndClearUpdateFlag() { return c(); } public boolean c() { // Paper - OBFHELPER
         int i = this.get();
         boolean flag = i != this.a;
 
diff --git a/src/main/java/net/minecraft/world/inventory/ContainerStonecutter.java b/src/main/java/net/minecraft/world/inventory/ContainerStonecutter.java
index 1589d9ca201d386d11d9fd57fa8ba6848bae215c..dc667dd5d8514ae4c8a2087cb913f44320ebfc48 100644
--- a/src/main/java/net/minecraft/world/inventory/ContainerStonecutter.java
+++ b/src/main/java/net/minecraft/world/inventory/ContainerStonecutter.java
@@ -22,13 +22,14 @@ import org.bukkit.craftbukkit.inventory.CraftInventoryStonecutter;
 import org.bukkit.craftbukkit.inventory.CraftInventoryView;
 import org.bukkit.entity.Player;
 // CraftBukkit end
+import io.papermc.paper.event.player.PlayerStonecutterRecipeSelectEvent; // Paper
 
 public class ContainerStonecutter extends Container {
 
     private final ContainerAccess containerAccess;
     private final ContainerProperty containerProperty;
     private final World world;
-    private List<RecipeStonecutting> i;
+    private List<RecipeStonecutting> i; public final List<RecipeStonecutting> getRecipes() { return this.i; } // Paper - OBFHELPER
     private ItemStack j;
     private long k;
     final Slot c;
@@ -58,7 +59,7 @@ public class ContainerStonecutter extends Container {
 
     public ContainerStonecutter(int i, PlayerInventory playerinventory, final ContainerAccess containeraccess) {
         super(Containers.STONECUTTER, i);
-        this.containerProperty = ContainerProperty.a();
+        this.containerProperty = addDataSlot(ContainerProperty.shared(new int[1], 0)); // Paper - allow replication
         this.i = Lists.newArrayList();
         this.j = ItemStack.b;
         this.l = () -> {
@@ -136,13 +137,36 @@ public class ContainerStonecutter extends Container {
     @Override
     public boolean a(EntityHuman entityhuman, int i) {
         if (this.d(i)) {
-            this.containerProperty.set(i);
+            // Paper start
+            int recipeIndex = i;
+            this.containerProperty.set(recipeIndex);
+            this.containerProperty.checkAndClearUpdateFlag(); // mark as changed
+            if (this.isValidRecipeIndex(i)) {
+                PlayerStonecutterRecipeSelectEvent event = new PlayerStonecutterRecipeSelectEvent((Player) entityhuman.getBukkitEntity(), (org.bukkit.inventory.StonecutterInventory) getBukkitView().getTopInventory(), (org.bukkit.inventory.StonecuttingRecipe) this.getRecipes().get(i).toBukkitRecipe());
+                if (!event.callEvent()) {
+                    ((Player) entityhuman.getBukkitEntity()).updateInventory();
+                    return false;
+                }
+                int newRecipeIndex;
+                if (!this.getRecipes().get(recipeIndex).getKey().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).getKey().equals(org.bukkit.craftbukkit.util.CraftNamespacedKey.toMinecraft(event.getStonecuttingRecipe().getKey()))) {
+                            recipeIndex = newRecipeIndex;
+                            break;
+                        }
+                    }
+                }
+            }
+            ((Player) entityhuman.getBukkitEntity()).updateInventory();
+            this.containerProperty.set(recipeIndex); // set new index, so that listeners can read it
+            // Paper end
             this.i();
         }
 
         return true;
     }
 
+    private boolean isValidRecipeIndex(int index) { return this.d(index); } // Paper - OBFHELPER
     private boolean d(int i) {
         return i >= 0 && i < this.i.size();
     }