From 76f09be39d7c5064ef4a6862901cf9c705bfb0b3 Mon Sep 17 00:00:00 2001 From: mezz Date: Mon, 4 Sep 2017 13:52:01 -0700 Subject: [PATCH 1/4] Close #1838 Make IHiveFrame's getBeeModifier() ItemStack aware --- gradle.properties | 2 +- .../java/forestry/api/apiculture/IHiveFrame.java | 13 +++++++++++++ .../apiculture/entities/EntityMinecartApiary.java | 8 ++++++-- .../apiculture/inventory/InventoryApiary.java | 8 +++++--- .../forestry/apiculture/items/ItemHiveFrame.java | 5 +++++ .../java/forestry/apiculture/tiles/TileApiary.java | 9 +++++++-- 6 files changed, 37 insertions(+), 8 deletions(-) diff --git a/gradle.properties b/gradle.properties index c892b980f..5e7abb5c9 100755 --- a/gradle.properties +++ b/gradle.properties @@ -11,4 +11,4 @@ ic2_version=2.8.17-ex112 version_major=5 version_minor=5 -version_patch=0 +version_patch=1 diff --git a/src/main/java/forestry/api/apiculture/IHiveFrame.java b/src/main/java/forestry/api/apiculture/IHiveFrame.java index d6266cf84..5cf7fdf25 100644 --- a/src/main/java/forestry/api/apiculture/IHiveFrame.java +++ b/src/main/java/forestry/api/apiculture/IHiveFrame.java @@ -20,5 +20,18 @@ public interface IHiveFrame { */ ItemStack frameUsed(IBeeHousing housing, ItemStack frame, IBee queen, int wear); + /** + * @param frame ItemStack containing the actual frame. + * @return the {@link IBeeModifier} for this frame. + * @since Forestry 5.5.1 + */ + default IBeeModifier getBeeModifier(ItemStack frame) { + return getBeeModifier(); + } + + /** + * @deprecated since Forestry 5.5.1. Use {@link #getBeeModifier(ItemStack)} + */ + @Deprecated IBeeModifier getBeeModifier(); } diff --git a/src/main/java/forestry/apiculture/entities/EntityMinecartApiary.java b/src/main/java/forestry/apiculture/entities/EntityMinecartApiary.java index eb8cc7006..22540a11e 100644 --- a/src/main/java/forestry/apiculture/entities/EntityMinecartApiary.java +++ b/src/main/java/forestry/apiculture/entities/EntityMinecartApiary.java @@ -33,6 +33,7 @@ import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.item.ItemStack; +import net.minecraft.util.Tuple; import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -89,8 +90,11 @@ public class EntityMinecartApiary extends EntityMinecartBeeHousingBase implement beeModifiers.add(beeModifier); - for (IHiveFrame frame : inventory.getFrames()) { - beeModifiers.add(frame.getBeeModifier()); + for (Tuple frame : inventory.getFrames()) { + IHiveFrame hiveFrame = frame.getFirst(); + ItemStack stack = frame.getSecond(); + IBeeModifier beeModifier = hiveFrame.getBeeModifier(stack); + beeModifiers.add(beeModifier); } return beeModifiers; diff --git a/src/main/java/forestry/apiculture/inventory/InventoryApiary.java b/src/main/java/forestry/apiculture/inventory/InventoryApiary.java index a0947685e..181869b2e 100644 --- a/src/main/java/forestry/apiculture/inventory/InventoryApiary.java +++ b/src/main/java/forestry/apiculture/inventory/InventoryApiary.java @@ -22,6 +22,7 @@ import forestry.apiculture.InventoryBeeHousing; import forestry.core.utils.SlotUtil; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.util.Tuple; public class InventoryApiary extends InventoryBeeHousing implements IApiaryInventory { public static final int SLOT_FRAMES_1 = 9; @@ -47,14 +48,15 @@ public class InventoryApiary extends InventoryBeeHousing implements IApiaryInven super.isItemValidForSlot(slotIndex, itemStack); } - public Collection getFrames() { - Collection hiveFrames = new ArrayList<>(SLOT_FRAMES_COUNT); + public Collection> getFrames() { + Collection> hiveFrames = new ArrayList<>(SLOT_FRAMES_COUNT); for (int i = SLOT_FRAMES_1; i < SLOT_FRAMES_1 + SLOT_FRAMES_COUNT; i++) { ItemStack stackInSlot = getStackInSlot(i); Item itemInSlot = stackInSlot.getItem(); if (itemInSlot instanceof IHiveFrame) { - hiveFrames.add((IHiveFrame) itemInSlot); + IHiveFrame frame = (IHiveFrame) itemInSlot; + hiveFrames.add(new Tuple<>(frame, stackInSlot.copy())); } } diff --git a/src/main/java/forestry/apiculture/items/ItemHiveFrame.java b/src/main/java/forestry/apiculture/items/ItemHiveFrame.java index c88089b3b..2639ac273 100755 --- a/src/main/java/forestry/apiculture/items/ItemHiveFrame.java +++ b/src/main/java/forestry/apiculture/items/ItemHiveFrame.java @@ -52,6 +52,11 @@ public class ItemHiveFrame extends ItemForestry implements IHiveFrame { } } + @Override + public IBeeModifier getBeeModifier(ItemStack frame) { + return beeModifier; + } + @Override public IBeeModifier getBeeModifier() { return beeModifier; diff --git a/src/main/java/forestry/apiculture/tiles/TileApiary.java b/src/main/java/forestry/apiculture/tiles/TileApiary.java index 57a5b9882..7b6ef2834 100644 --- a/src/main/java/forestry/apiculture/tiles/TileApiary.java +++ b/src/main/java/forestry/apiculture/tiles/TileApiary.java @@ -29,6 +29,8 @@ import forestry.apiculture.inventory.InventoryApiary; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Tuple; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -58,8 +60,11 @@ public class TileApiary extends TileBeeHousingBase implements IApiary { beeModifiers.add(beeModifier); - for (IHiveFrame frame : inventory.getFrames()) { - beeModifiers.add(frame.getBeeModifier()); + for (Tuple frame : inventory.getFrames()) { + IHiveFrame hiveFrame = frame.getFirst(); + ItemStack stack = frame.getSecond(); + IBeeModifier beeModifier = hiveFrame.getBeeModifier(stack); + beeModifiers.add(beeModifier); } return beeModifiers; From 42bde9e5761f6be2186fa3f29cbf10cdcab3ef4b Mon Sep 17 00:00:00 2001 From: mezz Date: Mon, 4 Sep 2017 14:10:55 -0700 Subject: [PATCH 2/4] Fix #1836 Update minimum required JEI version --- gradle.properties | 8 ++++---- src/main/java/forestry/Forestry.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5e7abb5c9..6e82d3847 100755 --- a/gradle.properties +++ b/gradle.properties @@ -1,13 +1,13 @@ mcversion=1.12.1 -forgeversion=14.22.0.2462 -mcp_mappings=snapshot_20170826 +forgeversion=14.22.0.2469 +mcp_mappings=snapshot_20170904 curse_project_id=59751 tesla_version=1.0.60 tesla_mcversion=1.12 -jei_version=4.7.8.91 +jei_version=4.7.8.92 jei_mcversion=1.12.1 -ic2_version=2.8.17-ex112 +ic2_version=2.8.24-ex112 version_major=5 version_minor=5 diff --git a/src/main/java/forestry/Forestry.java b/src/main/java/forestry/Forestry.java index cc76d3448..b1bab8020 100755 --- a/src/main/java/forestry/Forestry.java +++ b/src/main/java/forestry/Forestry.java @@ -60,7 +60,7 @@ import forestry.plugins.compat.PluginIC2; guiFactory = "forestry.core.config.ForestryGuiConfigFactory", acceptedMinecraftVersions = "[1.12, 1.12.1]", dependencies = "required-after:forge@[14.21.1.2395,);" - + "after:jei@[4.7.0,);" + + "after:jei@[4.7.8.91,);" + "after:" + PluginIC2.modId + ";" + "after:" + PluginNatura.modId + ";") public class Forestry { From 5707d27598106a643a641f808b2728cf92575d62 Mon Sep 17 00:00:00 2001 From: mezz Date: Mon, 4 Sep 2017 14:46:56 -0700 Subject: [PATCH 3/4] Fix #1577 Hives load new chunks when retrogen is active --- .../forestry/core/TickHandlerCoreServer.java | 64 ++++++++++++++----- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/src/main/java/forestry/core/TickHandlerCoreServer.java b/src/main/java/forestry/core/TickHandlerCoreServer.java index 8490e396a..4f9ab7b36 100755 --- a/src/main/java/forestry/core/TickHandlerCoreServer.java +++ b/src/main/java/forestry/core/TickHandlerCoreServer.java @@ -12,18 +12,26 @@ package forestry.core; import com.google.common.collect.LinkedListMultimap; +import java.util.Iterator; import java.util.List; import java.util.Random; +import it.unimi.dsi.fastutil.ints.IntArraySet; +import it.unimi.dsi.fastutil.ints.IntSet; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; +import net.minecraft.world.WorldServer; import net.minecraft.world.chunk.Chunk; +import net.minecraft.world.chunk.IChunkProvider; +import net.minecraft.world.gen.ChunkProviderServer; +import net.minecraft.world.gen.IChunkGenerator; import net.minecraftforge.event.world.ChunkDataEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent.Phase; import net.minecraftforge.fml.common.gameevent.TickEvent.WorldTickEvent; +import net.minecraftforge.fml.common.registry.GameRegistry; import forestry.core.config.Config; import forestry.core.config.Constants; @@ -34,6 +42,7 @@ public class TickHandlerCoreServer { private final WorldGenerator worldGenerator; private final LinkedListMultimap chunkRegenList = LinkedListMultimap.create(); + private final IntSet checkForRetrogen = new IntArraySet(); public TickHandlerCoreServer(WorldGenerator worldGenerator) { this.worldGenerator = worldGenerator; @@ -54,27 +63,49 @@ public class TickHandlerCoreServer { } } - if (Config.doRetrogen) { - World world = event.world; + if (Config.doRetrogen && event.world instanceof WorldServer) { + WorldServer world = (WorldServer) event.world; int dimensionID = world.provider.getDimension(); - List chunkList = chunkRegenList.get(dimensionID); - - if (!chunkList.isEmpty()) { - ChunkCoords coords = chunkList.get(0); - chunkList.remove(0); - - // This bit is from FML's GameRegistry.generateWorld where the seed is constructed. - long worldSeed = world.getSeed(); - Random random = new Random(worldSeed); - long xSeed = random.nextLong() >> 2 + 1L; - long zSeed = random.nextLong() >> 2 + 1L; - random.setSeed(xSeed * coords.x + zSeed * coords.z ^ worldSeed); - - worldGenerator.retroGen(random, coords.x, coords.z, world); + if (checkForRetrogen.contains(dimensionID)) { + List chunkList = chunkRegenList.get(dimensionID); + Iterator iterator = chunkList.iterator(); + while (iterator.hasNext()) { + ChunkCoords coords = iterator.next(); + if (canDecorate(world, coords)) { + iterator.remove(); + Random random = getRetrogenRandom(world, coords); + worldGenerator.retroGen(random, coords.x, coords.z, world); + } + } + checkForRetrogen.remove(dimensionID); } } } + /** + * This is from {@link GameRegistry#generateWorld(int, int, World, IChunkGenerator, IChunkProvider)} where the seed is constructed. + */ + private static Random getRetrogenRandom(World world, ChunkCoords coords) { + long worldSeed = world.getSeed(); + Random random = new Random(worldSeed); + long xSeed = random.nextLong() >> 2 + 1L; + long zSeed = random.nextLong() >> 2 + 1L; + random.setSeed(xSeed * coords.x + zSeed * coords.z ^ worldSeed); + return random; + } + + private static boolean canDecorate(WorldServer server, ChunkCoords chunkCoords) { + ChunkProviderServer chunkProvider = server.getChunkProvider(); + for (int x = 0; x <= 1; x++) { + for (int z = 0; z <= 1; z++) { + if (!chunkProvider.chunkExists(chunkCoords.x + x, chunkCoords.z + z)) { + return false; + } + } + } + return true; + } + @SubscribeEvent public void chunkSaveEventHandler(ChunkDataEvent.Save event) { NBTTagCompound tag = new NBTTagCompound(); @@ -94,6 +125,7 @@ public class TickHandlerCoreServer { if (!tag.hasKey("retrogen") || Config.forceRetrogen) { ChunkCoords coords = new ChunkCoords(event.getChunk()); chunkRegenList.put(coords.dimension, coords); + checkForRetrogen.add(coords.dimension); } } } From b9924ea98b07547bdba4ba5390366b8f7d390c4c Mon Sep 17 00:00:00 2001 From: mezz Date: Wed, 6 Sep 2017 00:35:54 -0700 Subject: [PATCH 4/4] Fix #1843 Improve performance of worldgen hives --- gradle.properties | 2 +- .../api/apiculture/IBeekeepingLogic.java | 15 +- .../forestry/api/core/IBlockPosPredicate.java | 13 ++ .../api/genetics/IFlowerRegistry.java | 23 +++ .../forestry/apiculture/BeekeepingLogic.java | 5 +- .../forestry/apiculture/HasFlowersCache.java | 131 +++++++------- .../apiculture/WorldgenBeekeepingLogic.java | 166 ++++++++++++++++++ .../EntityMinecartBeeHousingBase.java | 10 +- .../apiculture/flowers/FlowerRegistry.java | 111 ++++++++---- .../network/packets/PacketBeeLogicActive.java | 9 +- .../forestry/apiculture/tiles/TileHive.java | 15 +- .../forestry/core/render/ParticleRender.java | 42 +++-- .../forestry/core/tiles/TileForestry.java | 9 +- .../forestry/core/utils/BlockStateSet.java | 63 +++++++ .../java/forestry/core/utils/TickHelper.java | 16 ++ .../java/forestry/core/utils/VectUtil.java | 5 +- 16 files changed, 479 insertions(+), 156 deletions(-) create mode 100644 src/main/java/forestry/api/core/IBlockPosPredicate.java create mode 100644 src/main/java/forestry/apiculture/WorldgenBeekeepingLogic.java create mode 100644 src/main/java/forestry/core/utils/BlockStateSet.java create mode 100644 src/main/java/forestry/core/utils/TickHelper.java diff --git a/gradle.properties b/gradle.properties index 6e82d3847..d6c820eff 100755 --- a/gradle.properties +++ b/gradle.properties @@ -11,4 +11,4 @@ ic2_version=2.8.24-ex112 version_major=5 version_minor=5 -version_patch=1 +version_patch=2 diff --git a/src/main/java/forestry/api/apiculture/IBeekeepingLogic.java b/src/main/java/forestry/api/apiculture/IBeekeepingLogic.java index 48a38d1d7..d6fc44ac2 100644 --- a/src/main/java/forestry/api/apiculture/IBeekeepingLogic.java +++ b/src/main/java/forestry/api/apiculture/IBeekeepingLogic.java @@ -5,10 +5,13 @@ ******************************************************************************/ package forestry.api.apiculture; +import java.io.IOException; import java.util.List; import forestry.api.core.INbtReadable; import forestry.api.core.INbtWritable; +import forestry.core.network.IStreamable; +import forestry.core.network.PacketBufferForestry; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -21,7 +24,7 @@ import net.minecraftforge.fml.relauncher.SideOnly; * Get one with BeeManager.beeRoot.createBeekeepingLogic(IBeeHousing housing) * Save and load it to NBT using the INbtWritable methods. */ -public interface IBeekeepingLogic extends INbtWritable, INbtReadable { +public interface IBeekeepingLogic extends INbtWritable, INbtReadable, IStreamable { /* SERVER */ @@ -79,4 +82,14 @@ public interface IBeekeepingLogic extends INbtWritable, INbtReadable { * These positions are synced to the client from the server. */ List getFlowerPositions(); + + @Override + default void readData(PacketBufferForestry data) throws IOException { + + } + + @Override + default void writeData(PacketBufferForestry data) { + + } } diff --git a/src/main/java/forestry/api/core/IBlockPosPredicate.java b/src/main/java/forestry/api/core/IBlockPosPredicate.java new file mode 100644 index 000000000..9dfb97a48 --- /dev/null +++ b/src/main/java/forestry/api/core/IBlockPosPredicate.java @@ -0,0 +1,13 @@ +/******************************************************************************* + * Copyright 2011-2014 SirSengir + * + * This work (the API) is licensed under the "MIT" License, see LICENSE.txt for details. + ******************************************************************************/ +package forestry.api.core; + +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; + +public interface IBlockPosPredicate { + boolean test(World world, BlockPos blockPos); +} diff --git a/src/main/java/forestry/api/genetics/IFlowerRegistry.java b/src/main/java/forestry/api/genetics/IFlowerRegistry.java index a4b7f8b25..ab574fa02 100644 --- a/src/main/java/forestry/api/genetics/IFlowerRegistry.java +++ b/src/main/java/forestry/api/genetics/IFlowerRegistry.java @@ -5,10 +5,12 @@ ******************************************************************************/ package forestry.api.genetics; +import java.util.Iterator; import java.util.List; import forestry.api.apiculture.IBee; import forestry.api.apiculture.IBeeHousing; +import forestry.api.core.IBlockPosPredicate; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.util.math.BlockPos; @@ -60,17 +62,38 @@ public interface IFlowerRegistry { */ boolean growFlower(String flowerType, World world, IIndividual individual, BlockPos pos); + /** + * Gets an iterator over the area a bee can travel from its beeHousing. + * Starts at the bee housing and spirals outward. + * + * @param beeHousing The bee housing that will help determine the size of the area to check. + * @param bee The bee that will help determine the size of the area to check. + * @return an iterator over the area a bee can travel from its beeHousing. + * @since Forestry 5.5.2 + */ + Iterator getAreaIterator(IBeeHousing beeHousing, IBee bee); + + /** + * Checks a single coordinate to see if it is an accepted flower. + * @since Forestry 5.5.2 + */ + IBlockPosPredicate createAcceptedFlowerPredicate(String flowerType); + /** * @param beeHousing The bee housing that will help determine the size of the area to check. * @param bee The bee that will help determine the size of the area to check. * @param flowerType See {@link forestry.api.apiculture.FlowerManager}.FlowerTypeXXX * @param maxFlowers the largest number of flowers to check for before returning. Use 1 if you only need 1. * @return the coordinates of nearby accepted flowers, with size from 0 to maxFlowers. + * @deprecated since Forestry 5.5.2. Use {@link #getAreaIterator(IBeeHousing, IBee)} and {@link #createAcceptedFlowerPredicate(String)}. */ + @Deprecated List getAcceptedFlowerCoordinates(IBeeHousing beeHousing, IBee bee, String flowerType, int maxFlowers); /** * Checks a single coordinate to see if it is an accepted flower. + * @deprecated since Forestry 5.5.2. Use {@link #createAcceptedFlowerPredicate(String)}. */ + @Deprecated boolean isAcceptedFlower(String flowerType, World world, BlockPos pos); } diff --git a/src/main/java/forestry/apiculture/BeekeepingLogic.java b/src/main/java/forestry/apiculture/BeekeepingLogic.java index 2f7b042a4..f0159f085 100644 --- a/src/main/java/forestry/apiculture/BeekeepingLogic.java +++ b/src/main/java/forestry/apiculture/BeekeepingLogic.java @@ -54,7 +54,7 @@ import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -public class BeekeepingLogic implements IBeekeepingLogic, IStreamable { +public class BeekeepingLogic implements IBeekeepingLogic { private static final int totalBreedingTime = Constants.APIARY_BREEDING_TIME; @@ -222,7 +222,8 @@ public class BeekeepingLogic implements IBeekeepingLogic, IStreamable { errorLogic.setCondition(true, errorState); } - boolean hasFlowers = hasFlowersCache.hasFlowers(queen, housing); + hasFlowersCache.update(queen, housing); + boolean hasFlowers = hasFlowersCache.hasFlowers(); boolean flowerCacheNeedsSync = hasFlowersCache.needsSync(); errorLogic.setCondition(!hasFlowers, EnumErrorCode.NO_FLOWER); diff --git a/src/main/java/forestry/apiculture/HasFlowersCache.java b/src/main/java/forestry/apiculture/HasFlowersCache.java index c08013ad7..4d6ba8b90 100644 --- a/src/main/java/forestry/apiculture/HasFlowersCache.java +++ b/src/main/java/forestry/apiculture/HasFlowersCache.java @@ -10,80 +10,93 @@ ******************************************************************************/ package forestry.apiculture; +import javax.annotation.Nullable; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; -import java.util.Random; -import forestry.api.apiculture.BeeManager; import forestry.api.apiculture.FlowerManager; import forestry.api.apiculture.IBee; import forestry.api.apiculture.IBeeHousing; -import forestry.api.apiculture.IBeeModifier; import forestry.api.core.INbtReadable; import forestry.api.core.INbtWritable; +import forestry.api.core.IBlockPosPredicate; import forestry.api.genetics.IFlowerProvider; import forestry.core.network.IStreamable; import forestry.core.network.PacketBufferForestry; -import forestry.core.utils.VectUtil; +import forestry.core.utils.TickHelper; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Vec3i; import net.minecraft.world.World; public class HasFlowersCache implements INbtWritable, INbtReadable, IStreamable { - private static final String nbtKey = "hasFlowerCache"; - private static final String nbtKeyFlowers = "flowers"; - private static final String nbtKeyCooldown = "cooldown"; + private static final String NBT_KEY = "hasFlowerCache"; + private static final String NBT_KEY_FLOWERS = "flowers"; + private static final int FLOWER_CHECK_INTERVAL = 200; - private static final Random random = new Random(); - private static final int flowerCheckInterval = 128; + private final TickHelper tickHelper = new TickHelper(); - private final int flowerCheckTime = random.nextInt(flowerCheckInterval); - private List flowerCoords = new ArrayList<>(); - private int cooldown = 0; + @Nullable + private FlowerData flowerData; + private final List flowerCoords = new ArrayList<>(); private boolean needsSync = false; - public boolean hasFlowers(IBee queen, IBeeHousing beeHousing) { - IFlowerProvider flowerProvider = queen.getGenome().getFlowerProvider(); - String flowerType = flowerProvider.getFlowerType(); + private static class FlowerData { + public final String flowerType; + public final IBlockPosPredicate flowerPredicate; + public Iterator areaIterator; + + public FlowerData(IBee queen, IBeeHousing beeHousing) { + IFlowerProvider flowerProvider = queen.getGenome().getFlowerProvider(); + this.flowerType = flowerProvider.getFlowerType(); + this.flowerPredicate = FlowerManager.flowerRegistry.createAcceptedFlowerPredicate(flowerType); + this.areaIterator = FlowerManager.flowerRegistry.getAreaIterator(beeHousing, queen); + } + + public void resetIterator(IBee queen, IBeeHousing beeHousing) { + this.areaIterator = FlowerManager.flowerRegistry.getAreaIterator(beeHousing, queen); + } + } + + public void update(IBee queen, IBeeHousing beeHousing) { + if (flowerData == null) { + this.flowerData = new FlowerData(queen, beeHousing); + this.flowerCoords.clear(); + } World world = beeHousing.getWorldObj(); + tickHelper.onTick(); - if (!flowerCoords.isEmpty()) { - if (world.getTotalWorldTime() % flowerCheckInterval != flowerCheckTime) { - return true; - } - - IBeeModifier beeModifier = BeeManager.beeRoot.createBeeHousingModifier(beeHousing); - BlockPos housingCoords = beeHousing.getCoordinates(); - - Vec3i genomeTerritory = queen.getGenome().getTerritory(); - float housingModifier = beeModifier.getTerritoryModifier(queen.getGenome(), 1f); - Vec3i area = VectUtil.scale(genomeTerritory, housingModifier * 3.0f); - BlockPos min = VectUtil.scale(area, -0.5f).add(housingCoords); - BlockPos max = VectUtil.scale(area, 0.5f).add(housingCoords); - - for (BlockPos flowerPos : flowerCoords) { - if (!isFlowerValid(world, flowerPos, flowerType, min, max)) { - cooldown = 0; - break; + if (!flowerCoords.isEmpty() && tickHelper.updateOnInterval(FLOWER_CHECK_INTERVAL)) { + Iterator iterator = flowerCoords.iterator(); + while (iterator.hasNext()) { + BlockPos flowerPos = iterator.next(); + if (!flowerData.flowerPredicate.test(world, flowerPos) && world.isBlockLoaded(flowerPos)) { + iterator.remove(); + needsSync = true; } } } - if (cooldown <= 0) { - List newFlowerCoords = FlowerManager.flowerRegistry.getAcceptedFlowerCoordinates(beeHousing, queen, flowerType, 5); - cooldown = PluginApiculture.ticksPerBeeWorkCycle; - if (!flowerCoords.equals(newFlowerCoords)) { - flowerCoords = newFlowerCoords; - needsSync = true; - } - } else { - cooldown--; - } + final int flowerCount = flowerCoords.size(); + final int ticksPerCheck = 1 + (flowerCount * flowerCount); + if (tickHelper.updateOnInterval(ticksPerCheck)) { + if (flowerData.areaIterator.hasNext()) { + BlockPos.MutableBlockPos blockPos = flowerData.areaIterator.next(); + if (flowerData.flowerPredicate.test(world, blockPos)) { + flowerCoords.add(blockPos.toImmutable()); + needsSync = true; + } + } else { + flowerData.resetIterator(queen, beeHousing); + } + } + } + + public boolean hasFlowers() { return !flowerCoords.isEmpty(); } @@ -95,35 +108,23 @@ public class HasFlowersCache implements INbtWritable, INbtReadable, IStreamable public void clear() { flowerCoords.clear(); - cooldown = 0; + flowerData = null; } - public List getFlowerCoords() { return Collections.unmodifiableList(flowerCoords); } - private static boolean isFlowerValid(World world, BlockPos flowerCoords, String flowerType, BlockPos min, BlockPos max) { - if (!isFlowerCoordInRange(flowerCoords, min, max)) { - return false; - } - return FlowerManager.flowerRegistry.isAcceptedFlower(flowerType, world, flowerCoords); - } - - private static boolean isFlowerCoordInRange(BlockPos flowerCoords, BlockPos min, BlockPos max) { - return flowerCoords.getX() >= min.getX() && flowerCoords.getX() <= max.getX() && flowerCoords.getY() >= min.getY() && flowerCoords.getY() <= max.getY() && flowerCoords.getZ() >= min.getZ() && flowerCoords.getZ() <= max.getZ(); - } - @Override public void readFromNBT(NBTTagCompound nbttagcompound) { - if (!nbttagcompound.hasKey(nbtKey)) { + if (!nbttagcompound.hasKey(NBT_KEY)) { return; } - NBTTagCompound hasFlowerCacheNBT = nbttagcompound.getCompoundTag(nbtKey); + NBTTagCompound hasFlowerCacheNBT = nbttagcompound.getCompoundTag(NBT_KEY); - if (hasFlowerCacheNBT.hasKey(nbtKeyFlowers)) { - int[] flowersList = hasFlowerCacheNBT.getIntArray(nbtKeyFlowers); + if (hasFlowerCacheNBT.hasKey(NBT_KEY_FLOWERS)) { + int[] flowersList = hasFlowerCacheNBT.getIntArray(NBT_KEY_FLOWERS); if (flowersList.length % 3 == 0) { int flowerCount = flowersList.length / 3; for (int i = 0; i < flowerCount; i++) { @@ -133,8 +134,6 @@ public class HasFlowersCache implements INbtWritable, INbtReadable, IStreamable needsSync = true; } } - - cooldown = hasFlowerCacheNBT.getInteger(nbtKeyCooldown); } @Override @@ -151,12 +150,10 @@ public class HasFlowersCache implements INbtWritable, INbtReadable, IStreamable i++; } - hasFlowerCacheNBT.setIntArray(nbtKeyFlowers, flowersList); + hasFlowerCacheNBT.setIntArray(NBT_KEY_FLOWERS, flowersList); } - hasFlowerCacheNBT.setInteger(nbtKeyCooldown, cooldown); - - nbttagcompound.setTag(nbtKey, hasFlowerCacheNBT); + nbttagcompound.setTag(NBT_KEY, hasFlowerCacheNBT); return nbttagcompound; } diff --git a/src/main/java/forestry/apiculture/WorldgenBeekeepingLogic.java b/src/main/java/forestry/apiculture/WorldgenBeekeepingLogic.java new file mode 100644 index 000000000..dd3aa47e3 --- /dev/null +++ b/src/main/java/forestry/apiculture/WorldgenBeekeepingLogic.java @@ -0,0 +1,166 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.apiculture; + +import java.io.IOException; +import java.util.List; + +import forestry.api.apiculture.IBee; +import forestry.api.apiculture.IBeekeepingLogic; +import forestry.api.genetics.IEffectData; +import forestry.apiculture.network.packets.PacketBeeLogicActive; +import forestry.apiculture.tiles.TileHive; +import forestry.core.network.PacketBufferForestry; +import forestry.core.utils.NetworkUtil; +import forestry.core.utils.TickHelper; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +public class WorldgenBeekeepingLogic implements IBeekeepingLogic { + private final TileHive housing; + private IEffectData effectData[] = new IEffectData[2]; + private final HasFlowersCache hasFlowersCache = new HasFlowersCache(); + private final TickHelper tickHelper = new TickHelper(); + + // Client + private boolean active; + + public WorldgenBeekeepingLogic(TileHive housing) { + this.housing = housing; + } + + // / SAVING & LOADING + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + setActive(nbttagcompound.getBoolean("Active")); + hasFlowersCache.readFromNBT(nbttagcompound); + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound nbttagcompound) { + nbttagcompound.setBoolean("Active", active); + hasFlowersCache.writeToNBT(nbttagcompound); + + return nbttagcompound; + } + + @Override + public void writeData(PacketBufferForestry data) { + data.writeBoolean(active); + if (active) { + hasFlowersCache.writeData(data); + } + } + + @Override + public void readData(PacketBufferForestry data) throws IOException { + boolean active = data.readBoolean(); + setActive(active); + if (active) { + hasFlowersCache.readData(data); + } + } + + /* Activatable */ + private void setActive(boolean active) { + if (this.active == active) { + return; + } + this.active = active; + + syncToClient(); + } + + /* UPDATING */ + + @Override + public boolean canWork() { + tickHelper.onTick(); + + IBee queen = housing.getContainedBee(); + hasFlowersCache.update(queen, housing); + + if (tickHelper.updateOnInterval(200)) { + World world = housing.getWorldObj(); + boolean canWork = (world.isDaytime() || queen.getGenome().getNeverSleeps()) && + (!housing.isRaining() || queen.getGenome().getToleratesRain()); + boolean flowerCacheNeedsSync = hasFlowersCache.needsSync(); + + if (active != canWork) { + setActive(canWork); + } else if (flowerCacheNeedsSync) { + syncToClient(); + } + } + + return active; + } + + @Override + public void doWork() { + + } + + @Override + public void clearCachedValues() { + if (!housing.getWorldObj().isRemote) { + hasFlowersCache.clear(); + canWork(); + } + } + + /* CLIENT */ + + @Override + public void syncToClient() { + World world = housing.getWorldObj(); + if (world != null && !world.isRemote) { + NetworkUtil.sendNetworkPacket(new PacketBeeLogicActive(housing), housing.getCoordinates(), world); + } + } + + @Override + public void syncToClient(EntityPlayerMP player) { + World world = housing.getWorldObj(); + if (world != null && !world.isRemote) { + NetworkUtil.sendToPlayer(new PacketBeeLogicActive(housing), player); + } + } + + @Override + public int getBeeProgressPercent() { + return 0; + } + + @Override + @SideOnly(Side.CLIENT) + public boolean canDoBeeFX() { + return !Minecraft.getMinecraft().isGamePaused() && active; + } + + @Override + @SideOnly(Side.CLIENT) + public void doBeeFX() { + IBee queen = housing.getContainedBee(); + queen.doFX(effectData, housing); + } + + @Override + public List getFlowerPositions() { + return hasFlowersCache.getFlowerCoords(); + } + +} diff --git a/src/main/java/forestry/apiculture/entities/EntityMinecartBeeHousingBase.java b/src/main/java/forestry/apiculture/entities/EntityMinecartBeeHousingBase.java index 708ff0465..a74391bb8 100644 --- a/src/main/java/forestry/apiculture/entities/EntityMinecartBeeHousingBase.java +++ b/src/main/java/forestry/apiculture/entities/EntityMinecartBeeHousingBase.java @@ -36,6 +36,7 @@ import forestry.core.owner.OwnerHandler; import forestry.core.tiles.IClimatised; import forestry.core.utils.ClimateUtil; import forestry.core.utils.NetworkUtil; +import forestry.core.utils.TickHelper; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.network.datasync.DataParameter; import net.minecraft.network.datasync.EntityDataManager; @@ -47,12 +48,10 @@ import net.minecraft.world.biome.Biome; public abstract class EntityMinecartBeeHousingBase extends EntityMinecartContainerForestry implements IBeeHousing, IOwnedTile, IGuiBeeHousingDelegate, IClimatised, IStreamableGui { private static final DataParameter> OWNER = EntityDataManager.createKey(EntityMinecartBeeHousingBase.class, GameProfileDataSerializer.INSTANCE); - private static final Random random = new Random(); private static final int beeFXInterval = 4; private static final int pollenFXInterval = 50; - private final int beeFXTime = random.nextInt(beeFXInterval); - private final int pollenFXTime = random.nextInt(pollenFXInterval); + private final TickHelper tickHelper = new TickHelper(); private final IBeekeepingLogic beeLogic = BeeManager.beeRoot.createBeekeepingLogic(this); private final IErrorLogic errorLogic = ForestryAPI.errorStateRegistry.createErrorLogic(); @@ -184,6 +183,7 @@ public abstract class EntityMinecartBeeHousingBase extends EntityMinecartContain @Override public void onUpdate() { super.onUpdate(); + tickHelper.onTick(); if (!world.isRemote) { if (beeLogic.canWork()) { beeLogic.doWork(); @@ -196,11 +196,11 @@ public abstract class EntityMinecartBeeHousingBase extends EntityMinecartContain } if (beeLogic.canDoBeeFX()) { - if (world.getTotalWorldTime() % beeFXInterval == beeFXTime) { + if (tickHelper.updateOnInterval(beeFXInterval)) { beeLogic.doBeeFX(); } - if (world.getTotalWorldTime() % pollenFXInterval == pollenFXTime) { + if (tickHelper.updateOnInterval(pollenFXInterval)) { TileBeeHousingBase.doPollenFX(world, posX - 0.5, posY - 0.1, posZ - 0.5); } } diff --git a/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java b/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java index 7bf68600c..58e3c61ce 100644 --- a/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java +++ b/src/main/java/forestry/apiculture/flowers/FlowerRegistry.java @@ -12,6 +12,7 @@ package forestry.apiculture.flowers; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Random; @@ -27,11 +28,13 @@ import forestry.api.apiculture.IBee; import forestry.api.apiculture.IBeeGenome; import forestry.api.apiculture.IBeeHousing; import forestry.api.apiculture.IBeeModifier; +import forestry.api.core.IBlockPosPredicate; import forestry.api.genetics.IFlowerAcceptableRule; import forestry.api.genetics.IFlowerGrowthHelper; import forestry.api.genetics.IFlowerGrowthRule; import forestry.api.genetics.IFlowerRegistry; import forestry.api.genetics.IIndividual; +import forestry.core.utils.BlockStateSet; import forestry.core.utils.VectUtil; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; @@ -43,7 +46,7 @@ import net.minecraft.world.World; public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelper { private final HashMultimap registeredRules; private final HashMultimap acceptableBlocks; - private final HashMultimap acceptableBlockStates; + private final Map acceptableBlockStates; private final HashMultimap plantableFlowers; private final ArrayListMultimap growthRules; @@ -52,7 +55,7 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe public FlowerRegistry() { this.registeredRules = HashMultimap.create(); this.acceptableBlocks = HashMultimap.create(); - this.acceptableBlockStates = HashMultimap.create(); + this.acceptableBlockStates = new HashMap<>(); this.plantableFlowers = HashMultimap.create(); this.growthRules = ArrayListMultimap.create(); this.chances = new HashMap<>(); @@ -78,7 +81,7 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe throw new NullPointerException("Tried to register flower with null type. " + blockState); } - this.acceptableBlockStates.get(flowerType).add(blockState); + this.getAcceptedBlockStates(flowerType).add(blockState); } } @@ -113,7 +116,7 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe Set flowers = this.plantableFlowers.get(flowerType); flowers.add(newFlower); - Set blocks = this.acceptableBlockStates.get(flowerType); + Set blocks = this.getAcceptedBlockStates(flowerType); blocks.add(blockState); if (this.chances.containsKey(flowerType)) { @@ -129,43 +132,16 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe } @Override - public List getAcceptedFlowerCoordinates(IBeeHousing beeHousing, IBee bee, String flowerType, int maxFlowers) { - Set acceptableRules = this.registeredRules.get(flowerType); - Set acceptedBlockStates = this.acceptableBlockStates.get(flowerType); - Set acceptedBlocks = this.acceptableBlocks.get(flowerType); - World world = beeHousing.getWorldObj(); - - IBeeModifier beeModifier = BeeManager.beeRoot.createBeeHousingModifier(beeHousing); - - Vec3i area = getArea(bee.getGenome(), beeModifier); - BlockPos minPos = beeHousing.getCoordinates().add(-area.getX() / 2, -area.getY() / 2, -area.getZ() / 2); - BlockPos maxPos = minPos.add(area); - List flowerCoords = new ArrayList<>(); - for (BlockPos.MutableBlockPos posBlock : VectUtil.getAllInBoxFromCenterMutable(world, minPos, beeHousing.getCoordinates(), maxPos)) { - if (!world.isBlockLoaded(posBlock)) { - continue; - } - IBlockState blockState = world.getBlockState(posBlock); - if (blockState.getBlock().isAir(blockState, world, posBlock)) { - continue; - } - for (IFlowerAcceptableRule acceptableRule : acceptableRules) { - if (acceptableRule.isAcceptableFlower(blockState, world, posBlock, flowerType)) { - flowerCoords.add(posBlock.toImmutable()); - if (flowerCoords.size() >= maxFlowers) { - return flowerCoords; - } - } - } - - if (isAcceptedFlower(blockState, acceptedBlocks, acceptedBlockStates)) { - flowerCoords.add(posBlock.toImmutable()); - if (flowerCoords.size() >= maxFlowers) { - return flowerCoords; - } + World world = beeHousing.getWorldObj(); + IBlockPosPredicate acceptedFlowerPredicate = createAcceptedFlowerPredicate(flowerType); + Iterator areaIterator = getAreaIterator(beeHousing, bee); + while (areaIterator.hasNext() && flowerCoords.size() < maxFlowers) { + BlockPos.MutableBlockPos pos = areaIterator.next(); + if (acceptedFlowerPredicate.test(world, pos)) { + flowerCoords.add(pos.toImmutable()); } } @@ -173,6 +149,17 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe } @Override + public Iterator getAreaIterator(IBeeHousing beeHousing, IBee bee) { + IBeeModifier beeModifier = BeeManager.beeRoot.createBeeHousingModifier(beeHousing); + Vec3i area = getArea(bee.getGenome(), beeModifier); + BlockPos minPos = beeHousing.getCoordinates().add(-area.getX() / 2, -area.getY() / 2, -area.getZ() / 2); + BlockPos maxPos = minPos.add(area); + World world = beeHousing.getWorldObj(); + return VectUtil.getAllInBoxFromCenterMutable(world, minPos, beeHousing.getCoordinates(), maxPos); + } + + @Override + @Deprecated public boolean isAcceptedFlower(String flowerType, World world, BlockPos pos) { if (!world.isBlockLoaded(pos)) { return true; // Avoid actually checking until the flower's position is loaded. @@ -185,12 +172,25 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe } } - Set acceptedBlockStates = this.acceptableBlockStates.get(flowerType); + Set acceptedBlockStates = this.getAcceptedBlockStates(flowerType); Set acceptedBlocks = this.acceptableBlocks.get(flowerType); return isAcceptedFlower(blockState, acceptedBlocks, acceptedBlockStates); } + private Set getAcceptedBlockStates(String flowerType) { + return this.acceptableBlockStates.computeIfAbsent(flowerType, k -> new BlockStateSet()); + } + + @Override + public IBlockPosPredicate createAcceptedFlowerPredicate(String flowerType) { + Set acceptableRules = this.registeredRules.get(flowerType); + Set acceptedBlockStates = this.getAcceptedBlockStates(flowerType); + Set acceptedBlocks = this.acceptableBlocks.get(flowerType); + + return new AcceptedFlowerPredicate(flowerType, acceptableRules, acceptedBlocks, acceptedBlockStates); + } + private static boolean isAcceptedFlower(IBlockState blockState, Set acceptedBlocks, Set acceptedBlockStates) { Block block = blockState.getBlock(); return acceptedBlocks.contains(block) || acceptedBlockStates.contains(blockState); @@ -255,4 +255,37 @@ public final class FlowerRegistry implements IFlowerRegistry, IFlowerGrowthHelpe Flower flower = getRandomPlantableFlower(flowerType, world.rand); return world.setBlockState(pos, flower.getBlockState()); } + + private static class AcceptedFlowerPredicate implements IBlockPosPredicate { + private final String flowerType; + private final Set acceptableRules; + private final Set acceptedBlocks; + private final Set acceptedBlockStates; + + public AcceptedFlowerPredicate(String flowerType, Set acceptableRules, Set acceptedBlocks, Set acceptedBlockStates) { + this.flowerType = flowerType; + this.acceptableRules = acceptableRules; + this.acceptedBlocks = acceptedBlocks; + this.acceptedBlockStates = acceptedBlockStates; + } + + @Override + public boolean test(World world, BlockPos blockPos) { + if (world.isBlockLoaded(blockPos)) { + IBlockState blockState = world.getBlockState(blockPos); + if (!blockState.getBlock().isAir(blockState, world, blockPos)) { + for (IFlowerAcceptableRule acceptableRule : acceptableRules) { + if (acceptableRule.isAcceptableFlower(blockState, world, blockPos, flowerType)) { + return true; + } + } + + if (isAcceptedFlower(blockState, acceptedBlocks, acceptedBlockStates)) { + return true; + } + } + } + return false; + } + } } diff --git a/src/main/java/forestry/apiculture/network/packets/PacketBeeLogicActive.java b/src/main/java/forestry/apiculture/network/packets/PacketBeeLogicActive.java index 3546f8eb1..6820a9057 100644 --- a/src/main/java/forestry/apiculture/network/packets/PacketBeeLogicActive.java +++ b/src/main/java/forestry/apiculture/network/packets/PacketBeeLogicActive.java @@ -14,7 +14,6 @@ import java.io.IOException; import forestry.api.apiculture.IBeeHousing; import forestry.api.apiculture.IBeekeepingLogic; -import forestry.apiculture.BeekeepingLogic; import forestry.core.network.ForestryPacket; import forestry.core.network.IForestryPacketClient; import forestry.core.network.IForestryPacketHandlerClient; @@ -28,11 +27,11 @@ import net.minecraftforge.fml.relauncher.SideOnly; public class PacketBeeLogicActive extends ForestryPacket implements IForestryPacketClient { private final BlockPos tilePos; - private final BeekeepingLogic beekeepingLogic; + private final IBeekeepingLogic beekeepingLogic; public PacketBeeLogicActive(IBeeHousing tile) { this.tilePos = tile.getCoordinates(); - this.beekeepingLogic = (BeekeepingLogic) tile.getBeekeepingLogic(); + this.beekeepingLogic = tile.getBeekeepingLogic(); } @Override @@ -55,9 +54,7 @@ public class PacketBeeLogicActive extends ForestryPacket implements IForestryPac IBeeHousing beeHousing = TileUtil.getTile(player.world, tilePos, IBeeHousing.class); if (beeHousing != null) { IBeekeepingLogic beekeepingLogic = beeHousing.getBeekeepingLogic(); - if (beekeepingLogic instanceof BeekeepingLogic) { - ((BeekeepingLogic) beekeepingLogic).readData(data); - } + beekeepingLogic.readData(data); } } } diff --git a/src/main/java/forestry/apiculture/tiles/TileHive.java b/src/main/java/forestry/apiculture/tiles/TileHive.java index c3edaaf6d..488f49fd4 100644 --- a/src/main/java/forestry/apiculture/tiles/TileHive.java +++ b/src/main/java/forestry/apiculture/tiles/TileHive.java @@ -16,6 +16,8 @@ import javax.annotation.Nullable; import java.util.Collections; import java.util.List; +import forestry.apiculture.WorldgenBeekeepingLogic; +import forestry.core.utils.TickHelper; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityLivingBase; @@ -59,7 +61,6 @@ import forestry.api.core.EnumTemperature; import forestry.api.core.ForestryAPI; import forestry.api.core.IErrorLogic; import forestry.api.genetics.IAllele; -import forestry.apiculture.BeekeepingLogic; import forestry.apiculture.blocks.BlockBeeHives; import forestry.apiculture.genetics.BeeDefinition; import forestry.apiculture.genetics.alleles.AlleleEffect; @@ -78,9 +79,10 @@ public class TileHive extends TileEntity implements ITickable, IHiveTile, IActiv private final InventoryAdapter contained = new InventoryAdapter(2, "Contained"); private final HiveBeeHousingInventory inventory; - private final BeekeepingLogic beeLogic; + private final WorldgenBeekeepingLogic beeLogic; private final IErrorLogic errorLogic; private final Predicate beeTargetPredicate; + private final TickHelper tickHelper = new TickHelper(); @Nullable private IBee containedBee = null; @@ -96,7 +98,7 @@ public class TileHive extends TileEntity implements ITickable, IHiveTile, IActiv public TileHive() { inventory = new HiveBeeHousingInventory(this); - beeLogic = new BeekeepingLogic(this); + beeLogic = new WorldgenBeekeepingLogic(this); errorLogic = ForestryAPI.errorStateRegistry.createErrorLogic(); beeTargetPredicate = new BeeTargetPredicate(this); } @@ -106,12 +108,13 @@ public class TileHive extends TileEntity implements ITickable, IHiveTile, IActiv if (Config.generateBeehivesDebug) { return; } + tickHelper.onTick(); if (world.isRemote) { - if (!updatedLight && world.getWorldTime() % 100 == 0) { + if (!updatedLight && tickHelper.updateOnInterval(100)) { updatedLight = world.checkLightFor(EnumSkyBlock.BLOCK, getPos()); } - if (active && world.rand.nextInt(4) == 0) { + if (active && tickHelper.updateOnInterval(4)) { if (beeLogic.canDoBeeFX()) { beeLogic.doBeeFX(); } @@ -119,7 +122,7 @@ public class TileHive extends TileEntity implements ITickable, IHiveTile, IActiv } else { boolean canWork = beeLogic.canWork(); // must be called every tick to stay updated - if (world.rand.nextInt(angry ? 10 : 200) == 0) { + if (tickHelper.updateOnInterval(angry ? 10 : 200)) { if (calmTime == 0) { if (canWork) { if(world.getWorldInfo().getDifficulty() != EnumDifficulty.PEACEFUL) { diff --git a/src/main/java/forestry/core/render/ParticleRender.java b/src/main/java/forestry/core/render/ParticleRender.java index 47009b185..afd85ce6e 100755 --- a/src/main/java/forestry/core/render/ParticleRender.java +++ b/src/main/java/forestry/core/render/ParticleRender.java @@ -68,32 +68,30 @@ public class ParticleRender { int color = genome.getPrimary().getSpriteColour(0); - if (!flowerPositions.isEmpty()) { - int randomInt = world.rand.nextInt(100); + int randomInt = world.rand.nextInt(100); - if (housing instanceof IHiveTile) { - if (((IHiveTile) housing).isAngry() || randomInt >= 85) { - List entitiesInRange = AlleleEffect.getEntitiesInRange(genome, housing, EntityLivingBase.class); - if (!entitiesInRange.isEmpty()) { - EntityLivingBase entity = entitiesInRange.get(world.rand.nextInt(entitiesInRange.size())); - Particle particle = new ParticleBeeTargetEntity(world, particleStart, entity, color); - effectRenderer.addEffect(particle); - return; - } + if (housing instanceof IHiveTile) { + if (((IHiveTile) housing).isAngry() || randomInt >= 85) { + List entitiesInRange = AlleleEffect.getEntitiesInRange(genome, housing, EntityLivingBase.class); + if (!entitiesInRange.isEmpty()) { + EntityLivingBase entity = entitiesInRange.get(world.rand.nextInt(entitiesInRange.size())); + Particle particle = new ParticleBeeTargetEntity(world, particleStart, entity, color); + effectRenderer.addEffect(particle); + return; } } + } - if (randomInt < 75) { - BlockPos destination = flowerPositions.get(world.rand.nextInt(flowerPositions.size())); - Particle particle = new ParticleBeeRoundTrip(world, particleStart, destination, color); - effectRenderer.addEffect(particle); - } else { - Vec3i area = AlleleEffect.getModifiedArea(genome, housing); - Vec3i offset = housing.getCoordinates().add(-area.getX() / 2, -area.getY() / 4, -area.getZ() / 2); - BlockPos destination = VectUtil.getRandomPositionInArea(world.rand, area).add(offset); - Particle particle = new ParticleBeeExplore(world, particleStart, destination, color); - effectRenderer.addEffect(particle); - } + if (randomInt < 75 && !flowerPositions.isEmpty()) { + BlockPos destination = flowerPositions.get(world.rand.nextInt(flowerPositions.size())); + Particle particle = new ParticleBeeRoundTrip(world, particleStart, destination, color); + effectRenderer.addEffect(particle); + } else { + Vec3i area = AlleleEffect.getModifiedArea(genome, housing); + Vec3i offset = housing.getCoordinates().add(-area.getX() / 2, -area.getY() / 4, -area.getZ() / 2); + BlockPos destination = VectUtil.getRandomPositionInArea(world.rand, area).add(offset); + Particle particle = new ParticleBeeExplore(world, particleStart, destination, color); + effectRenderer.addEffect(particle); } } diff --git a/src/main/java/forestry/core/tiles/TileForestry.java b/src/main/java/forestry/core/tiles/TileForestry.java index 10dc8403a..8bcd2d173 100644 --- a/src/main/java/forestry/core/tiles/TileForestry.java +++ b/src/main/java/forestry/core/tiles/TileForestry.java @@ -27,6 +27,7 @@ import forestry.core.network.PacketBufferForestry; import forestry.core.network.packets.PacketTileStream; import forestry.core.utils.NBTUtilForestry; import forestry.core.utils.NetworkUtil; +import forestry.core.utils.TickHelper; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.ISidedInventory; import net.minecraft.item.ItemStack; @@ -48,14 +49,12 @@ import net.minecraftforge.items.wrapper.SidedInvWrapper; //@Optional.Interface(iface = "buildcraft.api.statements.ITriggerProvider", modid = "BuildCraftAPI|statements") public abstract class TileForestry extends TileEntity implements IStreamable, IErrorLogicSource, ISidedInventory, IFilterSlotDelegate, ITitled, ILocatable, IGuiHandlerTile, ITickable { - private static final Random rand = new Random(); - private final ErrorLogic errorHandler = new ErrorLogic(); private final AdjacentTileCache tileCache = new AdjacentTileCache(this); private IInventoryAdapter inventory = FakeInventoryAdapter.instance(); - private int tickCount = rand.nextInt(256); + private final TickHelper tickHelper = new TickHelper(); private boolean needsNetworkUpdate = false; protected AdjacentTileCache getTileCache() { @@ -81,7 +80,7 @@ public abstract class TileForestry extends TileEntity implements IStreamable, IE // / UPDATING @Override public final void update() { - tickCount++; + tickHelper.onTick(); if (!world.isRemote) { updateServerSide(); @@ -103,7 +102,7 @@ public abstract class TileForestry extends TileEntity implements IStreamable, IE } protected final boolean updateOnInterval(int tickInterval) { - return tickCount % tickInterval == 0; + return tickHelper.updateOnInterval(tickInterval); } // / SAVING & LOADING diff --git a/src/main/java/forestry/core/utils/BlockStateSet.java b/src/main/java/forestry/core/utils/BlockStateSet.java new file mode 100644 index 000000000..4656b433c --- /dev/null +++ b/src/main/java/forestry/core/utils/BlockStateSet.java @@ -0,0 +1,63 @@ +package forestry.core.utils; + +import java.util.AbstractSet; +import java.util.HashSet; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; + +/** + * A set with fast {@link #contains(Object)} for IBlockState + */ +public class BlockStateSet extends AbstractSet { + private final Set blockStates = new HashSet<>(); + private final Map blocks = new IdentityHashMap<>(); + + @Override + public boolean add(IBlockState blockState) { + Block block = blockState.getBlock(); + Integer count = blocks.getOrDefault(block, 0); + blocks.put(block, count + 1); + return blockStates.add(blockState); + } + + @Override + public boolean remove(Object o) { + IBlockState blockState = (IBlockState) o; + Block block = blockState.getBlock(); + Integer count = blocks.getOrDefault(block, 0); + if (count > 0) { + blocks.put(block, count - 1); + } else { + blocks.remove(block); + } + return blockStates.remove(blockState); + } + + @Override + public boolean contains(Object o) { + IBlockState blockState = (IBlockState) o; + Block block = blockState.getBlock(); + return blocks.containsKey(block) && blockStates.contains(blockState); + } + + @Override + public void clear() { + blockStates.clear(); + blocks.clear(); + } + + @Override + public Iterator iterator() { + return blockStates.iterator(); + } + + @Override + public int size() { + return blockStates.size(); + } +} diff --git a/src/main/java/forestry/core/utils/TickHelper.java b/src/main/java/forestry/core/utils/TickHelper.java new file mode 100644 index 000000000..9c19951fd --- /dev/null +++ b/src/main/java/forestry/core/utils/TickHelper.java @@ -0,0 +1,16 @@ +package forestry.core.utils; + +import java.util.Random; + +public final class TickHelper { + private static final Random rand = new Random(); + private int tickCount = rand.nextInt(2048); + + public void onTick() { + tickCount++; + } + + public boolean updateOnInterval(int tickInterval) { + return tickCount % tickInterval == 0; + } +} diff --git a/src/main/java/forestry/core/utils/VectUtil.java b/src/main/java/forestry/core/utils/VectUtil.java index e092b818f..4b0394fb7 100644 --- a/src/main/java/forestry/core/utils/VectUtil.java +++ b/src/main/java/forestry/core/utils/VectUtil.java @@ -11,6 +11,7 @@ package forestry.core.utils; import javax.annotation.Nullable; +import java.util.Iterator; import java.util.Random; import com.google.common.collect.AbstractIterator; @@ -57,11 +58,11 @@ public final class VectUtil { } } - public static Iterable getAllInBoxFromCenterMutable(World world, final BlockPos from, final BlockPos center, final BlockPos to) { + public static Iterator getAllInBoxFromCenterMutable(World world, final BlockPos from, final BlockPos center, final BlockPos to) { final BlockPos minPos = new BlockPos(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ())); final BlockPos maxPos = new BlockPos(Math.max(from.getX(), to.getX()), Math.max(from.getY(), to.getY()), Math.max(from.getZ(), to.getZ())); - return () -> new MutableBlockPosSpiralIterator(world, center, maxPos, minPos); + return new MutableBlockPosSpiralIterator(world, center, maxPos, minPos); } private static class MutableBlockPosSpiralIterator extends AbstractIterator {