From 2ba17103b986988cf485fa9c5b844d7ea8c4d1d4 Mon Sep 17 00:00:00 2001
From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com>
Date: Wed, 3 May 2023 13:07:51 +0200
Subject: [PATCH] Fix some NPE for EntityCompostItemEvent (#9169)

---
 patches/api/More-Teleport-API.patch           | 10 +++----
 ...ItemEvent-and-EntityCompostItemEvent.patch | 26 +++++++++----------
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/patches/api/More-Teleport-API.patch b/patches/api/More-Teleport-API.patch
index 5c5193817f..f0bcaf6b61 100644
--- a/patches/api/More-Teleport-API.patch
+++ b/patches/api/More-Teleport-API.patch
@@ -12,14 +12,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 @@ -0,0 +0,0 @@
 +package io.papermc.paper.entity;
 +
-+import org.bukkit.Location;
++import io.papermc.paper.math.Position;
 +import org.bukkit.entity.Entity;
 +import org.bukkit.entity.LivingEntity;
 +
 +/**
-+ * Represents what part of the entity should be used when determining where to face a location/entity.
++ * Represents what part of the entity should be used when determining where to face a position/entity.
 + *
-+ * @see org.bukkit.entity.Player#lookAt(Location, LookAnchor)
++ * @see org.bukkit.entity.Player#lookAt(Position, LookAnchor)
 + * @see org.bukkit.entity.Player#lookAt(Entity, LookAnchor, LookAnchor)
 + */
 +@org.jetbrains.annotations.ApiStatus.Experimental
@@ -194,10 +194,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    void lookAt(double x, double y, double z, @NotNull io.papermc.paper.entity.LookAnchor playerAnchor);
 +
 +    /**
-+     * Causes the player to look towards the given location.
++     * Causes the player to look towards the given position.
 +     *
 +     * @param position Position to look at in the player's current world
-+     * @param playerAnchor What part of player should face the location
++     * @param playerAnchor What part of the player should face the given position
 +     */
 +    @org.jetbrains.annotations.ApiStatus.Experimental
 +    default void lookAt(@NotNull io.papermc.paper.math.Position position, @NotNull io.papermc.paper.entity.LookAnchor playerAnchor) {
diff --git a/patches/server/Add-CompostItemEvent-and-EntityCompostItemEvent.patch b/patches/server/Add-CompostItemEvent-and-EntityCompostItemEvent.patch
index 844256de70..06a56fa82b 100644
--- a/patches/server/Add-CompostItemEvent-and-EntityCompostItemEvent.patch
+++ b/patches/server/Add-CompostItemEvent-and-EntityCompostItemEvent.patch
@@ -9,27 +9,27 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 --- a/src/main/java/net/minecraft/world/level/block/ComposterBlock.java
 +++ b/src/main/java/net/minecraft/world/level/block/ComposterBlock.java
 @@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder {
-                     return InteractionResult.sidedSuccess(world.isClientSide);
-                 }
-                 BlockState iblockdata1 = ComposterBlock.addItem(player, state, world, pos, itemstack, rand);
-+                if (iblockdata1 == null) {
+                 // Paper start - EntityChangeBlockEvent
+                 double rand = world.getRandom().nextDouble();
+                 BlockState dummyBlockState = ComposterBlock.addItem(player, state, org.bukkit.craftbukkit.util.DummyGeneratorAccess.INSTANCE, pos, itemstack, rand);
++                if (dummyBlockState == null) {
 +                    return InteractionResult.PASS;
 +                }
-                 // Paper end
- 
-                 world.levelEvent(1500, pos, state != iblockdata1 ? 1 : 0);
+                 if (state != dummyBlockState && org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(player, pos, dummyBlockState).isCancelled()) { // if block state will change and event cancelled
+                     return InteractionResult.sidedSuccess(world.isClientSide);
+                 }
 @@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder {
-                 return state;
-             }
-             iblockdata1 = ComposterBlock.addItem(user, state, world, pos, stack, rand);
+             // CraftBukkit start
+             double rand = world.getRandom().nextDouble();
+             BlockState iblockdata1 = ComposterBlock.addItem(user, state, DummyGeneratorAccess.INSTANCE, pos, stack, rand);
 +            // Paper start
 +            if (iblockdata1 == null) {
 +                return state;
 +            }
 +            // Paper end
-             // CraftBukkit end
- 
-             stack.shrink(1);
+             if (state == iblockdata1 || org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(user, pos, iblockdata1).isCancelled()) {
+                 return state;
+             }
 @@ -0,0 +0,0 @@ public class ComposterBlock extends Block implements WorldlyContainerHolder {
          int i = (Integer) iblockdata.getValue(ComposterBlock.LEVEL);
          float f = ComposterBlock.COMPOSTABLES.getFloat(itemstack.getItem());