Search in sources :

Example 6 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)

Example 7 with IntNBT

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

the class CCArgumentWrapperPropertyTest method checkSameInt.

// ===================
// Ints
// ===================
private boolean checkSameInt(int value, @Nullable Class<? extends INBT> targetClass) {
    IntNBT nbt = IntNBT.valueOf(value);
    Object sanitized = CCArgumentWrapperTestHelper.wrapAndSanitize(nbt, targetClass, false);
    if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
        return nbt.equals(sanitized);
    } else if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
        return ShortNBT.valueOf((short) value).equals(sanitized);
    }
    return ByteNBT.valueOf((byte) value).equals(sanitized);
}
Also used : IntNBT(net.minecraft.nbt.IntNBT)

Example 8 with IntNBT

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

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 9 with IntNBT

use of net.minecraft.nbt.IntNBT in project Magma-1.16.x by magmafoundation.

the class CraftPersistentDataTypeRegistry method createAdapter.

/**
 * Creates a suitable adapter instance for the primitive class type
 *
 * @param type the type to create an adapter for
 * @param <T> the generic type of that class
 *
 * @return the created adapter instance
 *
 * @throws IllegalArgumentException if no suitable tag type adapter for this
 * type was found
 */
private <T> TagAdapter createAdapter(Class<T> type) {
    if (!Primitives.isWrapperType(type)) {
        // Make sure we will always "switch" over the wrapper types
        type = Primitives.wrap(type);
    }
    /*
            Primitives
         */
    if (Objects.equals(Byte.class, type)) {
        return createAdapter(Byte.class, ByteNBT.class, ByteNBT::valueOf, ByteNBT::getAsByte);
    }
    if (Objects.equals(Short.class, type)) {
        return createAdapter(Short.class, ShortNBT.class, ShortNBT::valueOf, ShortNBT::getAsShort);
    }
    if (Objects.equals(Integer.class, type)) {
        return createAdapter(Integer.class, IntNBT.class, IntNBT::valueOf, IntNBT::getAsInt);
    }
    if (Objects.equals(Long.class, type)) {
        return createAdapter(Long.class, LongNBT.class, LongNBT::valueOf, LongNBT::getAsLong);
    }
    if (Objects.equals(Float.class, type)) {
        return createAdapter(Float.class, FloatNBT.class, FloatNBT::valueOf, FloatNBT::getAsFloat);
    }
    if (Objects.equals(Double.class, type)) {
        return createAdapter(Double.class, DoubleNBT.class, DoubleNBT::valueOf, DoubleNBT::getAsDouble);
    }
    /*
            String
         */
    if (Objects.equals(String.class, type)) {
        return createAdapter(String.class, StringNBT.class, StringNBT::valueOf, StringNBT::getAsString);
    }
    /*
            Primitive Arrays
         */
    if (Objects.equals(byte[].class, type)) {
        return createAdapter(byte[].class, ByteArrayNBT.class, array -> new ByteArrayNBT(Arrays.copyOf(array, array.length)), n -> Arrays.copyOf(n.getAsByteArray(), n.size()));
    }
    if (Objects.equals(int[].class, type)) {
        return createAdapter(int[].class, IntArrayNBT.class, array -> new IntArrayNBT(Arrays.copyOf(array, array.length)), n -> Arrays.copyOf(n.getAsIntArray(), n.size()));
    }
    if (Objects.equals(long[].class, type)) {
        return createAdapter(long[].class, LongArrayNBT.class, array -> new LongArrayNBT(Arrays.copyOf(array, array.length)), n -> Arrays.copyOf(n.getAsLongArray(), n.size()));
    }
    /*
            Complex Arrays
         */
    if (Objects.equals(PersistentDataContainer[].class, type)) {
        return createAdapter(PersistentDataContainer[].class, ListNBT.class, (containerArray) -> {
            ListNBT list = new ListNBT();
            for (int i = 0; i < containerArray.length; i++) {
                list.add(((CraftPersistentDataContainer) containerArray[i]).toTagCompound());
            }
            return list;
        }, (tag) -> {
            PersistentDataContainer[] containerArray = new CraftPersistentDataContainer[tag.size()];
            for (int i = 0; i < tag.size(); i++) {
                CraftPersistentDataContainer container = new CraftPersistentDataContainer(this);
                CompoundNBT compound = tag.getCompound(i);
                for (String key : compound.getAllKeys()) {
                    container.put(key, compound.get(key));
                }
                containerArray[i] = container;
            }
            return containerArray;
        });
    }
    /*
            Note that this will map the interface PersistentMetadataContainer directly to the CraftBukkit implementation
            Passing any other instance of this form to the tag type registry will throw a ClassCastException as defined in TagAdapter#build
         */
    if (Objects.equals(PersistentDataContainer.class, type)) {
        return createAdapter(CraftPersistentDataContainer.class, CompoundNBT.class, CraftPersistentDataContainer::toTagCompound, tag -> {
            CraftPersistentDataContainer container = new CraftPersistentDataContainer(this);
            for (String key : tag.getAllKeys()) {
                container.put(key, tag.get(key));
            }
            return container;
        });
    }
    throw new IllegalArgumentException("Could not find a valid TagAdapter implementation for the requested type " + type.getSimpleName());
}
Also used : IntNBT(net.minecraft.nbt.IntNBT) IntArrayNBT(net.minecraft.nbt.IntArrayNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) LongNBT(net.minecraft.nbt.LongNBT) StringNBT(net.minecraft.nbt.StringNBT) ShortNBT(net.minecraft.nbt.ShortNBT) DoubleNBT(net.minecraft.nbt.DoubleNBT) LongArrayNBT(net.minecraft.nbt.LongArrayNBT) FloatNBT(net.minecraft.nbt.FloatNBT) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer) ListNBT(net.minecraft.nbt.ListNBT) ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT) ByteNBT(net.minecraft.nbt.ByteNBT)

