com.mojang.datafixers.util.Pair Java Examples

The following examples show how to use com.mojang.datafixers.util.Pair. 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: AutoEatHack.java    From Wurst7 with GNU General Public License v3.0 6 votes vote down vote up
private boolean isAllowedFood(FoodComponent food)
{
	if(!allowChorus.isChecked() && food == FoodComponents.CHORUS_FRUIT)
		return false;
	
	for(Pair<StatusEffectInstance, Float> pair : food.getStatusEffects())
	{
		StatusEffect effect = pair.getFirst().getEffectType();
		
		if(!allowHunger.isChecked() && effect == StatusEffects.HUNGER)
			return false;
		
		if(!allowPoison.isChecked() && effect == StatusEffects.POISON)
			return false;
	}
	
	return true;
}
 
Example #2
Source File: KeyDispatchCodec.java    From DataFixerUpper with MIT License 6 votes vote down vote up
@Override
public <T> DataResult<V> decode(final DynamicOps<T> ops, final MapLike<T> input) {
    final T elementName = input.get(typeKey);
    if (elementName == null) {
        return DataResult.error("Input does not contain a key [" + typeKey + "]: " + input);
    }

    return keyCodec.decode(ops, elementName).flatMap(type -> {
        final DataResult<? extends Decoder<? extends V>> elementDecoder = decoder.apply(type.getFirst());
        return elementDecoder.flatMap(c -> {
            if (ops.compressMaps()) {
                final T value = input.get(ops.createString(valueKey));
                if (value == null) {
                    return DataResult.error("Input does not have a \"value\" entry: " + input);
                }
                return c.parse(ops, value).map(Function.identity());
            }
            if (c instanceof MapCodecCodec<?>) {
                return ((MapCodecCodec<? extends V>) c).codec().decode(ops, input).map(Function.identity());
            }
            return c.decode(ops, ops.createMap(input.entries())).map(Pair::getFirst);
        });
    });
}
 
Example #3
Source File: ListCodec.java    From DataFixerUpper with MIT License 6 votes vote down vote up
@Override
public <T> DataResult<Pair<List<A>, T>> decode(final DynamicOps<T> ops, final T input) {
    return ops.getList(input).setLifecycle(Lifecycle.stable()).flatMap(stream -> {
        final ImmutableList.Builder<A> read = ImmutableList.builder();
        final Stream.Builder<T> failed = Stream.builder();
        // TODO: AtomicReference.getPlain/setPlain in java9+
        final MutableObject<DataResult<Unit>> result = new MutableObject<>(DataResult.success(Unit.INSTANCE, Lifecycle.stable()));

        stream.accept(t -> {
            final DataResult<Pair<A, T>> element = elementCodec.decode(ops, t);
            element.error().ifPresent(e -> failed.add(t));
            result.setValue(result.getValue().apply2stable((r, v) -> {
                read.add(v.getFirst());
                return r;
            }, element));
        });

        final ImmutableList<A> elements = read.build();
        final T errors = ops.createList(failed.build());

        final Pair<List<A>, T> pair = Pair.of(elements, errors);

        return result.getValue().map(unit -> pair).setPartial(pair);
    });
}
 
Example #4
Source File: BaseMapCodec.java    From DataFixerUpper with MIT License 6 votes vote down vote up
default <T> DataResult<Map<K, V>> decode(final DynamicOps<T> ops, final MapLike<T> input) {
    final ImmutableMap.Builder<K, V> read = ImmutableMap.builder();
    final ImmutableList.Builder<Pair<T, T>> failed = ImmutableList.builder();

    final DataResult<Unit> result = input.entries().reduce(
        DataResult.success(Unit.INSTANCE, Lifecycle.stable()),
        (r, pair) -> {
            final DataResult<K> k = keyCodec().parse(ops, pair.getFirst());
            final DataResult<V> v = elementCodec().parse(ops, pair.getSecond());

            final DataResult<Pair<K, V>> entry = k.apply2stable(Pair::of, v);
            entry.error().ifPresent(e -> failed.add(pair));

            return r.apply2stable((u, p) -> {
                read.put(p.getFirst(), p.getSecond());
                return u;
            }, entry);
        },
        (r1, r2) -> r1.apply2stable((u1, u2) -> u1, r2)
    );

    final Map<K, V> elements = read.build();
    final T errors = ops.createMap(failed.build().stream());

    return result.map(unit -> elements).setPartial(elements).mapError(e -> e + " missed input: " + errors);
}
 
