Search in sources :

Example 1 with IGuardBuilding

use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.

the class ItemBannerRallyGuards method broadcastPlayerToRally.

/**
 * Broadcasts the player all the guardtowers rallied by the item are supposed to follow.
 *
 * @param banner   The banner that should broadcast
 * @param playerIn The player to follow. Can be null, if the towers should revert to "normal" mode
 * @return The number of guards rallied
 */
public static int broadcastPlayerToRally(final ItemStack banner, final World worldIn, @Nullable final PlayerEntity playerIn) {
    if (worldIn.isClientSide()) {
        Log.getLogger().error("Tried to run server-side function #broadcastPlayerToRally() on the client-side!");
        return 0;
    }
    @Nullable ILocation rallyTarget = null;
    if (!isActive(banner) || playerIn == null) {
        rallyTarget = null;
    } else {
        rallyTarget = new EntityLocation(playerIn.getUUID());
    }
    int numGuards = 0;
    for (final ILocation guardTowerLocation : getGuardTowerLocations(banner)) {
        // Note: getCurrentServer().getWorld() must be used here because MineColonies.proxy.getWorld() fails on single player worlds
        // We are sure we are on the server-side in this function though, so it's fine.
        final IGuardBuilding building = getGuardBuilding(ServerLifecycleHooks.getCurrentServer().getLevel(guardTowerLocation.getDimension()), guardTowerLocation.getInDimensionLocation());
        // Safely ignore this case, the player must remove the tower from the rallying list manually.
        if (building != null && (playerIn == null || building.getColony().getPermissions().hasPermission(playerIn, Action.RALLY_GUARDS))) {
            building.setRallyLocation(rallyTarget);
            numGuards += building.getAllAssignedCitizen().size();
        }
    }
    return numGuards;
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) EntityLocation(com.minecolonies.coremod.colony.requestsystem.locations.EntityLocation) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) Nullable(javax.annotation.Nullable)

Example 2 with IGuardBuilding

use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.

the class ItemScepterGuard method handleItemUsage.

/**
 * Handles the usage of the item.
 *
 * @param worldIn  the world it is used in.
 * @param pos      the position.
 * @param compound the compound.
 * @param playerIn the player using it.
 * @return if it has been successful.
 */
@NotNull
private static ActionResultType handleItemUsage(final World worldIn, final BlockPos pos, final CompoundNBT compound, final PlayerEntity playerIn) {
    if (!compound.getAllKeys().contains(TAG_ID)) {
        return ActionResultType.FAIL;
    }
    final IColony colony = IColonyManager.getInstance().getColonyByWorld(compound.getInt(TAG_ID), worldIn);
    if (colony == null) {
        return ActionResultType.FAIL;
    }
    final BlockPos guardTower = BlockPosUtil.read(compound, TAG_POS);
    final IBuilding hut = colony.getBuildingManager().getBuilding(guardTower);
    if (!(hut instanceof AbstractBuildingGuards)) {
        return ActionResultType.FAIL;
    }
    final IGuardBuilding tower = (IGuardBuilding) hut;
    if (BlockPosUtil.getDistance2D(pos, guardTower) > tower.getPatrolDistance()) {
        LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolClickGuardTooFar");
        return ActionResultType.FAIL;
    }
    if (hut.getSetting(AbstractBuildingGuards.GUARD_TASK).getValue().equals(GuardTaskSetting.GUARD)) {
        LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolclickguard", pos);
        tower.setGuardPos(pos);
        playerIn.inventory.removeItemNoUpdate(playerIn.inventory.selected);
    } else {
        if (!compound.getAllKeys().contains(TAG_LAST_POS)) {
            tower.resetPatrolTargets();
        }
        tower.addPatrolTargets(pos);
        LanguageHandler.sendPlayerMessage(playerIn, "com.minecolonies.coremod.job.guard.toolclickpatrol", pos);
    }
    BlockPosUtil.write(compound, TAG_LAST_POS, pos);
    return ActionResultType.SUCCESS;
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) AbstractBuildingGuards(com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards) IBuilding(com.minecolonies.api.colony.buildings.IBuilding) IColony(com.minecolonies.api.colony.IColony) BlockPos(net.minecraft.util.math.BlockPos) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with IGuardBuilding

use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.

the class AbstractEntityAIFight method atBuildingActions.

/**
 * Task to do when at the own building, as guards only go there on requests and on dump
 */
