PaperMC/Spigot-Server-Patches/0662-Added-PlayerStonecutterRecipeSelectEvent.patch

110 lines
5.7 KiB
Diff
Raw Normal View History

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/server/Container.java b/src/main/java/net/minecraft/server/Container.java
index 3de58cd344c37451873aa8a12b5d9e65f390ec8f..a5c92b2b7fe42b81e7feebf8969a08bd7d08e0e6 100644
--- a/src/main/java/net/minecraft/server/Container.java
+++ b/src/main/java/net/minecraft/server/Container.java
@@ -101,7 +101,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/server/ContainerProperty.java b/src/main/java/net/minecraft/server/ContainerProperty.java
index e26b8569340fb0240439ff85d481bd3de19c0ae3..7523c77a914d7e78b4ef08c1ef329d2ef38d3091 100644
--- a/src/main/java/net/minecraft/server/ContainerProperty.java
+++ b/src/main/java/net/minecraft/server/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/server/ContainerStonecutter.java b/src/main/java/net/minecraft/server/ContainerStonecutter.java
index 3506473f9b9f4c747f7b737d9bd02bef8ea6d83e..5a34796fa0f2a3c250722777b9131fee5e8cf04a 100644
--- a/src/main/java/net/minecraft/server/ContainerStonecutter.java
+++ b/src/main/java/net/minecraft/server/ContainerStonecutter.java
@@ -8,13 +8,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;
@@ -44,7 +45,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 = () -> {
@@ -122,13 +123,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();
}