use of com.minecolonies.coremod.colony.buildings.utils.BuilderBucket in project minecolonies by Minecolonies.
the class AbstractEntityAIStructure method pickUpMaterial.
/**
* State to pick up material before going back to work.
*
* @return the next state to go to.
*/
public IAIState pickUpMaterial() {
if (structurePlacer == null || !structurePlacer.getB().hasBluePrint()) {
return IDLE;
}
if (structurePlacer.getB().getStage() == null || structurePlacer.getB().getStage() == BuildingStructureHandler.Stage.CLEAR) {
pickUpCount = 0;
return START_WORKING;
}
final List<Tuple<Predicate<ItemStack>, Integer>> neededItemsList = new ArrayList<>();
final BuilderBucket neededRessourcesMap = getOwnBuilding().getRequiredResources();
final BuildingResourcesModule module = getOwnBuilding().getFirstModuleOccurance(BuildingResourcesModule.class);
if (neededRessourcesMap != null) {
for (final Map.Entry<String, Integer> entry : neededRessourcesMap.getResourceMap().entrySet()) {
final BuildingBuilderResource res = module.getResourceFromIdentifier(entry.getKey());
if (res != null) {
int amount = entry.getValue();
neededItemsList.add(new Tuple<>(itemstack -> ItemStackUtils.compareItemStacksIgnoreStackSize(res.getItemStack(), itemstack, true, true), amount));
}
}
}
if (neededItemsList.size() <= pickUpCount || InventoryUtils.openSlotCount(worker.getInventoryCitizen()) <= MIN_OPEN_SLOTS) {
getOwnBuilding().checkOrRequestBucket(getOwnBuilding().getRequiredResources(), worker.getCitizenData(), true);
getOwnBuilding().checkOrRequestBucket(getOwnBuilding().getNextBucket(), worker.getCitizenData(), false);
pickUpCount = 0;
return START_WORKING;
}
needsCurrently = neededItemsList.get(pickUpCount);
pickUpCount++;
if (InventoryUtils.hasItemInProvider(getOwnBuilding().getTileEntity(), needsCurrently.getA())) {
return GATHERING_REQUIRED_MATERIALS;
}
return pickUpMaterial();
}
use of com.minecolonies.coremod.colony.buildings.utils.BuilderBucket in project minecolonies by ldtteam.
the class BuildingResourcesModule method addNeededResource.
/**
* Add a new resource to the needed list.
*
* @param res the resource.
* @param amount the amount.
*/
public void addNeededResource(@Nullable final ItemStack res, final int amount) {
if (ItemStackUtils.isEmpty(res) || amount == 0) {
return;
}
final int hashCode = res.hasTag() ? res.getTag().hashCode() : 0;
final String key = res.getDescriptionId() + "-" + hashCode;
BuildingBuilderResource resource = this.neededResources.get(key);
if (resource == null) {
resource = new BuildingBuilderResource(res, amount);
} else {
resource.setAmount(resource.getAmount() + amount);
}
this.neededResources.put(key, resource);
BuilderBucket last = buckets.isEmpty() ? null : buckets.removeLast();
final int stacks = (int) Math.ceil((double) amount / res.getMaxStackSize());
final int max = building.getAllAssignedCitizen().iterator().next().getInventory().getSlots() - 9;
if (last == null || last.getTotalStacks() >= max || last.getTotalStacks() + stacks >= max) {
if (last != null) {
buckets.add(last);
}
last = new BuilderBucket();
last.setTotalStacks(stacks);
last.addOrAdjustResource(key, amount);
buckets.add(last);
} else {
int currentQty = last.getResourceMap().getOrDefault(key, 0);
final int currentStacks = (int) Math.ceil((double) currentQty / res.getMaxStackSize());
final int newStacks = (int) Math.ceil((double) (currentQty + amount) / res.getMaxStackSize());
final Map<String, Integer> map = last.getResourceMap();
last.setTotalStacks(last.getTotalStacks() + newStacks - currentStacks);
last.addOrAdjustResource(key, currentQty + amount);
buckets.add(last);
}
this.markDirty();
}
use of com.minecolonies.coremod.colony.buildings.utils.BuilderBucket in project minecolonies by Minecolonies.
the class BuildingResourcesModule method reduceNeededResource.
/**
* Reduce a resource of the needed list.
*
* @param res the resource.
* @param amount the amount.
*/
public void reduceNeededResource(final ItemStack res, final int amount) {
final int hashCode = res.hasTag() ? res.getTag().hashCode() : 0;
final String name = res.getDescriptionId() + "-" + hashCode;
final BuilderBucket last = buckets.isEmpty() ? null : getRequiredResources();
if (last != null) {
final Map<String, Integer> map = last.getResourceMap();
if (map.containsKey(name)) {
int qty = map.get(name) - amount;
if (qty > 0) {
last.addOrAdjustResource(name, map.get(name) - amount);
} else {
last.removeResources(name);
}
}
if (map.isEmpty()) {
buckets.remove();
}
}
int preAmount = 0;
if (this.neededResources.containsKey(name)) {
preAmount = this.neededResources.get(name).getAmount();
}
if (preAmount - amount <= 0) {
this.neededResources.remove(name);
} else {
this.neededResources.get(name).setAmount(preAmount - amount);
}
this.markDirty();
}
use of com.minecolonies.coremod.colony.buildings.utils.BuilderBucket in project minecolonies by Minecolonies.
the class BuildingResourcesModule method addNeededResource.
/**
* Add a new resource to the needed list.
*
* @param res the resource.
* @param amount the amount.
*/
public void addNeededResource(@Nullable final ItemStack res, final int amount) {
if (ItemStackUtils.isEmpty(res) || amount == 0) {
return;
}
final int hashCode = res.hasTag() ? res.getTag().hashCode() : 0;
final String key = res.getDescriptionId() + "-" + hashCode;
BuildingBuilderResource resource = this.neededResources.get(key);
if (resource == null) {
resource = new BuildingBuilderResource(res, amount);
} else {
resource.setAmount(resource.getAmount() + amount);
}
this.neededResources.put(key, resource);
BuilderBucket last = buckets.isEmpty() ? null : buckets.removeLast();
final int stacks = (int) Math.ceil((double) amount / res.getMaxStackSize());
final int max = building.getAllAssignedCitizen().iterator().next().getInventory().getSlots() - 9;
if (last == null || last.getTotalStacks() >= max || last.getTotalStacks() + stacks >= max) {
if (last != null) {
buckets.add(last);
}
last = new BuilderBucket();
last.setTotalStacks(stacks);
last.addOrAdjustResource(key, amount);
buckets.add(last);
} else {
int currentQty = last.getResourceMap().getOrDefault(key, 0);
final int currentStacks = (int) Math.ceil((double) currentQty / res.getMaxStackSize());
final int newStacks = (int) Math.ceil((double) (currentQty + amount) / res.getMaxStackSize());
final Map<String, Integer> map = last.getResourceMap();
last.setTotalStacks(last.getTotalStacks() + newStacks - currentStacks);
last.addOrAdjustResource(key, currentQty + amount);
buckets.add(last);
}
this.markDirty();
}
use of com.minecolonies.coremod.colony.buildings.utils.BuilderBucket in project minecolonies by ldtteam.
the class BuildingResourcesModule method reduceNeededResource.
/**
* Reduce a resource of the needed list.
*
* @param res the resource.
* @param amount the amount.
*/
public void reduceNeededResource(final ItemStack res, final int amount) {
final int hashCode = res.hasTag() ? res.getTag().hashCode() : 0;
final String name = res.getDescriptionId() + "-" + hashCode;
final BuilderBucket last = buckets.isEmpty() ? null : getRequiredResources();
if (last != null) {
final Map<String, Integer> map = last.getResourceMap();
if (map.containsKey(name)) {
int qty = map.get(name) - amount;
if (qty > 0) {
last.addOrAdjustResource(name, map.get(name) - amount);
} else {
last.removeResources(name);
}
}
if (map.isEmpty()) {
buckets.remove();
}
}
int preAmount = 0;
if (this.neededResources.containsKey(name)) {
preAmount = this.neededResources.get(name).getAmount();
}
if (preAmount - amount <= 0) {
this.neededResources.remove(name);
} else {
this.neededResources.get(name).setAmount(preAmount - amount);
}
this.markDirty();
}
Aggregations