Skip to content

Commit

Permalink
Rename map to xmap in Loadables
Browse files Browse the repository at this point in the history
This is done to prevent name conflict with StreamCodable#map in the future, along with to distinguish it from data structure maps better
  • Loading branch information
KnightMiner committed Mar 28, 2024
1 parent 24eb72c commit a22da91
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions src/main/java/slimeknights/mantle/data/loadable/Loadable.java
Expand Up @@ -163,22 +163,22 @@ default <V> Loadable<Map<T,V>> mapWithValues(int minSize, Function<T,V> valueGe
/* Mapping */

/** Maps this loader to another type, with error factory on both from and to */
default <M> Loadable<M> map(BiFunction<T,ErrorFactory,M> from, BiFunction<M,ErrorFactory,T> to) {
default <M> Loadable<M> xmap(BiFunction<T,ErrorFactory,M> from, BiFunction<M,ErrorFactory,T> to) {
return MappedLoadable.of(this, from, to);
}

/** Maps this loader to another type, with error factory on from */
default <M> Loadable<M> comapFlatMap(BiFunction<T,ErrorFactory,M> from, Function<M,T> to) {
return map(from, MappedLoadable.flatten(to));
return xmap(from, MappedLoadable.flatten(to));
}

/** Maps this loader to another type */
default <M> Loadable<M> flatComap(Function<T,M> from, BiFunction<M,ErrorFactory,T> to) {
return map(MappedLoadable.flatten(from), to);
return xmap(MappedLoadable.flatten(from), to);
}

/** Maps this loader to another type */
default <M> Loadable<M> flatMap(Function<T,M> from, Function<M,T> to) {
return map(MappedLoadable.flatten(from), MappedLoadable.flatten(to));
default <M> Loadable<M> flatXmap(Function<T,M> from, Function<M,T> to) {
return xmap(MappedLoadable.flatten(from), MappedLoadable.flatten(to));
}
}
Expand Up @@ -31,14 +31,14 @@ public class Loadables {
private Loadables() {}

/** Loadable for a resource location */
public static final StringLoadable<ResourceLocation> RESOURCE_LOCATION = StringLoadable.DEFAULT.map((s, e) -> {
public static final StringLoadable<ResourceLocation> RESOURCE_LOCATION = StringLoadable.DEFAULT.xmap((s, e) -> {
try {
return new ResourceLocation(s);
} catch (ResourceLocationException ex) {
throw e.create(ex);
}
}, (r, e) -> r.toString());
public static final StringLoadable<ToolAction> TOOL_ACTION = StringLoadable.DEFAULT.flatMap(ToolAction::get, ToolAction::name);
public static final StringLoadable<ToolAction> TOOL_ACTION = StringLoadable.DEFAULT.flatXmap(ToolAction::get, ToolAction::name);

/* Registries */
public static final StringLoadable<SoundEvent> SOUND_EVENT = new RegistryLoadable<>(Registry.SOUND_EVENT);
Expand Down Expand Up @@ -73,7 +73,7 @@ private Loadables() {}

/** Creates a tag key loadable */
public static <T> StringLoadable<TagKey<T>> tagKey(ResourceKey<? extends Registry<T>> registry) {
return RESOURCE_LOCATION.flatMap(key -> TagKey.create(registry, key), TagKey::location);
return RESOURCE_LOCATION.flatXmap(key -> TagKey.create(registry, key), TagKey::location);
}

/** Maps a loadable to a variant that disallows a particular value */
Expand All @@ -84,6 +84,6 @@ public static <T> StringLoadable<T> notValue(StringLoadable<T> loadable, T notVa
}
return value;
};
return loadable.map(mapper, mapper);
return loadable.xmap(mapper, mapper);
}
}
Expand Up @@ -81,7 +81,7 @@ public static Loadable<FluidStack> fixedSize(int amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Count must be positive, received " + amount);
}
return Loadables.FLUID.flatMap(fluid -> makeStack(fluid, amount, null), FLUID_GETTER);
return Loadables.FLUID.flatXmap(fluid -> makeStack(fluid, amount, null), FLUID_GETTER);
}

/** Creates a loadable for a stack with a single item */
Expand All @@ -95,11 +95,11 @@ public static RecordLoadable<FluidStack> fixedSizeNBT(int amount) {

/** Creates a non-empty variant of the loadable */
public static Loadable<FluidStack> notEmpty(Loadable<FluidStack> loadable) {
return loadable.map(NOT_EMPTY, NOT_EMPTY);
return loadable.xmap(NOT_EMPTY, NOT_EMPTY);
}

/** Creates a non-empty variant of the loadable */
public static RecordLoadable<FluidStack> notEmpty(RecordLoadable<FluidStack> loadable) {
return loadable.map(NOT_EMPTY, NOT_EMPTY);
return loadable.xmap(NOT_EMPTY, NOT_EMPTY);
}
}
Expand Up @@ -47,7 +47,7 @@ private ItemStackLoadable() {}

/* Optional */
/** Single item which may be empty with a count of 1 */
public static final Loadable<ItemStack> OPTIONAL_ITEM = Loadables.ITEM.flatMap(item -> makeStack(item, 1, null), ITEM_GETTER);
public static final Loadable<ItemStack> OPTIONAL_ITEM = Loadables.ITEM.flatXmap(item -> makeStack(item, 1, null), ITEM_GETTER);
/** Loadable for a stack that may be empty with variable count */
public static final RecordLoadable<ItemStack> OPTIONAL_STACK = RecordLoadable.create(ITEM, COUNT, (item, count) -> makeStack(item, count, null))
.compact(OPTIONAL_ITEM, stack -> stack.getCount() == 1);
Expand Down Expand Up @@ -83,12 +83,12 @@ private static ItemStack makeStack(Item item, int count, @Nullable CompoundTag n

/** Creates a non-empty variant of the loadable */
public static Loadable<ItemStack> notEmpty(Loadable<ItemStack> loadable) {
return loadable.map(NOT_EMPTY, NOT_EMPTY);
return loadable.xmap(NOT_EMPTY, NOT_EMPTY);
}

/** Creates a non-empty variant of the loadable */
public static RecordLoadable<ItemStack> notEmpty(RecordLoadable<ItemStack> loadable) {
return loadable.map(NOT_EMPTY, NOT_EMPTY);
return loadable.xmap(NOT_EMPTY, NOT_EMPTY);
}

/** Loadable for an item stack with NBT, requires special logic due to forges share tags */
Expand Down
Expand Up @@ -41,11 +41,11 @@ protected Collection<T> build(Builder<T> builder) {

/** Creates a map from this collection using the given getter to find keys for the map */
public <K> Loadable<Map<K,T>> mapWithKeys(Function<T,K> keyGetter) {
return flatMap(collection -> collection.stream().collect(Collectors.toUnmodifiableMap(keyGetter, Function.identity())), Map::values);
return flatXmap(collection -> collection.stream().collect(Collectors.toUnmodifiableMap(keyGetter, Function.identity())), Map::values);
}

/** Creates a map from this collection using the given getter to find values for the map */
public <V> Loadable<Map<T,V>> mapWithValues(Function<T,V> valueGetter) {
return flatMap(collection -> collection.stream().collect(Collectors.toUnmodifiableMap(Function.identity(), valueGetter)), Map::keySet);
return flatXmap(collection -> collection.stream().collect(Collectors.toUnmodifiableMap(Function.identity(), valueGetter)), Map::keySet);
}
}
Expand Up @@ -77,22 +77,22 @@ default <V> Loadable<Map<T,V>> mapWithValues(Loadable<V> valueLoadable) {
}

@Override
default <M> StringLoadable<M> map(BiFunction<T,ErrorFactory,M> from, BiFunction<M,ErrorFactory,T> to) {
default <M> StringLoadable<M> xmap(BiFunction<T,ErrorFactory,M> from, BiFunction<M,ErrorFactory,T> to) {
return MappedLoadable.of(this, from, to);
}

@Override
default <M> StringLoadable<M> comapFlatMap(BiFunction<T,ErrorFactory,M> from, Function<M,T> to) {
return map(from, MappedLoadable.flatten(to));
return xmap(from, MappedLoadable.flatten(to));
}

@Override
default <M> StringLoadable<M> flatComap(Function<T,M> from, BiFunction<M,ErrorFactory,T> to) {
return map(MappedLoadable.flatten(from), to);
return xmap(MappedLoadable.flatten(from), to);
}

@Override
default <M> StringLoadable<M> flatMap(Function<T,M> from, Function<M,T> to) {
return map(MappedLoadable.flatten(from), MappedLoadable.flatten(to));
default <M> StringLoadable<M> flatXmap(Function<T,M> from, Function<M,T> to) {
return xmap(MappedLoadable.flatten(from), MappedLoadable.flatten(to));
}
}
Expand Up @@ -109,23 +109,23 @@ default RecordLoadable<T> compact(Loadable<T> compact, Predicate<T> condition) {
/* Mapping - switches to the record version of the methods */

@Override
default <M> RecordLoadable<M> map(BiFunction<T,ErrorFactory,M> from, BiFunction<M,ErrorFactory,T> to) {
default <M> RecordLoadable<M> xmap(BiFunction<T,ErrorFactory,M> from, BiFunction<M,ErrorFactory,T> to) {
return MappedLoadable.of(this, from, to);
}

@Override
default <M> RecordLoadable<M> comapFlatMap(BiFunction<T,ErrorFactory,M> from, Function<M,T> to) {
return map(from, MappedLoadable.flatten(to));
return xmap(from, MappedLoadable.flatten(to));
}

@Override
default <M> RecordLoadable<M> flatComap(Function<T,M> from, BiFunction<M,ErrorFactory,T> to) {
return map(MappedLoadable.flatten(from), to);
return xmap(MappedLoadable.flatten(from), to);
}

@Override
default <M> RecordLoadable<M> flatMap(Function<T,M> from, Function<M,T> to) {
return map(MappedLoadable.flatten(from), MappedLoadable.flatten(to));
default <M> RecordLoadable<M> flatXmap(Function<T,M> from, Function<M,T> to) {
return xmap(MappedLoadable.flatten(from), MappedLoadable.flatten(to));
}


Expand Down
Expand Up @@ -38,7 +38,7 @@ private static EitherLoadable.TypedBuilder<EntityIngredient> loadableBuilder() {
/** Loadable for a tag match */
private static final RecordLoadable<TagMatch> TAG_MATCH = RecordLoadable.create(Loadables.ENTITY_TYPE_TAG.requiredField("tag", t -> t.tag), TagMatch::new);
/** Loadable disallows nested lists, just handles nested tags and sets */
private static final Loadable<Compound> COMPOUND = EntityIngredient.loadableBuilder().build(SET_MATCH).list(2).flatMap(Compound::new, c -> c.ingredients);
private static final Loadable<Compound> COMPOUND = EntityIngredient.loadableBuilder().build(SET_MATCH).list(2).flatXmap(Compound::new, c -> c.ingredients);
/** Loadable for any fluid ingredient */
public static final Loadable<EntityIngredient> LOADABLE = loadableBuilder().array(COMPOUND).build(SET_MATCH);

Expand Down
Expand Up @@ -40,13 +40,13 @@ private static EitherLoadable.TypedBuilder<FluidIngredient> loadableBuilder() {
return EitherLoadable.<FluidIngredient>typed().key("fluid", FLUID_MATCH).key("tag", TAG_MATCH);
}
/** Loadable for network writing of fluids */
private static final Loadable<FluidIngredient> NETWORK = FluidStackLoadable.REQUIRED_STACK.list(0).flatMap(fluids -> FluidIngredient.of(fluids.stream().map(FluidIngredient::of).toList()), FluidIngredient::getFluids);
private static final Loadable<FluidIngredient> NETWORK = FluidStackLoadable.REQUIRED_STACK.list(0).flatXmap(fluids -> FluidIngredient.of(fluids.stream().map(FluidIngredient::of).toList()), FluidIngredient::getFluids);
/** Loadable for fluid matches */
private static final RecordLoadable<FluidMatch> FLUID_MATCH = RecordLoadable.create(Loadables.FLUID.requiredField("fluid", i -> i.fluid), IntLoadable.FROM_ONE.requiredField("amount", i -> i.amount), FluidIngredient::of);
/** Loadable for tag matches */
private static final RecordLoadable<TagMatch> TAG_MATCH = RecordLoadable.create(Loadables.FLUID_TAG.requiredField("tag", i -> i.tag), IntLoadable.FROM_ONE.requiredField("amount", i -> i.amount), FluidIngredient::of);
/** Loadable for tag matches */
private static final Loadable<Compound> COMPOUND = loadableBuilder().build(NETWORK).list(2).flatMap(Compound::new, c -> c.ingredients);
private static final Loadable<Compound> COMPOUND = loadableBuilder().build(NETWORK).list(2).flatXmap(Compound::new, c -> c.ingredients);
/** Loadable for any fluid ingredient */
public static final Loadable<FluidIngredient> LOADABLE = loadableBuilder().array(COMPOUND).build(NETWORK);

Expand Down

0 comments on commit a22da91

Please sign in to comment.