Example #5
Source File: VBORenderType.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Can be called runtime to have the Buffer rebuilt,
 * doing so has very limited applications and is not recommended.
 */
public void rebuild() {
    if (bufferId == -1) {
        bufferId = GL15.glGenBuffers();
    }

    BufferBuilder builder = new BufferBuilder(getBufferSize());
    builder.begin(getDrawMode(), getVertexFormat());
    factory.accept(getVertexFormat(), builder);
    builder.finishDrawing();
    Pair<BufferBuilder.DrawState, ByteBuffer> pair = builder.getNextBuffer();
    ByteBuffer buffer = pair.getSecond();
    count = buffer.remaining() / getVertexFormat().getSize();

    GL15.glBindBuffer(GL_ARRAY_BUFFER, bufferId);
    GL15.glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
    GL15.glBindBuffer(GL_ARRAY_BUFFER, 0);
}
 
Example #6
Source File: DataFix.java    From DataFixerUpper with MIT License 6 votes vote down vote up
protected <A, B> TypeRewriteRule writeFixAndRead(final String name, final Type<A> type, final Type<B> newType, final Function<Dynamic<?>, Dynamic<?>> fix) {
    return fixTypeEverywhere(name, type, newType, ops -> input -> {
        final Optional<? extends Dynamic<?>> written = type.writeDynamic(ops, input).resultOrPartial(LOGGER::error);
        if (!written.isPresent()) {
            throw new RuntimeException("Could not write the object in " + name);
        }
        final Optional<? extends Pair<Typed<B>, ?>> read = newType.readTyped(fix.apply(written.get())).resultOrPartial(LOGGER::error);
        if (!read.isPresent()) {
            throw new RuntimeException("Could not read the new object in " + name);
        }
        return read.get().getFirst().getValue();
    });
}
 
Example #7
Source File: Product.java    From DataFixerUpper with MIT License 6 votes vote down vote up
private <A, B, LS, RS, LT, RT> OpticParts<A, B> cap(final FamilyOptic<A, B> lo, final FamilyOptic<A, B> ro, final int index) {
    final TypeToken<TraversalP.Mu> bound = TraversalP.Mu.TYPE_TOKEN;

    final OpticParts<A, B> lp = lo.apply(index);
    final OpticParts<A, B> rp = ro.apply(index);

    final Optic<? super TraversalP.Mu, ?, ?, A, B> l = lp.optic().upCast(lp.bounds(), bound).orElseThrow(IllegalArgumentException::new);
    final Optic<? super TraversalP.Mu, ?, ?, A, B> r = rp.optic().upCast(rp.bounds(), bound).orElseThrow(IllegalArgumentException::new);

    final Traversal<LS, LT, A, B> lt = Optics.toTraversal((Optic<? super TraversalP.Mu, LS, LT, A, B>) l);
    final Traversal<RS, RT, A, B> rt = Optics.toTraversal((Optic<? super TraversalP.Mu, RS, RT, A, B>) r);

    return new OpticParts<>(
        ImmutableSet.of(bound),
        new Traversal<Pair<LS, RS>, Pair<LT, RT>, A, B>() {
            @Override
            public <F extends K1> FunctionType<Pair<LS, RS>, App<F, Pair<LT, RT>>> wander(final Applicative<F, ?> applicative, final FunctionType<A, App<F, B>> input) {
                return p -> applicative.ap2(applicative.point(Pair::of),
                    lt.wander(applicative, input).apply(p.getFirst()),
                    rt.wander(applicative, input).apply(p.getSecond())
                );
            }
        }
    );
}
 
