use of com.minecolonies.api.colony.requestsystem.token.IToken in project minecolonies by Minecolonies.
the class StandardRequestFactories method serializeToNBT.
public static <T extends IRequestable> NBTTagCompound serializeToNBT(final IFactoryController controller, final IRequest<T> request, final IObjectToNBTConverter<T> typeSerialization) {
final NBTTagCompound compound = new NBTTagCompound();
final NBTTagCompound requesterCompound = controller.serialize(request.getRequester());
final NBTTagCompound tokenCompound = controller.serialize(request.getToken());
final NBTTagInt stateCompound = request.getState().serializeNBT();
final NBTTagCompound requestedCompound = typeSerialization.apply(controller, request.getRequest());
final NBTTagList childrenCompound = new NBTTagList();
for (final IToken token : request.getChildren()) {
childrenCompound.appendTag(controller.serialize(token));
}
compound.setTag(NBT_REQUESTER, requesterCompound);
compound.setTag(NBT_TOKEN, tokenCompound);
compound.setTag(NBT_STATE, stateCompound);
compound.setTag(NBT_REQUESTED, requestedCompound);
if (request.hasResult()) {
compound.setTag(NBT_RESULT, typeSerialization.apply(controller, request.getResult()));
}
if (request.hasParent()) {
compound.setTag(NBT_PARENT, controller.serialize(request.getParent()));
}
compound.setTag(NBT_CHILDREN, childrenCompound);
return compound;
}
use of com.minecolonies.api.colony.requestsystem.token.IToken in project minecolonies by Minecolonies.
the class WindowClipBoard method getOpenRequests.
private ImmutableList<IRequest> getOpenRequests() {
final ArrayList<IRequest> requests = Lists.newArrayList();
final ColonyView view = ColonyManager.getColonyView(colonyId);
if (view == null) {
return ImmutableList.of();
}
final IPlayerRequestResolver resolver = view.getRequestManager().getPlayerResolver();
final IRetryingRequestResolver retryingRequestResolver = view.getRequestManager().getRetryingRequestResolver();
final Set<IToken> requestTokens = new HashSet<>();
requestTokens.addAll(resolver.getAllAssignedRequests());
requestTokens.addAll(retryingRequestResolver.getAllAssignedRequests());
requests.addAll(requestTokens.stream().map(view.getRequestManager()::getRequestForToken).filter(Objects::nonNull).collect(Collectors.toSet()));
final BlockPos playerPos = Minecraft.getMinecraft().player.getPosition();
requests.sort(Comparator.comparing((IRequest request) -> request.getRequester().getDeliveryLocation().getInDimensionLocation().getDistance(playerPos.getX(), playerPos.getY(), playerPos.getZ())).thenComparingInt(request -> request.getToken().hashCode()));
return ImmutableList.copyOf(requests);
}
use of com.minecolonies.api.colony.requestsystem.token.IToken in project minecolonies by Minecolonies.
the class IRequestFactory method getNewInstance.
/**
* Method to get a new instance of the output given the input and additional context data.
*
* @param factoryController The factory controller that called this facotry method.
* @param t The input to build a new output for.
* @param context The context of the request.
* @return The new output instance for a given input.
*
* @throws IllegalArgumentException is thrown when the factory cannot produce a new instance out of the given context and input.
*/
@NotNull
@Override
default R getNewInstance(@NotNull final IFactoryController factoryController, @NotNull final T t, @NotNull final Object... context) throws IllegalArgumentException {
if (context.length != 2 && context.length != 3) {
throw new IllegalArgumentException("Unsupported context - Too many parameters.");
}
if (!(context[0] instanceof IToken)) {
throw new IllegalArgumentException("Unsupported context - First context object is not a token");
}
if (!(context[1] instanceof IRequester)) {
throw new IllegalArgumentException("Unsupported context - Second context object should be a location");
}
if (context.length == NUMBER_OF_PROPERTIES) {
final IRequester requester = (IRequester) context[1];
final IToken token = (IToken) context[0];
return this.getNewInstance(t, requester, token);
}
if (!(context[2] instanceof RequestState)) {
throw new IllegalArgumentException("Unsupported context - Third context object is not a request state");
}
final IRequester requester = (IRequester) context[1];
final IToken token = (IToken) context[0];
final RequestState state = (RequestState) context[2];
return this.getNewInstance(t, requester, token, state);
}
use of com.minecolonies.api.colony.requestsystem.token.IToken in project minecolonies by Minecolonies.
the class RecipeStorageFactory method deserialize.
@NotNull
@Override
public RecipeStorage deserialize(@NotNull final IFactoryController controller, @NotNull final NBTTagCompound nbt) {
final List<ItemStack> input = new ArrayList<>();
final NBTTagList inputTagList = nbt.getTagList(INPUT_TAG, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < inputTagList.tagCount(); ++i) {
final NBTTagCompound inputTag = inputTagList.getCompoundTagAt(i);
input.add(new ItemStack(inputTag));
}
final ItemStack primaryOutput = new ItemStack(nbt);
final Block intermediate = NBTUtil.readBlockState(nbt).getBlock();
final int gridSize = nbt.getInteger(TAG_GRID);
final IToken token = StandardFactoryController.getInstance().deserialize(nbt.getCompoundTag(TAG_TOKEN));
return this.getNewInstance(token, input, gridSize, primaryOutput, intermediate);
}
use of com.minecolonies.api.colony.requestsystem.token.IToken in project minecolonies by Minecolonies.
the class StandardFactoryControllerTest method testSerialize.
@Test
public void testSerialize() {
final StandardToken standardToken = new StandardToken(UUID.randomUUID());
final IToken token = standardToken;
final NBTTagCompound compound = StandardFactoryController.getInstance().serialize(token);
assertTrue(compound.hasKey(StandardFactoryController.NBT_TYPE));
assertTrue(compound.hasKey(StandardFactoryController.NBT_DATA));
assertEquals(compound.getString(StandardFactoryController.NBT_TYPE), new TypeToken<StandardToken>() {
}.toString());
assertEquals(compound.getCompoundTag(StandardFactoryController.NBT_DATA).getLong(StandardTokenFactory.NBT_MSB), standardToken.getIdentifier().getMostSignificantBits());
assertEquals(compound.getCompoundTag(StandardFactoryController.NBT_DATA).getLong(StandardTokenFactory.NBT_LSB), standardToken.getIdentifier().getLeastSignificantBits());
}
Aggregations