-
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.
Merge tag '2.8.0+1.21.1' into 1.21.3
- Loading branch information
Showing
11 changed files
with
181 additions
and
9 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
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
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
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
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
38 changes: 38 additions & 0 deletions
38
src/main/java/fr/estecka/variantscit/modules/ASimpleItemCachingModule.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,38 @@ | ||
package fr.estecka.variantscit.modules; | ||
|
||
import java.util.WeakHashMap; | ||
import java.util.function.Predicate; | ||
import org.jetbrains.annotations.Nullable; | ||
import fr.estecka.variantscit.VariantsCitMod; | ||
import fr.estecka.variantscit.api.ISimpleCitModule; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.Identifier; | ||
|
||
abstract class ASimpleItemCachingModule | ||
implements ISimpleCitModule | ||
{ | ||
final WeakHashMap<ItemStack, CacheEntry> cache = new WeakHashMap<>(); | ||
|
||
protected record CacheEntry(@Nullable Identifier variant, Predicate<ItemStack> isDirty) {} | ||
|
||
@Override | ||
public final Identifier GetItemVariant(ItemStack stack){ | ||
CacheEntry entry = this.cache.get(stack); | ||
|
||
if (entry == null || entry.isDirty.test(stack)){ | ||
entry = new CacheEntry(this.RecomputeItemVariant(stack), this.GetValidator(stack)); | ||
cache.put(stack, entry); | ||
VariantsCitMod.LOGGER.warn("Item Cache: [{}] {}", cache.size(), entry.variant); | ||
} | ||
|
||
return entry.variant; | ||
} | ||
|
||
/** | ||
* Returns a predicate that checks whether the cache for this stack should | ||
* be invalidated. | ||
*/ | ||
public abstract Predicate<ItemStack> GetValidator(ItemStack stack); | ||
|
||
public abstract @Nullable Identifier RecomputeItemVariant(ItemStack stack); | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/fr/estecka/variantscit/modules/TrimFormatModule.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,66 @@ | ||
package fr.estecka.variantscit.modules; | ||
|
||
import java.util.function.Predicate; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.equipment.trim.ArmorTrim; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.util.Identifier; | ||
import static net.minecraft.component.DataComponentTypes.TRIM; | ||
|
||
/** | ||
* TODO: An option to choose the namespace of the variant. | ||
* TODO: The name of the item is probably unnecessary. | ||
*/ | ||
@Deprecated | ||
public class TrimFormatModule | ||
extends ASimpleItemCachingModule | ||
{ | ||
static public final MapCodec<TrimFormatModule> CODEC = RecordCodecBuilder.mapCodec(builder->builder | ||
.group( | ||
Codec.STRING.fieldOf("variantFormat").orElse("${item}/${pattern}.${material}").forGetter(p->p.format) | ||
) | ||
.apply(builder, TrimFormatModule::new) | ||
); | ||
|
||
private final String format; | ||
|
||
private TrimFormatModule(String format) | ||
throws IllegalStateException | ||
{ | ||
this.format = format; | ||
|
||
if (!Identifier.isPathValid(this.Substitute("a","b","c"))) | ||
throw new IllegalStateException("Invalid path format: "+format); | ||
} | ||
|
||
|
||
@Override | ||
public Predicate<ItemStack> GetValidator(ItemStack stack){ | ||
ArmorTrim trim = stack.get(TRIM); | ||
return (futureStack) -> futureStack.get(TRIM) != trim; | ||
} | ||
|
||
@Override | ||
public Identifier RecomputeItemVariant(ItemStack stack){ | ||
ArmorTrim trim = stack.get(TRIM); | ||
if (trim == null) | ||
return null; | ||
|
||
Identifier type = Registries.ITEM.getEntry(stack.getItem()).getKey().get().getValue(); | ||
Identifier pattern = trim.pattern().getKey().get().getValue(); | ||
String material = trim.material().value().assetName(); | ||
|
||
return pattern.withPath( this.Substitute(type.getPath(), pattern.getPath(), material)); | ||
} | ||
|
||
private String Substitute(String itemType, String pattern, String material){ | ||
return this.format | ||
.replace("${item}", itemType) | ||
.replace("${pattern}", pattern) | ||
.replace("${material}", material) | ||
; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/fr/estecka/variantscit/modules/TrimMaterialModule.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,20 @@ | ||
package fr.estecka.variantscit.modules; | ||
|
||
import fr.estecka.variantscit.api.ISimpleCitModule; | ||
import net.minecraft.component.DataComponentTypes; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.equipment.trim.ArmorTrim; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class TrimMaterialModule | ||
implements ISimpleCitModule | ||
{ | ||
@Override | ||
public Identifier GetItemVariant(ItemStack stack){ | ||
ArmorTrim trim = stack.get(DataComponentTypes.TRIM); | ||
if (trim == null) | ||
return null; | ||
|
||
return trim.material().getKey().get().getValue(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/fr/estecka/variantscit/modules/TrimModule.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,24 @@ | ||
package fr.estecka.variantscit.modules; | ||
|
||
import net.minecraft.component.DataComponentTypes; | ||
import net.minecraft.item.equipment.trim.ArmorTrim; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class TrimModule | ||
extends ASimpleComponentCachingModule<ArmorTrim> | ||
{ | ||
public TrimModule(){ | ||
super(DataComponentTypes.TRIM); | ||
} | ||
|
||
@Override | ||
public Identifier GetVariantForComponent(ArmorTrim trim){ | ||
if (trim == null) | ||
return null; | ||
|
||
Identifier pattern = trim.pattern().getKey().get().getValue(); | ||
Identifier material = trim.material().getKey().get().getValue(); | ||
|
||
return pattern.withSuffixedPath("_" + material.getPath()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/fr/estecka/variantscit/modules/TrimPatternModule.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,20 @@ | ||
package fr.estecka.variantscit.modules; | ||
|
||
import fr.estecka.variantscit.api.ISimpleCitModule; | ||
import net.minecraft.component.DataComponentTypes; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.equipment.trim.ArmorTrim; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class TrimPatternModule | ||
implements ISimpleCitModule | ||
{ | ||
@Override | ||
public Identifier GetItemVariant(ItemStack stack){ | ||
ArmorTrim trim = stack.get(DataComponentTypes.TRIM); | ||
if (trim == null) | ||
return null; | ||
|
||
return trim.pattern().getKey().get().getValue(); | ||
} | ||
} |
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