Search in sources :

Example 21 with IToken

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;
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagInt(net.minecraft.nbt.NBTTagInt) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 22 with IToken

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);
}
Also used : IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) java.util(java.util) Button(com.minecolonies.blockout.controls.Button) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) ItemIcon(com.minecolonies.blockout.controls.ItemIcon) ColonyView(com.minecolonies.coremod.colony.ColonyView) ItemStack(net.minecraft.item.ItemStack) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ColonyManager(com.minecolonies.coremod.colony.ColonyManager) Minecraft(net.minecraft.client.Minecraft) IRetryingRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.retrying.IRetryingRequestResolver) Constants(com.minecolonies.api.util.constant.Constants) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) BlockPos(net.minecraft.util.math.BlockPos) ScrollingList(com.minecolonies.blockout.views.ScrollingList) Collectors(java.util.stream.Collectors) UpdateRequestStateMessage(com.minecolonies.coremod.network.messages.UpdateRequestStateMessage) IPlayerRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.player.IPlayerRequestResolver) Nullable(org.jetbrains.annotations.Nullable) GuiScreen(net.minecraft.client.gui.GuiScreen) MineColonies(com.minecolonies.coremod.MineColonies) Image(com.minecolonies.blockout.controls.Image) Label(com.minecolonies.blockout.controls.Label) NotNull(org.jetbrains.annotations.NotNull) ColonyView(com.minecolonies.coremod.colony.ColonyView) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) IPlayerRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.player.IPlayerRequestResolver) BlockPos(net.minecraft.util.math.BlockPos) IRequest(com.minecolonies.api.colony.requestsystem.request.IRequest) IRetryingRequestResolver(com.minecolonies.api.colony.requestsystem.resolver.retrying.IRetryingRequestResolver)

Example 23 with IToken

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);
}
Also used : IToken(com.minecolonies.api.colony.requestsystem.token.IToken) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with IToken

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);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) ArrayList(java.util.ArrayList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Block(net.minecraft.block.Block) ItemStack(net.minecraft.item.ItemStack) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with IToken

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());
}
Also used : StandardToken(com.minecolonies.api.colony.requestsystem.token.StandardToken) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) TypeToken(com.google.common.reflect.TypeToken) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Test(org.junit.Test)

Aggregations

IToken (com.minecolonies.api.colony.requestsystem.token.IToken)25 NotNull (org.jetbrains.annotations.NotNull)12 ILocation (com.minecolonies.api.colony.requestsystem.location.ILocation)7 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)7 IRequest (com.minecolonies.api.colony.requestsystem.request.IRequest)6 TypeToken (com.google.common.reflect.TypeToken)5 Collectors (java.util.stream.Collectors)5 MineColonies (com.minecolonies.coremod.MineColonies)4 NBTTagList (net.minecraft.nbt.NBTTagList)4 ImmutableList (com.google.common.collect.ImmutableList)3 IRequestManager (com.minecolonies.api.colony.requestsystem.manager.IRequestManager)3 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)3 IRequester (com.minecolonies.api.colony.requestsystem.requester.IRequester)3 StandardToken (com.minecolonies.api.colony.requestsystem.token.StandardToken)3 NBTUtils (com.minecolonies.api.util.NBTUtils)3 TypeConstants (com.minecolonies.api.util.constant.TypeConstants)3 CitizenData (com.minecolonies.coremod.colony.CitizenData)3 Colony (com.minecolonies.coremod.colony.Colony)3 java.util (java.util)3 Nullable (org.jetbrains.annotations.Nullable)3