From 708be695398c23fe230cbaf7a5c21e6f1e150533 Mon Sep 17 00:00:00 2001 From: ShaneBee Date: Sun, 9 Feb 2020 10:47:11 +1100 Subject: [PATCH] SPIGOT-5468: Improve Beehive TileEntity API --- nms-patches/EntityBee.patch | 9 ++ nms-patches/TileEntityBeehive.patch | 92 ++++++++++++++++++- .../craftbukkit/block/CraftBeehive.java | 54 +++++++++++ .../bukkit/craftbukkit/entity/CraftBee.java | 10 ++ 4 files changed, 160 insertions(+), 5 deletions(-) diff --git a/nms-patches/EntityBee.patch b/nms-patches/EntityBee.patch index a1aa32f1eb..9ede031bb7 100644 --- a/nms-patches/EntityBee.patch +++ b/nms-patches/EntityBee.patch @@ -1,5 +1,14 @@ --- a/net/minecraft/server/EntityBee.java +++ b/net/minecraft/server/EntityBee.java +@@ -21,7 +21,7 @@ + private float bA; + private int bB; + private int ticksSincePollination; +- private int cannotEnterHiveTicks; ++ public int cannotEnterHiveTicks; // PAIL private -> public + private int numCropsGrownSincePollination; + private int bF = 0; + private int bG = 0; @@ -152,12 +152,12 @@ } diff --git a/nms-patches/TileEntityBeehive.patch b/nms-patches/TileEntityBeehive.patch index 35c932a8c6..2258fcae05 100644 --- a/nms-patches/TileEntityBeehive.patch +++ b/nms-patches/TileEntityBeehive.patch @@ -1,9 +1,48 @@ --- a/net/minecraft/server/TileEntityBeehive.java +++ b/net/minecraft/server/TileEntityBeehive.java -@@ -108,6 +108,18 @@ +@@ -10,6 +10,7 @@ + private final List bees = Lists.newArrayList(); + @Nullable + public BlockPosition flowerPos = null; ++ public int maxBees = 3; // CraftBukkit - allow setting max amount of bees a hive can hold + + public TileEntityBeehive() { + super(TileEntityTypes.BEEHIVE); +@@ -49,7 +50,7 @@ + } + + public boolean isFull() { +- return this.bees.size() == 3; ++ return this.bees.size() == this.maxBees; // CraftBukkit + } + + public void a(@Nullable EntityHuman entityhuman, IBlockData iblockdata, TileEntityBeehive.ReleaseStatus tileentitybeehive_releasestatus) { +@@ -77,11 +78,17 @@ + + } + +- private List a(IBlockData iblockdata, TileEntityBeehive.ReleaseStatus tileentitybeehive_releasestatus) { ++ // CraftBukkit start - This allows us to bypass the night/rain/emergency check ++ private List a(IBlockData iblockdata, TileEntityBeehive.ReleaseStatus tileentitybeehive_releasestatus) { // PAIL rename releaseBees ++ return releaseBees(iblockdata, tileentitybeehive_releasestatus, false); ++ } ++ ++ public List releaseBees(IBlockData iblockdata, TileEntityBeehive.ReleaseStatus tileentitybeehive_releasestatus, boolean force) { + List list = Lists.newArrayList(); + + this.bees.removeIf((tileentitybeehive_hivebee) -> { +- return this.a(iblockdata, tileentitybeehive_hivebee.entityData, list, tileentitybeehive_releasestatus); ++ return this.releaseBee(iblockdata, tileentitybeehive_hivebee.entityData, list, tileentitybeehive_releasestatus, force); ++ // CraftBukkit end + }); + return list; + } +@@ -107,7 +114,19 @@ + } public void a(Entity entity, boolean flag, int i) { - if (this.bees.size() < 3) { +- if (this.bees.size() < 3) { ++ if (this.bees.size() < this.maxBees) { // CraftBukkit + // CraftBukkit start + if (this.world != null) { + org.bukkit.event.entity.EntityEnterBlockEvent event = new org.bukkit.event.entity.EntityEnterBlockEvent(entity.getBukkitEntity(), org.bukkit.craftbukkit.block.CraftBlock.at(world, getPosition())); @@ -19,7 +58,26 @@ entity.stopRiding(); entity.ejectPassengers(); NBTTagCompound nbttagcompound = new NBTTagCompound(); -@@ -163,6 +175,7 @@ +@@ -132,10 +151,16 @@ + } + } + +- private boolean a(IBlockData iblockdata, NBTTagCompound nbttagcompound, @Nullable List list, TileEntityBeehive.ReleaseStatus tileentitybeehive_releasestatus) { ++ // CraftBukkit start - This allows us to bypass the night/rain/emergency check ++ private boolean a(IBlockData iblockdata, NBTTagCompound nbttagcompound, @Nullable List list, TileEntityBeehive.ReleaseStatus tileentitybeehive_releasestatus) { // PAIL rename releaseBee ++ return releaseBee(iblockdata, nbttagcompound, list, tileentitybeehive_releasestatus, false); ++ } ++ ++ private boolean releaseBee(IBlockData iblockdata, NBTTagCompound nbttagcompound, @Nullable List list, TileEntityBeehive.ReleaseStatus tileentitybeehive_releasestatus, boolean force) { + BlockPosition blockposition = this.getPosition(); + +- if ((this.world.isNight() || this.world.isRaining()) && tileentitybeehive_releasestatus != TileEntityBeehive.ReleaseStatus.EMERGENCY) { ++ if (!force && (this.world.isNight() || this.world.isRaining()) && tileentitybeehive_releasestatus != TileEntityBeehive.ReleaseStatus.EMERGENCY) { ++ // CraftBukkit end + return false; + } else { + nbttagcompound.remove("Passengers"); +@@ -163,6 +188,7 @@ if (!entity.getEntityType().a(TagsEntity.BEEHIVE_INHABITORS)) { return false; } else { @@ -27,7 +85,7 @@ if (entity instanceof EntityBee) { EntityBee entitybee = (EntityBee) entity; -@@ -196,7 +209,7 @@ +@@ -196,7 +222,7 @@ BlockPosition blockposition2 = this.getPosition(); this.world.playSound((EntityHuman) null, (double) blockposition2.getX(), (double) blockposition2.getY(), (double) blockposition2.getZ(), SoundEffects.BLOCK_BEEHIVE_EXIT, SoundCategory.BLOCKS, 1.0F, 1.0F); @@ -36,7 +94,7 @@ } } else { return false; -@@ -223,6 +236,11 @@ +@@ -223,6 +249,11 @@ if (this.a(iblockdata, nbttagcompound, (List) null, tileentitybeehive_releasestatus)) { iterator.remove(); } @@ -48,3 +106,27 @@ } else { tileentitybeehive_hivebee.ticksInHive++; } +@@ -266,6 +297,11 @@ + this.flowerPos = GameProfileSerializer.c(nbttagcompound.getCompound("FlowerPos")); + } + ++ // CraftBukkit start ++ if (nbttagcompound.hasKey("Bukkit.MaxEntities")) { ++ this.maxBees = nbttagcompound.getInt("Bukkit.MaxEntities"); ++ } ++ // CraftBukkit end + } + + @Override +@@ -275,6 +311,11 @@ + if (this.x()) { + nbttagcompound.set("FlowerPos", GameProfileSerializer.a(this.flowerPos)); + } ++ // CraftBukkit start ++ if (this.maxBees != 3) { ++ nbttagcompound.setInt("Bukkit.MaxEntities", this.maxBees); ++ } ++ // CraftBukkit end + + return nbttagcompound; + } diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBeehive.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBeehive.java index 2b815eb86a..717ccd47e2 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBeehive.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBeehive.java @@ -1,12 +1,18 @@ package org.bukkit.craftbukkit.block; import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.List; import net.minecraft.server.BlockPosition; +import net.minecraft.server.Entity; import net.minecraft.server.TileEntityBeehive; +import net.minecraft.server.TileEntityBeehive.ReleaseStatus; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.Beehive; +import org.bukkit.craftbukkit.entity.CraftBee; +import org.bukkit.entity.Bee; public class CraftBeehive extends CraftBlockEntityState implements Beehive { @@ -29,4 +35,52 @@ public class CraftBeehive extends CraftBlockEntityState imple Preconditions.checkArgument(location == null || this.getWorld().equals(location.getWorld()), "Flower must be in same world"); getSnapshot().flowerPos = (location == null) ? null : new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()); } + + @Override + public boolean isFull() { + return getSnapshot().isFull(); + } + + @Override + public boolean isSedated() { + return isPlaced() && getSnapshot().k(); // PAIL rename isSedated + } + + @Override + public int getEntityCount() { + return getSnapshot().j(); // PAIL rename beeCount + } + + @Override + public int getMaxEntities() { + return getSnapshot().maxBees; + } + + @Override + public void setMaxEntities(int max) { + Preconditions.checkArgument(max > 0, "Max bees must be more than 0"); + + getSnapshot().maxBees = max; + } + + @Override + public List releaseEntities() { + List bees = new ArrayList<>(); + + if (isPlaced()) { + TileEntityBeehive beehive = ((TileEntityBeehive) this.getTileEntityFromWorld()); + for (Entity bee : beehive.releaseBees(this.getHandle(), ReleaseStatus.BEE_RELEASED, true)) { + bees.add((Bee) bee.getBukkitEntity()); + } + } + + return bees; + } + + @Override + public void addEntity(Bee entity) { + Preconditions.checkArgument(entity != null, "Entity must not be null"); + + getSnapshot().a(((CraftBee) entity).getHandle(), false); // PAIL rename addBee + } } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftBee.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftBee.java index 9d2936d926..937209b820 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftBee.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftBee.java @@ -82,4 +82,14 @@ public class CraftBee extends CraftAnimals implements Bee { public void setAnger(int anger) { getHandle().setAnger(anger); } + + @Override + public int getCannotEnterHiveTicks() { + return getHandle().cannotEnterHiveTicks; + } + + @Override + public void setCannotEnterHiveTicks(int ticks) { + getHandle().setCannotEnterHiveTicks(ticks); + } }