mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-30 04:19:27 +00:00
盔甲重构part4
This commit is contained in:
@@ -137,6 +137,7 @@ public class BukkitFurniture implements Furniture {
|
||||
@NotNull
|
||||
public Object spawnPacket(Player player) {
|
||||
// TODO hasPermission might be slow, can we use a faster way in the future?
|
||||
// TODO Make it based on conditions. So we can dynamically control which furniture should be sent to the player
|
||||
if (!this.minimized || player.hasPermission(FurnitureManager.FURNITURE_ADMIN_NODE)) {
|
||||
return this.cachedSpawnPacket;
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package net.momirealms.craftengine.bukkit.item;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import net.momirealms.craftengine.bukkit.item.behavior.AxeItemBehavior;
|
||||
import net.momirealms.craftengine.bukkit.item.behavior.FlintAndSteelItemBehavior;
|
||||
import net.momirealms.craftengine.bukkit.item.factory.BukkitItemFactory;
|
||||
@@ -23,6 +27,7 @@ import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigExce
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
import net.momirealms.craftengine.core.registry.Holder;
|
||||
import net.momirealms.craftengine.core.registry.WritableRegistry;
|
||||
import net.momirealms.craftengine.core.util.GsonHelper;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceKey;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
@@ -34,6 +39,9 @@ import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
public class BukkitItemManager extends AbstractItemManager<ItemStack> {
|
||||
@@ -143,7 +151,9 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
|
||||
protected void registerArmorTrimPattern(Collection<Key> equipments) {
|
||||
if (equipments.isEmpty()) return;
|
||||
this.lastRegisteredPatterns = new HashSet<>(equipments);
|
||||
this.lastRegisteredPatterns.add(Config.sacrificedAssetId());
|
||||
// 可能还没加载
|
||||
if (Config.sacrificedAssetId() != null)
|
||||
this.lastRegisteredPatterns.add(Config.sacrificedAssetId());
|
||||
Object registry = FastNMS.INSTANCE.method$RegistryAccess$lookupOrThrow(FastNMS.INSTANCE.registryAccess(), MRegistries.TRIM_PATTERN);
|
||||
try {
|
||||
CoreReflections.field$MappedRegistry$frozen.set(registry, false);
|
||||
@@ -168,9 +178,50 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
|
||||
}
|
||||
|
||||
private void persistLastRegisteredPatterns() {
|
||||
Path persistTrimPatternPath = this.plugin.dataFolderPath()
|
||||
.resolve("cache")
|
||||
.resolve("trim_patterns.json");
|
||||
try {
|
||||
Files.createDirectories(persistTrimPatternPath.getParent());
|
||||
JsonObject json = new JsonObject();
|
||||
JsonArray jsonElements = new JsonArray();
|
||||
for (Key key : this.lastRegisteredPatterns) {
|
||||
jsonElements.add(new JsonPrimitive(key.toString()));
|
||||
}
|
||||
json.add("patterns", jsonElements);
|
||||
if (jsonElements.isEmpty()) {
|
||||
if (Files.exists(persistTrimPatternPath)) {
|
||||
Files.delete(persistTrimPatternPath);
|
||||
}
|
||||
} else {
|
||||
GsonHelper.writeJsonFile(json, persistTrimPatternPath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
this.plugin.logger().warn("Failed to persist registered trim patterns.", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 需要持久化存储上一次注册的新trim类型,如果注册晚了,加载世界可能导致一些物品损坏
|
||||
private void loadLastRegisteredPatterns() {
|
||||
Path persistTrimPatternPath = this.plugin.dataFolderPath()
|
||||
.resolve("cache")
|
||||
.resolve("trim_patterns.json");
|
||||
if (Files.exists(persistTrimPatternPath) && Files.isRegularFile(persistTrimPatternPath)) {
|
||||
try {
|
||||
JsonObject cache = GsonHelper.readJsonFile(persistTrimPatternPath).getAsJsonObject();
|
||||
JsonArray patterns = cache.getAsJsonArray("patterns");
|
||||
Set<Key> trims = new HashSet<>();
|
||||
for (JsonElement element : patterns) {
|
||||
if (element instanceof JsonPrimitive primitive) {
|
||||
trims.add(Key.of(primitive.getAsString()));
|
||||
}
|
||||
}
|
||||
this.registerArmorTrimPattern(trims);
|
||||
this.lastRegisteredPatterns = trims;
|
||||
} catch (IOException e) {
|
||||
this.plugin.logger().warn("Failed to load registered trim patterns.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerCustomTrimMaterial() {
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
public class LegacyNetworkItemHandler implements NetworkItemHandler<ItemStack> {
|
||||
public final class LegacyNetworkItemHandler implements NetworkItemHandler<ItemStack> {
|
||||
|
||||
@Override
|
||||
public Optional<Item<ItemStack>> c2s(Item<ItemStack> wrapped) {
|
||||
|
||||
@@ -15,12 +15,14 @@ import net.momirealms.craftengine.bukkit.util.ComponentUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.ItemUtils;
|
||||
import net.momirealms.craftengine.bukkit.util.LegacyInventoryUtils;
|
||||
import net.momirealms.craftengine.core.item.*;
|
||||
import net.momirealms.craftengine.core.item.equipment.TrimBasedEquipment;
|
||||
import net.momirealms.craftengine.core.item.recipe.*;
|
||||
import net.momirealms.craftengine.core.item.recipe.Recipe;
|
||||
import net.momirealms.craftengine.core.item.recipe.input.CraftingInput;
|
||||
import net.momirealms.craftengine.core.item.recipe.input.SingleItemInput;
|
||||
import net.momirealms.craftengine.core.item.recipe.input.SmithingInput;
|
||||
import net.momirealms.craftengine.core.item.setting.AnvilRepairItem;
|
||||
import net.momirealms.craftengine.core.item.setting.ItemEquipment;
|
||||
import net.momirealms.craftengine.core.plugin.config.Config;
|
||||
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
|
||||
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
|
||||
@@ -933,6 +935,24 @@ public class RecipeEventListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onSmithingTrim(PrepareSmithingEvent event) {
|
||||
SmithingInventory inventory = event.getInventory();
|
||||
if (!(inventory.getRecipe() instanceof SmithingTrimRecipe)) return;
|
||||
ItemStack equipment = inventory.getInputEquipment();
|
||||
if (equipment == null) return;
|
||||
Item<ItemStack> wrappedEquipment = this.itemManager.wrap(equipment);
|
||||
Optional<CustomItem<ItemStack>> optionalCustomItem = wrappedEquipment.getCustomItem();
|
||||
if (optionalCustomItem.isEmpty()) return;
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
ItemEquipment itemEquipmentSettings = customItem.settings().equipment();
|
||||
if (itemEquipmentSettings == null) return;
|
||||
// 不允许trim类型的盔甲再次被使用trim
|
||||
if (itemEquipmentSettings.equipment() instanceof TrimBasedEquipment) {
|
||||
event.setResult(null);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onSmithingTransform(PrepareSmithingEvent event) {
|
||||
if (!Config.enableRecipeSystem()) return;
|
||||
|
||||
@@ -138,6 +138,7 @@ public class BukkitCraftEngine extends CraftEngine {
|
||||
super.onPluginLoad();
|
||||
super.blockManager.init();
|
||||
super.networkManager = new BukkitNetworkManager(this);
|
||||
super.itemManager = new BukkitItemManager(this);
|
||||
this.successfullyLoaded = true;
|
||||
super.compatibilityManager().onLoad();
|
||||
}
|
||||
@@ -182,7 +183,6 @@ public class BukkitCraftEngine extends CraftEngine {
|
||||
PacketConsumers.initEntities(RegistryUtils.currentEntityTypeRegistrySize());
|
||||
super.packManager = new BukkitPackManager(this);
|
||||
super.senderFactory = new BukkitSenderFactory(this);
|
||||
super.itemManager = new BukkitItemManager(this);
|
||||
super.recipeManager = new BukkitRecipeManager(this);
|
||||
super.commandManager = new BukkitCommandManager(this);
|
||||
super.itemBrowserManager = new ItemBrowserManagerImpl(this);
|
||||
|
||||
Reference in New Issue
Block a user