Skip to content

Commit

Permalink
Merge pull request #318 from Gu-ZT/data
Browse files Browse the repository at this point in the history
调整配方顺序
  • Loading branch information
Gu-ZT authored Apr 18, 2024
2 parents 366b504 + 368b8ae commit 1bb9318
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public static void init(RegistrateRecipeProvider provider) {
.hasBlock(Blocks.CAULDRON)
.hasItemIngredient(new Vec3(0.0, -1.0, 0.0), 1, ModItemTags.STONE_FORGE)
.setBlock(Blocks.LAVA_CAULDRON)
.unlockedBy(AnvilCraftDatagen.hasItem(ModItemTags.STONE_FORGE), AnvilCraftDatagen.has(ModItemTags.STONE_FORGE))
.save(provider, AnvilCraft.of("heating/" + BuiltInRegistries.BLOCK.getKey(Blocks.LAVA).getPath()));
.unlockedBy(AnvilCraftDatagen.hasItem(ModItemTags.STONE_FORGE),
AnvilCraftDatagen.has(ModItemTags.STONE_FORGE))
.save(provider, AnvilCraft.of("heating/"
+ BuiltInRegistries.BLOCK.getKey(Blocks.LAVA).getPath() + "_forge"));
AnvilRecipe.Builder.create(RecipeCategory.MISC)
.icon(ModItems.ROYAL_STEEL_INGOT)
.hasBlock(ModBlocks.HEATER.get(), new Vec3(0.0, -2.0, 0.0), Map.entry(OVERLOAD, false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
@SuppressWarnings("unused")
public class AnvilRecipe implements Recipe<AnvilCraftingContainer> {
private final ResourceLocation id;
@Getter
private final List<RecipePredicate> predicates = new ArrayList<>();
@Getter
private final List<RecipeOutcome> outcomes = new ArrayList<>();
private final ItemStack icon;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dev.dubhe.anvilcraft.data.recipe.anvil;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Recipe;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class AnvilRecipeMap extends LinkedHashMap<ResourceLocation, Recipe<?>> {
/**
* 排序
*/
public void sort() {
List<Map.Entry<ResourceLocation, Recipe<?>>> entryList = new ArrayList<>();
this.entrySet().forEach(entryList::add);
this.clear();
entryList.sort(this::compare);
entryList.forEach(entry -> this.put(entry.getKey(), entry.getValue()));
}

/**
* @return 不可变 Map
*/
public Map<ResourceLocation, Recipe<?>> unmodifiable() {
return Collections.unmodifiableMap(this);
}

private int compare(
@NotNull Map.Entry<ResourceLocation, Recipe<?>> entry1,
@NotNull Map.Entry<ResourceLocation, Recipe<?>> entry2
) {
if (!(entry1.getValue() instanceof AnvilRecipe value1 && entry2.getValue() instanceof AnvilRecipe value2)) {
return 0;
}
int size1 = value1.getOutcomes().size();
int size2 = value2.getOutcomes().size();
if (size1 != size2) return size1 - size2;
size1 = value1.getPredicates().size();
size2 = value2.getPredicates().size();
return size1 - size2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.dubhe.anvilcraft.api.hammer.HammerManager;
import dev.dubhe.anvilcraft.api.power.IPowerComponent;
import dev.dubhe.anvilcraft.data.recipe.anvil.AnvilRecipe;
import dev.dubhe.anvilcraft.data.recipe.anvil.AnvilRecipeMap;
import dev.dubhe.anvilcraft.data.recipe.anvil.outcome.SpawnItem;
import dev.dubhe.anvilcraft.data.recipe.anvil.predicate.HasBlock;
import dev.dubhe.anvilcraft.data.recipe.anvil.predicate.HasItemIngredient;
Expand Down Expand Up @@ -37,7 +38,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class ServerEventListener {
/**
Expand All @@ -59,24 +59,14 @@ public void onDataPackReloaded(@NotNull ServerEndDataPackReloadEvent event) {
ServerEventListener.processRecipes(server);
}

private static int compare(@NotNull ResourceLocation l1, ResourceLocation l2) {
if (l1.getPath().startsWith("heating/")) return -1;
return 1;
}

private static int getPriority(@NotNull ResourceLocation location) {
if (location.getPath().startsWith("heating/")) return 999;
return 1000;
}

/**
* 处理配方
*
* @param server 服务器
*/
public static void processRecipes(@NotNull MinecraftServer server) {
Map<ResourceLocation, Recipe<?>> newRecipes = new TreeMap<>(ServerEventListener::compare);
Map<RecipeType<?>, Map<ResourceLocation, Recipe<?>>> newRecipeMap = new HashMap<>();
AnvilRecipeMap anvilRecipes = new AnvilRecipeMap();
for (Map.Entry<RecipeType<?>, Map<ResourceLocation, Recipe<?>>> entry
: server.getRecipeManager().recipes.entrySet()) {
RecipeType<?> type = entry.getKey();
Expand All @@ -87,13 +77,21 @@ public static void processRecipes(@NotNull MinecraftServer server) {
recipeMap.put(id, recipe);
if (type != RecipeType.CRAFTING && type != RecipeType.BLASTING) continue;
Pair<ResourceLocation, Recipe<?>> newRecipe = ServerEventListener.processRecipes(id, recipe);
if (newRecipe != null) newRecipes.put(newRecipe.getFirst(), newRecipe.getSecond());
if (newRecipe == null) continue;
ResourceLocation location = newRecipe.getFirst();
Recipe<?> recipe1 = newRecipe.getSecond();
anvilRecipes.put(location, recipe1);
}
if (type == ModRecipeTypes.ANVIL_RECIPE) {
newRecipes.putAll(recipeMap);
for (Map.Entry<ResourceLocation, Recipe<?>> recipeEntry : recipeMap.entrySet()) {
ResourceLocation location = recipeEntry.getKey();
Recipe<?> recipe = recipeEntry.getValue();
anvilRecipes.put(location, recipe);
}
} else newRecipeMap.put(type, recipeMap);
}
newRecipeMap.put(ModRecipeTypes.ANVIL_RECIPE, newRecipes);
anvilRecipes.sort();
newRecipeMap.put(ModRecipeTypes.ANVIL_RECIPE, anvilRecipes.unmodifiable());
newRecipeMap.replaceAll(
(type, resourceLocationRecipeMap) -> Collections.unmodifiableMap(resourceLocationRecipeMap)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"anvilcraft:heater"
],
"state": {
"lit": "true"
"overload": "false"
}
},
"offset": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"anvilcraft:heater"
],
"state": {
"lit": "true"
"overload": "false"
}
},
"offset": [
Expand Down

0 comments on commit 1bb9318

Please sign in to comment.