generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plus a bit of the client tag manager, since some of the data stuff is used for it
- Loading branch information
Showing
19 changed files
with
373 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
common/src/main/java/dev/dhyces/trimmed/api/data/client/tag/BaseClientTagDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package dev.dhyces.trimmed.api.data.client.tag; | ||
|
||
import dev.dhyces.trimmed.api.KeyResolver; | ||
import dev.dhyces.trimmed.api.client.tag.ClientTagKey; | ||
import dev.dhyces.trimmed.api.data.tag.ClientTagBuilder; | ||
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap; | ||
import net.minecraft.data.DataProvider; | ||
import net.minecraft.data.PackOutput; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
import java.util.Map; | ||
|
||
public abstract class BaseClientTagDataProvider<T, R extends KeyResolver<T>> implements DataProvider { | ||
protected final PackOutput packOutput; | ||
protected final PackOutput.PathProvider pathProvider; | ||
protected final String modid; | ||
protected final Map<ResourceLocation, ClientTagBuilder<T>> builders = new Object2ObjectLinkedOpenHashMap<>(); | ||
protected final R keyResolver; | ||
|
||
public BaseClientTagDataProvider(PackOutput packOutput, String modid, String prefix, R keyResolver) { | ||
this.packOutput = packOutput; | ||
this.modid = modid; | ||
this.pathProvider = packOutput.createPathProvider(PackOutput.Target.RESOURCE_PACK, prefix); | ||
this.keyResolver = keyResolver; | ||
} | ||
|
||
protected ClientTagBuilder<T> getOrCreateBuilder(ClientTagKey<T> clientTagKey) { | ||
return this.builders.computeIfAbsent(clientTagKey.getTagId(), resourceLocation -> { | ||
onBuilderCreation(resourceLocation); | ||
return new ClientTagBuilder<>(clientTagKey.getKeyResolver()); | ||
}); | ||
} | ||
|
||
protected void onBuilderCreation(ResourceLocation id) {} | ||
|
||
protected boolean doesTagExist(ClientTagKey<T> clientTagKey) { | ||
return builders.get(clientTagKey.getTagId()) != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 29 additions & 51 deletions
80
common/src/main/java/dev/dhyces/trimmed/api/data/client/tag/appenders/ClientTagAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
common/src/main/java/dev/dhyces/trimmed/api/data/tag/ClientTagBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package dev.dhyces.trimmed.api.data.tag; | ||
|
||
import dev.dhyces.trimmed.api.KeyResolver; | ||
import dev.dhyces.trimmed.api.client.tag.ClientTagKey; | ||
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet; | ||
|
||
import java.util.Set; | ||
|
||
public class ClientTagBuilder<T> { | ||
private final KeyResolver<T> keyResolver; | ||
private final Set<ClientTagEntry<T>> elements; | ||
private boolean replaces; | ||
|
||
public ClientTagBuilder(KeyResolver<T> keyResolver) { | ||
this.keyResolver = keyResolver; | ||
this.elements = new ObjectLinkedOpenHashSet<>(); | ||
} | ||
|
||
public ClientTagBuilder<T> add(T element, boolean isRequired) { | ||
this.elements.add(ClientTagEntry.element(element, isRequired)); | ||
return this; | ||
} | ||
|
||
public ClientTagBuilder<T> addTag(ClientTagKey<T> clientTagKey, boolean isRequired) { | ||
this.elements.add(ClientTagEntry.clientTagKey(clientTagKey, isRequired)); | ||
return this; | ||
} | ||
|
||
public ClientTagBuilder<T> setReplaces(boolean doesReplace) { | ||
this.replaces = doesReplace; | ||
return this; | ||
} | ||
|
||
public ClientTagFile<T> build() { | ||
return new ClientTagFile<>(elements, replaces); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
common/src/main/java/dev/dhyces/trimmed/api/data/tag/ClientTagEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package dev.dhyces.trimmed.api.data.tag; | ||
|
||
import com.mojang.datafixers.util.Either; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import dev.dhyces.trimmed.api.KeyResolver; | ||
import dev.dhyces.trimmed.api.client.tag.ClientTagKey; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public record ClientTagEntry<T>(Either<T, ClientTagKey<T>> element, boolean isRequired) { | ||
public static <T> Codec<Either<T, ClientTagKey<T>>> elementOrTagCodec(KeyResolver<T> keyResolver) { | ||
return Codec.either(keyResolver.getCodec(), ClientTagKey.tagCodec(keyResolver)); | ||
} | ||
public static <T> Codec<ClientTagEntry<T>> fullCodec(KeyResolver<T> keyResolver) { | ||
return RecordCodecBuilder.create(instance -> | ||
instance.group( | ||
elementOrTagCodec(keyResolver).fieldOf("value").forGetter(ClientTagEntry::element), | ||
Codec.BOOL.fieldOf("required").forGetter(ClientTagEntry::isRequired) | ||
).apply(instance, ClientTagEntry::new) | ||
); | ||
} | ||
public static <T> Codec<ClientTagEntry<T>> codec(KeyResolver<T> keyResolver) { | ||
return Codec.withAlternative(elementOrTagCodec(keyResolver).xmap( | ||
either -> new ClientTagEntry<>(either, true), | ||
ClientTagEntry::element), fullCodec(keyResolver) | ||
); | ||
} | ||
|
||
public boolean isTag() { | ||
return element.right().isPresent(); | ||
} | ||
|
||
@Nullable | ||
public T getElement() { | ||
return element.left().orElse(null); | ||
} | ||
|
||
@Nullable | ||
public ClientTagKey<T> getTag() { | ||
return element.right().orElse(null); | ||
} | ||
|
||
public boolean verifyExists(Predicate<T> elementPredicate, Predicate<ClientTagKey<T>> keyPredicate) { | ||
return element.map(elementPredicate::test, keyPredicate::test); | ||
} | ||
|
||
public static <T> ClientTagEntry<T> element(T element, boolean isRequired) { | ||
return new ClientTagEntry<>(Either.left(element), isRequired); | ||
} | ||
|
||
public static <T> ClientTagEntry<T> clientTagKey(ClientTagKey<T> clientTagKey, boolean isRequired) { | ||
return new ClientTagEntry<>(Either.right(clientTagKey), isRequired); | ||
} | ||
} |
Oops, something went wrong.