9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-25 01:49:30 +00:00

Merge pull request #8 from Xiao-MoMi/main

Optimize remove recipes
This commit is contained in:
jhqwqmc
2025-02-12 03:31:26 +08:00
committed by GitHub
5 changed files with 81 additions and 13 deletions

View File

@@ -31,12 +31,10 @@ items:
data:
display-name: "<!i>Topaz"
model:
type: "minecraft:model"
path: "minecraft:item/custom/topaz"
generation:
parent: "minecraft:item/generated"
textures:
"layer0": "minecraft:item/custom/topaz"
template: models:generated
arguments:
model_path: "minecraft:item/custom/topaz"
texture_path: "minecraft:item/custom/topaz"
blocks:
default:topaz_ore:

View File

@@ -1,5 +1,13 @@
# map-color: https://minecraft.wiki/w/Map_item_format
templates:
# Models
models:generated:
type: "minecraft:model"
path: "{model_path}"
generation:
parent: "minecraft:item/generated"
textures:
"layer0": "{texture_path}"
# Block Settings
block_settings:surface_decoration:

View File

@@ -128,14 +128,30 @@ public class BukkitRecipeManager implements RecipeManager<ItemStack> {
this.recipes.clear();
this.dataPackRecipes.clear();
this.customRecipes.clear();
for (NamespacedKey key : this.injectedDataPackRecipes) {
Bukkit.removeRecipe(key);
}
this.injectedDataPackRecipes.clear();
for (NamespacedKey key : this.registeredCustomRecipes) {
Bukkit.removeRecipe(key);
if (VersionHelper.isVersionNewerThan1_21_2()) {
try {
Object recipeManager = Reflections.method$MinecraftServer$getRecipeManager.invoke(Reflections.method$MinecraftServer$getServer.invoke(null));
Object recipeMap = Reflections.field$RecipeManager$recipes.get(recipeManager);
for (NamespacedKey key : this.injectedDataPackRecipes) {
Reflections.method$RecipeMap$removeRecipe.invoke(recipeMap, Reflections.method$CraftRecipe$toMinecraft.invoke(null, key));
}
for (NamespacedKey key : this.registeredCustomRecipes) {
Reflections.method$RecipeMap$removeRecipe.invoke(recipeMap, Reflections.method$CraftRecipe$toMinecraft.invoke(null, key));
}
Reflections.method$RecipeManager$finalizeRecipeLoading.invoke(recipeManager);
} catch (ReflectiveOperationException e) {
plugin.logger().warn("Failed to unload custom recipes", e);
}
} else {
for (NamespacedKey key : this.injectedDataPackRecipes) {
Bukkit.removeRecipe(key);
}
for (NamespacedKey key : this.registeredCustomRecipes) {
Bukkit.removeRecipe(key);
}
}
this.registeredCustomRecipes.clear();
this.injectedDataPackRecipes.clear();
}
@Override

View File

@@ -10,6 +10,7 @@ import io.netty.handler.codec.MessageToByteEncoder;
import net.momirealms.craftengine.core.util.ReflectionUtils;
import net.momirealms.craftengine.core.util.VersionHelper;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
@@ -3796,4 +3797,44 @@ public class Reflections {
clazz$InventoryView, HumanEntity.class
)
);
public static final Class<?> clazz$RecipeManager = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("world.item.crafting.RecipeManager"),
BukkitReflectionUtils.assembleMCClass("world.item.crafting.CraftingManager")
)
);
public static final Method method$RecipeManager$finalizeRecipeLoading =
ReflectionUtils.getMethod(
clazz$RecipeManager, new String[]{"finalizeRecipeLoading"}
);
public static final Method method$MinecraftServer$getRecipeManager = requireNonNull(
ReflectionUtils.getMethod(
clazz$MinecraftServer, clazz$RecipeManager
)
);
public static final Class<?> clazz$RecipeMap =
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("world.item.crafting.RecipeMap")
);
public static final Field field$RecipeManager$recipes = Optional.ofNullable(clazz$RecipeMap)
.map(it -> ReflectionUtils.getDeclaredField(clazz$RecipeManager, it, 0))
.orElse(null);
public static final Method method$RecipeMap$removeRecipe = Optional.ofNullable(clazz$RecipeMap)
.map(it -> ReflectionUtils.getMethod(it, boolean.class, clazz$ResourceKey))
.orElse(null);
public static final Class<?> clazz$CraftRecipe =
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleCBClass("inventory.CraftRecipe")
);
public static final Method method$CraftRecipe$toMinecraft = Optional.ofNullable(clazz$CraftRecipe)
.map(it -> ReflectionUtils.getStaticMethod(it, clazz$ResourceKey, NamespacedKey.class))
.orElse(null);
}

View File

@@ -169,6 +169,7 @@ public class PackManagerImpl implements PackManager {
}
private void loadConfigs() {
long o1 = System.nanoTime();
for (Pack pack : loadedPacks()) {
List<Path> files = FileUtils.getConfigsDeeply(pack.configurationFolder());
for (Path path : files) {
@@ -190,9 +191,11 @@ public class PackManagerImpl implements PackManager {
}
}
}
long o2 = System.nanoTime();
this.plugin.logger().info("Loaded packs. Took " + String.format("%.2f", ((o2 - o1) / 1_000_000.0)) + " ms");
for (Map.Entry<ConfigSectionParser, List<CachedConfig>> entry : this.cachedConfigs.entrySet()) {
ConfigSectionParser parser = entry.getKey();
this.plugin.logger().info("Loading config type: " + parser.sectionId());
long t1 = System.nanoTime();
for (CachedConfig cached : entry.getValue()) {
for (Map.Entry<String, Object> configEntry : cached.config().entrySet()) {
String key = configEntry.getKey();
@@ -211,6 +214,8 @@ public class PackManagerImpl implements PackManager {
}
}
}
long t2 = System.nanoTime();
this.plugin.logger().info("Loaded config type: " + parser.sectionId() + ". Took " + String.format("%.2f", ((t2 - t1) / 1_000_000.0)) + " ms");
}
}