Search in sources :

Example 46 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class CommandCitizenKill method onExecute.

/**
 * What happens when the command is executed after preConditions are successful.
 *
 * @param context the context of the command execution
 */
@Override
public int onExecute(final CommandContext<CommandSource> context) {
    // Colony
    final int colonyID = IntegerArgumentType.getInteger(context, COLONYID_ARG);
    final IColony colony = IColonyManager.getInstance().getColonyByDimension(colonyID, context.getSource().getLevel().dimension());
    if (colony == null) {
        context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_COLONY_ID_NOT_FOUND, colonyID), true);
        return 0;
    }
    if (!context.getSource().hasPermission(OP_PERM_LEVEL) && !MineColonies.getConfig().getServer().canPlayerUseKillCitizensCommand.get()) {
        context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_DISABLED_IN_CONFIG), true);
        return 0;
    }
    final ICitizenData citizenData = colony.getCitizenManager().getCivilian(IntegerArgumentType.getInteger(context, CITIZENID_ARG));
    if (citizenData == null) {
        context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_NOT_FOUND), true);
        return 0;
    }
    final Optional<AbstractEntityCitizen> optionalEntityCitizen = citizenData.getEntity();
    if (!optionalEntityCitizen.isPresent()) {
        context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_NOT_LOADED), true);
        return 0;
    }
    context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_INFO, citizenData.getId(), citizenData.getName()), true);
    final BlockPos position = optionalEntityCitizen.get().blockPosition();
    context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_INFO_POSITION, position.getX(), position.getY(), position.getZ()), true);
    context.getSource().sendSuccess(new TranslationTextComponent(CommandTranslationConstants.COMMAND_CITIZEN_KILL_SUCCESS, position.getX(), position.getY(), position.getZ()), true);
    optionalEntityCitizen.get().die(CONSOLE_DAMAGE_SOURCE);
    return 1;
}
Also used : AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) IColony(com.minecolonies.api.colony.IColony) TranslationTextComponent(net.minecraft.util.text.TranslationTextComponent) ICitizenData(com.minecolonies.api.colony.ICitizenData) BlockPos(net.minecraft.util.math.BlockPos)

Example 47 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class RestartCitizenMessage method onExecute.

@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony) {
    final ServerPlayerEntity player = ctxIn.getSender();
    if (player == null) {
        return;
    }
    final ICitizenData citizen = colony.getCitizenManager().getCivilian(citizenID);
    // Restart also worker building and AI
    citizen.scheduleRestart(player);
    MessageUtils.format(MESSAGE_CITIZEN_RESTART_SCHEDULED, citizen.getName()).sendTo(player);
}
Also used : ICitizenData(com.minecolonies.api.colony.ICitizenData) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity)

Example 48 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class TransferItemsToCitizenRequestMessage method onExecute.

