Skip to content

Commit

Permalink
[1.10.13]修复新建物品文件时无法正确保存物品的问题,修复RegisteredListener性能问题
Browse files Browse the repository at this point in the history
  • Loading branch information
YufiriaMazenta committed Dec 2, 2024
1 parent 4fccd31 commit d6a97b2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import java.text.SimpleDateFormat
version = "1.10.12"
version = "1.10.13"

plugins {
`java-library`
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
crypticlibVer=1.8.0
crypticlibVer=1.8.2
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package com.github.yufiriamazenta.craftorithm.listener.hook;

import com.github.yufiriamazenta.craftorithm.Craftorithm;
import com.github.yufiriamazenta.craftorithm.config.PluginConfigs;
import com.github.yufiriamazenta.craftorithm.util.EventUtils;
import crypticlib.CrypticLibBukkit;
import crypticlib.MinecraftVersion;
import crypticlib.lifecycle.AutoTask;
import crypticlib.lifecycle.BukkitLifeCycleTask;
import crypticlib.lifecycle.LifeCycle;
import crypticlib.lifecycle.TaskRule;
import crypticlib.listener.EventListener;
import crypticlib.platform.Platform;
import crypticlib.util.ReflectionHelper;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockCookEvent;
import org.bukkit.event.block.CrafterCraftEvent;
import org.bukkit.event.inventory.*;
import org.bukkit.plugin.EventExecutor;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.TimedRegisteredListener;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@EventListener
@AutoTask(
Expand All @@ -28,7 +38,7 @@ public enum OtherPluginsListenerManager implements Listener, BukkitLifeCycleTask
private final Field executorField = ReflectionHelper.getDeclaredField(RegisteredListener.class, "executor");

public void convertOtherPluginsListeners() {
for (HandlerList handlerList : HandlerList.getHandlerLists()) {
for (HandlerList handlerList : getCraftEventHandlerLists()) {
for (RegisteredListener registeredListener : handlerList.getRegisteredListeners()) {
if (registeredListener.getPlugin().equals(Craftorithm.instance())) continue;
if (registeredListener instanceof RecipeCheckRegisteredListener || registeredListener instanceof RecipeCheckTimedRegisteredListener)
Expand Down Expand Up @@ -57,9 +67,29 @@ public EventExecutor getRegisteredListenerExecutor(RegisteredListener registered
return ReflectionHelper.getDeclaredFieldObj(executorField, registeredListener);
}

private List<HandlerList> getCraftEventHandlerLists() {
List<HandlerList> handlersLists = new ArrayList<>();
handlersLists.add(CraftItemEvent.getHandlerList());
handlersLists.add(PrepareItemCraftEvent.getHandlerList());
handlersLists.add(PrepareSmithingEvent.getHandlerList());
handlersLists.add(SmithItemEvent.getHandlerList());
handlersLists.add(FurnaceSmeltEvent.getHandlerList());
handlersLists.add(BlockCookEvent.getHandlerList());
if (CrypticLibBukkit.isPaper() && PluginConfigs.ENABLE_ANVIL_RECIPE.value()) {
handlersLists.add(PrepareAnvilEvent.getHandlerList());
}
if (MinecraftVersion.current().afterOrEquals(MinecraftVersion.V1_17_1)) {
handlersLists.add(FurnaceStartSmeltEvent.getHandlerList());
}
if (EventUtils.hasCrafterCraftEvent) {
handlersLists.add(CrafterCraftEvent.getHandlerList());
}
return handlersLists;
}

@Override
public void run(Plugin plugin, LifeCycle lifeCycle) {
convertOtherPluginsListeners();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.custom.AnvilRecipe;
import crypticlib.CrypticLibBukkit;
import crypticlib.platform.Platform;
import crypticlib.MinecraftVersion;
import crypticlib.util.ItemHelper;
import org.bukkit.NamespacedKey;
import org.bukkit.event.Event;
Expand All @@ -16,30 +16,33 @@

public class EventUtils {

private static Boolean hasCrafterEvent;
public static final Boolean hasCrafterCraftEvent;

static {
hasCrafterCraftEvent = getHasCrafterCraftEvent();
}

private static Boolean getHasCrafterCraftEvent() {
try {
Class.forName("org.bukkit.event.block.CrafterCraftEvent");
hasCrafterEvent = true;
return true;
} catch (ClassNotFoundException e) {
hasCrafterEvent = false;
return false;
}
}

public static boolean isCraftorithmRecipeEvent(Event event) {
Recipe recipe;
Recipe recipe = null;
switch (event) {
case CraftItemEvent craftItemEvent -> recipe = craftItemEvent.getRecipe();
case PrepareItemCraftEvent prepareItemCraftEvent -> recipe = prepareItemCraftEvent.getRecipe();
case PrepareSmithingEvent prepareSmithingEvent -> recipe = prepareSmithingEvent.getInventory().getRecipe();
case SmithItemEvent smithItemEvent -> recipe = smithItemEvent.getInventory().getRecipe();
case FurnaceStartSmeltEvent furnaceStartSmeltEvent -> recipe = furnaceStartSmeltEvent.getRecipe();
case FurnaceSmeltEvent furnaceSmeltEvent -> recipe = furnaceSmeltEvent.getRecipe();
case BlockCookEvent blockCookEvent -> recipe = blockCookEvent.getRecipe();
case PrepareAnvilEvent prepareAnvilEvent -> {
//因为只有paper及下游服务端才有这个问题,如果识别到是bukkit或者spigot,就不用处理
if (CrypticLibBukkit.platform().type().equals(Platform.PlatformType.BUKKIT)) {
if (!CrypticLibBukkit.isPaper()) {
return false;
}
if (!PluginConfigs.ENABLE_ANVIL_RECIPE.value())
Expand All @@ -52,20 +55,27 @@ public static boolean isCraftorithmRecipeEvent(Event event) {
AnvilRecipe anvilRecipe = RecipeManager.INSTANCE.matchAnvilRecipe(base, addition);
return anvilRecipe != null;
}
case null, default -> {
if (hasCrafterEvent) {
case null -> {
return false;
}
default -> {
if (MinecraftVersion.current().afterOrEquals(MinecraftVersion.V1_17_1)) {
if (event instanceof FurnaceStartSmeltEvent furnaceStartSmeltEvent) {
recipe = furnaceStartSmeltEvent.getRecipe();
}
}
if (hasCrafterCraftEvent) {
if (event instanceof CrafterCraftEvent crafterCraftEvent) {
recipe = crafterCraftEvent.getRecipe();
} else {
return false;
}
} else {
return false;
}
}
}
if (recipe == null) {
return false;
}
NamespacedKey recipeKey = RecipeManager.INSTANCE.getRecipeKey(recipe);
return recipeKey != null && recipeKey.getNamespace().equals(RecipeManager.INSTANCE.PLUGIN_RECIPE_NAMESPACE);
}

}
}

0 comments on commit d6a97b2

Please sign in to comment.