Search in sources :

Example 6 with IWorkOrder

use of com.minecolonies.api.colony.workorders.IWorkOrder in project AdvancedPeripherals by Seniorendi.

the class ColonyPeripheral method getWorkOrderResources.

@LuaFunction(mainThread = true)
public final Object getWorkOrderResources(int id) throws LuaException {
    IColony colony = getColony();
    IWorkOrder workOrder = colony.getWorkManager().getWorkOrder(id);
    if (workOrder == null)
        return null;
    return MineColonies.builderResourcesToObject(colony, workOrder.getClaimedBy());
}
Also used : IWorkOrder(com.minecolonies.api.colony.workorders.IWorkOrder) IColony(com.minecolonies.api.colony.IColony) LuaFunction(dan200.computercraft.api.lua.LuaFunction)

Example 7 with IWorkOrder

use of com.minecolonies.api.colony.workorders.IWorkOrder in project AdvancedPeripherals by Seniorendi.

the class ColonyPeripheral method getWorkOrders.

@LuaFunction(mainThread = true)
public final Object getWorkOrders() throws LuaException {
    IColony colony = getColony();
    List<Object> worksData = new ArrayList<>();
    for (IWorkOrder workOrder : colony.getWorkManager().getWorkOrders().values()) worksData.add(MineColonies.workOrderToObject(workOrder));
    return worksData;
}
Also used : IWorkOrder(com.minecolonies.api.colony.workorders.IWorkOrder) IColony(com.minecolonies.api.colony.IColony) LuaFunction(dan200.computercraft.api.lua.LuaFunction)

Example 8 with IWorkOrder

use of com.minecolonies.api.colony.workorders.IWorkOrder in project minecolonies by Minecolonies.

the class BuildingBuilder method searchWorkOrder.

@Override
public void searchWorkOrder() {
    final ICitizenData citizen = getFirstModuleOccurance(WorkerBuildingModule.class).getFirstCitizen();
    if (citizen == null) {
        return;
    }
    final List<IWorkOrder> list = getColony().getWorkManager().getOrderedList(wo -> wo.canBeMadeBy(citizen.getJob()), getPosition());
    list.sort((a, b) -> {
        if (a.getWorkOrderType() == WorkOrderType.REMOVE) {
            return -1;
        }
        if (b.getWorkOrderType() == WorkOrderType.REMOVE) {
            return 1;
        }
        return 0;
    });
    final IWorkOrder order = list.stream().filter(w -> w.getClaimedBy() != null && w.getClaimedBy().equals(getPosition())).findFirst().orElse(null);
    if (order != null) {
        citizen.getJob(JobBuilder.class).setWorkOrder(order);
        order.setClaimedBy(citizen);
        return;
    }
    if (getManualMode()) {
        return;
    }
    for (final IWorkOrder wo : list) {
        double distanceToBuilder = Double.MAX_VALUE;
        if (wo instanceof WorkOrderBuilding && wo.getWorkOrderType() != WorkOrderType.REMOVE && !wo.canBuild(citizen)) {
            continue;
        }
        for (@NotNull final ICitizenData otherBuilder : getColony().getCitizenManager().getCitizens()) {
            final JobBuilder job = otherBuilder.getJob(JobBuilder.class);
            if (job == null || otherBuilder.getWorkBuilding() == null || citizen.getId() == otherBuilder.getId()) {
                continue;
            }
            if (!job.hasWorkOrder() && wo instanceof WorkOrderBuilding && wo.canBuild(otherBuilder)) {
                final double distance = otherBuilder.getWorkBuilding().getID().distSqr(wo.getLocation());
                if (distance < distanceToBuilder) {
                    distanceToBuilder = distance;
                }
            }
        }
        if (citizen.getWorkBuilding().getID().distSqr(wo.getLocation()) < distanceToBuilder) {
            citizen.getJob(JobBuilder.class).setWorkOrder(wo);
            wo.setClaimedBy(citizen);
            return;
        }
    }
}
Also used : IWorkOrder(com.minecolonies.api.colony.workorders.IWorkOrder) WorkerBuildingModule(com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule) JobBuilder(com.minecolonies.coremod.colony.jobs.JobBuilder) ICitizenData(com.minecolonies.api.colony.ICitizenData) WorkOrderBuilding(com.minecolonies.coremod.colony.workorders.WorkOrderBuilding) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with IWorkOrder

use of com.minecolonies.api.colony.workorders.IWorkOrder in project minecolonies by Minecolonies.

