Solved Make bees active at night

Discussion in 'Spigot Plugin Development' started by eccentric, Dec 19, 2019.

  1. Probably a long shot as early days for 1.15.x, but is there any way to make bees active during the night time?
    I know that bees are stored in a hive's NBT data and the bees have values for how long they stay in the hive:
    From the Minecraft wiki (https://minecraft.gamepedia.com/Beehive#Block_entity):
    I think I can get the NBT via the hive blockstate's persistent data container (though that raises another question **), but not sure how to get the list of bees from the container. EDIT my thinking is totally backwards here - persistent data containers are part of the NBT data not the other way around... so I just need to get the hive's NBT data and read that - see solution below!

    **what Namedspaced key do I use to check for / get a beehive's list of bees?
    Code (Text):
    NamespacedKey.minecraft("beehive")
    ?
    I thinking I can set one or both of the values shown above and that might make the bees leave the hive... then I could use some NMS to keep the bee outside for a period:
    Code (Text):
    EntityBee bee = ((CraftBee) b).getHandle();
    bee.setCannotEnterHiveTicks(3600);
    Any help / thoughts appreciated...
     
    #1 eccentric, Dec 19, 2019
    Last edited: Dec 22, 2019
  2. Well setting the TicksInHive value didn't seem to work, so I used some of the TileEntityBeehive code to force the bees out of the hive then set their CannotEnterHiveTicks value to a random amount once every Minecraft day. Seems to do the trick.
    Code (Text):
    public void releaseBees(Block block) {
        if (block.getType().equals(Material.BEEHIVE) || block.getType().equals(Material.BEE_NEST)) { // must be a beehive
            CraftWorld craftWorld = (CraftWorld) block.getWorld();
            // get the tile entity
            TileEntityBeehive beehive = (TileEntityBeehive) craftWorld.getHandle().getTileEntity(new BlockPosition(block.getX(), block.getY(), block.getZ()));
            IBlockData blockData = beehive.getBlock();
            EnumDirection direction = blockData.get(BlockBeehive.b);
            BlockPosition blockPosition = beehive.getPosition();
            // get the NBT data
            NBTTagCompound tileNBT = new NBTTagCompound();
            beehive.save(tileNBT);
            // get the bees
            NBTTagList bees = tileNBT.getList("Bees", 10);
            for (int i = 0; i < bees.size(); i++) {
                NBTTagCompound bee = bees.getCompound(i);
                // get the bee entity data
                NBTTagCompound data = (NBTTagCompound) bee.get("EntityData");
                data.remove("Passengers");
                data.remove("Leash");
                data.c("UUID");
                // create an entity
                Entity entity = EntityTypes.a(data, beehive.getWorld(), (var0x) -> var0x);
                if (entity != null) {
                    float width = entity.getWidth();
                    double offset = 0.55D + (double) (width / 2.0F);
                    double x = (double) blockPosition.getX() + 0.5D + offset * (double) direction.getAdjacentX();
                    double y = (double) blockPosition.getY() + 0.5D - (double) (entity.getHeight() / 2.0F);
                    double z = (double) blockPosition.getZ() + 0.5D + offset * (double) direction.getAdjacentZ();
                    // set the entity's position
                    entity.setPositionRotation(x, y, z, entity.yaw, entity.pitch);
                    if (entity instanceof EntityBee) {
                        EntityBee entityBee = (EntityBee) entity;
                        if (!entityBee.hasFlowerPos() && beehive.getWorld().random.nextFloat() < 0.9F) {
                           entityBee.setFlowerPos(beehive.flowerPos);
                        }
                        entityBee.eG();
                        // update the hive with more honey levels
                        int level = beehive.a(blockData);
                        if (level < 5) {
                            int amount = beehive.getWorld().random.nextInt(100) == 0 ? 2 : 1;
                            if (level + amount > 5) {
                                --amount;
                            }
                            beehive.getWorld().setTypeUpdate(blockPosition, blockData.set(BlockBeehive.c, level + amount));
                        }
                        entityBee.eu();
                     }
                     // spawn the bee - listen for the event in a CreatureSpawnEvent
                     beehive.getWorld().playSound(null, blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), SoundEffects.BLOCK_BEEHIVE_EXIT, SoundCategory.BLOCKS, 1.0F, 1.0F);
                     beehive.getWorld().addEntity(entity);
                     bees.remove(bee);
                }
            }
            // remove the bees from the hive NBT data
            tileNBT.set("Bees", bees);
            beehive.load(tileNBT);
            beehive.update();
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onEntitySpawn(CreatureSpawnEvent event) {
        SpawnReason spawnReason = event.getSpawnReason();
        if (spawnReason.equals(SpawnReason.BEEHIVE) || (spawnReason.equals(SpawnReason.DEFAULT) && event.getEntity() instanceof Bee)) {
           EntityBee bee = ((CraftBee) event.getEntity()).getHandle();
           bee.setCannotEnterHiveTicks(bee.getWorld().random.nextInt(1200) + 1200);
        }
    }
     
    #3 eccentric, Dec 22, 2019
    Last edited: Dec 22, 2019
    • Winner Winner x 2
  3. Love how your mind works! Happy winter Solstice!