use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class TransferItemsToCitizenRequestMessage method messageOnServerThread.
@Override
public void messageOnServerThread(final TransferItemsToCitizenRequestMessage message, final EntityPlayerMP player) {
final Colony colony = ColonyManager.getColony(message.colonyId);
if (colony == null) {
Log.getLogger().warn("TransferItemsRequestMessage colony is null");
return;
}
final CitizenData citizenData = colony.getCitizenManager().getCitizen(message.citizenId);
if (citizenData == null) {
Log.getLogger().warn("TransferItemsRequestMessage citizenData is null");
return;
}
final Optional<EntityCitizen> optionalEntityCitizen = citizenData.getCitizenEntity();
if (!optionalEntityCitizen.isPresent()) {
Log.getLogger().warn("TransferItemsRequestMessage entity citizen is null");
return;
}
final boolean isCreative = player.capabilities.isCreativeMode;
if (message.quantity <= 0 && !isCreative) {
Log.getLogger().warn("TransferItemsRequestMessage quantity below 0");
return;
}
final Item item = message.itemStack.getItem();
final int amountToTake;
if (isCreative) {
amountToTake = message.quantity;
} else {
amountToTake = Math.min(message.quantity, InventoryUtils.getItemCountInItemHandler(new InvWrapper(player.inventory), item, message.itemStack.getItemDamage()));
}
final ItemStack itemStackToTake = message.itemStack.copy();
ItemStackUtils.setSize(itemStackToTake, message.quantity);
final EntityCitizen citizen = optionalEntityCitizen.get();
final ItemStack remainingItemStack = InventoryUtils.addItemStackToItemHandlerWithResult(new InvWrapper(citizen.getInventoryCitizen()), itemStackToTake);
if (!isCreative) {
int amountToRemoveFromPlayer = amountToTake - ItemStackUtils.getSize(remainingItemStack);
while (amountToRemoveFromPlayer > 0) {
final int slot = InventoryUtils.findFirstSlotInItemHandlerWith(new InvWrapper(player.inventory), item, message.itemStack.getItemDamage());
final ItemStack itemsTaken = player.inventory.decrStackSize(slot, amountToRemoveFromPlayer);
amountToRemoveFromPlayer -= ItemStackUtils.getSize(itemsTaken);
}
}
}
use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class AbstractBuildingWorker method readFromNBT.
@Override
public void readFromNBT(@NotNull final NBTTagCompound compound) {
super.readFromNBT(compound);
if (compound.hasKey(TAG_WORKER)) {
try {
final NBTTagList workersTagList = compound.getTagList(TAG_WORKER, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < workersTagList.tagCount(); ++i) {
final CitizenData data = getColony().getCitizenManager().getCitizen(workersTagList.getCompoundTagAt(i).getInteger(TAG_ID));
if (data != null) {
data.setWorkBuilding(this);
workers.add(data);
}
}
} catch (final Exception e) {
MineColonies.getLogger().warn("Warning: Updating data structures:", e);
final CitizenData worker = getColony().getCitizenManager().getCitizen(compound.getInteger(TAG_WORKER));
workers.add(worker);
if (worker != null) {
worker.setWorkBuilding(this);
}
}
}
recipes.clear();
final NBTTagList recipesTags = compound.getTagList(TAG_RECIPES, Constants.NBT.TAG_COMPOUND);
recipes.addAll(NBTUtils.streamCompound(recipesTags).map(recipeCompound -> (IToken) StandardFactoryController.getInstance().deserialize(recipeCompound)).collect(Collectors.toList()));
}
use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class AbstractBuildingWorker method serializeToView.
@Override
public void serializeToView(@NotNull final ByteBuf buf) {
super.serializeToView(buf);
buf.writeInt(workers.size());
for (final CitizenData data : workers) {
buf.writeInt(data == null ? 0 : data.getId());
}
final List<IRecipeStorage> storages = new ArrayList<>();
for (final IToken token : new ArrayList<>(recipes)) {
final IRecipeStorage storage = ColonyManager.getRecipeManager().getRecipes().get(token);
if (storage == null) {
removeRecipe(token);
} else {
storages.add(storage);
}
}
buf.writeInt(storages.size());
for (final IRecipeStorage storage : storages) {
ByteBufUtils.writeTag(buf, StandardFactoryController.getInstance().serialize(storage));
}
}
use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class BuildingBuilder method serializeToView.
/**
* Method to serialize data to send it to the view.
*
* @param buf the used ByteBuffer.
*/
@Override
public void serializeToView(@NotNull final ByteBuf buf) {
super.serializeToView(buf);
updateAvailableResources();
buf.writeInt(neededResources.size());
for (@NotNull final BuildingBuilderResource resource : neededResources.values()) {
ByteBufUtils.writeItemStack(buf, resource.getItemStack());
buf.writeInt(resource.getAvailable());
buf.writeInt(resource.getAmount());
}
final CitizenData data = this.getMainWorker();
if (data != null && data.getJob() instanceof JobBuilder) {
final JobBuilder builderJob = (JobBuilder) data.getJob();
final WorkOrderBuildDecoration workOrderBuildDecoration = builderJob.getWorkOrder();
if (workOrderBuildDecoration != null) {
final BlockPos pos = workOrderBuildDecoration.getBuildingLocation();
final String name = workOrderBuildDecoration instanceof WorkOrderBuild ? ((WorkOrderBuild) workOrderBuildDecoration).getUpgradeName() : workOrderBuildDecoration.getName();
ByteBufUtils.writeUTF8String(buf, name);
final String desc;
if (pos.equals(getLocation())) {
desc = "here";
} else {
final BlockPos relativePos = getLocation().subtract(pos);
final EnumFacing facingX = EnumFacing.getFacingFromVector(relativePos.getX(), 0, 0);
final EnumFacing facingZ = EnumFacing.getFacingFromVector(0, 0, relativePos.getZ());
desc = relativePos.getX() + " " + facingX + " " + relativePos.getZ() + " " + facingZ;
}
ByteBufUtils.writeUTF8String(buf, desc);
} else {
ByteBufUtils.writeUTF8String(buf, "-");
ByteBufUtils.writeUTF8String(buf, "");
}
} else {
ByteBufUtils.writeUTF8String(buf, "-");
ByteBufUtils.writeUTF8String(buf, "");
}
}
use of com.minecolonies.coremod.colony.CitizenData in project minecolonies by Minecolonies.
the class BuildingManager method removeBuilding.
@Override
public void removeBuilding(@NotNull final AbstractBuilding building, final Set<EntityPlayerMP> subscribers) {
if (buildings.remove(building.getID()) != null) {
for (final EntityPlayerMP player : subscribers) {
MineColonies.getNetwork().sendTo(new ColonyViewRemoveBuildingMessage(colony, building.getID()), player);
}
Log.getLogger().info(String.format("Colony %d - removed AbstractBuilding %s of type %s", colony.getID(), building.getID(), building.getSchematicName()));
}
if (building instanceof BuildingTownHall) {
townHall = null;
} else if (building instanceof BuildingWareHouse) {
wareHouse = null;
}
colony.getRequestManager().onProviderRemovedFromColony(building);
// Allow Citizens to fix up any data that wasn't fixed up by the AbstractBuilding's own onDestroyed
for (@NotNull final CitizenData citizen : colony.getCitizenManager().getCitizens()) {
citizen.onRemoveBuilding(building);
}
colony.getCitizenManager().calculateMaxCitizens();
}
Aggregations