Search in sources :

Example 6 with IGuardBuilding

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

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()) {
        MessageUtils.format(TOOL_GUARD_SCEPTER_TOWER_TOO_FAR).sendTo(playerIn);
        return ActionResultType.FAIL;
    }
    if (hut.getSetting(AbstractBuildingGuards.GUARD_TASK).getValue().equals(GuardTaskSetting.GUARD)) {
        MessageUtils.format(TOOL_GUARD_SCEPTER_ADD_GUARD_TARGET, pos).sendTo(playerIn);
        tower.setGuardPos(pos);
        playerIn.inventory.removeItemNoUpdate(playerIn.inventory.selected);
    } else {
        if (!compound.getAllKeys().contains(TAG_LAST_POS)) {
            tower.resetPatrolTargets();
        }
        tower.addPatrolTargets(pos);
        MessageUtils.format(TOOL_GUARD_SCEPTER_ADD_PATROL_TARGET, pos).sendTo(playerIn);
    }
    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 7 with IGuardBuilding

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

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 8 with IGuardBuilding

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

the class ItemBannerRallyGuards method useOn.

@NotNull
@Override
public ActionResultType useOn(final ItemUseContext context) {
    final PlayerEntity player = context.getPlayer();
    if (player == null) {
        return ActionResultType.FAIL;
    }
    final ItemStack banner = context.getPlayer().getItemInHand(context.getHand());
    final CompoundNBT compound = checkForCompound(banner);
    if (isGuardBuilding(context.getLevel(), context.getClickedPos())) {
        if (context.getLevel().isClientSide()) {
            return ActionResultType.SUCCESS;
        } else {
            final IGuardBuilding building = getGuardBuilding(context.getLevel(), context.getClickedPos());
            if (!building.getColony().getPermissions().hasPermission(player, Action.RALLY_GUARDS)) {
                MessageUtils.format(PERMISSION_DENIED).sendTo(player);
                return ActionResultType.FAIL;
            }
            final ILocation location = building.getLocation();
            if (removeGuardTowerAtLocation(banner, location)) {
                MessageUtils.format(COM_MINECOLONIES_BANNER_RALLY_GUARDS_DESELECTED, building.getSchematicName(), location.toString()).sendTo(player);
            } else {
                final ListNBT guardTowers = compound.getList(TAG_RALLIED_GUARDTOWERS, TAG_COMPOUND);
                guardTowers.add(StandardFactoryController.getInstance().serialize(location));
                MessageUtils.format(COM_MINECOLONIES_BANNER_RALLY_GUARDS_SELECTED, building.getSchematicName(), location.toString()).sendTo(player);
            }
        }
    } else {
        handleRightClick(banner, context.getPlayer());
    }
    return ActionResultType.SUCCESS;
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) ListNBT(net.minecraft.nbt.ListNBT) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) CompoundNBT(net.minecraft.nbt.CompoundNBT) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with IGuardBuilding

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

the class ItemBannerRallyGuards method useOn.

@NotNull
@Override
public ActionResultType useOn(final ItemUseContext context) {
    final PlayerEntity player = context.getPlayer();
    if (player == null) {
        return ActionResultType.FAIL;
    }
    final ItemStack banner = context.getPlayer().getItemInHand(context.getHand());
    final CompoundNBT compound = checkForCompound(banner);
    if (isGuardBuilding(context.getLevel(), context.getClickedPos())) {
        if (context.getLevel().isClientSide()) {
            return ActionResultType.SUCCESS;
        } else {
            final IGuardBuilding building = getGuardBuilding(context.getLevel(), context.getClickedPos());
            if (!building.getColony().getPermissions().hasPermission(player, Action.RALLY_GUARDS)) {
                LanguageHandler.sendPlayerMessage(player, "com.minecolonies.coremod.permission.no");
                return ActionResultType.FAIL;
            }
            final ILocation location = building.getLocation();
            if (removeGuardTowerAtLocation(banner, location)) {
                LanguageHandler.sendPlayerMessage(context.getPlayer(), TranslationConstants.COM_MINECOLONIES_BANNER_RALLY_GUARDS_DESELECTED, building.getSchematicName(), location.toString());
            } else {
                final ListNBT guardTowers = compound.getList(TAG_RALLIED_GUARDTOWERS, TAG_COMPOUND);
                guardTowers.add(StandardFactoryController.getInstance().serialize(location));
                LanguageHandler.sendPlayerMessage(context.getPlayer(), TranslationConstants.COM_MINECOLONIES_BANNER_RALLY_GUARDS_SELECTED, building.getSchematicName(), location.toString());
            }
        }
    } else {
        handleRightClick(banner, context.getPlayer());
    }
    return ActionResultType.SUCCESS;
}
Also used : IGuardBuilding(com.minecolonies.api.colony.buildings.IGuardBuilding) ListNBT(net.minecraft.nbt.ListNBT) ILocation(com.minecolonies.api.colony.requestsystem.location.ILocation) CompoundNBT(net.minecraft.nbt.CompoundNBT) ItemStack(net.minecraft.item.ItemStack) PlayerEntity(net.minecraft.entity.player.PlayerEntity) NotNull(org.jetbrains.annotations.NotNull)

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