Search in sources :

Example 1 with IntNBT

use of net.minecraft.nbt.IntNBT in project Mekanism by mekanism.

the class CCArgumentWrapperTest method testInvalidHintNumber.

@Test
@DisplayName("Test hint mismatch expected type using numbers as actual")
void testInvalidHintNumber() {
    IntNBT nbt = IntNBT.valueOf(100);
    Object withHint = addInvalidHint(nbt, Constants.NBT.TAG_COMPOUND);
    Assertions.assertNotEquals(nbt, CCArgumentWrapperTestHelper.sanitize(nbt.getClass(), withHint));
}
Also used : IntNBT(net.minecraft.nbt.IntNBT) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with IntNBT

use of net.minecraft.nbt.IntNBT in project minecolonies by ldtteam.

the class StandardRequestFactories method serializeToNBT.

public static <T extends IRequestable> CompoundNBT serializeToNBT(final IFactoryController controller, final IRequest<T> request, final IObjectToNBTConverter<T> typeSerialization) {
    final CompoundNBT compound = new CompoundNBT();
    final CompoundNBT requesterCompound = controller.serialize(request.getRequester());
    final CompoundNBT tokenCompound = controller.serialize(request.getId());
    final IntNBT stateCompound = request.getState().serialize();
    final CompoundNBT requestedCompound = typeSerialization.apply(controller, request.getRequest());
    final ListNBT childrenCompound = new ListNBT();
    for (final IToken<?> token : request.getChildren()) {
        childrenCompound.add(controller.serialize(token));
    }
    compound.put(NBT_REQUESTER, requesterCompound);
    compound.put(NBT_TOKEN, tokenCompound);
    compound.put(NBT_STATE, stateCompound);
    compound.put(NBT_REQUESTED, requestedCompound);
    if (request.hasResult()) {
        compound.put(NBT_RESULT, typeSerialization.apply(controller, request.getResult()));
    }
    if (request.hasParent()) {
        compound.put(NBT_PARENT, controller.serialize(request.getParent()));
    }
    compound.put(NBT_CHILDREN, childrenCompound);
    final ListNBT deliveriesList = new ListNBT();
    request.getDeliveries().forEach(itemStack -> deliveriesList.add(itemStack.save(new CompoundNBT())));
    compound.put(NBT_DELIVERIES, deliveriesList);
    return compound;
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) IntNBT(net.minecraft.nbt.IntNBT)

Example 3 with IntNBT

use of net.minecraft.nbt.IntNBT in project minecolonies by Minecolonies.

the class StandardRequestFactories method serializeToNBT.

public static <T extends IRequestable> CompoundNBT serializeToNBT(final IFactoryController controller, final IRequest<T> request, final IObjectToNBTConverter<T> typeSerialization) {
    final CompoundNBT compound = new CompoundNBT();
    final CompoundNBT requesterCompound = controller.serialize(request.getRequester());
    final CompoundNBT tokenCompound = controller.serialize(request.getId());
    final IntNBT stateCompound = request.getState().serialize();
    final CompoundNBT requestedCompound = typeSerialization.apply(controller, request.getRequest());
    final ListNBT childrenCompound = new ListNBT();
    for (final IToken<?> token : request.getChildren()) {
        childrenCompound.add(controller.serialize(token));
    }
    compound.put(NBT_REQUESTER, requesterCompound);
    compound.put(NBT_TOKEN, tokenCompound);
    compound.put(NBT_STATE, stateCompound);
    compound.put(NBT_REQUESTED, requestedCompound);
    if (request.hasResult()) {
        compound.put(NBT_RESULT, typeSerialization.apply(controller, request.getResult()));
    }
    if (request.hasParent()) {
        compound.put(NBT_PARENT, controller.serialize(request.getParent()));
    }
    compound.put(NBT_CHILDREN, childrenCompound);
    final ListNBT deliveriesList = new ListNBT();
    request.getDeliveries().forEach(itemStack -> deliveriesList.add(itemStack.save(new CompoundNBT())));
    compound.put(NBT_DELIVERIES, deliveriesList);
    return compound;
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) IntNBT(net.minecraft.nbt.IntNBT)

Example 4 with IntNBT

use of net.minecraft.nbt.IntNBT in project minecolonies by Minecolonies.

the class StandardRequestFactories method deserializeFromNBT.

