mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-21 04:22:32 +00:00
Giant multiblock refactor
This commit is contained in:
1 parent
201111cb83
commit
aa47599278
259 files changed
+10117
-6495
No files matched your search
@@ -10,6 +10,9 @@
|
||||
******************************************************************************/
|
||||
package forestry.apiculture;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
@@ -17,76 +20,97 @@ import java.util.Stack;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import forestry.api.apiculture.BeeManager;
|
||||
import forestry.api.apiculture.EnumBeeType;
|
||||
import forestry.api.apiculture.IApiaristTracker;
|
||||
import forestry.api.apiculture.IBee;
|
||||
import forestry.api.apiculture.IBeeHousing;
|
||||
import forestry.api.apiculture.IBeeHousingInventory;
|
||||
import forestry.api.apiculture.IBeeListener;
|
||||
import forestry.api.apiculture.IBeeModifier;
|
||||
import forestry.api.apiculture.IBeekeepingLogic;
|
||||
import forestry.api.core.IErrorLogic;
|
||||
import forestry.api.core.IErrorState;
|
||||
import forestry.api.genetics.IEffectData;
|
||||
import forestry.api.genetics.IIndividual;
|
||||
import forestry.apiculture.network.PacketBeekeepingLogicActive;
|
||||
import forestry.core.EnumErrorCode;
|
||||
import forestry.core.config.Defaults;
|
||||
import forestry.core.config.ForestryItem;
|
||||
import forestry.core.network.DataInputStreamForestry;
|
||||
import forestry.core.network.DataOutputStreamForestry;
|
||||
import forestry.core.network.IStreamable;
|
||||
import forestry.core.proxy.Proxies;
|
||||
import forestry.plugins.PluginApiculture;
|
||||
|
||||
public class BeekeepingLogic implements IBeekeepingLogic {
|
||||
public class BeekeepingLogic implements IBeekeepingLogic, IStreamable {
|
||||
|
||||
private static final int MAX_POLLINATION_ATTEMPTS = 20;
|
||||
private static final int totalBreedingTime = Defaults.APIARY_BREEDING_TIME;
|
||||
private static final int ticksPerCheckQueenCanWork = 10;
|
||||
|
||||
private final IBeeHousing housing;
|
||||
private final boolean housingSupportsMultipleErrorStates;
|
||||
private final IBeeModifier beeModifier;
|
||||
private final IBeeListener beeListener;
|
||||
|
||||
private int beeProgress;
|
||||
private int beeProgressMax;
|
||||
|
||||
// Breeding
|
||||
private int breedingTime;
|
||||
private int queenWorkCycleThrottle;
|
||||
private IEffectData effectData[] = new IEffectData[2];
|
||||
private IBee queen;
|
||||
private IIndividual pollen;
|
||||
private int attemptedPollinations = 0;
|
||||
|
||||
private final Stack<ItemStack> spawn = new Stack<ItemStack>();
|
||||
|
||||
// Cached flowers check
|
||||
private boolean hasFlowersCached = false;
|
||||
private int hasFlowersCooldown = 0;
|
||||
private Set<IErrorState> queenCanWorkCached = Collections.emptySet();
|
||||
private int queenCanWorkCooldown = 0;
|
||||
private final HasFlowersCache hasFlowersCache = new HasFlowersCache();
|
||||
private final QueenCanWorkCache queenCanWorkCache = new QueenCanWorkCache();
|
||||
private final PollenHandler pollenHandler = new PollenHandler();
|
||||
|
||||
// Client
|
||||
private boolean active;
|
||||
private IBee queen;
|
||||
private ItemStack queenStack; // used to detect server changes and sync clientQueen
|
||||
|
||||
public BeekeepingLogic(IBeeHousing housing) {
|
||||
this.housing = housing;
|
||||
|
||||
Set<IErrorState> errorStateSet = null;
|
||||
try {
|
||||
errorStateSet = housing.getErrorStates();
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
|
||||
housingSupportsMultipleErrorStates = (errorStateSet != null);
|
||||
this.beeModifier = new BeeHousingModifier(housing);
|
||||
this.beeListener = new BeeHousingListener(housing);
|
||||
}
|
||||
|
||||
// / SAVING & LOADING
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound nbttagcompound) {
|
||||
breedingTime = nbttagcompound.getInteger("BreedingTime");
|
||||
beeProgress = nbttagcompound.getInteger("BreedingTime");
|
||||
queenWorkCycleThrottle = nbttagcompound.getInteger("Throttle");
|
||||
|
||||
NBTTagCompound queenNBT = nbttagcompound.getCompoundTag("queen");
|
||||
queenStack = ItemStack.loadItemStackFromNBT(queenNBT);
|
||||
queen = BeeManager.beeRoot.getMember(queenStack);
|
||||
|
||||
setActive(nbttagcompound.getBoolean("Active"));
|
||||
|
||||
hasFlowersCache.readFromNBT(nbttagcompound);
|
||||
|
||||
NBTTagList nbttaglist = nbttagcompound.getTagList("Offspring", 10);
|
||||
for (int i = 0; i < nbttaglist.tagCount(); i++) {
|
||||
spawn.add(ItemStack.loadItemStackFromNBT(nbttaglist.getCompoundTagAt(i)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToNBT(NBTTagCompound nbttagcompound) {
|
||||
nbttagcompound.setInteger("BreedingTime", breedingTime);
|
||||
nbttagcompound.setInteger("BreedingTime", beeProgress);
|
||||
nbttagcompound.setInteger("Throttle", queenWorkCycleThrottle);
|
||||
|
||||
if (queenStack != null) {
|
||||
NBTTagCompound queenNBT = new NBTTagCompound();
|
||||
queenStack.writeToNBT(queenNBT);
|
||||
nbttagcompound.setTag("queen", queenNBT);
|
||||
}
|
||||
|
||||
nbttagcompound.setBoolean("Active", active);
|
||||
|
||||
hasFlowersCache.writeToNBT(nbttagcompound);
|
||||
|
||||
Stack<ItemStack> spawnCopy = new Stack<ItemStack>();
|
||||
spawnCopy.addAll(spawn);
|
||||
NBTTagList nbttaglist = new NBTTagList();
|
||||
@@ -96,108 +120,112 @@ public class BeekeepingLogic implements IBeekeepingLogic {
|
||||
nbttaglist.appendTag(nbttagcompound1);
|
||||
}
|
||||
nbttagcompound.setTag("Offspring", nbttaglist);
|
||||
|
||||
}
|
||||
|
||||
// / STATE INFORMATION
|
||||
@Override
|
||||
public int getBreedingTime() {
|
||||
return this.breedingTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTotalBreedingTime() {
|
||||
return totalBreedingTime;
|
||||
public void writeData(DataOutputStreamForestry data) throws IOException {
|
||||
data.writeBoolean(active);
|
||||
if (active) {
|
||||
data.writeItemStack(queenStack);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBee getQueen() {
|
||||
return this.queen;
|
||||
public void readData(DataInputStreamForestry data) throws IOException {
|
||||
boolean active = data.readBoolean();
|
||||
setActive(active);
|
||||
if (active) {
|
||||
queenStack = data.readItemStack();
|
||||
queen = BeeManager.beeRoot.getMember(queenStack);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBeeHousing getHousing() {
|
||||
return this.housing;
|
||||
/* Activatable */
|
||||
private void setActive(boolean active) {
|
||||
if (this.active == active) {
|
||||
return;
|
||||
}
|
||||
this.active = active;
|
||||
|
||||
if (!housing.getWorld().isRemote) {
|
||||
Proxies.net.sendNetworkPacket(new PacketBeekeepingLogicActive(housing));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IEffectData[] getEffectData() {
|
||||
return this.effectData;
|
||||
public boolean isActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
/* UPDATING */
|
||||
|
||||
@Override
|
||||
public boolean canWork() {
|
||||
if (housingSupportsMultipleErrorStates) {
|
||||
Set<IErrorState> errorStates = housing.getErrorStates();
|
||||
for (IErrorState errorState : errorStates) {
|
||||
housing.setErrorCondition(false, errorState);
|
||||
|
||||
IErrorLogic errorLogic = housing.getErrorLogic();
|
||||
errorLogic.clearErrors();
|
||||
|
||||
IBeeHousingInventory beeInventory = housing.getBeeInventory();
|
||||
|
||||
boolean hasSpace = addPendingProducts(beeInventory, spawn);
|
||||
errorLogic.setCondition(!hasSpace, EnumErrorCode.NOSPACE);
|
||||
|
||||
ItemStack queenStack = beeInventory.getQueen();
|
||||
|
||||
// check if we're breeding
|
||||
if (ForestryItem.beePrincessGE.isItemEqual(queenStack)) {
|
||||
boolean hasDrone = BeeManager.beeRoot.isDrone(beeInventory.getDrone());
|
||||
errorLogic.setCondition(!hasDrone, EnumErrorCode.NODRONE);
|
||||
|
||||
setActive(false); // not active (no bee FX) when we are breeding
|
||||
return !errorLogic.hasErrors();
|
||||
}
|
||||
|
||||
if (ForestryItem.beeQueenGE.isItemEqual(queenStack)) {
|
||||
if (!isQueenAlive(queenStack)) {
|
||||
IBee dyingQueen = BeeManager.beeRoot.getMember(queenStack);
|
||||
Collection<ItemStack> spawned = killQueen(dyingQueen, housing, beeListener);
|
||||
spawn.addAll(spawned);
|
||||
queenStack = null;
|
||||
}
|
||||
} else {
|
||||
queenStack = null;
|
||||
}
|
||||
|
||||
boolean hasSpace = addPendingProducts();
|
||||
|
||||
if (hasBreedablePrincess()) {
|
||||
return hasSpace;
|
||||
if (!ItemStack.areItemStacksEqual(this.queenStack, queenStack)) {
|
||||
this.queen = BeeManager.beeRoot.getMember(queenStack);
|
||||
this.queenStack = queenStack;
|
||||
}
|
||||
|
||||
if (hasHealthyQueen()) {
|
||||
boolean canWork = queenCanWork();
|
||||
boolean hasFlowers = hasFlowers();
|
||||
return hasSpace && canWork && hasFlowers;
|
||||
if (errorLogic.setCondition(queen == null, EnumErrorCode.NOQUEEN)) {
|
||||
setActive(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
Set<IErrorState> queenErrors = queenCanWorkCache.queenCanWork(queen, housing);
|
||||
for (IErrorState errorState : queenErrors) {
|
||||
errorLogic.setCondition(true, errorState);
|
||||
}
|
||||
|
||||
boolean hasFlowers = hasFlowersCache.hasFlowers(queen, housing);
|
||||
errorLogic.setCondition(!hasFlowers, EnumErrorCode.NOFLOWER);
|
||||
|
||||
boolean canWork = !errorLogic.hasErrors();
|
||||
setActive(canWork);
|
||||
return canWork;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doWork() {
|
||||
if (hasBreedablePrincess()) {
|
||||
IBeeHousingInventory beeInventory = housing.getBeeInventory();
|
||||
ItemStack queenStack = beeInventory.getQueen();
|
||||
if (ForestryItem.beePrincessGE.isItemEqual(queenStack)) {
|
||||
tickBreed();
|
||||
} else if (queen != null) {
|
||||
queenWorkTick();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
if (canWork()) {
|
||||
doWork();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasFlowers() {
|
||||
if (queen == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (hasFlowersCooldown <= 0) {
|
||||
hasFlowersCached = queen.hasFlower(housing);
|
||||
hasFlowersCooldown = PluginApiculture.ticksPerBeeWorkCycle;
|
||||
|
||||
// check more often if we haven't found flowers
|
||||
if (!hasFlowersCached) {
|
||||
hasFlowersCooldown /= 2;
|
||||
}
|
||||
} else {
|
||||
hasFlowersCooldown--;
|
||||
queenWorkTick(queen);
|
||||
}
|
||||
|
||||
if (housingSupportsMultipleErrorStates) {
|
||||
housing.setErrorCondition(!hasFlowersCached, EnumErrorCode.NOFLOWER);
|
||||
} else {
|
||||
if (hasFlowersCached) {
|
||||
housing.setErrorState(EnumErrorCode.OK);
|
||||
} else {
|
||||
housing.setErrorState(EnumErrorCode.NOFLOWER);
|
||||
}
|
||||
}
|
||||
|
||||
return hasFlowersCached;
|
||||
}
|
||||
|
||||
private void queenWorkTick() {
|
||||
private void queenWorkTick(IBee queen) {
|
||||
if (queen == null) {
|
||||
return;
|
||||
}
|
||||
@@ -210,90 +238,53 @@ public class BeekeepingLogic implements IBeekeepingLogic {
|
||||
if (queenWorkCycleThrottle >= PluginApiculture.ticksPerBeeWorkCycle) {
|
||||
queenWorkCycleThrottle = 0;
|
||||
|
||||
doProduction();
|
||||
doProduction(queen, housing, beeListener);
|
||||
queen.plantFlowerRandom(housing);
|
||||
doPollination();
|
||||
pollenHandler.doPollination(queen, housing, beeListener);
|
||||
|
||||
// Age the queen
|
||||
queen.age(housing.getWorld(), housing.getLifespanModifier(queen.getGenome(), queen.getMate(), 0f));
|
||||
updateQueenItemNBT();
|
||||
float lifespanModifier = beeModifier.getLifespanModifier(queen.getGenome(), queen.getMate(), 1.0f);
|
||||
queen.age(housing.getWorld(), lifespanModifier);
|
||||
|
||||
// Write the changed queen back into the item stack.
|
||||
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
||||
queen.writeToNBT(nbttagcompound);
|
||||
housing.getBeeInventory().getQueen().setTagCompound(nbttagcompound);
|
||||
}
|
||||
|
||||
beeProgress = queen.getHealth();
|
||||
beeProgressMax = queen.getMaxHealth();
|
||||
}
|
||||
|
||||
private void doProduction() {
|
||||
if (queen == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
private static void doProduction(IBee queen, IBeeHousing beeHousing, IBeeListener beeListener) {
|
||||
// Produce and add stacks
|
||||
ItemStack[] products = queen.produceStacks(housing);
|
||||
ItemStack[] products = queen.produceStacks(beeHousing);
|
||||
if (products == null) {
|
||||
return;
|
||||
}
|
||||
housing.wearOutEquipment(1);
|
||||
beeListener.wearOutEquipment(1);
|
||||
|
||||
IBeeHousingInventory beeInventory = beeHousing.getBeeInventory();
|
||||
|
||||
for (ItemStack stack : products) {
|
||||
housing.addProduct(stack, false);
|
||||
beeInventory.addProduct(stack, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void doPollination() {
|
||||
if (queen == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get pollen if none available yet
|
||||
if (pollen == null) {
|
||||
pollen = queen.retrievePollen(housing);
|
||||
attemptedPollinations = 0;
|
||||
if (pollen != null) {
|
||||
if (housing.onPollenRetrieved(queen, pollen, false)) {
|
||||
pollen = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pollen != null) {
|
||||
attemptedPollinations++;
|
||||
if (queen.pollinateRandom(housing, pollen) || attemptedPollinations >= MAX_POLLINATION_ATTEMPTS) {
|
||||
pollen = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateQueenItemNBT() {
|
||||
if (queen == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Write the changed queen back into the item stack.
|
||||
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
||||
queen.writeToNBT(nbttagcompound);
|
||||
housing.getQueen().setTagCompound(nbttagcompound);
|
||||
}
|
||||
|
||||
private boolean addPendingProducts() {
|
||||
EnumErrorCode housingErrorState = null;
|
||||
private static boolean addPendingProducts(IBeeHousingInventory beeInventory, Stack<ItemStack> spawn) {
|
||||
boolean housingHasSpace = true;
|
||||
|
||||
while (!spawn.isEmpty()) {
|
||||
ItemStack next = spawn.peek();
|
||||
if (housing.addProduct(next, true)) {
|
||||
if (beeInventory.addProduct(next, true)) {
|
||||
spawn.pop();
|
||||
housingErrorState = EnumErrorCode.OK;
|
||||
} else {
|
||||
housingErrorState = EnumErrorCode.NOSPACE;
|
||||
housingHasSpace = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (housingErrorState != null) {
|
||||
if (housingSupportsMultipleErrorStates) {
|
||||
boolean hasSpace = (housingErrorState == EnumErrorCode.OK);
|
||||
housing.setErrorCondition(!hasSpace, EnumErrorCode.NOSPACE);
|
||||
} else {
|
||||
housing.setErrorState(housingErrorState);
|
||||
}
|
||||
}
|
||||
|
||||
return housingErrorState != EnumErrorCode.NOSPACE;
|
||||
return housingHasSpace;
|
||||
}
|
||||
|
||||
/** Checks if a queen is alive. Much faster than reading the whole bee nbt */
|
||||
@@ -303,170 +294,68 @@ public class BeekeepingLogic implements IBeekeepingLogic {
|
||||
return health > 0;
|
||||
}
|
||||
|
||||
private boolean hasHealthyQueen() {
|
||||
boolean hasQueen = true;
|
||||
EnumErrorCode housingErrorState = null;
|
||||
|
||||
ItemStack queenStack = housing.getQueen();
|
||||
|
||||
if (queenStack == null || !ForestryItem.beeQueenGE.isItemEqual(queenStack)) {
|
||||
housingErrorState = EnumErrorCode.NOQUEEN;
|
||||
hasQueen = false;
|
||||
queen = null;
|
||||
} else if (!isQueenAlive(queenStack)) {
|
||||
if (queen == null) {
|
||||
queen = BeeManager.beeRoot.getMember(queenStack);
|
||||
}
|
||||
killQueen();
|
||||
housingErrorState = EnumErrorCode.OK;
|
||||
hasQueen = false;
|
||||
queen = null;
|
||||
}
|
||||
|
||||
if (housingSupportsMultipleErrorStates) {
|
||||
housing.setErrorCondition(!hasQueen, EnumErrorCode.NOQUEEN);
|
||||
} else {
|
||||
if (housingErrorState != null) {
|
||||
housing.setErrorState(housingErrorState);
|
||||
}
|
||||
}
|
||||
|
||||
if (hasQueen && queen == null) {
|
||||
queen = BeeManager.beeRoot.getMember(queenStack);
|
||||
}
|
||||
|
||||
return hasQueen;
|
||||
}
|
||||
|
||||
private boolean hasBreedablePrincess() {
|
||||
boolean isBreedingPrincess = false;
|
||||
if (ForestryItem.beePrincessGE.isItemEqual(housing.getQueen())) {
|
||||
boolean hasDrone = ForestryItem.beeDroneGE.isItemEqual(housing.getDrone());
|
||||
if (housingSupportsMultipleErrorStates) {
|
||||
housing.setErrorCondition(!hasDrone, EnumErrorCode.NODRONE);
|
||||
} else {
|
||||
if (hasDrone) {
|
||||
housing.setErrorState(EnumErrorCode.OK);
|
||||
} else {
|
||||
housing.setErrorState(EnumErrorCode.NODRONE);
|
||||
}
|
||||
}
|
||||
isBreedingPrincess = true;
|
||||
}
|
||||
|
||||
return isBreedingPrincess;
|
||||
}
|
||||
|
||||
private boolean queenCanWork() {
|
||||
if (queenCanWorkCooldown > 0) {
|
||||
queenCanWorkCooldown--;
|
||||
} else {
|
||||
if (queen == null) {
|
||||
return false;
|
||||
}
|
||||
queenCanWorkCached = checkQueenCanWork();
|
||||
queenCanWorkCooldown = ticksPerCheckQueenCanWork;
|
||||
}
|
||||
|
||||
Set<IErrorState> errorStates = queenCanWorkCached;
|
||||
|
||||
if (errorStates.size() > 0) {
|
||||
if (housingSupportsMultipleErrorStates) {
|
||||
for (IErrorState errorState : errorStates) {
|
||||
housing.setErrorCondition(true, errorState);
|
||||
}
|
||||
} else {
|
||||
IErrorState state = errorStates.iterator().next();
|
||||
housing.setErrorState(state);
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private Set<IErrorState> checkQueenCanWork() {
|
||||
try {
|
||||
return queen.getCanWork(housing);
|
||||
} catch (Throwable ignored) {
|
||||
// queen might not support getCanWork
|
||||
}
|
||||
|
||||
IErrorState state = queen.canWork(housing);
|
||||
if (state != EnumErrorCode.OK) {
|
||||
return Collections.singleton(state);
|
||||
}
|
||||
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
// / BREEDING
|
||||
private void tickBreed() {
|
||||
if (!tryBreed()) {
|
||||
breedingTime = 0;
|
||||
beeProgressMax = totalBreedingTime;
|
||||
|
||||
IBeeHousingInventory beeInventory = housing.getBeeInventory();
|
||||
|
||||
ItemStack droneStack = beeInventory.getDrone();
|
||||
ItemStack princessStack = beeInventory.getQueen();
|
||||
|
||||
if (!ForestryItem.beeDroneGE.isItemEqual(droneStack) || !ForestryItem.beePrincessGE.isItemEqual(princessStack)) {
|
||||
beeProgress = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (breedingTime < totalBreedingTime) {
|
||||
breedingTime++;
|
||||
if (beeProgress < totalBreedingTime) {
|
||||
beeProgress++;
|
||||
}
|
||||
if (breedingTime < totalBreedingTime) {
|
||||
if (beeProgress < totalBreedingTime) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Breeding done, create new queen if slot available
|
||||
if (!ForestryItem.beePrincessGE.isItemEqual(housing.getQueen())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Replace
|
||||
IBee princess = BeeManager.beeRoot.getMember(housing.getQueen());
|
||||
IBee drone = BeeManager.beeRoot.getMember(housing.getDrone());
|
||||
// Mate and replace princess with queen
|
||||
IBee princess = BeeManager.beeRoot.getMember(princessStack);
|
||||
IBee drone = BeeManager.beeRoot.getMember(droneStack);
|
||||
princess.mate(drone);
|
||||
|
||||
NBTTagCompound nbttagcompound = new NBTTagCompound();
|
||||
princess.writeToNBT(nbttagcompound);
|
||||
ItemStack queen = ForestryItem.beeQueenGE.getItemStack();
|
||||
queen.setTagCompound(nbttagcompound);
|
||||
queenStack = ForestryItem.beeQueenGE.getItemStack();
|
||||
queenStack.setTagCompound(nbttagcompound);
|
||||
|
||||
housing.setQueen(queen);
|
||||
housing.onQueenChange(housing.getQueen());
|
||||
beeInventory.setQueen(queenStack);
|
||||
|
||||
// Register the new queen with the breeding tracker
|
||||
BeeManager.beeRoot.getBreedingTracker(housing.getWorld(), housing.getOwnerName()).registerQueen(princess);
|
||||
BeeManager.beeRoot.getBreedingTracker(housing.getWorld(), housing.getOwner()).registerQueen(princess);
|
||||
|
||||
// Remove drone
|
||||
housing.getDrone().stackSize--;
|
||||
if (housing.getDrone().stackSize <= 0) {
|
||||
housing.setDrone(null);
|
||||
beeInventory.getDrone().stackSize--;
|
||||
if (beeInventory.getDrone().stackSize <= 0) {
|
||||
beeInventory.setDrone(null);
|
||||
}
|
||||
|
||||
// Reset breeding time
|
||||
breedingTime = 0;
|
||||
queen = princess;
|
||||
beeProgress = princess.getHealth();
|
||||
beeProgressMax = princess.getMaxHealth();
|
||||
}
|
||||
|
||||
private boolean tryBreed() {
|
||||
if (housing.getDrone() == null || housing.getQueen() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!ForestryItem.beeDroneGE.isItemEqual(housing.getDrone()) || !ForestryItem.beePrincessGE.isItemEqual(housing.getQueen())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return housing.canBreed();
|
||||
|
||||
}
|
||||
|
||||
private void killQueen() {
|
||||
private static Collection<ItemStack> killQueen(IBee queen, IBeeHousing beeHousing, IBeeListener beeListener) {
|
||||
if (queen == null) {
|
||||
return;
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
IBeeHousingInventory beeInventory = beeHousing.getBeeInventory();
|
||||
|
||||
Collection<ItemStack> spawn;
|
||||
|
||||
if (queen.canSpawn()) {
|
||||
spawnOffspring(queen);
|
||||
housing.getQueen().stackSize = 0;
|
||||
housing.setQueen(null);
|
||||
spawn = spawnOffspring(queen, beeHousing);
|
||||
beeListener.onQueenDeath();
|
||||
beeInventory.getQueen().stackSize = 0;
|
||||
beeInventory.setQueen(null);
|
||||
} else {
|
||||
Proxies.log.warning("Tried to spawn offspring off an unmated queen. Devolving her to a princess.");
|
||||
|
||||
@@ -475,28 +364,29 @@ public class BeekeepingLogic implements IBeekeepingLogic {
|
||||
queen.writeToNBT(nbttagcompound);
|
||||
convert.setTagCompound(nbttagcompound);
|
||||
|
||||
spawn.add(convert);
|
||||
housing.setQueen(null);
|
||||
spawn = Collections.singleton(convert);
|
||||
beeInventory.setQueen(null);
|
||||
}
|
||||
housing.onQueenChange(housing.getQueen());
|
||||
|
||||
return spawn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the succeeding princess and between one and three drones.
|
||||
*/
|
||||
private void spawnOffspring(IBee queen) {
|
||||
private static Collection<ItemStack> spawnOffspring(IBee queen, IBeeHousing beeHousing) {
|
||||
|
||||
World world = beeHousing.getWorld();
|
||||
|
||||
Stack<ItemStack> offspring = new Stack<ItemStack>();
|
||||
IApiaristTracker breedingTracker = BeeManager.beeRoot.getBreedingTracker(housing.getWorld(), housing.getOwnerName());
|
||||
|
||||
housing.onQueenDeath(getQueen());
|
||||
IApiaristTracker breedingTracker = BeeManager.beeRoot.getBreedingTracker(world, beeHousing.getOwner());
|
||||
|
||||
// Princess
|
||||
boolean secondPrincess = this.housing.getWorld().rand.nextInt(10000) < PluginApiculture.getSecondPrincessChance() * 100;
|
||||
boolean secondPrincess = world.rand.nextInt(10000) < PluginApiculture.getSecondPrincessChance() * 100;
|
||||
int count = secondPrincess ? 2 : 1;
|
||||
while (count > 0) {
|
||||
count--;
|
||||
IBee heiress = queen.spawnPrincess(housing);
|
||||
IBee heiress = queen.spawnPrincess(beeHousing);
|
||||
if (heiress != null) {
|
||||
ItemStack princess = BeeManager.beeRoot.getMemberStack(heiress, EnumBeeType.PRINCESS.ordinal());
|
||||
breedingTracker.registerPrincess(heiress);
|
||||
@@ -505,21 +395,93 @@ public class BeekeepingLogic implements IBeekeepingLogic {
|
||||
}
|
||||
|
||||
// Drones
|
||||
IBee[] larvae = queen.spawnDrones(housing);
|
||||
for (IBee larva : larvae) {
|
||||
ItemStack drone = BeeManager.beeRoot.getMemberStack(larva, EnumBeeType.DRONE.ordinal());
|
||||
breedingTracker.registerDrone(larva);
|
||||
offspring.push(drone);
|
||||
IBee[] drones = queen.spawnDrones(beeHousing);
|
||||
for (IBee drone : drones) {
|
||||
ItemStack droneStack = BeeManager.beeRoot.getMemberStack(drone, EnumBeeType.DRONE.ordinal());
|
||||
breedingTracker.registerDrone(drone);
|
||||
offspring.push(droneStack);
|
||||
}
|
||||
|
||||
IBeeHousingInventory beeInventory = beeHousing.getBeeInventory();
|
||||
|
||||
Collection<ItemStack> spawn = new ArrayList<ItemStack>();
|
||||
|
||||
while (!offspring.isEmpty()) {
|
||||
ItemStack spawned = offspring.pop();
|
||||
if (!housing.addProduct(spawned, true)) {
|
||||
if (!beeInventory.addProduct(spawned, true)) {
|
||||
spawn.add(spawned);
|
||||
}
|
||||
}
|
||||
|
||||
housing.onPostQueenDeath(getQueen());
|
||||
|
||||
return spawn;
|
||||
}
|
||||
|
||||
/* CLIENT */
|
||||
|
||||
@Override
|
||||
public int getBeeProgressPercent() {
|
||||
if (beeProgressMax == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Math.round(beeProgress * 100f / beeProgressMax);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDoBeeFX() {
|
||||
return active;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doBeeFX() {
|
||||
if (queen != null) {
|
||||
queen.doFX(effectData, housing);
|
||||
}
|
||||
}
|
||||
|
||||
private static class QueenCanWorkCache {
|
||||
private static final int ticksPerCheckQueenCanWork = 10;
|
||||
|
||||
private Set<IErrorState> queenCanWorkCached = Collections.emptySet();
|
||||
private int queenCanWorkCooldown = 0;
|
||||
|
||||
public Set<IErrorState> queenCanWork(IBee queen, IBeeHousing beeHousing) {
|
||||
if (queenCanWorkCooldown <= 0) {
|
||||
queenCanWorkCached = queen.getCanWork(beeHousing);
|
||||
queenCanWorkCooldown = ticksPerCheckQueenCanWork;
|
||||
} else {
|
||||
queenCanWorkCooldown--;
|
||||
}
|
||||
|
||||
return queenCanWorkCached;
|
||||
}
|
||||
}
|
||||
|
||||
private static class PollenHandler {
|
||||
private static final int MAX_POLLINATION_ATTEMPTS = 20;
|
||||
|
||||
private IIndividual pollen;
|
||||
private int attemptedPollinations = 0;
|
||||
|
||||
public void doPollination(IBee queen, IBeeHousing beeHousing, IBeeListener beeListener) {
|
||||
// Get pollen if none available yet
|
||||
if (pollen == null) {
|
||||
attemptedPollinations = 0;
|
||||
pollen = queen.retrievePollen(beeHousing);
|
||||
if (pollen != null) {
|
||||
if (beeListener.onPollenRetrieved(pollen)) {
|
||||
pollen = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pollen != null) {
|
||||
attemptedPollinations++;
|
||||
if (queen.pollinateRandom(beeHousing, pollen) || attemptedPollinations >= MAX_POLLINATION_ATTEMPTS) {
|
||||
pollen = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user