|
| 1 | +/* |
| 2 | + * WorldEdit, a Minecraft world manipulation toolkit |
| 3 | + * Copyright (C) sk89q <http://www.sk89q.com> |
| 4 | + * Copyright (C) WorldEdit team and contributors |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.sk89q.jnbt; |
| 21 | + |
| 22 | +import com.google.common.collect.BiMap; |
| 23 | +import com.google.common.collect.ImmutableBiMap; |
| 24 | +import com.google.common.collect.ImmutableMap; |
| 25 | +import com.sk89q.worldedit.util.nbt.BinaryTag; |
| 26 | +import com.sk89q.worldedit.util.nbt.BinaryTagType; |
| 27 | +import com.sk89q.worldedit.util.nbt.BinaryTagTypes; |
| 28 | + |
| 29 | +import java.lang.reflect.Constructor; |
| 30 | +import java.lang.reflect.InvocationTargetException; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Objects; |
| 33 | +import java.util.function.Function; |
| 34 | + |
| 35 | +/** |
| 36 | + * Converts between JNBT and Adventure-NBT classes. |
| 37 | + * |
| 38 | + * @deprecated JNBT is being removed in WE8. |
| 39 | + */ |
| 40 | +@Deprecated |
| 41 | +public class AdventureNBTConverter { |
| 42 | + private static final BiMap<Class<? extends Tag>, BinaryTagType<?>> TAG_TYPES = |
| 43 | + new ImmutableBiMap.Builder<Class<? extends Tag>, BinaryTagType<?>>() |
| 44 | + .put(ByteArrayTag.class, BinaryTagTypes.BYTE_ARRAY) |
| 45 | + .put(ByteTag.class, BinaryTagTypes.BYTE) |
| 46 | + .put(CompoundTag.class, BinaryTagTypes.COMPOUND) |
| 47 | + .put(DoubleTag.class, BinaryTagTypes.DOUBLE) |
| 48 | + .put(EndTag.class, BinaryTagTypes.END) |
| 49 | + .put(FloatTag.class, BinaryTagTypes.FLOAT) |
| 50 | + .put(IntArrayTag.class, BinaryTagTypes.INT_ARRAY) |
| 51 | + .put(IntTag.class, BinaryTagTypes.INT) |
| 52 | + .put(ListTag.class, BinaryTagTypes.LIST) |
| 53 | + .put(LongArrayTag.class, BinaryTagTypes.LONG_ARRAY) |
| 54 | + .put(LongTag.class, BinaryTagTypes.LONG) |
| 55 | + .put(ShortTag.class, BinaryTagTypes.SHORT) |
| 56 | + .put(StringTag.class, BinaryTagTypes.STRING) |
| 57 | + .build(); |
| 58 | + |
| 59 | + private static final Map<BinaryTagType<?>, Function<BinaryTag, Tag>> CONVERSION; |
| 60 | + |
| 61 | + static { |
| 62 | + ImmutableMap.Builder<BinaryTagType<?>, Function<BinaryTag, Tag>> conversion = |
| 63 | + ImmutableMap.builder(); |
| 64 | + |
| 65 | + for (Map.Entry<Class<? extends Tag>, BinaryTagType<?>> tag : TAG_TYPES.entrySet()) { |
| 66 | + Constructor<?>[] constructors = tag.getKey().getConstructors(); |
| 67 | + for (Constructor<?> c : constructors) { |
| 68 | + if (c.getParameterCount() == 1 && BinaryTag.class.isAssignableFrom(c.getParameterTypes()[0])) { |
| 69 | + conversion.put(tag.getValue(), binaryTag -> { |
| 70 | + try { |
| 71 | + return (Tag) c.newInstance(binaryTag); |
| 72 | + } catch (InstantiationException | IllegalAccessException e) { |
| 73 | + throw new IllegalStateException(e); |
| 74 | + } catch (InvocationTargetException e) { |
| 75 | + // I assume this is always a RuntimeException since we control the ctor |
| 76 | + throw (RuntimeException) e.getCause(); |
| 77 | + } |
| 78 | + }); |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + CONVERSION = conversion.build(); |
| 85 | + } |
| 86 | + |
| 87 | + public static BinaryTagType<?> getAdventureType(Class<? extends Tag> type) { |
| 88 | + return Objects.requireNonNull(TAG_TYPES.get(type), () -> "Missing entry for " + type); |
| 89 | + } |
| 90 | + |
| 91 | + public static Class<? extends Tag> getJNBTType(BinaryTagType<?> type) { |
| 92 | + return Objects.requireNonNull(TAG_TYPES.inverse().get(type), () -> "Missing entry for " + type); |
| 93 | + } |
| 94 | + |
| 95 | + private AdventureNBTConverter() { |
| 96 | + } |
| 97 | + |
| 98 | + public static Tag fromAdventure(BinaryTag other) { |
| 99 | + Function<BinaryTag, Tag> conversion = CONVERSION.get(other.type()); |
| 100 | + if (conversion == null) { |
| 101 | + throw new IllegalArgumentException("Can't convert other of type " + other.getClass().getCanonicalName()); |
| 102 | + } |
| 103 | + return conversion.apply(other); |
| 104 | + } |
| 105 | +} |
0 commit comments