Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REA filters #260

Open
wants to merge 4 commits into
base: 1.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public class BlockContentRegistries {

return DataResult.success(block);
}))
.filter(block -> block.getDefaultState().contains(Properties.AXIS))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this.

While I like the idea, I also find it a bit bad, thought that comes from strippable being kind of awful from the start.
Allowing other kind of blocks to be strippable would be interesting but that's a debate for another PR. But it shows that the conditions system has potential.

.build();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

import com.mojang.serialization.Codec;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -287,6 +288,15 @@ default Optional<V> get(R entry) {
*/
boolean remove(TagKey<R> tag);

/**
* The entry filter can prevent entries from being shown in the attachment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The entry filter can prevent entries from being shown in the attachment.
* Gets the entry filter that determines if certain registry entries can be associated with values in this attachment.

* This can be used to prevent values from tags leaking into the registry entry attachment when not intended,
* such as nether wood in furnace fuels.
*
* @return the associated entry filter.
*/
Predicate<R> entryFilter();

/**
* {@return this attachment's "value associated with entry" event}
*/
Expand Down Expand Up @@ -444,6 +454,8 @@ final class Builder<R, V> {
private @Nullable V defaultValue;
private @Nullable DefaultValueProvider<R, V> defaultValueProvider;

private Predicate<R> filter = o -> true;

private Builder(Registry<R> registry, Identifier id, Class<V> valueClass, Codec<V> codec) {
this.registry = registry;
this.id = id;
Expand Down Expand Up @@ -501,6 +513,17 @@ public Builder<R, V> defaultValueProvider(@Nullable DefaultValueProvider<R, V> d
return this;
}

/**
* Sets the filter for the attachment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Sets the filter for the attachment.
* Sets the registry entry filter for the attachment.

*
* @param filter the entry filter
* @return this builder
*/
public Builder<R, V> filter(Predicate<R> filter) {
this.filter = filter;
return this;
}

/**
* Builds a new attachment.
*
Expand All @@ -510,10 +533,10 @@ public RegistryEntryAttachment<R, V> build() {
RegistryEntryAttachment<R, V> attachment;
if (this.defaultValueProvider == null) {
attachment = new ConstantDefaultRegistryEntryAttachmentImpl<>(this.registry, this.id, this.valueClass,
this.codec, this.side, this.defaultValue);
this.codec, this.side, this.defaultValue, this.filter);
} else {
attachment = new ComputedDefaultRegistryEntryAttachmentImpl<>(this.registry, this.id, this.valueClass,
this.codec, this.side, this.defaultValueProvider);
this.codec, this.side, this.defaultValueProvider, this.filter);
}
RegistryEntryAttachmentHolder.registerAttachment(this.registry, attachment);
return attachment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.quiltmc.qsl.registry.attachment.impl;

import java.util.function.Predicate;

import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import org.jetbrains.annotations.NotNull;
Expand All @@ -33,8 +35,8 @@ public final class ComputedDefaultRegistryEntryAttachmentImpl<R, V> extends Regi

private final @NotNull DefaultValueProvider<R, V> defaultValueProvider;

public ComputedDefaultRegistryEntryAttachmentImpl(Registry<R> registry, Identifier id, Class<V> valueClass, Codec<V> codec, Side side, @NotNull DefaultValueProvider<R, V> defaultValueProvider) {
super(registry, id, valueClass, codec, side);
public ComputedDefaultRegistryEntryAttachmentImpl(Registry<R> registry, Identifier id, Class<V> valueClass, Codec<V> codec, Side side, @NotNull DefaultValueProvider<R, V> defaultValueProvider, Predicate<R> filter) {
super(registry, id, valueClass, codec, side, filter);
this.defaultValueProvider = defaultValueProvider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.quiltmc.qsl.registry.attachment.impl;

import java.util.function.Predicate;

import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import org.jetbrains.annotations.Nullable;
Expand All @@ -27,8 +29,8 @@ public final class ConstantDefaultRegistryEntryAttachmentImpl<R, V> extends Regi
private final @Nullable V defaultValue;

public ConstantDefaultRegistryEntryAttachmentImpl(Registry<R> registry, Identifier id, Class<V> valueClass,
Codec<V> codec, Side side, @Nullable V defaultValue) {
super(registry, id, valueClass, codec, side);
Codec<V> codec, Side side, @Nullable V defaultValue, Predicate<R> filter) {
super(registry, id, valueClass, codec, side, filter);

if (defaultValue != null) {
var encoded = this.codec.encodeStart(JsonOps.INSTANCE, defaultValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

import com.mojang.serialization.Codec;
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashBigSet;
Expand All @@ -44,14 +45,16 @@ public abstract class RegistryEntryAttachmentImpl<R, V> implements RegistryEntry
protected final Event<TagValueAdded<R, V>> tagValueAddedEvent;
protected final Event<ValueRemoved<R>> valueRemovedEvent;
protected final Event<TagValueRemoved<R>> tagValueRemovedEvent;
protected final Predicate<R> filter;

public RegistryEntryAttachmentImpl(Registry<R> registry, Identifier id, Class<V> valueClass, Codec<V> codec,
Side side) {
Side side, Predicate<R> filter) {
this.registry = registry;
this.id = id;
this.valueClass = valueClass;
this.codec = codec;
this.side = side;
this.filter = filter;

this.valueAddedEvent = Event.create(ValueAdded.class, listeners -> (entry, value) -> {
for (var listener : listeners) {
Expand Down Expand Up @@ -108,6 +111,10 @@ public Side side() {
ClientSideGuard.assertAccessAllowed();
}

if (!this.filter.test(entry)) {
return null;
}

V value = RegistryEntryAttachmentHolder.getData(this.registry).getValue(this, entry);
if (value != null) {
return value;
Expand All @@ -130,6 +137,7 @@ public Set<R> keySet() {
Set<R> set = new ReferenceOpenHashBigSet<>();
set.addAll(RegistryEntryAttachmentHolder.getData(this.registry).valueTable.row(this).keySet());
set.addAll(RegistryEntryAttachmentHolder.getBuiltin(this.registry).valueTable.row(this).keySet());
set.removeIf(Predicate.not(this.filter));
return set;
}

Expand Down Expand Up @@ -213,6 +221,11 @@ public boolean remove(TagKey<R> tag) {
return false;
}

@Override
public Predicate<R> entryFilter() {
return this.filter;
}

@Override
public Event<ValueAdded<R, V>> valueAddedEvent() {
return this.valueAddedEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.Identifier;

import org.quiltmc.qsl.registry.attachment.api.RegistryEntryAttachment;
Expand All @@ -39,6 +41,11 @@ public class ItemContentRegistries {
*/
public static final String NAMESPACE = "quilt";

/**
* A tag that filters out values from being included as furnace fuels.
*/
public static final TagKey<Item> FUEL_FILTERS = TagKey.of(RegistryKeys.ITEM, new Identifier("quilt", "fuel_filters"));

/**
* A {@link RegistryEntryAttachment} for how long different items burn in a furnace. The value is stored in ticks.
* <p>
Expand All @@ -49,6 +56,7 @@ public class ItemContentRegistries {
new Identifier(NAMESPACE, "fuel_times"),
Integer.class,
Codec.intRange(0, Integer.MAX_VALUE))
.filter(item -> !item.getBuiltInRegistryHolder().isIn(FUEL_FILTERS))
.build();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.Identifier;

import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.qsl.base.api.entrypoint.ModInitializer;
Expand All @@ -47,8 +45,6 @@ public class ItemContentRegistriesInitializer implements ModInitializer {

public static final Map<ItemConvertible, Float> INITIAL_COMPOST_CHANCE = ImmutableMap.copyOf(ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE);

public static final TagKey<Item> FUEL_FILTERS = TagKey.of(RegistryKeys.ITEM, new Identifier("quilt", "fuel_filters"));

private static boolean collectInitialTags = false;

@Override
Expand All @@ -62,12 +58,7 @@ public void onInitialize(ModContainer mod) {

ResourceLoaderEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, error) -> {
FUEL_MAP.clear();
// Fill the fuel map with all entries on the FUEL_TIMES registry attachment but filter using the #quilt:fuel_filters tag
for (var entry : ItemContentRegistries.FUEL_TIMES) {
if (!entry.entry().getBuiltInRegistryHolder().isIn(FUEL_FILTERS)) {
FUEL_MAP.put(entry.entry(), entry.value());
}
}
setMapFromAttachment(FUEL_MAP::put, ItemContentRegistries.FUEL_TIMES);

ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.clear();
setMapFromAttachment(ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE::put, ItemContentRegistries.COMPOST_CHANCES);
Expand Down