@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony) {
    final ICitizenData citizenData = colony.getCitizenManager().getCivilian(citizenId);
    if (citizenData == null) {
        Log.getLogger().warn("TransferItemsRequestMessage citizenData is null");
        return;
    }
    final Optional<AbstractEntityCitizen> optionalEntityCitizen = citizenData.getEntity();
    if (!optionalEntityCitizen.isPresent()) {
        Log.getLogger().warn("TransferItemsRequestMessage entity citizen is null");
        return;
    }
    final PlayerEntity player = ctxIn.getSender();
    if (player == null) {
        return;
    }
    final boolean isCreative = player.isCreative();
    if (quantity <= 0 && !isCreative) {
        Log.getLogger().warn("TransferItemsRequestMessage quantity below 0");
        return;
    }
    // Inventory content before
    Map<ItemStorage, ItemStorage> previousContent = null;
    final int amountToTake;
    if (isCreative) {
        amountToTake = quantity;
    } else {
        amountToTake = Math.min(quantity, InventoryUtils.getItemCountInItemHandler(new InvWrapper(player.inventory), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, itemStack)));
    }
    final List<ItemStack> itemsToPut = new ArrayList<>();
    int tempAmount = amountToTake;
    while (tempAmount > 0) {
        int count = Math.min(itemStack.getMaxStackSize(), tempAmount);
        ItemStack stack = itemStack.copy();
        stack.setCount(count);
        itemsToPut.add(stack);
        tempAmount -= count;
    }
    final AbstractEntityCitizen citizen = optionalEntityCitizen.get();
    if (!isCreative && MineColonies.getConfig().getServer().debugInventories.get()) {
        previousContent = InventoryUtils.getAllItemsForProviders(citizen.getInventoryCitizen(), new InvWrapper(player.inventory));
    }
    tempAmount = 0;
    for (final ItemStack insertStack : itemsToPut) {
        final ItemStack remainingItemStack = InventoryUtils.addItemStackToItemHandlerWithResult(citizen.getInventoryCitizen(), insertStack);
        if (!ItemStackUtils.isEmpty(remainingItemStack)) {
            tempAmount += (insertStack.getCount() - remainingItemStack.getCount());
            break;
        }
        tempAmount += insertStack.getCount();
    }
    if (!isCreative) {
        int amountToRemoveFromPlayer = tempAmount;
        while (amountToRemoveFromPlayer > 0) {
            final int slot = InventoryUtils.findFirstSlotInItemHandlerWith(new InvWrapper(player.inventory), stack -> ItemStackUtils.compareItemStacksIgnoreStackSize(stack, itemStack));
            final ItemStack itemsTaken = player.inventory.removeItem(slot, amountToRemoveFromPlayer);
            amountToRemoveFromPlayer -= ItemStackUtils.getSize(itemsTaken);
        }
    }
    if (!isCreative && previousContent != null && MineColonies.getConfig().getServer().debugInventories.get()) {
        InventoryUtils.doStorageSetsMatch(previousContent, InventoryUtils.getAllItemsForProviders(citizen.getInventoryCitizen(), new InvWrapper(player.inventory)), true);
    }
}
Also used : InvWrapper(net.minecraftforge.items.wrapper.InvWrapper) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ArrayList(java.util.ArrayList) ICitizenData(com.minecolonies.api.colony.ICitizenData) ItemStack(net.minecraft.item.ItemStack) ItemStorage(com.minecolonies.api.crafting.ItemStorage) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Example 49 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class AssignUnassignMessage method onExecute.

@Override
public void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony, final DefaultBuildingInstance building) {
    final ICitizenData citizen = colony.getCitizenManager().getCivilian(citizenID);
    final AbstractAssignedCitizenModule module;
    if (jobEntry == null) {
        module = building.getFirstModuleOccurance(LivingBuildingModule.class);
    } else {
        module = building.getModuleMatching(WorkerBuildingModule.class, m -> m.getJobEntry() == jobEntry);
    }
    if (assign && !module.isFull() && !building.equals(citizen.getHomeBuilding())) {
        if (citizen.getHomeBuilding() != null) {
            citizen.getHomeBuilding().getFirstModuleOccurance(LivingBuildingModule.class).removeCitizen(citizen);
        }
        module.assignCitizen(citizen);
    } else if (module.hasAssignedCitizen(citizen)) {
        module.removeCitizen(citizen);
    }
}
Also used : DefaultBuildingInstance(com.minecolonies.coremod.colony.buildings.DefaultBuildingInstance) WorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule) AbstractAssignedCitizenModule(com.minecolonies.coremod.colony.buildings.modules.AbstractAssignedCitizenModule) AbstractBuildingServerMessage(com.minecolonies.coremod.network.messages.server.AbstractBuildingServerMessage) IBuildingView(com.minecolonies.api.colony.buildings.views.IBuildingView) IColony(com.minecolonies.api.colony.IColony) NetworkEvent(net.minecraftforge.fml.network.NetworkEvent) ICitizenData(com.minecolonies.api.colony.ICitizenData) JobEntry(com.minecolonies.api.colony.jobs.registry.JobEntry) LivingBuildingModule(com.minecolonies.coremod.colony.buildings.modules.LivingBuildingModule) NotNull(org.jetbrains.annotations.NotNull) PacketBuffer(net.minecraft.network.PacketBuffer) LivingBuildingModule(com.minecolonies.coremod.colony.buildings.modules.LivingBuildingModule) WorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule) ICitizenData(com.minecolonies.api.colony.ICitizenData) AbstractAssignedCitizenModule(com.minecolonies.coremod.colony.buildings.modules.AbstractAssignedCitizenModule)

