From 3fc4268b76093324a656010cb1a047f9c822be42 Mon Sep 17 00:00:00 2001 From: minglipro Date: Tue, 15 Jul 2025 17:02:32 +0800 Subject: [PATCH] add 1.20 --- build.gradle | 5 +- gradle.properties | 17 +-- .../EnchantmentDoNotConflictNeoForge.java | 13 +- .../conflict/config/ModConfig.java | 127 +++++++++--------- .../AddAllowEnchantmentFunInf.java | 9 ++ .../conflict/enchantment/Enchantment.java | 82 +++++++++++ .../mixin/ArrowInfiniteEnchantmentMixin.java | 26 ---- .../mixin/ArrowPiercingEnchantmentMixin.java | 26 ---- .../mixin/DamageEnchantmentMixin.java | 26 ---- .../conflict/mixin/EnchantmentMixin.java | 25 ++++ .../mixin/ProtectionEnchantmentMixin.java | 26 ---- .../conflict/network/ConfigPayload.java | 76 +++++++++++ .../EnchantmentDoNotConflict.json | 23 ++++ .../enchantmentdoesnotconflict.mixins.json | 5 +- 14 files changed, 302 insertions(+), 184 deletions(-) create mode 100644 src/main/java/com/mingliqiye/minecraft/enchantment/conflict/enchantment/AddAllowEnchantmentFunInf.java create mode 100644 src/main/java/com/mingliqiye/minecraft/enchantment/conflict/enchantment/Enchantment.java delete mode 100644 src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ArrowInfiniteEnchantmentMixin.java delete mode 100644 src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ArrowPiercingEnchantmentMixin.java delete mode 100644 src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/DamageEnchantmentMixin.java create mode 100644 src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/EnchantmentMixin.java delete mode 100644 src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ProtectionEnchantmentMixin.java create mode 100644 src/main/java/com/mingliqiye/minecraft/enchantment/conflict/network/ConfigPayload.java create mode 100644 src/main/resources/assets/enchantmentdoesnotconflict/EnchantmentDoNotConflict.json diff --git a/build.gradle b/build.gradle index f3c6030..1aa3894 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,8 @@ runs { // You can set various levels here. // Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels systemProperty 'forge.logging.console.level', 'info' + jvmArgument "--add-opens=java.base/java.lang.invoke=ALL-UNNAMED" + jvmArgument "--add-opens=java.base/java.util.jar=ALL-UNNAMED" modSource project.sourceSets.main } @@ -154,7 +156,8 @@ publishing { } repositories { maven { - url "file://${project.projectDir}/repo" + name= "Disk" + url = uri("D:/git/maven-repository-raw") } } } diff --git a/gradle.properties b/gradle.properties index 3d6cdbe..ee2527b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,18 +1,13 @@ -org.gradle.jvmargs=-Xmx1G -org.gradle.daemon=true -org.gradle.parallel=true -org.gradle.caching=true -org.gradle.configuration-cache=true -neogradle.subsystems.parchment.minecraftVersion=1.20.4 -neogradle.subsystems.parchment.mappingsVersion=2024.04.14 -minecraft_version=1.20.5 -minecraft_version_range=[1.20.5,1.21) -neo_version=20.5.21-beta +neogradle.subsystems.parchment.minecraftVersion=1.21 +neogradle.subsystems.parchment.mappingsVersion=2024.11.10 +minecraft_version=1.21 +minecraft_version_range=[1.21,1.22) +neo_version=21.0.167 loader_version_range=[1,) mod_id=enchantmentdoesnotconflict mod_name=EnchantmentDoNotConflictNeoForge mod_license=Apache License 2.0 -mod_version=1.0 +mod_version=1.1 mod_group_id=com.mingliqiye.minecraft.enchantment.conflict mod_authors=minglipro mod_description= Any Enchantment Now is not Conflict diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/EnchantmentDoNotConflictNeoForge.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/EnchantmentDoNotConflictNeoForge.java index 1ec7b84..36c2c35 100644 --- a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/EnchantmentDoNotConflictNeoForge.java +++ b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/EnchantmentDoNotConflictNeoForge.java @@ -1,7 +1,9 @@ package com.mingliqiye.minecraft.enchantment.conflict; +import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig; +import com.mingliqiye.minecraft.enchantment.conflict.network.ConfigPayload; +import net.minecraft.server.MinecraftServer; import net.neoforged.fml.ModContainer; -import static net.neoforged.fml.config.ModConfig.Type.COMMON; import org.slf4j.Logger; import com.mojang.logging.LogUtils; @@ -15,25 +17,28 @@ import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.server.ServerStartingEvent; import net.neoforged.fml.common.EventBusSubscriber; -import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig; + @Mod(EnchantmentDoNotConflictNeoForge.MODID) public class EnchantmentDoNotConflictNeoForge { public static final String MODID = "enchantmentdoesnotconflict"; public static final Logger LOGGER = LogUtils.getLogger(); + public static MinecraftServer minecraftServer; public EnchantmentDoNotConflictNeoForge(IEventBus modEventBus, ModContainer container) { - container.registerConfig(COMMON, ModConfig.SPEC); modEventBus.addListener(this::commonSetup); + modEventBus.addListener(ConfigPayload::register); + NeoForge.EVENT_BUS.addListener(ConfigPayload::onPlayerLogin); NeoForge.EVENT_BUS.register(this); } private void commonSetup(FMLCommonSetupEvent event) { LOGGER.info("Mod {} Setup", MODID); - ModConfig.load(); } // 服务端启动时执行 @SubscribeEvent public void onServerStarting(ServerStartingEvent event) { + minecraftServer = event.getServer(); + ModConfig.setInstance(ModConfig.load()); LOGGER.info("Server Mod {} Setup", MODID); } diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/config/ModConfig.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/config/ModConfig.java index 46bb9f9..63eddc1 100644 --- a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/config/ModConfig.java +++ b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/config/ModConfig.java @@ -1,78 +1,85 @@ package com.mingliqiye.minecraft.enchantment.conflict.config; -import net.neoforged.neoforge.common.ModConfigSpec; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import com.mingliqiye.minecraft.enchantment.conflict.EnchantmentDoNotConflictNeoForge; +import com.mojang.logging.LogUtils; +import net.neoforged.fml.loading.FMLPaths; +import org.slf4j.Logger; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Type; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class ModConfig { - private static final ModConfigSpec.Builder BUILDER = new ModConfigSpec.Builder(); - public static final ModConfigSpec.BooleanValue AllowDamageEnchantment = - BUILDER.define("AllowDamageEnchantment", true); - public static final ModConfigSpec.BooleanValue AllowInfinityEnchantment = - BUILDER.define("AllowInfinityEnchantment", true); - public static final ModConfigSpec.BooleanValue AllowPiercingEnchantment = - BUILDER.define("AllowPiercingEnchantment", true); - public static final ModConfigSpec.BooleanValue AllowProtectionEnchantment = - BUILDER.define("AllowProtectionEnchantment", true); - public static final ModConfigSpec SPEC = BUILDER.build(); + private final static Path CONFIG_PATH = FMLPaths.CONFIGDIR.get().resolve(EnchantmentDoNotConflictNeoForge.MODID + + ".json"); + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().create(); + public static final Type TYPE = new TypeToken>>() { + }.getType(); + private static Map> instance; + private static final Logger LOGGER = LogUtils.getLogger(); - private static ConfigItem instance; - - public static ConfigItem getInstance() { - return instance; + public ModConfig() { } - public static void load() { - instance = new ConfigItem(AllowDamageEnchantment.get(), AllowInfinityEnchantment.get(), - AllowPiercingEnchantment.get(), AllowProtectionEnchantment.get()); + public static Map> getInstance() { + return ModConfig.instance; } - public static class ConfigItem { - private boolean allowDamageEnchantment; - private boolean allowInfinityEnchantment; - private boolean allowPiercingEnchantment; - private boolean allowProtectionEnchantment; + public static void reload() { + load(); + } - ConfigItem() { + public static void setInstance(Map> instance) { + ModConfig.instance = instance; + } + + public static Map> load() { + try { + if (Files.exists(CONFIG_PATH)) { + return GSON.fromJson(Files.newBufferedReader(CONFIG_PATH), TYPE); + } + } catch (IOException e) { + e.printStackTrace(); } + Map> modConfig = getDefData(); + setInstance(modConfig); + save(); + return modConfig; + } - ConfigItem(boolean a, boolean b, boolean c, boolean d) { - this.allowDamageEnchantment = a; - this.allowInfinityEnchantment = b; - this.allowPiercingEnchantment = c; - this.allowProtectionEnchantment = d; - + public static void save() { + try { + Files.writeString(CONFIG_PATH, GSON.toJson(instance)); + } catch (IOException e) { + e.printStackTrace(); } + } - public boolean isAllowDamageEnchantment() { - return allowDamageEnchantment; - } - - public void setAllowDamageEnchantment(boolean allowDamageEnchantment) { - this.allowDamageEnchantment = allowDamageEnchantment; - } - - public boolean isAllowInfinityEnchantment() { - return allowInfinityEnchantment; - } - - public void setAllowInfinityEnchantment(boolean allowInfinityEnchantment) { - this.allowInfinityEnchantment = allowInfinityEnchantment; - } - - public boolean isAllowPiercingEnchantment() { - return allowPiercingEnchantment; - } - - public void setAllowPiercingEnchantment(boolean allowPiercingEnchantment) { - this.allowPiercingEnchantment = allowPiercingEnchantment; - } - - public boolean isAllowProtectionEnchantment() { - return allowProtectionEnchantment; - } - - public void setAllowProtectionEnchantment(boolean allowProtectionEnchantment) { - this.allowProtectionEnchantment = allowProtectionEnchantment; + public static Map> getDefData() { + try { + InputStream inputStream = ModConfig.class.getResourceAsStream( + "/assets/enchantmentdoesnotconflict" + "/EnchantmentDoNotConflict.json"); + if (inputStream != null) { + try (InputStreamReader reader = new InputStreamReader(inputStream)) { + return GSON.fromJson(reader, TYPE); + } + } else { + throw new RuntimeException("resources 文件内 没有 EnchantmentDoNotConflict.json?"); + } + } catch (RuntimeException | IOException e) { + LOGGER.error(e.getMessage(), e); + throw new RuntimeException(e); } } } diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/enchantment/AddAllowEnchantmentFunInf.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/enchantment/AddAllowEnchantmentFunInf.java new file mode 100644 index 0000000..a2c635b --- /dev/null +++ b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/enchantment/AddAllowEnchantmentFunInf.java @@ -0,0 +1,9 @@ +package com.mingliqiye.minecraft.enchantment.conflict.enchantment; + +import java.util.List; +import java.util.Map; + +@FunctionalInterface +public interface AddAllowEnchantmentFunInf { + Map> call(); +} diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/enchantment/Enchantment.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/enchantment/Enchantment.java new file mode 100644 index 0000000..12797cd --- /dev/null +++ b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/enchantment/Enchantment.java @@ -0,0 +1,82 @@ +package com.mingliqiye.minecraft.enchantment.conflict.enchantment; + +import com.mingliqiye.minecraft.enchantment.conflict.EnchantmentDoNotConflictNeoForge; +import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig; +import com.mingliqiye.minecraft.enchantment.conflict.network.ConfigPayload; +import com.mojang.logging.LogUtils; +import net.minecraft.server.MinecraftServer; +import net.neoforged.neoforge.network.PacketDistributor; +import org.slf4j.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static com.mingliqiye.minecraft.enchantment.conflict.network.ConfigPayload.sendToAllPlayers; + +public class Enchantment { + private static final Logger LOGGER = LogUtils.getLogger(); + private final String id; + private final String father; + private final static List allowEnchantmentFunInfs = new ArrayList<>(); + + public static void registerAllowEnchantment(AddAllowEnchantmentFunInf fun) { + allowEnchantmentFunInfs.add(fun); + } + + public static void reload() { + ModConfig.reload(); + allowEnchantmentFunInfs.forEach(e -> { + Map> conf = ModConfig.getInstance(); + conf.putAll(e.call()); + ModConfig.setInstance(conf); + }); + sendToAllPlayers(); + LOGGER.info("reload {} Ok Entity: \n{}", EnchantmentDoNotConflictNeoForge.MODID, ModConfig.getInstance()); + } + + public Enchantment(String id, String father) { + this.id = id; + this.father = father; + } + + public static List ofId(String id, Map> listMap) { + final List enchantmentList = new ArrayList<>(); + for (String i : listMap.keySet()) { + if (listMap.get(i).contains(id)) { + enchantmentList.add(new Enchantment(id, i)); + } + } + return enchantmentList; + } + + public static Boolean canBeCombined(String enchantmentA, String enchantmentB) { + if (enchantmentA.equals(enchantmentB)) { + return true; + } + Map> config = ModConfig.getInstance(); + List groupsA = ofId(enchantmentA, config); + List groupsB = ofId(enchantmentB, config); + + if (groupsA.isEmpty() && groupsB.isEmpty()) { + return null; + } + + for (Enchantment groupA : groupsA) { + for (Enchantment groupB : groupsB) { + if (groupA.getFather().equals(groupB.getFather())) { + return true; + } + } + } + return null; + } + + public String getId() { + return id; + } + + public String getFather() { + return father; + } +} diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ArrowInfiniteEnchantmentMixin.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ArrowInfiniteEnchantmentMixin.java deleted file mode 100644 index b603bbd..0000000 --- a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ArrowInfiniteEnchantmentMixin.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mingliqiye.minecraft.enchantment.conflict.mixin; - -import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig; -import net.minecraft.world.item.enchantment.Enchantment; -import net.minecraft.world.item.enchantment.ArrowInfiniteEnchantment; -import net.minecraft.world.item.enchantment.MendingEnchantment; -import org.jetbrains.annotations.NotNull; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Mixin(ArrowInfiniteEnchantment.class) -public abstract class ArrowInfiniteEnchantmentMixin { - - @Inject(method = "checkCompatibility", at = @At("HEAD"), cancellable = true) - private void canAccept( - @NotNull Enchantment enchantment, - @NotNull CallbackInfoReturnable cir - ) { - if (enchantment instanceof MendingEnchantment) { - cir.setReturnValue(ModConfig.getInstance().isAllowInfinityEnchantment()); - } - } -} - diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ArrowPiercingEnchantmentMixin.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ArrowPiercingEnchantmentMixin.java deleted file mode 100644 index a57f845..0000000 --- a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ArrowPiercingEnchantmentMixin.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mingliqiye.minecraft.enchantment.conflict.mixin; - - -import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig; -import net.minecraft.world.item.enchantment.Enchantment; -import net.minecraft.world.item.enchantment.Enchantments; -import net.minecraft.world.item.enchantment.ArrowPiercingEnchantment; -import org.jetbrains.annotations.NotNull; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Mixin(ArrowPiercingEnchantment.class) -public abstract class ArrowPiercingEnchantmentMixin { - - @Inject(method = "checkCompatibility", at = @At("HEAD"), cancellable = true) - private void canAccept( - @NotNull Enchantment enchantment, - @NotNull CallbackInfoReturnable cir - ) { - if (enchantment == Enchantments.MULTISHOT) { - cir.setReturnValue(ModConfig.getInstance().isAllowPiercingEnchantment()); - } - } -} diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/DamageEnchantmentMixin.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/DamageEnchantmentMixin.java deleted file mode 100644 index 07a45aa..0000000 --- a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/DamageEnchantmentMixin.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mingliqiye.minecraft.enchantment.conflict.mixin; - - -import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig; -import net.minecraft.world.item.enchantment.DamageEnchantment; -import net.minecraft.world.item.enchantment.Enchantment; -import org.jetbrains.annotations.NotNull; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Mixin(DamageEnchantment.class) -public abstract class DamageEnchantmentMixin { - - @Inject(method = "checkCompatibility", at = @At("HEAD"), cancellable = true) - private void canAccept( - @NotNull Enchantment enchantment, - @NotNull CallbackInfoReturnable cir - ) { - if (enchantment instanceof DamageEnchantment) { - cir.setReturnValue(ModConfig.getInstance().isAllowDamageEnchantment()); - } - } - -} diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/EnchantmentMixin.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/EnchantmentMixin.java new file mode 100644 index 0000000..93a4a89 --- /dev/null +++ b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/EnchantmentMixin.java @@ -0,0 +1,25 @@ +package com.mingliqiye.minecraft.enchantment.conflict.mixin; + +import com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment; +import net.minecraft.core.Holder; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(net.minecraft.world.item.enchantment.Enchantment.class) +public abstract class EnchantmentMixin { + + + @Inject(method = "areCompatible", at = @At("HEAD"), cancellable = true) + private static void canBeCombined( + Holder first, + Holder second, CallbackInfoReturnable cir + ) { + Boolean cbc = Enchantment.canBeCombined(first.getRegisteredName(), second.getRegisteredName()); + if (cbc == null) { + return; + } + cir.setReturnValue(cbc); + } +} diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ProtectionEnchantmentMixin.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ProtectionEnchantmentMixin.java deleted file mode 100644 index 8414b5f..0000000 --- a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/mixin/ProtectionEnchantmentMixin.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.mingliqiye.minecraft.enchantment.conflict.mixin; - - -import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig; -import net.minecraft.world.item.enchantment.Enchantment; -import net.minecraft.world.item.enchantment.ProtectionEnchantment; -import org.jetbrains.annotations.NotNull; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Mixin(ProtectionEnchantment.class) -public abstract class ProtectionEnchantmentMixin { - - @Inject(method = "checkCompatibility", at = @At("HEAD"), cancellable = true) - - private void onCheckCompatibility( - @NotNull Enchantment enchantment, - @NotNull CallbackInfoReturnable cir - ) { - if (enchantment instanceof ProtectionEnchantment) { - cir.setReturnValue(ModConfig.getInstance().isAllowProtectionEnchantment()); - } - } -} diff --git a/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/network/ConfigPayload.java b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/network/ConfigPayload.java new file mode 100644 index 0000000..a6534c6 --- /dev/null +++ b/src/main/java/com/mingliqiye/minecraft/enchantment/conflict/network/ConfigPayload.java @@ -0,0 +1,76 @@ +package com.mingliqiye.minecraft.enchantment.conflict.network; + +import com.google.gson.Gson; +import com.mingliqiye.minecraft.enchantment.conflict.EnchantmentDoNotConflictNeoForge; +import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig; +import com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment; +import com.mojang.logging.LogUtils; +import io.netty.buffer.ByteBuf; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.neoforge.event.entity.player.PlayerEvent; +import net.neoforged.neoforge.network.PacketDistributor; +import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; +import net.neoforged.neoforge.network.registration.PayloadRegistrar; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; + +import java.util.List; +import java.util.Map; + +public record ConfigPayload(Map> data) implements CustomPacketPayload { + + private static final Logger LOGGER = LogUtils.getLogger(); + public static final CustomPacketPayload.Type TYPE = new CustomPacketPayload.Type<>( + ResourceLocation.fromNamespaceAndPath(EnchantmentDoNotConflictNeoForge.MODID, "config_packet")); + + private static final Gson GSON = new Gson(); + + + public static final StreamCodec STREAM_CODEC = + StreamCodec.composite(ByteBufCodecs.STRING_UTF8, (p) -> GSON.toJson(p.data()), + (s) -> new ConfigPayload(GSON.fromJson(s, ModConfig.TYPE))); + + public static void sendConfig(ServerPlayer player) { + Map> conf = ModConfig.getInstance(); + LOGGER.info("Send Server Config {} to {} data={}", EnchantmentDoNotConflictNeoForge.MODID, + player.getName().getString(), conf); + PacketDistributor.sendToPlayer(player, new ConfigPayload(conf)); + } + + public static void sendToAllPlayers() { + Map> conf = ModConfig.getInstance(); + LOGGER.info("Send Server Config {} to @a data={}", EnchantmentDoNotConflictNeoForge.MODID, conf); + PacketDistributor.sendToAllPlayers(new ConfigPayload(conf)); + } + + @SubscribeEvent + public static void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) { + if (event.getEntity() instanceof ServerPlayer player) { + sendConfig(player); + } + + } + + @SubscribeEvent + public static void register(final RegisterPayloadHandlersEvent event) { + PayloadRegistrar registrar = event.registrar(EnchantmentDoNotConflictNeoForge.MODID).versioned("1.1"); + registrar.playToClient(ConfigPayload.TYPE, ConfigPayload.STREAM_CODEC, (payload, context) -> { + context.enqueueWork(() -> { + ModConfig.setInstance(payload.data()); + LOGGER.info("Load Server Config {} data={}", EnchantmentDoNotConflictNeoForge.MODID, payload.data()); + }); + }); + } + + @Override + @NotNull + public CustomPacketPayload.Type type() { + return TYPE; + } +} diff --git a/src/main/resources/assets/enchantmentdoesnotconflict/EnchantmentDoNotConflict.json b/src/main/resources/assets/enchantmentdoesnotconflict/EnchantmentDoNotConflict.json new file mode 100644 index 0000000..a307151 --- /dev/null +++ b/src/main/resources/assets/enchantmentdoesnotconflict/EnchantmentDoNotConflict.json @@ -0,0 +1,23 @@ +{ + "minecraft:damageEnchantment": [ + "minecraft:sharpness", + "minecraft:smite", + "minecraft:bane_of_arthropods", + "minecraft:density", + "minecraft:breach" + ], + "minecraft:infinityEnchantment": [ + "minecraft:infinity", + "minecraft:mending" + ], + "minecraft:piercingEnchantment": [ + "minecraft:piercing", + "minecraft:multishot" + ], + "minecraft:protectionEnchantment": [ + "minecraft:protection", + "minecraft:fire_protection", + "minecraft:blast_protection", + "minecraft:projectile_protection" + ] +} diff --git a/src/main/resources/enchantmentdoesnotconflict.mixins.json b/src/main/resources/enchantmentdoesnotconflict.mixins.json index 6a54408..23f3341 100644 --- a/src/main/resources/enchantmentdoesnotconflict.mixins.json +++ b/src/main/resources/enchantmentdoesnotconflict.mixins.json @@ -3,10 +3,7 @@ "package": "com.mingliqiye.minecraft.enchantment.conflict.mixin", "compatibilityLevel": "JAVA_21", "mixins": [ - "ArrowInfiniteEnchantmentMixin", - "ArrowPiercingEnchantmentMixin", - "DamageEnchantmentMixin", - "ProtectionEnchantmentMixin" + "EnchantmentMixin" ], "injectors": { "defaultRequire": 1