protected void atBuildingActions() {
    final IGuardBuilding building = getOwnBuilding();
    for (final List<GuardGear> itemList : itemsNeeded) {
        for (final GuardGear item : itemList) {
            if (!(building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired())) {
                continue;
            }
            if (item.getItemNeeded() == ToolType.SHIELD && worker.getCitizenColonyHandler().getColony().getResearchManager().getResearchEffects().getEffectStrength(SHIELD_USAGE) <= 0) {
                continue;
            }
            int bestSlot = -1;
            int bestLevel = -1;
            IItemHandler bestHandler = null;
            if (!ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType()))) {
                bestLevel = ItemStackUtils.getMiningLevel(worker.getItemBySlot(item.getType()), item.getItemNeeded());
            }
            final Map<IItemHandler, List<Integer>> items = InventoryUtils.findAllSlotsInProviderWith(building, item::test);
            if (items.isEmpty()) {
                // None found, check for equipped
                if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType()))) {
                    // create request
                    checkForToolorWeaponASync(item.getItemNeeded(), item.getMinArmorLevel(), item.getMaxArmorLevel());
                }
            } else {
                // Compare levels
                for (Map.Entry<IItemHandler, List<Integer>> entry : items.entrySet()) {
                    for (final Integer slot : entry.getValue()) {
                        final ItemStack stack = entry.getKey().getStackInSlot(slot);
                        if (ItemStackUtils.isEmpty(stack)) {
                            continue;
                        }
                        int currentLevel = ItemStackUtils.getMiningLevel(stack, item.getItemNeeded());
                        if (currentLevel > bestLevel) {
                            bestLevel = currentLevel;
                            bestSlot = slot;
                            bestHandler = entry.getKey();
                        }
                    }
                }
            }
            // Transfer if needed
            if (bestHandler != null) {
                if (!ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType()))) {
                    final int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), stack -> stack == worker.getItemBySlot(item.getType()));
                    if (slot > -1) {
                        InventoryUtils.transferItemStackIntoNextFreeSlotInProvider(worker.getInventoryCitizen(), slot, building);
                    }
                }
                // Used for further comparisons, set to the right inventory slot afterwards
                worker.setItemSlot(item.getType(), bestHandler.getStackInSlot(bestSlot));
                InventoryUtils.transferItemStackIntoNextFreeSlotInItemHandler(bestHandler, bestSlot, worker.getInventoryCitizen());
            }
        }
    }
    equipInventoryArmor();
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) IItemHandler(net.minecraftforge.items.IItemHandler) GuardGear(com.minecolonies.api.entity.ai.citizen.guards.GuardGear) ArrayList(java.util.ArrayList) List(java.util.List) ItemStack(net.minecraft.item.ItemStack) Map(java.util.Map)

Example 4 with IGuardBuilding

use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by ldtteam.

the class AbstractEntityAIFight method equipInventoryArmor.

/**
 * Equips armor existing in inventory
 */
public void equipInventoryArmor() {
    cleanArmor();
    final IGuardBuilding building = getOwnBuilding();
    for (final List<GuardGear> itemList : itemsNeeded) {
        for (final GuardGear item : itemList) {
            if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType())) && building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired()) {
                int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), item::test);
                if (slot > -1) {
                    worker.setItemSlot(item.getType(), worker.getInventoryCitizen().getStackInSlot(slot));
                }
            }
        }
    }
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) GuardGear(com.minecolonies.api.entity.ai.citizen.guards.GuardGear)

Example 5 with IGuardBuilding

use of com.minecolonies.api.colony.buildings.IGuardBuilding in project minecolonies by Minecolonies.

the class AbstractEntityAIFight method equipInventoryArmor.

/**
 * Equips armor existing in inventory
 */
public void equipInventoryArmor() {
    cleanArmor();
    final IGuardBuilding building = getOwnBuilding();
    for (final List<GuardGear> itemList : itemsNeeded) {
        for (final GuardGear item : itemList) {
            if (ItemStackUtils.isEmpty(worker.getItemBySlot(item.getType())) && building.getBuildingLevel() >= item.getMinBuildingLevelRequired() && building.getBuildingLevel() <= item.getMaxBuildingLevelRequired()) {
                int slot = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getInventoryCitizen(), item::test);
                if (slot > -1) {
                    worker.setItemSlot(item.getType(), worker.getInventoryCitizen().getStackInSlot(slot));
                }
            }
        }
    }
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) GuardGear(com.minecolonies.api.entity.ai.citizen.guards.GuardGear)

Aggregations

IGuardBuilding (com.minecolonies.api.colony.buildings.IGuardBuilding)9 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)4 NotNull (org.jetbrains.annotations.NotNull)4 GuardGear (com.minecolonies.api.entity.ai.citizen.guards.GuardGear)3 ItemStack (net.minecraft.item.ItemStack)3 IColony (com.minecolonies.api.colony.IColony)2 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)2 AbstractBuildingGuards (com.minecolonies.coremod.colony.buildings.AbstractBuildingGuards)2 EntityLocation (com.minecolonies.coremod.colony.requestsystem.locations.EntityLocation)2 Nullable (javax.annotation.Nullable)2 PlayerEntity (net.minecraft.entity.player.PlayerEntity)2 CompoundNBT (net.minecraft.nbt.CompoundNBT)2 ListNBT (net.minecraft.nbt.ListNBT)2 BlockPos (net.minecraft.util.math.BlockPos)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 IItemHandler (net.minecraftforge.items.IItemHandler)1