Example #8
Source File: DynamicOps.java    From DataFixerUpper with MIT License 5 votes vote down vote up
default DataResult<MapLike<T>> getMap(final T input) {
    return getMapValues(input).flatMap(s -> {
        try {
            return DataResult.success(MapLike.forMap(s.collect(Pair.toMap()), this));
        } catch (final IllegalStateException e) {
            return DataResult.error("Error while building map: " + e.getMessage());
        }
    });
}
 
Example #9
Source File: TaggedChoice.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public Optional<RewriteResult<Pair<K, ?>, ?>> one(final TypeRewriteRule rule) {
    for (final Map.Entry<K, Type<?>> entry : types.entrySet()) {
        final Optional<? extends RewriteResult<?, ?>> elementResult = rule.rewrite(entry.getValue());
        if (elementResult.isPresent()) {
            return Optional.of(elementResult(entry.getKey(), this, elementResult.get()));
        }
    }
    return Optional.empty();
}
 
Example #10
Source File: Affine.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C> App2<Affine.Mu<A2, B2>, Pair<C, A>, Pair<C, B>> second(final App2<Affine.Mu<A2, B2>, A, B> input) {
    final Affine<A, B, A2, B2> affine = Affine.unbox(input);
    return Optics.affine(
        pair -> affine.preview(pair.getSecond()).mapBoth(b -> Pair.of(pair.getFirst(), b), Function.identity()),
        (b2, pair) -> Pair.of(pair.getFirst(), affine.set(b2, pair.getSecond()))
    );
}
 
Example #11
Source File: TypedOptic.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public static <F, G, F2> TypedOptic<Pair<F, G>, Pair<F2, G>, F, F2> proj1(final Type<F> fType, final Type<G> gType, final Type<F2> newType) {
    return new TypedOptic<>(
        Cartesian.Mu.TYPE_TOKEN,
        DSL.and(fType, gType),
        DSL.and(newType, gType),
        fType,
        newType,
        new Proj1<>()
    );
}
 
Example #12
Source File: TypedOptic.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public static <K, V, K2> TypedOptic<List<Pair<K, V>>, List<Pair<K2, V>>, K, K2> compoundListKeys(final Type<K> aType, final Type<K2> bType, final Type<V> valueType) {
    return new TypedOptic<>(
        TraversalP.Mu.TYPE_TOKEN,
        DSL.compoundList(aType, valueType),
        DSL.compoundList(bType, valueType),
        aType,
        bType,
        new ListTraversal<Pair<K, V>, Pair<K2, V>>().compose(Optics.proj1())
    );
}
 
Example #13
Source File: Decoder.java    From DataFixerUpper with MIT License 5 votes vote down vote up
default Decoder<A> decoder() {
    return new Decoder<A>() {
        @Override
        public <T> DataResult<Pair<A, T>> decode(final DynamicOps<T> ops, final T input) {
            return Simple.this.decode(new Dynamic<>(ops, input)).map(a -> Pair.of(a, ops.empty()));
        }

        @Override
        public String toString() {
            return "SimpleDecoder[" + Simple.this + "]";
        }
    };
}
 
Example #14
Source File: EitherCodec.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <T> DataResult<Pair<Either<F, S>, T>> decode(final DynamicOps<T> ops, final T input) {
    final DataResult<Pair<Either<F, S>, T>> firstRead = first.decode(ops, input).map(vo -> vo.mapFirst(Either::left));
    if (firstRead.result().isPresent()) {
        return firstRead;
    }
    return second.decode(ops, input).map(vo -> vo.mapFirst(Either::right));
}
 
