Add PrepareResultEvent (#3776)

Adds a new event for all crafting stations that generate a result slot item

Anvil and Grindstone now extend this event
This commit is contained in:
BillyGalbreath 2020-07-04 22:36:46 -05:00
parent 3de79cd7c3
commit b7c653f532
4 changed files with 299 additions and 99 deletions

View file

@ -1,66 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jan Tuck <jan@tuck.dk>
Date: Sat, 20 Jun 2020 22:34:21 +0200
Subject: [PATCH] Add PrepareGrindstoneEvent
diff --git a/src/main/java/com/destroystokyo/paper/event/inventory/PrepareGrindstoneEvent.java b/src/main/java/com/destroystokyo/paper/event/inventory/PrepareGrindstoneEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/inventory/PrepareGrindstoneEvent.java
@@ -0,0 +0,0 @@
+package com.destroystokyo.paper.event.inventory;
+
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.inventory.InventoryEvent;
+import org.bukkit.inventory.GrindstoneInventory;
+import org.bukkit.inventory.InventoryView;
+import org.bukkit.inventory.ItemStack;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Called when an item is put in a slot for grinding in a Grindstone
+ */
+public class PrepareGrindstoneEvent extends InventoryEvent {
+
+ private static final HandlerList handlers = new HandlerList();
+ private ItemStack result;
+
+ public PrepareGrindstoneEvent(@NotNull InventoryView inventory, @Nullable ItemStack result) {
+ super(inventory);
+ this.result = result;
+ }
+
+ @NotNull
+ @Override
+ public GrindstoneInventory getInventory() {
+ return (GrindstoneInventory) super.getInventory();
+ }
+
+ /**
+ * Get result item, may be null.
+ *
+ * @return result item
+ */
+ @Nullable
+ public ItemStack getResult() {
+ return result;
+ }
+
+ public void setResult(@Nullable ItemStack result) {
+ this.result = result;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}

View file

@ -0,0 +1,155 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Fri, 3 Jul 2020 11:58:56 -0500
Subject: [PATCH] Add PrepareResultEvent / PrepareGrindstoneEvent
Adds a new event for all crafting stations that generate a result slot item
Anvil and Grindstone now extend this event
Grindstone is a backwards compat from a previous PrepareGrindstoneEvent
diff --git a/src/main/java/com/destroystokyo/paper/event/inventory/PrepareGrindstoneEvent.java b/src/main/java/com/destroystokyo/paper/event/inventory/PrepareGrindstoneEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/inventory/PrepareGrindstoneEvent.java
@@ -0,0 +0,0 @@
+package com.destroystokyo.paper.event.inventory;
+
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.inventory.InventoryEvent;
+import org.bukkit.inventory.GrindstoneInventory;
+import org.bukkit.inventory.InventoryView;
+import org.bukkit.inventory.ItemStack;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Called when an item is put in a slot for grinding in a Grindstone
+ * @see com.destroystokyo.paper.event.inventory.PrepareResultEvent
+ */
+@Deprecated
+public class PrepareGrindstoneEvent extends PrepareResultEvent {
+
+ public PrepareGrindstoneEvent(@NotNull InventoryView inventory, @Nullable ItemStack result) {
+ super(inventory, result);
+ }
+
+ @NotNull
+ @Override
+ public GrindstoneInventory getInventory() {
+ return (GrindstoneInventory) super.getInventory();
+ }
+
+}
diff --git a/src/main/java/com/destroystokyo/paper/event/inventory/PrepareResultEvent.java b/src/main/java/com/destroystokyo/paper/event/inventory/PrepareResultEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/inventory/PrepareResultEvent.java
@@ -0,0 +0,0 @@
+package com.destroystokyo.paper.event.inventory;
+
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.inventory.InventoryEvent;
+import org.bukkit.event.inventory.InventoryType;
+import org.bukkit.inventory.InventoryView;
+import org.bukkit.inventory.ItemStack;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Called when an item is put in an inventory containing a result slot
+ */
+public class PrepareResultEvent extends InventoryEvent {
+
+ private static final HandlerList handlers = new HandlerList();
+ private ItemStack result;
+
+ public PrepareResultEvent(@NotNull InventoryView inventory, @Nullable ItemStack result) {
+ super(inventory);
+ this.result = result;
+ }
+
+ /**
+ * Get result item, may be null.
+ *
+ * @return result item
+ */
+ @Nullable
+ public ItemStack getResult() {
+ return result;
+ }
+
+ public void setResult(@Nullable ItemStack result) {
+ this.result = result;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
diff --git a/src/main/java/org/bukkit/event/inventory/PrepareAnvilEvent.java b/src/main/java/org/bukkit/event/inventory/PrepareAnvilEvent.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/event/inventory/PrepareAnvilEvent.java
+++ b/src/main/java/org/bukkit/event/inventory/PrepareAnvilEvent.java
@@ -0,0 +0,0 @@
package org.bukkit.event.inventory;
+import com.destroystokyo.paper.event.inventory.PrepareResultEvent;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.InventoryView;
@@ -0,0 +0,0 @@ import org.jetbrains.annotations.Nullable;
/**
* Called when an item is put in a slot for repair by an anvil.
*/
-public class PrepareAnvilEvent extends InventoryEvent {
+// Paper start - extend PrepareResultEvent
+public class PrepareAnvilEvent extends PrepareResultEvent {
- private static final HandlerList handlers = new HandlerList();
- private ItemStack result;
+ //private static final HandlerList handlers = new HandlerList();
+ //private ItemStack result;
public PrepareAnvilEvent(@NotNull InventoryView inventory, @Nullable ItemStack result) {
- super(inventory);
- this.result = result;
+ super(inventory, result);
+ //this.result = result;
+ // Paper end
}
@NotNull
@@ -0,0 +0,0 @@ public class PrepareAnvilEvent extends InventoryEvent {
*/
@Nullable
public ItemStack getResult() {
- return result;
+ return super.getResult(); // Paper
}
public void setResult(@Nullable ItemStack result) {
- this.result = result;
+ super.setResult(result); // Paper
}
+ /* // Paper - comment out
@NotNull
@Override
public HandlerList getHandlers() {
@@ -0,0 +0,0 @@ public class PrepareAnvilEvent extends InventoryEvent {
public static HandlerList getHandlerList() {
return handlers;
}
+ */ // Paper
}

View file

@ -1,33 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jan Tuck <jan@tuck.dk>
Date: Sat, 20 Jun 2020 22:34:20 +0200
Subject: [PATCH] Add PrepareGrindstoneEvent
diff --git a/src/main/java/net/minecraft/server/ContainerGrindstone.java b/src/main/java/net/minecraft/server/ContainerGrindstone.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ContainerGrindstone.java
+++ b/src/main/java/net/minecraft/server/ContainerGrindstone.java
@@ -0,0 +0,0 @@ import org.bukkit.craftbukkit.inventory.CraftInventoryGrindstone;
import org.bukkit.craftbukkit.inventory.CraftInventoryView;
import org.bukkit.entity.Player;
// CraftBukkit end
+// Paper start
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import com.destroystokyo.paper.event.inventory.PrepareGrindstoneEvent;
+// Paper end
public class ContainerGrindstone extends Container {
@@ -0,0 +0,0 @@ public class ContainerGrindstone extends Container {
super.a(iinventory);
if (iinventory == this.craftInventory) {
this.e();
+ // Paper start
+ PrepareGrindstoneEvent event = new PrepareGrindstoneEvent(this.getBukkitView(), CraftItemStack.asBukkitCopy(this.resultInventory.getItem(0)));
+ event.callEvent();
+ this.resultInventory.setItem(0, CraftItemStack.asNMSCopy(event.getResult()));
+ // Paper end
}
}

View file

@ -0,0 +1,144 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
Date: Fri, 3 Jul 2020 11:58:56 -0500
Subject: [PATCH] Add PrepareResultEvent
Adds a new event for all crafting stations that generate a result slot item
Anvil and Grindstone now extend this event
diff --git a/src/main/java/net/minecraft/server/ContainerAnvil.java b/src/main/java/net/minecraft/server/ContainerAnvil.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ContainerAnvil.java
+++ b/src/main/java/net/minecraft/server/ContainerAnvil.java
@@ -0,0 +0,0 @@ public class ContainerAnvil extends ContainerAnvilAbstract {
}
this.e();
+
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareResultEvent(getBukkitView(), this.resultInventory.getItem(0), 2); // Paper
}
// CraftBukkit start
diff --git a/src/main/java/net/minecraft/server/ContainerAnvilAbstract.java b/src/main/java/net/minecraft/server/ContainerAnvilAbstract.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ContainerAnvilAbstract.java
+++ b/src/main/java/net/minecraft/server/ContainerAnvilAbstract.java
@@ -0,0 +0,0 @@ public abstract class ContainerAnvilAbstract extends Container {
this.e();
}
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareResultEvent(getBukkitView(), this.resultInventory.getItem(0), 2); // Paper
}
@Override
diff --git a/src/main/java/net/minecraft/server/ContainerCartography.java b/src/main/java/net/minecraft/server/ContainerCartography.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ContainerCartography.java
+++ b/src/main/java/net/minecraft/server/ContainerCartography.java
@@ -0,0 +0,0 @@ public class ContainerCartography extends Container {
this.a(itemstack, itemstack1, itemstack2);
}
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareResultEvent(getBukkitView(), this.resultInventory.getItem(0), 2); // Paper
}
private void a(ItemStack itemstack, ItemStack itemstack1, ItemStack itemstack2) {
diff --git a/src/main/java/net/minecraft/server/ContainerGrindstone.java b/src/main/java/net/minecraft/server/ContainerGrindstone.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ContainerGrindstone.java
+++ b/src/main/java/net/minecraft/server/ContainerGrindstone.java
@@ -0,0 +0,0 @@ import org.bukkit.craftbukkit.inventory.CraftInventoryGrindstone;
import org.bukkit.craftbukkit.inventory.CraftInventoryView;
import org.bukkit.entity.Player;
// CraftBukkit end
+// Paper start
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import com.destroystokyo.paper.event.inventory.PrepareGrindstoneEvent;
+// Paper end
public class ContainerGrindstone extends Container {
@@ -0,0 +0,0 @@ public class ContainerGrindstone extends Container {
super.a(iinventory);
if (iinventory == this.craftInventory) {
this.e();
+ // Paper start
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareResultEvent(getBukkitView(), this.resultInventory.getItem(0), 2); // Paper
+ // Paper end
}
}
diff --git a/src/main/java/net/minecraft/server/ContainerLoom.java b/src/main/java/net/minecraft/server/ContainerLoom.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ContainerLoom.java
+++ b/src/main/java/net/minecraft/server/ContainerLoom.java
@@ -0,0 +0,0 @@ public class ContainerLoom extends Container {
this.j();
this.c();
+
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareResultEvent(getBukkitView(), this.resultInventory.getItem(0), 3); // Paper
}
@Override
diff --git a/src/main/java/net/minecraft/server/ContainerSmithing.java b/src/main/java/net/minecraft/server/ContainerSmithing.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ContainerSmithing.java
+++ b/src/main/java/net/minecraft/server/ContainerSmithing.java
@@ -0,0 +0,0 @@ public class ContainerSmithing extends ContainerAnvilAbstract {
this.resultInventory.setItem(0, itemstack);
}
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareResultEvent(getBukkitView(), this.resultInventory.getItem(0), 2); // Paper
}
@Override
diff --git a/src/main/java/net/minecraft/server/ContainerStonecutter.java b/src/main/java/net/minecraft/server/ContainerStonecutter.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/ContainerStonecutter.java
+++ b/src/main/java/net/minecraft/server/ContainerStonecutter.java
@@ -0,0 +0,0 @@ public class ContainerStonecutter extends Container {
this.a(iinventory, itemstack);
}
+ org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareResultEvent(getBukkitView(), this.resultInventory.getItem(0), 1); // Paper
}
private void a(IInventory iinventory, ItemStack itemstack) {
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -0,0 +0,0 @@ public class CraftEventFactory {
return event;
}
- public static PrepareAnvilEvent callPrepareAnvilEvent(InventoryView view, ItemStack item) {
+ public static void callPrepareAnvilEvent(InventoryView view, ItemStack item) { // Paper - verify nothing uses return - handled below in PrepareResult
+ if (true) return; // Paper - nothing
PrepareAnvilEvent event = new PrepareAnvilEvent(view, CraftItemStack.asCraftMirror(item).clone());
event.getView().getPlayer().getServer().getPluginManager().callEvent(event);
event.getInventory().setItem(2, event.getResult());
+ //return event; // Paper
+ }
+
+ // Paper start - support specific overrides for prepare result
+ public static com.destroystokyo.paper.event.inventory.PrepareResultEvent callPrepareResultEvent(InventoryView view, ItemStack item, int resultSlot) {
+ com.destroystokyo.paper.event.inventory.PrepareResultEvent event;
+ CraftItemStack result = CraftItemStack.asCraftMirror(item).clone();
+ if (view.getTopInventory() instanceof org.bukkit.inventory.AnvilInventory) {
+ event = new PrepareAnvilEvent(view, result);
+ } else if (view.getTopInventory() instanceof org.bukkit.inventory.GrindstoneInventory) {
+ event = new com.destroystokyo.paper.event.inventory.PrepareGrindstoneEvent(view, result);
+ } else {
+ event = new com.destroystokyo.paper.event.inventory.PrepareResultEvent(view, result);
+ }
+ event.callEvent();
+ event.getInventory().setItem(resultSlot, event.getResult());
return event;
}
+ // Paper end
/**
* Mob spawner event.