Search in sources :

Example 16 with CitizenData

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);
        }
    }
}
Also used : Item(net.minecraft.item.Item) InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) Colony(com.minecolonies.coremod.colony.Colony) CitizenData(com.minecolonies.coremod.colony.CitizenData) EntityCitizen(com.minecolonies.coremod.entity.EntityCitizen) ItemStack(net.minecraft.item.ItemStack)

Example 17 with CitizenData

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()));
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) CitizenData(com.minecolonies.coremod.colony.CitizenData)

Example 18 with CitizenData

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));
    }
}
Also used : IToken(com.minecolonies.api.colony.requestsystem.token.IToken) IRecipeStorage(com.minecolonies.api.crafting.IRecipeStorage) CitizenData(com.minecolonies.coremod.colony.CitizenData)

Example 19 with CitizenData

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, "");
    }
}
Also used : WorkOrderBuild(com.minecolonies.coremod.colony.workorders.WorkOrderBuild) EnumFacing(net.minecraft.util.EnumFacing) JobBuilder(com.minecolonies.coremod.colony.jobs.JobBuilder) CitizenData(com.minecolonies.coremod.colony.CitizenData) BlockPos(net.minecraft.util.math.BlockPos) BuildingBuilderResource(com.minecolonies.coremod.colony.buildings.utils.BuildingBuilderResource) NotNull(org.jetbrains.annotations.NotNull) WorkOrderBuildDecoration(com.minecolonies.coremod.colony.workorders.WorkOrderBuildDecoration)

Example 20 with CitizenData

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();
}
Also used : EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) CitizenData(com.minecolonies.coremod.colony.CitizenData) ColonyViewRemoveBuildingMessage(com.minecolonies.coremod.network.messages.ColonyViewRemoveBuildingMessage) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CitizenData (com.minecolonies.coremod.colony.CitizenData)29 BlockPos (net.minecraft.util.math.BlockPos)15 Colony (com.minecolonies.coremod.colony.Colony)14 NotNull (org.jetbrains.annotations.NotNull)9 EntityCitizen (com.minecolonies.coremod.entity.EntityCitizen)7 Nullable (org.jetbrains.annotations.Nullable)7 TextComponentString (net.minecraft.util.text.TextComponentString)6 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)4 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)4 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)3 MineColonies (com.minecolonies.coremod.MineColonies)3 JobDeliveryman (com.minecolonies.coremod.colony.jobs.JobDeliveryman)3 ItemStack (net.minecraft.item.ItemStack)3 Lists (com.google.common.collect.Lists)2 TypeToken (com.google.common.reflect.TypeToken)2 IColony (com.minecolonies.api.colony.IColony)2 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)2 IRequestManager (com.minecolonies.api.colony.requestsystem.manager.IRequestManager)2 Delivery (com.minecolonies.api.colony.requestsystem.requestable.Delivery)2 IRecipeStorage (com.minecolonies.api.crafting.IRecipeStorage)2