Example 10 with IntNBT

use of net.minecraft.nbt.IntNBT in project LoliServer by Loli-Server.

the class CraftPersistentDataTypeRegistry method createAdapter.

/**
 * Creates a suitable adapter instance for the primitive class type
 *
 * @param type the type to create an adapter for
 * @param <T> the generic type of that class
 *
 * @return the created adapter instance
 *
 * @throws IllegalArgumentException if no suitable tag type adapter for this
 * type was found
 */
private <T> TagAdapter createAdapter(Class<T> type) {
    if (!Primitives.isWrapperType(type)) {
        // Make sure we will always "switch" over the wrapper types
        type = Primitives.wrap(type);
    }
    /*
            Primitives
         */
    if (Objects.equals(Byte.class, type)) {
        return createAdapter(Byte.class, ByteNBT.class, ByteNBT::valueOf, ByteNBT::getAsByte);
    }
    if (Objects.equals(Short.class, type)) {
        return createAdapter(Short.class, ShortNBT.class, ShortNBT::valueOf, ShortNBT::getAsShort);
    }
    if (Objects.equals(Integer.class, type)) {
        return createAdapter(Integer.class, IntNBT.class, IntNBT::valueOf, IntNBT::getAsInt);
    }
    if (Objects.equals(Long.class, type)) {
        return createAdapter(Long.class, LongNBT.class, LongNBT::valueOf, LongNBT::getAsLong);
    }
    if (Objects.equals(Float.class, type)) {
        return createAdapter(Float.class, FloatNBT.class, FloatNBT::valueOf, FloatNBT::getAsFloat);
    }
    if (Objects.equals(Double.class, type)) {
        return createAdapter(Double.class, DoubleNBT.class, DoubleNBT::valueOf, DoubleNBT::getAsDouble);
    }
    /*
            String
         */
    if (Objects.equals(String.class, type)) {
        return createAdapter(String.class, StringNBT.class, StringNBT::valueOf, StringNBT::getAsString);
    }
    /*
            Primitive Arrays
         */
    if (Objects.equals(byte[].class, type)) {
        return createAdapter(byte[].class, ByteArrayNBT.class, array -> new ByteArrayNBT(Arrays.copyOf(array, array.length)), n -> Arrays.copyOf(n.getAsByteArray(), n.size()));
    }
    if (Objects.equals(int[].class, type)) {
        return createAdapter(int[].class, IntArrayNBT.class, array -> new IntArrayNBT(Arrays.copyOf(array, array.length)), n -> Arrays.copyOf(n.getAsIntArray(), n.size()));
    }
    if (Objects.equals(long[].class, type)) {
        return createAdapter(long[].class, LongArrayNBT.class, array -> new LongArrayNBT(Arrays.copyOf(array, array.length)), n -> Arrays.copyOf(n.getAsLongArray(), n.size()));
    }
    /*
            Complex Arrays
         */
    if (Objects.equals(PersistentDataContainer[].class, type)) {
        return createAdapter(PersistentDataContainer[].class, ListNBT.class, (containerArray) -> {
            ListNBT list = new ListNBT();
            for (int i = 0; i < containerArray.length; i++) {
                list.add(((CraftPersistentDataContainer) containerArray[i]).toTagCompound());
            }
            return list;
        }, (tag) -> {
            PersistentDataContainer[] containerArray = new CraftPersistentDataContainer[tag.size()];
            for (int i = 0; i < tag.size(); i++) {
                CraftPersistentDataContainer container = new CraftPersistentDataContainer(this);
                CompoundNBT compound = tag.getCompound(i);
                for (String key : compound.getAllKeys()) {
                    container.put(key, compound.get(key));
                }
                containerArray[i] = container;
            }
            return containerArray;
        });
    }
    /*
            Note that this will map the interface PersistentMetadataContainer directly to the CraftBukkit implementation
            Passing any other instance of this form to the tag type registry will throw a ClassCastException as defined in TagAdapter#build
         */
    if (Objects.equals(PersistentDataContainer.class, type)) {
        return createAdapter(CraftPersistentDataContainer.class, CompoundNBT.class, CraftPersistentDataContainer::toTagCompound, tag -> {
            CraftPersistentDataContainer container = new CraftPersistentDataContainer(this);
            for (String key : tag.getAllKeys()) {
                container.put(key, tag.get(key));
            }
            return container;
        });
    }
    throw new IllegalArgumentException("Could not find a valid TagAdapter implementation for the requested type " + type.getSimpleName());
}
Also used : IntNBT(net.minecraft.nbt.IntNBT) IntArrayNBT(net.minecraft.nbt.IntArrayNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) LongNBT(net.minecraft.nbt.LongNBT) StringNBT(net.minecraft.nbt.StringNBT) ShortNBT(net.minecraft.nbt.ShortNBT) DoubleNBT(net.minecraft.nbt.DoubleNBT) LongArrayNBT(net.minecraft.nbt.LongArrayNBT) FloatNBT(net.minecraft.nbt.FloatNBT) PersistentDataContainer(org.bukkit.persistence.PersistentDataContainer) ListNBT(net.minecraft.nbt.ListNBT) ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT) ByteNBT(net.minecraft.nbt.ByteNBT)

Aggregations

IntNBT (net.minecraft.nbt.IntNBT)10 CompoundNBT (net.minecraft.nbt.CompoundNBT)8 ListNBT (net.minecraft.nbt.ListNBT)8 ArrayList (java.util.ArrayList)4 ByteNBT (net.minecraft.nbt.ByteNBT)4 DoubleNBT (net.minecraft.nbt.DoubleNBT)4 FloatNBT (net.minecraft.nbt.FloatNBT)4 LongNBT (net.minecraft.nbt.LongNBT)4 ShortNBT (net.minecraft.nbt.ShortNBT)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 ByteArrayNBT (net.minecraft.nbt.ByteArrayNBT)2 INBT (net.minecraft.nbt.INBT)2 IntArrayNBT (net.minecraft.nbt.IntArrayNBT)2 LongArrayNBT (net.minecraft.nbt.LongArrayNBT)2 StringNBT (net.minecraft.nbt.StringNBT)2