public static <T extends IRequestable, R extends IRequest<T>> R deserializeFromNBT(final IFactoryController controller, final CompoundNBT compound, final INBTToObjectConverter<T> typeDeserialization, final IObjectConstructor<T, R> objectConstructor) {
    final IRequester requester = controller.deserialize(compound.getCompound(NBT_REQUESTER));
    final IToken<?> token = controller.deserialize(compound.getCompound(NBT_TOKEN));
    final RequestState state = RequestState.deserialize((IntNBT) compound.get(NBT_STATE));
    final T requested = typeDeserialization.apply(controller, compound.getCompound(NBT_REQUESTED));
    final List<IToken<?>> childTokens = new ArrayList<>();
    final ListNBT childCompound = compound.getList(NBT_CHILDREN, Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < childCompound.size(); i++) {
        childTokens.add(controller.deserialize(childCompound.getCompound(i)));
    }
    @SuppressWarnings(Suppression.LEFT_CURLY_BRACE) final R request = objectConstructor.construct(requested, token, requester, state);
    request.addChildren(childTokens);
    if (compound.getAllKeys().contains(NBT_PARENT)) {
        request.setParent(controller.deserialize(compound.getCompound(NBT_PARENT)));
    }
    if (compound.getAllKeys().contains(NBT_RESULT)) {
        request.setResult(typeDeserialization.apply(controller, compound.getCompound(NBT_RESULT)));
    }
    if (compound.getAllKeys().contains(NBT_DELIVERIES)) {
        final ImmutableList.Builder<ItemStack> stackBuilder = ImmutableList.builder();
        final ListNBT deliveriesList = compound.getList(NBT_DELIVERIES, Constants.NBT.TAG_COMPOUND);
        NBTUtils.streamCompound(deliveriesList).forEach(itemStackCompound -> stackBuilder.add(ItemStack.of(itemStackCompound)));
        request.overrideCurrentDeliveries(stackBuilder.build());
    }
    return request;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) IRequester(com.minecolonies.api.colony.requestsystem.requester.IRequester) RequestState(com.minecolonies.api.colony.requestsystem.request.RequestState) ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) ListNBT(net.minecraft.nbt.ListNBT) IntNBT(net.minecraft.nbt.IntNBT) IToken(com.minecolonies.api.colony.requestsystem.token.IToken) ItemStack(net.minecraft.item.ItemStack)

Example 5 with IntNBT

use of net.minecraft.nbt.IntNBT in project dynmap by webbukkit.

the class ForgeMapChunkCache method getNBTValue.

private Object getNBTValue(INBT v) {
    Object val = null;
    switch(v.getId()) {
        case // Byte
        1:
            val = Byte.valueOf(((ByteNBT) v).getByte());
            break;
        case // Short
        2:
            val = Short.valueOf(((ShortNBT) v).getShort());
            break;
        case // Int
        3:
            val = Integer.valueOf(((IntNBT) v).getInt());
            break;
        case // Long
        4:
            val = Long.valueOf(((LongNBT) v).getLong());
            break;
        case // Float
        5:
            val = Float.valueOf(((FloatNBT) v).getFloat());
            break;
        case // Double
        6:
            val = Double.valueOf(((DoubleNBT) v).getDouble());
            break;
        case // Byte[]
        7:
            val = ((ByteArrayNBT) v).getByteArray();
            break;
        case // String
        8:
            val = ((StringNBT) v).getString();
            break;
        case // List
        9:
            ListNBT tl = (ListNBT) v;
            ArrayList<Object> vlist = new ArrayList<Object>();
            int type = tl.getTagType();
            for (int i = 0; i < tl.size(); i++) {
                switch(type) {
                    case 5:
                        float fv = tl.getFloat(i);
                        vlist.add(fv);
                        break;
                    case 6:
                        double dv = tl.getDouble(i);
                        vlist.add(dv);
                        break;
                    case 8:
                        String sv = tl.getString(i);
                        vlist.add(sv);
                        break;
                    case 10:
                        CompoundNBT tc = tl.getCompound(i);
                        vlist.add(getNBTValue(tc));
                        break;
                    case 11:
                        int[] ia = tl.getIntArray(i);
                        vlist.add(ia);
                        break;
                }
            }
            val = vlist;
            break;
        case // Map
        10:
            CompoundNBT tc = (CompoundNBT) v;
            HashMap<String, Object> vmap = new HashMap<String, Object>();
            for (Object t : tc.keySet()) {
                String st = (String) t;
                INBT tg = tc.get(st);
                vmap.put(st, getNBTValue(tg));
            }
            val = vmap;
            break;
        case // Int[]
        11:
            val = ((IntArrayNBT) v).getIntArray();
            break;
    }
    return val;
}
Also used : IntNBT(net.minecraft.nbt.IntNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) LongNBT(net.minecraft.nbt.LongNBT) DynIntHashMap(org.dynmap.utils.DynIntHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ShortNBT(net.minecraft.nbt.ShortNBT) DoubleNBT(net.minecraft.nbt.DoubleNBT) FloatNBT(net.minecraft.nbt.FloatNBT) ListNBT(net.minecraft.nbt.ListNBT) INBT(net.minecraft.nbt.INBT) ByteNBT(net.minecraft.nbt.ByteNBT)

Aggregations

IntNBT (net.minecraft.nbt.IntNBT)8 CompoundNBT (net.minecraft.nbt.CompoundNBT)6 ListNBT (net.minecraft.nbt.ListNBT)6 ArrayList (java.util.ArrayList)4 ImmutableList (com.google.common.collect.ImmutableList)2 RequestState (com.minecolonies.api.colony.requestsystem.request.RequestState)2 IRequester (com.minecolonies.api.colony.requestsystem.requester.IRequester)2 IToken (com.minecolonies.api.colony.requestsystem.token.IToken)2 HashMap (java.util.HashMap)2 ItemStack (net.minecraft.item.ItemStack)2 ByteNBT (net.minecraft.nbt.ByteNBT)2 DoubleNBT (net.minecraft.nbt.DoubleNBT)2 FloatNBT (net.minecraft.nbt.FloatNBT)2 INBT (net.minecraft.nbt.INBT)2 LongNBT (net.minecraft.nbt.LongNBT)2 ShortNBT (net.minecraft.nbt.ShortNBT)2 DynIntHashMap (org.dynmap.utils.DynIntHashMap)2 DisplayName (org.junit.jupiter.api.DisplayName)1 Test (org.junit.jupiter.api.Test)1