Example 50 with ICitizenData

use of com.minecolonies.api.colony.ICitizenData in project minecolonies by Minecolonies.

the class RecallCitizenMessage method onExecute.

@Override
protected void onExecute(final NetworkEvent.Context ctxIn, final boolean isLogicalServer, final IColony colony, final IBuilding building) {
    final List<ICitizenData> citizens = new ArrayList<>(building.getAllAssignedCitizen());
    for (int i = 0; i < building.getAllAssignedCitizen().size(); i++) {
        Optional<AbstractEntityCitizen> optionalEntityCitizen = citizens.get(i).getEntity();
        final ICitizenData citizenData = citizens.get(i);
        if (!optionalEntityCitizen.isPresent()) {
            if (citizenData != null) {
                Log.getLogger().warn(String.format("Citizen #%d:%d has gone AWOL, respawning them!", colony.getID(), citizenData.getId()));
                citizenData.setNextRespawnPosition(EntityUtils.getSpawnPoint(colony.getWorld(), building.getPosition()));
                citizenData.updateEntityIfNecessary();
            } else {
                Log.getLogger().warn("Citizen is AWOL and citizenData is null!");
                return;
            }
        } else if (optionalEntityCitizen.get().getTicksExisted() == 0) {
            citizenData.getEntity().ifPresent(Entity::remove);
            citizenData.updateEntityIfNecessary();
        }
        final BlockPos loc = building.getPosition();
        if (optionalEntityCitizen.isPresent() && !TeleportHelper.teleportCitizen(optionalEntityCitizen.get(), colony.getWorld(), loc)) {
            final PlayerEntity player = ctxIn.getSender();
            if (player == null) {
                return;
            }
            MessageUtils.format(WARNING_CITIZEN_RECALL_FAILED).sendTo(player);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AbstractEntityCitizen(com.minecolonies.api.entity.citizen.AbstractEntityCitizen) ICitizenData(com.minecolonies.api.colony.ICitizenData) BlockPos(net.minecraft.util.math.BlockPos) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Aggregations

ICitizenData (com.minecolonies.api.colony.ICitizenData)180 NotNull (org.jetbrains.annotations.NotNull)49 BlockPos (net.minecraft.util.math.BlockPos)47 IColony (com.minecolonies.api.colony.IColony)46 AbstractEntityCitizen (com.minecolonies.api.entity.citizen.AbstractEntityCitizen)40 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)33 TranslationTextComponent (net.minecraft.util.text.TranslationTextComponent)31 ItemStack (net.minecraft.item.ItemStack)28 WorkerBuildingModule (com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule)23 PlayerEntity (net.minecraft.entity.player.PlayerEntity)23 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)22 Nullable (org.jetbrains.annotations.Nullable)22 CompoundNBT (net.minecraft.nbt.CompoundNBT)18 Colony (com.minecolonies.coremod.colony.Colony)16 ArrayList (java.util.ArrayList)16 AbstractJobCrafter (com.minecolonies.coremod.colony.jobs.AbstractJobCrafter)14 EntityCitizen (com.minecolonies.coremod.entity.citizen.EntityCitizen)14 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)13 AbstractAssignedCitizenModule (com.minecolonies.coremod.colony.buildings.modules.AbstractAssignedCitizenModule)12 java.util (java.util)12