Example #15
Source File: CompoundListCodec.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <T> DataResult<Pair<List<Pair<K, V>>, T>> decode(final DynamicOps<T> ops, final T input) {
    return ops.getMapEntries(input).flatMap(map -> {
        final ImmutableList.Builder<Pair<K, V>> read = ImmutableList.builder();
        final ImmutableMap.Builder<T, T> failed = ImmutableMap.builder();

        // TODO: AtomicReference.getPlain/setPlain in java9+
        final MutableObject<DataResult<Unit>> result = new MutableObject<>(DataResult.success(Unit.INSTANCE, Lifecycle.experimental()));

        map.accept((key, value) -> {
            final DataResult<K> k = keyCodec.parse(ops, key);
            final DataResult<V> v = elementCodec.parse(ops, value);

            final DataResult<Pair<K, V>> readEntry = k.apply2stable(Pair::new, v);

            readEntry.error().ifPresent(e -> failed.put(key, value));

            result.setValue(result.getValue().apply2stable((u, e) -> {
                read.add(e);
                return u;
            }, readEntry));
        });

        final ImmutableList<Pair<K, V>> elements = read.build();
        final T errors = ops.createMap(failed.build());

        final Pair<List<Pair<K, V>>, T> pair = Pair.of(elements, errors);

        return result.getValue().map(unit -> pair).setPartial(pair);
    });
}
 
Example #16
Source File: TaggedChoice.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
protected Codec<Pair<K, ?>> buildCodec() {
    return new KeyDispatchCodec<K, Pair<K, ?>>(
        name,
        keyType.codec(),
        p -> DataResult.success(p.getFirst()),
        k -> getCodec(k).map(c -> c.map(v -> Pair.of(k, v))),
        this::encoder
    ).codec();
}
 
Example #17
Source File: Lens.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C> App2<Lens.Mu<A2, B2>, Pair<A, C>, Pair<B, C>> first(final App2<Lens.Mu<A2, B2>, A, B> input) {
    return Optics.lens(
        pair -> Lens.unbox(input).view(pair.getFirst()),
        (b2, pair) -> Pair.of(Lens.unbox(input).update(b2, pair.getFirst()), pair.getSecond())
    );
}
 
Example #18
Source File: Decoder.java    From DataFixerUpper with MIT License 5 votes vote down vote up
default Decoder<A> promotePartial(final Consumer<String> onError) {
    return new Decoder<A>() {
        @Override
        public <T> DataResult<Pair<A, T>> decode(final DynamicOps<T> ops, final T input) {
            return Decoder.this.decode(ops, input).promotePartial(onError);
        }

        @Override
        public String toString() {
            return Decoder.this.toString() + "[promotePartial]";
        }
    };
}
 
Example #19
Source File: CompoundList.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public Optional<RewriteResult<List<Pair<K, V>>, ?>> one(final TypeRewriteRule rule) {
    return DataFixUtils.or(
        rule.rewrite(key).map(v -> fixKeys(this, key, element, v)),
        () -> rule.rewrite(element).map(v -> fixValues(this, key, element, v))
    );
}
 
Example #20
Source File: Decoder.java    From DataFixerUpper with MIT License 5 votes vote down vote up
default Decoder<A> withLifecycle(final Lifecycle lifecycle) {
    return new Decoder<A>() {
        @Override
        public <T> DataResult<Pair<A, T>> decode(final DynamicOps<T> ops, final T input) {
            return Decoder.this.decode(ops, input).setLifecycle(lifecycle);
        }

        @Override
        public String toString() {
            return Decoder.this.toString();
        }
    };
}
 
Example #21
Source File: ReForgetEP.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C, D> FunctionType<App2<ReForgetEP.Mu<R>, A, B>, App2<ReForgetEP.Mu<R>, C, D>> dimap(final Function<C, A> g, final Function<B, D> h) {
    return input -> Optics.reForgetEP("dimap", e -> {
        final Either<A, Pair<A, R>> either = e.mapBoth(g, p -> Pair.of(g.apply(p.getFirst()), p.getSecond()));
        final B b = ReForgetEP.unbox(input).run(either);
        final D d = h.apply(b);
        return d;
    });
}
 
