net.minecraft.nbt.Tag Java Examples

The following examples show how to use net.minecraft.nbt.Tag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 6 votes vote down vote up
private boolean modify_merge(NbtPathArgumentType.NbtPath nbtPath, Tag replacement) //nbtPathArgumentType$NbtPath_1, list_1)
{
    if (!(replacement instanceof CompoundTag))
    {
        return false;
    }
    Tag ownTag = getTag();
    try
    {
        for (Tag target : nbtPath.getOrInit(ownTag, CompoundTag::new))
        {
            if (!(target instanceof CompoundTag))
            {
                continue;
            }
            ((CompoundTag) target).copyFrom((CompoundTag) replacement);
        }
    }
    catch (CommandSyntaxException ignored)
    {
        return false;
    }
    return true;
}
 
Example #2
Source File: FileModule.java    From fabric-carpet with MIT License 6 votes vote down vote up
public static boolean write(Tag tag_1, File file)
{
    File file_2 = new File(file.getAbsolutePath() + "_tmp");
    if (file_2.exists()) file_2.delete();

    try(DataOutputStream dataOutputStream_1 = new DataOutputStream(new FileOutputStream(file_2)))
    {
        dataOutputStream_1.writeByte(tag_1.getType());
        if (tag_1.getType() != 0)
        {
            dataOutputStream_1.writeUTF("");
            tag_1.write(dataOutputStream_1);
        }
    }
    catch (IOException e)
    {
        return false;
    }
    if (file.exists()) file.delete();
    if (!file.exists()) file_2.renameTo(file);
    return true;
}
 
Example #3
Source File: FileModule.java    From fabric-carpet with MIT License 6 votes vote down vote up
public static Tag read(File file)
{
    try (DataInputStream dataInput_1 = new DataInputStream(new FileInputStream(file)))
    {
        byte byte_1 = dataInput_1.readByte();
        if (byte_1 == 0)
        {
            return null;
        }
        else
        {
            dataInput_1.readUTF();
            return TagReaders.of(byte_1).read(dataInput_1, 0, PositionTracker.DEFAULT);
        }
    }
    catch (IOException ignored)
    {
    }
    return null;
}
 
Example #4
Source File: ShapeDispatcher.java    From fabric-carpet with MIT License 6 votes vote down vote up
@Override
public Tag toTag(Value pointsValue)
{
    List<Value> lv = ((ListValue)pointsValue).getItems();
    ListTag ltag = new ListTag();
    for (Value value : lv)
    {
        List<Value> coords = ((ListValue)value).getItems();
        ListTag tag = new ListTag();
        tag.add(DoubleTag.of(NumericValue.asNumber(lv.get(0), "x").getDouble()));
        tag.add(DoubleTag.of(NumericValue.asNumber(lv.get(1), "y").getDouble()));
        tag.add(DoubleTag.of(NumericValue.asNumber(lv.get(2), "z").getDouble()));
        ltag.add(tag);
    }
    return ltag;
}
 
Example #5
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 6 votes vote down vote up
@Override
public Value get(Value value)
{
    NbtPathArgumentType.NbtPath path = cachePath(value.getString());
    try
    {
        List<Tag> tags = path.get(getTag());
        if (tags.size()==0)
            return Value.NULL;
        if (tags.size()==1)
            return NBTSerializableValue.decodeTag(tags.get(0));
        return ListValue.wrap(tags.stream().map(NBTSerializableValue::decodeTag).collect(Collectors.toList()));
    }
    catch (CommandSyntaxException ignored) { }
    return Value.NULL;
}
 
Example #6
Source File: CarpetScriptHost.java    From fabric-carpet with MIT License 6 votes vote down vote up
public boolean writeTagFile(Tag tag, String file, boolean isShared)
{
    if (getName() == null && !isShared) return false; // if belongs to an app, cannot be default host.

    if (file!= null)
    {
        return Module.saveData(main, file, tag, isShared);
    }

    CarpetScriptHost responsibleHost = (parent != null)?(CarpetScriptHost) parent:this;
    responsibleHost.globalState = tag;
    if (responsibleHost.saveTimeout == 0)
    {
        responsibleHost.dumpState();
        responsibleHost.saveTimeout = 200;
    }
    return true;
}
 
Example #7
Source File: MapValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public Tag toTag(boolean force)
{
    CompoundTag tag = new CompoundTag() ;
    map.forEach( (k, v) ->
    {
        if (!force && !(k instanceof StringValue)) throw new NBTSerializableValue.IncompatibleTypeException(k);
        tag.put(k.getString(), v.toTag(force));
    });
    return tag;
}
 
