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());
}
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;
}
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;
}
}
}
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;
}
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());
}
}
}
Aggregations