Skip to content

Commit

Permalink
add PlayerSetTrialSpawnerTypeWithEggEvent, fixes #1546 (#1547)
Browse files Browse the repository at this point in the history
  • Loading branch information
Onako2 authored Jul 4, 2024
1 parent d5c06b4 commit faa1f93
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
91 changes: 91 additions & 0 deletions patches/api/0013-PlayerSetSpawnerTypeWithEggEvent.patch
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,94 @@ index 0000000000000000000000000000000000000000..519809eab5d926dc7b0a7bad5d446d0d
+ return handlers;
+ }
+}
diff --git a/src/main/java/org/purpurmc/purpur/event/PlayerSetTrialSpawnerTypeWithEggEvent.java b/src/main/java/org/purpurmc/purpur/event/PlayerSetTrialSpawnerTypeWithEggEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..c8d206abf8f6a08f746322e63d198a368311f28d
--- /dev/null
+++ b/src/main/java/org/purpurmc/purpur/event/PlayerSetTrialSpawnerTypeWithEggEvent.java
@@ -0,0 +1,85 @@
+package org.purpurmc.purpur.event;
+
+import org.bukkit.block.Block;
+import org.bukkit.block.TrialSpawner;
+import org.bukkit.entity.EntityType;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent;
+import org.jetbrains.annotations.NotNull;
+
+public class PlayerSetTrialSpawnerTypeWithEggEvent extends PlayerEvent implements Cancellable {
+ private static final HandlerList handlers = new HandlerList();
+ private final Block block;
+ private final TrialSpawner spawner;
+ private EntityType type;
+ private boolean cancel;
+
+ public PlayerSetTrialSpawnerTypeWithEggEvent(@NotNull Player player, @NotNull Block block, @NotNull TrialSpawner spawner, @NotNull EntityType type) {
+ super(player);
+ this.block = block;
+ this.spawner = spawner;
+ this.type = type;
+ }
+
+ /**
+ * Get the spawner Block in the world
+ *
+ * @return Spawner Block
+ */
+ @NotNull
+ public Block getBlock() {
+ return block;
+ }
+
+ /**
+ * Get the spawner state
+ *
+ * @return Spawner state
+ */
+ @NotNull
+ public TrialSpawner getSpawner() {
+ return spawner;
+ }
+
+ /**
+ * Gets the EntityType being set on the spawner
+ *
+ * @return EntityType being set
+ */
+ @NotNull
+ public EntityType getEntityType() {
+ return type;
+ }
+
+ /**
+ * Sets the EntityType being set on the spawner
+ *
+ * @param type EntityType to set
+ */
+ public void setEntityType(@NotNull EntityType type) {
+ this.type = type;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancel;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancel = cancel;
+ }
+
+ @Override
+ @NotNull
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
23 changes: 16 additions & 7 deletions patches/server/0014-PlayerSetSpawnerTypeWithEggEvent.patch
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,30 @@ index cb61462d4691a055a4b25f7b953609d8a154fdfe..9d6ca8c6f009cb34fab07cc4d17a6dd5
if (this.descriptionId == null) {
this.descriptionId = Util.makeDescriptionId("entity", BuiltInRegistries.ENTITY_TYPE.getKey(this));
diff --git a/src/main/java/net/minecraft/world/item/SpawnEggItem.java b/src/main/java/net/minecraft/world/item/SpawnEggItem.java
index 9cea8da84f39bb3f687139ef213ccea358724dee..d43be19fdb4ca917d08f84cda5a67667960f28fe 100644
index 9cea8da84f39bb3f687139ef213ccea358724dee..c513e3b414a1b87b1aa37bb5d51fc2dd0fae1c54 100644
--- a/src/main/java/net/minecraft/world/item/SpawnEggItem.java
+++ b/src/main/java/net/minecraft/world/item/SpawnEggItem.java
@@ -74,6 +74,15 @@ public class SpawnEggItem extends Item {
@@ -74,6 +74,24 @@ public class SpawnEggItem extends Item {
Spawner spawner = (Spawner) tileentity;

entitytypes = this.getType(itemstack);
+
+ // Purpur start
+ org.bukkit.block.Block bukkitBlock = world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ org.purpurmc.purpur.event.PlayerSetSpawnerTypeWithEggEvent event = new org.purpurmc.purpur.event.PlayerSetSpawnerTypeWithEggEvent((org.bukkit.entity.Player) context.getPlayer().getBukkitEntity(), bukkitBlock, (org.bukkit.block.CreatureSpawner) bukkitBlock.getState(), org.bukkit.entity.EntityType.fromName(entitytypes.getName()));
+ if (!event.callEvent()) {
+ return InteractionResult.FAIL;
+ if (spawner instanceof net.minecraft.world.level.block.entity.SpawnerBlockEntity) {
+ org.bukkit.block.Block bukkitBlock = world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ org.purpurmc.purpur.event.PlayerSetSpawnerTypeWithEggEvent event = new org.purpurmc.purpur.event.PlayerSetSpawnerTypeWithEggEvent((org.bukkit.entity.Player) context.getPlayer().getBukkitEntity(), bukkitBlock, (org.bukkit.block.CreatureSpawner) bukkitBlock.getState(), org.bukkit.entity.EntityType.fromName(entitytypes.getName()));
+ if (!event.callEvent()) {
+ return InteractionResult.FAIL;
+ }
+ entitytypes = EntityType.getFromBukkitType(event.getEntityType());
+ } else if (spawner instanceof net.minecraft.world.level.block.entity.TrialSpawnerBlockEntity) {
+ org.bukkit.block.Block bukkitBlock = world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ org.purpurmc.purpur.event.PlayerSetTrialSpawnerTypeWithEggEvent event = new org.purpurmc.purpur.event.PlayerSetTrialSpawnerTypeWithEggEvent((org.bukkit.entity.Player) context.getPlayer().getBukkitEntity(), bukkitBlock, (org.bukkit.block.TrialSpawner) bukkitBlock.getState(), org.bukkit.entity.EntityType.fromName(entitytypes.getName()));
+ if (!event.callEvent()) {
+ return InteractionResult.FAIL;
+ }
+ entitytypes = EntityType.getFromBukkitType(event.getEntityType());
+ }
+ entitytypes = EntityType.getFromBukkitType(event.getEntityType());
+ // Purpur end
spawner.setEntityId(entitytypes, world.getRandom());
world.sendBlockUpdated(blockposition, iblockdata, iblockdata, 3);
Expand Down

0 comments on commit faa1f93

Please sign in to comment.