Example #8
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
private static Value decodeTag(Tag t)
{
    if (t instanceof CompoundTag)
        return new NBTSerializableValue(() -> t);
    if (t instanceof AbstractNumberTag)
        return new NumericValue(((AbstractNumberTag) t).getDouble());
    // more can be done here
    return new StringValue(t.asString());
}
 
Example #9
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
public static NBTSerializableValue parseString(String nbtString)
{
    Tag tag;
    try
    {
        tag = (new StringNbtReader(new StringReader(nbtString))).parseTag();
    }
    catch (CommandSyntaxException e)
    {
       throw new InternalExpressionException("Incorrect NBT tag: nbtString");
    }
    NBTSerializableValue value = new NBTSerializableValue(tag);
    value.nbtString = nbtString;
    return value;
}
 
Example #10
Source File: BlockValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public Tag toTag(boolean force)
{
    if (!force) throw new NBTSerializableValue.IncompatibleTypeException(this);
    // follows falling block convertion
    CompoundTag tag =  new CompoundTag();
    CompoundTag state = new CompoundTag();
    BlockState s = getBlockState();
    state.put("Name", StringTag.of(Registry.BLOCK.getId(s.getBlock()).toString()));
    Collection<Property<?>> properties = s.getProperties();
    if (!properties.isEmpty())
    {
        CompoundTag props = new CompoundTag();
        for (Property<?> p: properties)
        {
            props.put(p.getName(), StringTag.of(s.get(p).toString().toLowerCase(Locale.ROOT)));
        }
        state.put("Properties", props);
    }
    tag.put("BlockState", state);
    CompoundTag dataTag = getData();
    if (dataTag != null)
    {
        tag.put("TileEntityData", dataTag);
    }
    return tag;
}
 
Example #11
Source File: ListValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public Tag toTag(boolean force)
{
    int argSize = items.size();
    if (argSize == 0) return new ListTag();
    ListTag tag = new ListTag();
    if (argSize ==1)
    {
        tag.add(items.get(0).toTag(force));
        return tag;
    }
    // figuring out the types
    List<Tag> tags= new ArrayList<>();
    items.forEach(v -> tags.add(v.toTag(force)));
    Set<TagTypeCompat> cases = EnumSet.noneOf(TagTypeCompat.class);
    tags.forEach(t -> cases.add(TagTypeCompat.getType(t)));
    if (cases.size()==1) // well, one type of items
    {
        tag.addAll(tags);
        return tag;
    }
    if (cases.contains(TagTypeCompat.LIST)
            || cases.contains(TagTypeCompat.MAP)
            || cases.contains(TagTypeCompat.STRING)) // incompatible types
    {
        if (!force) throw new NBTSerializableValue.IncompatibleTypeException(this);
        tags.forEach(t -> tag.add(StringTag.of(t.asString())));
        return tag;
    }
    // only numbers / mixed types
    if (cases.contains(TagTypeCompat.DBL))
    {
        tags.forEach(t -> tag.add(DoubleTag.of(((AbstractNumberTag)t).getDouble())));
    }
    else
    {
        tags.forEach(t -> tag.add(LongTag.of(((AbstractNumberTag)t).getLong())));
    }
    return tag;
}
 
Example #12
Source File: ListValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
private static TagTypeCompat getType(Tag tag)
{
    if (tag instanceof IntTag) return INT;
    if (tag instanceof LongTag) return LONG;
    if (tag instanceof DoubleTag) return DBL;
    if (tag instanceof ListTag) return LIST;
    if (tag instanceof CompoundTag) return MAP;
    return STRING;
}
 
Example #13
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public boolean put(Value where, Value value, Value conditions)
{
    /// WIP
    ensureOwnership();
    NbtPathArgumentType.NbtPath path = cachePath(where.getString());
    Tag tagToInsert = value instanceof NBTSerializableValue ?
            ((NBTSerializableValue) value).getTag() :
            new NBTSerializableValue(value.getString()).getTag();
    boolean modifiedTag;
    if (conditions instanceof NumericValue)
    {
        modifiedTag = modify_insert((int)((NumericValue) conditions).getLong(), path, tagToInsert);
    }
    else
    {
        String ops = conditions.getString();
        if (ops.equalsIgnoreCase("merge"))
        {
            modifiedTag = modify_merge(path, tagToInsert);
        }
        else if (ops.equalsIgnoreCase("replace"))
        {
            modifiedTag = modify_replace(path, tagToInsert);
        }
        else
        {
            return false;
        }
    }
    return modifiedTag;
}
 