the class AbstractWorkOrder method createFromNBT.

/**
 * Create a Work Order from a saved CompoundNBT.
 *
 * @param compound the compound that contains the data for the Work Order
 * @param manager  the work manager.
 * @return {@link IWorkOrder} from the NBT
 */
public static IWorkOrder createFromNBT(@NotNull final CompoundNBT compound, final WorkManager manager) {
    @Nullable IWorkOrder order = null;
    @Nullable Class<? extends IWorkOrder> oclass = null;
    try {
        // TODO: In 1.19 remove this check as this is purely for backwards compatibility with old class mappings
        String type = compound.getString(TAG_TYPE);
        if (type.equals("removal")) {
            oclass = WorkOrderBuilding.class;
        } else {
            oclass = nameToClassBiMap.get(type).getA();
        }
        if (oclass != null) {
            final Constructor<?> constructor = oclass.getDeclaredConstructor();
            order = (IWorkOrder) constructor.newInstance();
        }
    } catch (@NotNull NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
        Log.getLogger().trace(e);
    }
    if (order == null) {
        Log.getLogger().warn(String.format("Unknown WorkOrder type '%s' or missing constructor of proper format.", compound.getString(TAG_TYPE)));
        return null;
    }
    try {
        order.read(compound, manager);
    } catch (final RuntimeException ex) {
        Log.getLogger().error(String.format("A WorkOrder %s(%s) has thrown an exception during loading, its state cannot be restored. Report this to the mod author", compound.getString(TAG_TYPE), oclass.getName()), ex);
        return null;
    }
    return order;
}
Also used : IWorkOrder(com.minecolonies.api.colony.workorders.IWorkOrder) Nullable(org.jetbrains.annotations.Nullable) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with IWorkOrder

use of com.minecolonies.api.colony.workorders.IWorkOrder in project minecolonies by Minecolonies.

the class WorkManager method read.

/**
 * Restore the Work Manager.
 *
 * @param compound Compound to read from.
 */
@Override
public void read(@NotNull final CompoundNBT compound) {
    workOrders.clear();
    // Work Orders
    final ListNBT list = compound.getList(TAG_WORK_ORDERS, NBT.TAG_COMPOUND);
    for (int i = 0; i < list.size(); ++i) {
        final CompoundNBT orderCompound = list.getCompound(i);
        @Nullable final IWorkOrder o = AbstractWorkOrder.createFromNBT(orderCompound, this);
        if (o != null) {
            addWorkOrder(o, true);
            // This is just a failsafe cleanup; this should not happen under normal circumstances
            if (o.isClaimed() && colony.getBuildingManager().getBuilding(o.getClaimedBy()) == null) {
                o.clearClaimedBy();
            }
            topWorkOrderId = Math.max(topWorkOrderId, o.getID());
        }
    }
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) IWorkOrder(com.minecolonies.api.colony.workorders.IWorkOrder) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IWorkOrder (com.minecolonies.api.colony.workorders.IWorkOrder)27 NotNull (org.jetbrains.annotations.NotNull)9 BlockPos (net.minecraft.util.math.BlockPos)7 ICitizenData (com.minecolonies.api.colony.ICitizenData)5 IColony (com.minecolonies.api.colony.IColony)5 WorkOrderBuilding (com.minecolonies.coremod.colony.workorders.WorkOrderBuilding)5 Nullable (org.jetbrains.annotations.Nullable)5 PlacementSettings (com.ldtteam.structurize.util.PlacementSettings)3 IBuilding (com.minecolonies.api.colony.buildings.IBuilding)3 IWorkManager (com.minecolonies.api.colony.workorders.IWorkManager)3 LoadOnlyStructureHandler (com.minecolonies.api.util.LoadOnlyStructureHandler)3 Log (com.minecolonies.api.util.Log)3 WorkerBuildingModule (com.minecolonies.coremod.colony.buildings.modules.WorkerBuildingModule)3 JobBuilder (com.minecolonies.coremod.colony.jobs.JobBuilder)3 CompoundNBT (net.minecraft.nbt.CompoundNBT)3 ListNBT (net.minecraft.nbt.ListNBT)3 Blueprint (com.ldtteam.structures.blueprints.v1.Blueprint)2 StructureName (com.ldtteam.structurize.management.StructureName)2 IColonyManager (com.minecolonies.api.colony.IColonyManager)2 Action (com.minecolonies.api.colony.permissions.Action)2