Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -31,12 +31,15 @@
import team.unnamed.creative.atlas.AtlasSource;
import team.unnamed.creative.base.Writable;
import team.unnamed.creative.blockstate.BlockState;
import team.unnamed.creative.blockstate.MultiVariant;
import team.unnamed.creative.equipment.Equipment;
import team.unnamed.creative.equipment.EquipmentLayer;
import team.unnamed.creative.equipment.EquipmentLayerType;
import team.unnamed.creative.font.Font;
import team.unnamed.creative.font.FontProvider;
import team.unnamed.creative.item.Item;
import team.unnamed.creative.item.ItemModel;
import team.unnamed.creative.item.RangeDispatchItemModel;
import team.unnamed.creative.lang.Language;
import team.unnamed.creative.model.ItemOverride;
import team.unnamed.creative.model.Model;
Expand All @@ -50,6 +53,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -378,6 +382,7 @@ public boolean removeUnknownFile(final @NotNull String path) {
@Override
public void merge(final @NotNull ResourceContainer other, final @NotNull MergeStrategy strategy) {
final boolean override = strategy == MergeStrategy.override();
final boolean shouldOverride = override || strategy == MergeStrategy.mergeBothAndPrioritizeSecondOnError();

// merge atlases
for (final Atlas atlas : other.atlases()) {
Expand All @@ -396,11 +401,34 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
// merge block states
for (final BlockState blockState : other.blockStates()) {
if (blockStates.containsKey(blockState.key())) {
if (strategy == MergeStrategy.override()) {
if (override) {
blockStates.put(blockState.key(), blockState);
} else if (strategy == MergeStrategy.mergeAndFailOnError()) {
throw new MergeException("Duplicate block state '" + blockState.key()
+ "': exists in both resource containers");
} else if (MergeStrategy.isMergeBoth(strategy)) {
var thisVariants = blockStates.get(blockState.key()).variants();
var thisMultipart = blockStates.get(blockState.key()).multipart();
var otherVariants = blockState.variants();
var otherMultipart = blockState.multipart();

var finalVariants = new HashMap<String, MultiVariant>();
if (strategy == MergeStrategy.mergeBothAndPrioritizeFirstOnError()) {
finalVariants.putAll(thisVariants);
finalVariants.putAll(otherVariants);
} else {
finalVariants.putAll(otherVariants);
finalVariants.putAll(thisVariants);
}

var finalMultipart = new ArrayList<>(thisMultipart);
finalMultipart.addAll(otherMultipart);

blockStates.put(blockState.key(), BlockState.of(
blockState.key(),
finalVariants,
finalMultipart
));
}
} else {
blockStates.put(blockState.key(), blockState);
Expand All @@ -410,7 +438,7 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
// merge equipment
for (final Equipment equipment : other.equipment()) {
final Equipment oldEquipment = this.equipment.get(equipment.key());
if (oldEquipment == null || override) {
if (oldEquipment == null || shouldOverride) {
this.equipment.put(equipment.key(), equipment);
continue;
}
Expand Down Expand Up @@ -448,15 +476,59 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt

// merge items
for (final Item item : other.items()) {
if (items.containsKey(item.key())) {
if (override) {
items.put(item.key(), item);
} else if (strategy == MergeStrategy.mergeAndFailOnError()) {
throw new MergeException("Duplicated item '" + item.key()
+ "': exists in both resource containers");
}
} else {
if (!items.containsKey(item.key())) {
items.put(item.key(), item);
continue;
}

var prevItem = items.get(item.key());

if (override) {
items.put(item.key(), item);
} else if (strategy == MergeStrategy.mergeAndFailOnError()) {
throw new MergeException("Duplicated item '" + item.key()
+ "': exists in both resource containers");
} else if (MergeStrategy.isMergeBoth(strategy)) {
var thisModel = prevItem.model();
var otherModel = item.model();
var isFirstPriority = strategy == MergeStrategy.mergeBothAndPrioritizeFirstOnError();

ItemModel finalModel;
// TODO : Add merging for other Item models
if (thisModel instanceof RangeDispatchItemModel a &&
otherModel instanceof RangeDispatchItemModel b) {

var first = isFirstPriority ? a : b;
var second = isFirstPriority ? b : a;

var entries = new ArrayList<>(first.entries());
for (var entry : second.entries()) {
var exists = entries.stream().anyMatch(b2 -> {
return entry.threshold() == b2.threshold();
});

if (!exists) {
entries.add(entry);
}
}

finalModel = ItemModel.rangeDispatch()
.addEntries(entries)
.scale(first.scale())
.fallback(first.fallback() == null ? second.fallback() : first.fallback())
.property(first.property())
.build();
} else {
finalModel = otherModel;
}

items.put(item.key(), Item.item(
item.key(),
finalModel,
isFirstPriority ? prevItem.handAnimationOnSwap() : item.handAnimationOnSwap(),
isFirstPriority ? prevItem.oversizedInGui() : item.oversizedInGui(),
isFirstPriority ? prevItem.swapAnimationScale() : item.swapAnimationScale()
));
}
}

Expand All @@ -478,14 +550,15 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
+ ". Exists in both resource containers."
);
}
// TODO: Add merge both strategies
}
languages.put(language.key(), Language.language(language.key(), translations));
}

// merge models
for (final Model model : other.models()) {
final Model oldModel = models.get(model.key());
if (oldModel == null || strategy == MergeStrategy.override()) {
if (oldModel == null || override) {
models.put(model.key(), model);
continue;
}
Expand Down Expand Up @@ -517,6 +590,7 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
throw new MergeException("Duplicated sound event '" + soundEvent + "': exists" +
" in both resource-packs");
}
// TODO: Add merge both strategies
}

soundRegistries.put(
Expand All @@ -531,7 +605,7 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
// merge sounds
for (final Sound sound : other.sounds()) {
if (sounds.containsKey(sound.key())) {
if (override) {
if (shouldOverride) {
sounds.put(sound.key(), sound);
} else if (strategy == MergeStrategy.mergeAndFailOnError()) {
throw new MergeException("Duplicated sound '" + sound.key()
Expand All @@ -546,7 +620,7 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
// todo: should we merge metadata?
for (final Texture texture : other.textures()) {
if (textures.containsKey(texture.key())) {
if (override) {
if (shouldOverride) {
textures.put(texture.key(), texture);
} else if (strategy == MergeStrategy.mergeAndFailOnError()) {
throw new MergeException("Duplicated texture '" + texture.key()
Expand All @@ -560,7 +634,7 @@ public void merge(final @NotNull ResourceContainer other, final @NotNull MergeSt
// merge unknown files
for (final Map.Entry<String, Writable> entry : other.unknownFiles().entrySet()) {
if (files.containsKey(entry.getKey())) {
if (override) {
if (shouldOverride) {
files.put(entry.getKey(), entry.getValue());
} else if (strategy == MergeStrategy.mergeAndFailOnError()) {
throw new MergeException("Duplicated unknown file: '" + entry.getKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,33 @@ public interface MergeStrategy {
static @NotNull MergeStrategy mergeAndKeepFirstOnError() {
return MergeStrategyImpl.MERGE_AND_KEEP_FIRST_ON_ERROR;
}

/**
* Gets a merge strategy that merges the resources of the
* first container with the resources of the second container.
* This will try to merge both files and prioritize the first container.
*
* @return The merge both and prioritize first on error merge strategy
* @since 1.11.13
*/
static @NotNull MergeStrategy mergeBothAndPrioritizeFirstOnError() {
return MergeStrategyImpl.MERGE_BOTH_AND_PRIORITIZE_FIRST_ON_ERROR;
}

/**
* Gets a merge strategy that merges the resources of the
* first container with the resources of the second container.
* This will try to merge both files and prioritize the second container.
* It will act as an override if there are duplicates.
*
* @return The merge both and prioritize second on error merge strategy
* @since 1.11.13
*/
static @NotNull MergeStrategy mergeBothAndPrioritizeSecondOnError() {
return MergeStrategyImpl.MERGE_BOTH_AND_PRIORITIZE_SECOND_ON_ERROR;
}

static boolean isMergeBoth(MergeStrategy strategy) {
return strategy == MergeStrategyImpl.MERGE_BOTH_AND_PRIORITIZE_FIRST_ON_ERROR || strategy == MergeStrategyImpl.MERGE_BOTH_AND_PRIORITIZE_SECOND_ON_ERROR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
enum MergeStrategyImpl implements MergeStrategy {
OVERRIDE,
MERGE_AND_FAIL_ON_ERROR,
MERGE_AND_KEEP_FIRST_ON_ERROR
MERGE_AND_KEEP_FIRST_ON_ERROR,
MERGE_BOTH_AND_PRIORITIZE_FIRST_ON_ERROR,
MERGE_BOTH_AND_PRIORITIZE_SECOND_ON_ERROR,
;
}