Example #14
Source File: ShapeDispatcher.java    From fabric-carpet with MIT License 5 votes vote down vote up
public Value decode(Tag tag)
{
    ListTag ltag = (ListTag)tag;
    List<Value> points = new ArrayList<>();
    for (int i=0, ll = ltag.size(); i<ll; i++)
    {
        ListTag ptag = ltag.getList(i);
        points.add(ListValue.of(
                new NumericValue(ptag.getDouble(0)),
                new NumericValue(ptag.getDouble(1)),
                new NumericValue(ptag.getDouble(2))
        ));
    }
    return ListValue.wrap(points);
}
 
Example #15
Source File: ShapeDispatcher.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public Tag toTag(Value value)
{
    List<Value> lv = ((ListValue)value).getItems();
    ListTag tag = new ListTag();
    tag.add(DoubleTag.of(NumericValue.asNumber(lv.get(0), "x").getDouble()));
    tag.add(DoubleTag.of(NumericValue.asNumber(lv.get(1), "y").getDouble()));
    tag.add(DoubleTag.of(NumericValue.asNumber(lv.get(2), "z").getDouble()));
    return tag;
}
 
Example #16
Source File: ShapeDispatcher.java    From fabric-carpet with MIT License 5 votes vote down vote up
public Value decode(Tag tag)
{
    ListTag ctag = (ListTag)tag;
    return ListValue.of(
            new NumericValue(ctag.getDouble(0)),
            new NumericValue(ctag.getDouble(1)),
            new NumericValue(ctag.getDouble(2))
    );
}
 
Example #17
Source File: CarpetScriptHost.java    From fabric-carpet with MIT License 5 votes vote down vote up
public Tag readFileTag(String file, boolean isShared)
{
    if (getName() == null && !isShared) return null;
    if (file != null)
        return Module.getData(main, file, isShared);
    if (parent == null)
        return globalState;
    return ((CarpetScriptHost)parent).globalState;
}
 
Example #18
Source File: ShapeDispatcher.java    From fabric-carpet with MIT License 5 votes vote down vote up
public static CompoundTag toTag(Map<String, Value> params)
{
    CompoundTag tag = new CompoundTag();
    params.forEach((k, v) -> {
        Tag valTag = Param.of.get(k).toTag(v);
        if (valTag != null) tag.put(k, valTag);
    });
    return tag;
}
 
Example #19
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
private static Value decodeTagDeep(Tag t)
{
    if (t instanceof CompoundTag)
    {
        Map<Value, Value> pairs = new HashMap<>();
        CompoundTag ctag = (CompoundTag)t;
        for (String key: ctag.getKeys())
        {
            pairs.put(new StringValue(key), decodeTagDeep(ctag.get(key)));
        }
        return MapValue.wrap(pairs);
    }
    if (t instanceof AbstractListTag)
    {
        List<Value> elems = new ArrayList<>();
        AbstractListTag<? extends Tag> ltag = (AbstractListTag<? extends Tag>)t;
        for (Tag elem: ltag)
        {
            elems.add(decodeTagDeep(elem));
        }
        return ListValue.wrap(elems);
    }
    if (t instanceof AbstractNumberTag)
        return new NumericValue(((AbstractNumberTag) t).getDouble());
    // more can be done here
    return new StringValue(t.asString());
}
 
Example #20
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public boolean getBoolean()
{
    Tag tag = getTag();
    if (tag instanceof CompoundTag)
        return !((CompoundTag) tag).isEmpty();
    if (tag instanceof AbstractListTag)
        return ((AbstractListTag) tag).isEmpty();
    if (tag instanceof AbstractNumberTag)
        return ((AbstractNumberTag) tag).getDouble()!=0.0;
    if (tag instanceof StringTag)
        return tag.asString().isEmpty();
    return true;
}
 
Example #21
Source File: MixinBannerBlockEntity.java    From multiconnect with MIT License 5 votes vote down vote up
@Inject(method = "fromTag", at = @At("RETURN"))
private void onFromTag(BlockState blockState, CompoundTag tag, CallbackInfo ci) {
    if (ConnectionInfo.protocolVersion <= Protocols.V1_12_2) {
        for (Tag t : patternListTag) {
            if (t instanceof CompoundTag) {
                CompoundTag pattern = (CompoundTag) t;
                pattern.putInt("Color", 15 - pattern.getInt("Color"));
            }
        }
    }
}
 
