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
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ neoForge {
// Include resources generated by data generators.
sourceSets.main.resources { srcDir 'src/generated/resources' }

configurations.testImplementation.extendsFrom(configurations.neoForgeCompileDependencies)

repositories {
maven {
name 'jared maven'
Expand Down Expand Up @@ -83,6 +85,8 @@ repositories {
}

dependencies {
testImplementation platform('org.junit:junit-bom:5.11.4')
testImplementation 'org.junit.jupiter:junit-jupiter'
compileOnly "mezz.jei:jei-1.21.1-common-api:$jei_version"
compileOnly "mezz.jei:jei-1.21.1-neoforge-api:$jei_version"
runtimeOnly "mezz.jei:jei-1.21.1-neoforge:$jei_version"
Expand Down Expand Up @@ -122,6 +126,10 @@ tasks.withType(JavaCompile).configureEach {
options.compilerArgs << '-Xmaxerrs' << '2000'
}

tasks.named('test', Test) {
useJUnitPlatform()
}

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package com.buuz135.replication.calculation.client;

import com.buuz135.replication.ReplicationRegistry;
import com.buuz135.replication.calculation.MatterCompound;
import com.buuz135.replication.calculation.ReplicationCalculation;
import com.buuz135.replication.calculation.client.matteropedia.MatterOpediaCatalog;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class ClientReplicationCalculation {

public static HashMap<String, MatterCompound> DEFAULT_MATTER_COMPOUND = new HashMap<String, MatterCompound>();
private static volatile MatterOpediaCatalog matterOpediaCatalog = MatterOpediaCatalog.empty(0, 0);

@Nullable
public static MatterCompound getMatterCompound(ItemStack stack) {
Expand All @@ -29,5 +43,104 @@ public static void acceptData(HolderLookup.Provider provider, CompoundTag compou
matter.deserializeNBT(provider, compoundTag.getCompound(allKey));
DEFAULT_MATTER_COMPOUND.put(allKey, matter);
}

List<MatterOpediaCatalog.Seed> seeds = new ArrayList<>(DEFAULT_MATTER_COMPOUND.size());
DEFAULT_MATTER_COMPOUND.forEach((itemName, compound) -> {
ifRegisteredItem(itemName, (itemId, item) -> {
ItemStack stack = item.getDefaultInstance();
if (stack.isEmpty()) {
return;
}

Map<ResourceLocation, Double> amounts = new LinkedHashMap<>();
compound.getValues().values().forEach(value -> {
ResourceLocation matterKey = ReplicationRegistry.MATTER_TYPES_REGISTRY.getKey(value.getMatter());
if (matterKey != null) {
amounts.put(matterKey, value.getAmount());
}
});
seeds.add(new MatterOpediaCatalog.Seed(
itemId,
compound,
amounts,
amounts.size(),
stack.getDisplayName().getString().toLowerCase(Locale.ROOT)
));
});
});

MatterOpediaCatalog current = matterOpediaCatalog;
MatterOpediaCatalog replacement = MatterOpediaCatalog.fromSeeds(
seeds,
getMatterTypeKeys(),
current.generation() + 1,
current.languageGeneration()
);
matterOpediaCatalog = replacement;
}

public static MatterOpediaCatalog getMatterOpediaCatalog() {
return matterOpediaCatalog;
}

public static void rebuildMatterOpediaNameIndexes() {
MatterOpediaCatalog current = matterOpediaCatalog;
if (current.isEmpty()) {
return;
}

Map<ResourceLocation, String> displayNames = new LinkedHashMap<>();
for (String itemName : DEFAULT_MATTER_COMPOUND.keySet()) {
ifRegisteredItem(itemName, (itemId, item) -> {
ItemStack stack = item.getDefaultInstance();
if (!stack.isEmpty()) {
displayNames.put(
itemId,
stack.getDisplayName().getString().toLowerCase(Locale.ROOT)
);
}
});
}
if (displayNames.isEmpty()) {
return;
}

MatterOpediaCatalog replacement = current.withDisplayNames(
displayNames,
current.languageGeneration() + 1
);
matterOpediaCatalog = replacement;
}

public static void clearClientData() {
DEFAULT_MATTER_COMPOUND.clear();
MatterOpediaCatalog current = matterOpediaCatalog;
matterOpediaCatalog = MatterOpediaCatalog.empty(
current.generation() + 1,
current.languageGeneration()
);
}

static void ifRegisteredItem(String itemName, BiConsumer<ResourceLocation, Item> action) {
ResourceLocation itemId = ResourceLocation.tryParse(itemName);
if (itemId == null) {
return;
}
BuiltInRegistries.ITEM.getOptional(itemId).ifPresent(item -> action.accept(itemId, item));
}

private static Set<ResourceLocation> getMatterTypeKeys() {
return getMatterTypeKeys(ReplicationRegistry.MATTER_TYPES_REGISTRY);
}

static <T> Set<ResourceLocation> getMatterTypeKeys(Registry<T> matterTypes) {
Set<ResourceLocation> matterTypeKeys = new LinkedHashSet<>();
matterTypes.forEach(matterType -> {
ResourceLocation matterKey = matterTypes.getKey(matterType);
if (matterKey != null) {
matterTypeKeys.add(matterKey);
}
});
return matterTypeKeys;
}
}
Loading