Example #22
Source File: Decoder.java    From DataFixerUpper with MIT License 5 votes vote down vote up
default Decoder<A> decoder() {
    return new Decoder<A>() {
        @Override
        public <T> DataResult<Pair<A, T>> decode(final DynamicOps<T> ops, final T input) {
            return Boxed.this.decode(new Dynamic<>(ops, input));
        }

        @Override
        public String toString() {
            return "BoxedDecoder[" + Boxed.this + "]";
        }
    };
}
 
Example #23
Source File: Lens.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C> App2<Lens.Mu<A2, B2>, Pair<C, A>, Pair<C, B>> second(final App2<Lens.Mu<A2, B2>, A, B> input) {
    return Optics.lens(
        pair -> Lens.unbox(input).view(pair.getSecond()),
        (b2, pair) -> Pair.of(pair.getFirst(), Lens.unbox(input).update(b2, pair.getSecond()))
    );
}
 
Example #24
Source File: MapDecoder.java    From DataFixerUpper with MIT License 5 votes vote down vote up
default Decoder<A> decoder() {
    return new Decoder<A>() {
        @Override
        public <T> DataResult<Pair<A, T>> decode(final DynamicOps<T> ops, final T input) {
            return compressedDecode(ops, input).map(r -> Pair.of(r, input));
        }

        @Override
        public String toString() {
            return MapDecoder.this.toString();
        }
    };
}
 
Example #25
Source File: FieldFinder.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <V> TypedOptic<Pair<FT, V>, ?, FT, FT> capChoice(final Type<?> choiceType) {
    return new TypedOptic<>(
        Cartesian.Mu.TYPE_TOKEN,
        (Type<Pair<FT, V>>) choiceType,
        (Type<Pair<FT, V>>) choiceType,
        type,
        type,
        new Proj1<>()
    );
}
 
Example #26
Source File: TypedOptic.java    From DataFixerUpper with MIT License 5 votes vote down vote up
public static <F, G, G2> TypedOptic<Pair<F, G>, Pair<F, G2>, G, G2> proj2(final Type<F> fType, final Type<G> gType, final Type<G2> newType) {
    return new TypedOptic<>(
        Cartesian.Mu.TYPE_TOKEN,
        DSL.and(fType, gType),
        DSL.and(fType, newType),
        gType,
        newType,
        new Proj2<>()
    );
}
 
Example #27
Source File: ReForgetEP.java    From DataFixerUpper with MIT License 5 votes vote down vote up
@Override
public <A, B, C> App2<ReForgetEP.Mu<R>, Either<C, A>, Either<C, B>> right(final App2<ReForgetEP.Mu<R>, A, B> input) {
    final ReForgetEP<R, A, B> reForgetEP = ReForgetEP.unbox(input);
    return Optics.reForgetEP("right",
        e -> e.map(
            e2 -> e2.mapRight(
                a -> reForgetEP.run(Either.left(a))
            ),
            (Pair<Either<C, A>, R> p) -> p.getFirst().mapRight(
                a -> reForgetEP.run(Either.right(Pair.of(a, p.getSecond())))
            )
        )
    );
}
 
Example #28
Source File: Typed.java    From DataFixerUpper with MIT License 4 votes vote down vote up
public static <A, B> Typed<Pair<A, B>> pair(final Typed<A> first, final Typed<B> second) {
    return new Typed<>(DSL.and(first.type, second.type), first.ops, Pair.of(first.value, second.value));
}
 
Example #29
Source File: Codec.java    From DataFixerUpper with MIT License 4 votes vote down vote up
static <K, V> Codec<List<Pair<K, V>>> compoundList(final Codec<K> keyCodec, final Codec<V> elementCodec) {
    return new CompoundListCodec<>(keyCodec, elementCodec);
}
 
Example #30
Source File: Proj1.java    From DataFixerUpper with MIT License 4 votes vote down vote up
@Override
public F view(final Pair<F, G> pair) {
    return pair.getFirst();
}