Example #22
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
private boolean modify_insert(int index, NbtPathArgumentType.NbtPath nbtPath, Tag newElement, Tag currentTag)
{
    Collection<Tag> targets;
    try
    {
        targets = nbtPath.getOrInit(currentTag, ListTag::new);
    }
    catch (CommandSyntaxException e)
    {
        return false;
    }

    boolean modified = false;
    for (Tag target : targets)
    {
        if (!(target instanceof AbstractListTag))
        {
            continue;
        }
        try
        {
            AbstractListTag<?> targetList = (AbstractListTag) target;
            if (!targetList.addTag(index < 0 ? targetList.size() + index + 1 : index, newElement.copy()))
                return false;
            modified = true;
        }
        catch (IndexOutOfBoundsException ignored)
        {
        }
    }
    return modified;
}
 
Example #23
Source File: NBTSerializableValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public Tag toTag(boolean force)
{
    if (!force) throw new NBTSerializableValue.IncompatibleTypeException(this);
    ensureOwnership();
    return getTag();
}
 
Example #24
Source File: NumericValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public Tag toTag(boolean force)
{
    long longValue = getLong();
    if (value == (double)longValue)
    {
        if (abs(value) < Integer.MAX_VALUE-2)
            return IntTag.of((int)longValue);
        return LongTag.of(getLong());
    }
    else
    {
        return DoubleTag.of(value);
    }
}
 
Example #25
Source File: EntityValue.java    From fabric-carpet with MIT License 5 votes vote down vote up
@Override
public Tag toTag(boolean force)
{
    if (!force) throw new NBTSerializableValue.IncompatibleTypeException(this);
    CompoundTag tag = new CompoundTag();
    tag.put("Data", getEntity().toTag( new CompoundTag()));
    tag.put("Name", StringTag.of(Registry.ENTITY_TYPE.getId(entity.getType()).toString()));
    return tag;
}
 
Example #26
Source File: Module.java    From fabric-carpet with MIT License 5 votes vote down vote up
public static Tag getData(Module module, String file, boolean isShared)
{
    File dataFile = resolveResource(module, file, "nbt", isShared);
    if (dataFile == null) return null;
    if (!Files.exists(dataFile.toPath()) || !(dataFile.isFile())) return null;
    synchronized (writeIOSync) { return FileModule.read(dataFile); }
}
 
Example #27
Source File: Module.java    From fabric-carpet with MIT License 5 votes vote down vote up
public static boolean saveData(Module module, String file, Tag globalState, boolean isShared)
{
    File dataFile = resolveResource(module, file, "nbt", isShared);
    if (dataFile == null) return false;
    if (!Files.exists(dataFile.toPath().getParent()) && !dataFile.getParentFile().mkdirs()) return false;
    synchronized (writeIOSync) { return FileModule.write(globalState, dataFile); }
}
 
Example #28
Source File: ServerNetworkHandler.java    From fabric-carpet with MIT License 5 votes vote down vote up
public static void broadcastCustomCommand(String command, Tag data)
{
    if (CarpetSettings.superSecretSetting) return;
    for (ServerPlayerEntity player : validCarpetPlayers)
    {
        player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
                CarpetClient.CARPET_CHANNEL,
                DataBuilder.create().withCustomNbt(command, data).build()
        ));
    }
}
 
Example #29
Source File: ServerNetworkHandler.java    From fabric-carpet with MIT License 5 votes vote down vote up
public static void sendCustomCommand(ServerPlayerEntity player, String command, Tag data)
{
    if (isValidCarpetPlayer(player))
    {
        player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
                CarpetClient.CARPET_CHANNEL,
                DataBuilder.create().withCustomNbt(command, data).build()
        ));
    }
}
 
Example #30
Source File: CarpetClient.java    From fabric-carpet with MIT License 5 votes vote down vote up
public static void onClientCommand(Tag t)
{
    CarpetSettings.LOG.info("Server Response:");
    CompoundTag tag = (CompoundTag)t;
    CarpetSettings.LOG.info(" - id: "+tag.getString("id"));
    CarpetSettings.LOG.info(" - code: "+tag.getInt("code"));
    if (tag.contains("error")) CarpetSettings.LOG.warn(" - error: "+tag.getString("error"));
    if (tag.contains("output"))
    {
        ListTag outputTag = (ListTag) tag.get("output");
        for (int i = 0; i < outputTag.size(); i++)
            CarpetSettings.LOG.info(" - response: " + Text.Serializer.fromJson(outputTag.getString(i)).getString());
    }
}