V1.4 更新数据驱动模式

This commit is contained in:
Armamem0t 2025-07-14 21:16:44 +08:00
parent 49eacad0d9
commit 187ca0de0e
Signed by: minglipro
GPG Key ID: 5F355A77B22AA93B
13 changed files with 246 additions and 249 deletions

71
README.MD Normal file
View File

@ -0,0 +1,71 @@
# EnchantmentDoNotConflict (附魔不冲突)
- 1.21 ++ 数据驱动模式的附魔
- 由于麻将大改附魔系统 现在变为了 数据包模式的 我是用内置数据类实现的不冲突 外置的是配置文件
## 让我们看看这个版本之后的配置文件
- 用户定义配置就行 ID 请前往 [zh.minecraft.wiki](https://zh.minecraft.wiki/w/Java%E7%89%88%E6%95%B0%E6%8D%AE%E5%80%BC#%E9%AD%94%E5%92%92)
- 配置文件位置 config/enchantmentdoesnotconflict.json
```json
{
"minecraft:damageEnchantment": [ //这个key是自定义的
"minecraft:sharpness", // 附魔ID 在一组的表示不冲突
"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"
]
}
```
- mod 开发者适配 Demo
```java
import com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.fabricmc.api.ModInitializer;
public class Mod implements ModInitializer {
@Override
public void onInitialize() {
Enchantment.registerAllowEnchantment(() -> {
Map<String, List<String>> map = new HashMap<>();
map.put("modId:enchantmentGroup",
List.of("modId:enchantmentId", "modId:enchantmentId1",
"modId:enchantmentId2"));
return map;
});
// 无限与经验修补 示例
Enchantment.registerAllowEnchantment(() -> {
Map<String, List<String>> map = new HashMap<>();
map.put("minecraft:infinityEnchantment", List.of("minecraft:infinity", "minecraft:mending"));
return map;
});
// 如果是注册一次的话则不需要 它会在服务器运行后运行你的表达式
// 如果是动态加载的话 请重载一下 它会运行你的表达式
// Enchantment.reload(); //这样也会同步到客户端
}
}
```
### 数据优先级
- 最最最低 原版
- 最最低 数据包
- 最低 配置文件
- 最高 最后注册的 AllowEnchantment

View File

@ -1,7 +1,7 @@
minecraft_version=1.21
yarn_mappings=build.9
loader_version=0.16.13
mod_version=1.3
mod_version=1.4
maven_group=com.mingliqiye.minecraft.enchantment.conflict
archives_base_name=enchantmentdoesnotconflict
fabric_version=0.102.0

View File

@ -1,13 +1,25 @@
package com.mingliqiye.minecraft.enchantment.conflict;
import com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment;
import com.mingliqiye.minecraft.enchantment.conflict.network.ConfigPayload;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.api.ModInitializer;
import net.minecraft.server.MinecraftServer;
public class Mod implements ModInitializer {
public static String MOD_ID = "enchantmentdoesnotconflict";
public static MinecraftServer server;
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register((s) -> {
server = s;
Enchantment.reload();
});
ConfigPayload.initializeServer();
}
public static MinecraftServer getServer() {
return server;
}
}

View File

@ -2,51 +2,57 @@ package com.mingliqiye.minecraft.enchantment.conflict.config;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.mojang.logging.LogUtils;
import net.fabricmc.loader.api.FabricLoader;
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;
import static com.mingliqiye.minecraft.enchantment.conflict.Mod.MOD_ID;
public class ModConfig {
private static final Path CONFIG_PATH =
FabricLoader.getInstance().getConfigDir().resolve(MOD_ID + ".json");
private static final Gson GSON =
new GsonBuilder().setPrettyPrinting().serializeNulls().create();
private static ModConfig instance;
private boolean allowDamageEnchantment = true;
private boolean allowInfinityEnchantment = true;
private boolean allowPiercingEnchantment = true;
private boolean allowProtectionEnchantment = true;
private static final Path CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve(MOD_ID + ".json");
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
public static final Type TYPE = new TypeToken<HashMap<String, ArrayList<String>>>() {
}.getType();
private static Map<String, List<String>> instance;
private static final Logger LOGGER = LogUtils.getLogger();
public ModConfig() {
}
public static ModConfig getInstance() {
public static Map<String, List<String>> getInstance() {
return ModConfig.instance;
}
public static void setInstance(ModConfig instance) {
public static void reload() {
load();
}
public static void setInstance(Map<String, List<String>> instance) {
ModConfig.instance = instance;
}
public static ModConfig load() {
public static Map<String, List<String>> load() {
try {
if (Files.exists(CONFIG_PATH)) {
return GSON.fromJson(Files.newBufferedReader(CONFIG_PATH),
ModConfig.class);
return GSON.fromJson(Files.newBufferedReader(CONFIG_PATH), TYPE);
}
} catch (IOException e) {
e.printStackTrace();
}
ModConfig modConfig = new ModConfig();
Map<String, List<String>> modConfig = getDefData();
setInstance(modConfig);
save();
return modConfig;
@ -60,50 +66,24 @@ public class ModConfig {
}
}
public static Map<String, List<String>> 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);
}
}
static {
setInstance(load());
}
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 String toString() {
return this.getClass().getName() + "(" + "allowDamageEnchantment" +
"=" + allowDamageEnchantment + "," +
"allowInfinityEnchantment" + "=" + allowInfinityEnchantment +
"," + "allowPiercingEnchantment" + "=" +
allowPiercingEnchantment + "," +
"allowProtectionEnchantment" + "=" +
allowProtectionEnchantment + ")";
}
public boolean isAllowProtectionEnchantment() {
return allowProtectionEnchantment;
}
public void setAllowProtectionEnchantment(boolean allowProtectionEnchantment) {
this.allowProtectionEnchantment = allowProtectionEnchantment;
}
}

View File

@ -0,0 +1,9 @@
package com.mingliqiye.minecraft.enchantment.conflict.enchantment;
import java.util.List;
import java.util.Map;
@FunctionalInterface
public interface AddAllowEnchantmentFunInf {
Map<String,List<String>> call();
}

View File

@ -1,37 +0,0 @@
package com.mingliqiye.minecraft.enchantment.conflict.enchantment;
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
import java.util.Arrays;
import java.util.List;
public class DamageEnchantment extends Enchantment {
private final String id;
public DamageEnchantment(String id) {
this.id = id;
}
private final static List<String> enchantmentIds =
Arrays.asList("minecraft:sharpness", "minecraft:smite", "minecraft:bane_of_arthropods", "minecraft:density",
"minecraft:breach");
public static DamageEnchantment ofId(String id) {
if (enchantmentIds.contains(id)) {
return new DamageEnchantment(id);
}
return null;
}
public Boolean canBeCombined(Enchantment enchantment) {
if (enchantment instanceof DamageEnchantment) {
return ModConfig.getInstance().isAllowDamageEnchantment();
}
return super.canBeCombined(enchantment);
}
public String getId() {
return id;
}
}

View File

@ -1,25 +1,84 @@
package com.mingliqiye.minecraft.enchantment.conflict.enchantment;
public abstract class Enchantment {
public Boolean canBeCombined(Enchantment enchantment) {
import com.mingliqiye.minecraft.enchantment.conflict.Mod;
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.minecraft.server.network.ServerPlayerEntity;
import org.slf4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Enchantment {
private static final Logger LOGGER = LogUtils.getLogger();
private final String id;
private final String father;
private final static List<AddAllowEnchantmentFunInf> allowEnchantmentFunInfs = new ArrayList<>();
public static void registerAllowEnchantment(AddAllowEnchantmentFunInf fun) {
allowEnchantmentFunInfs.add(fun);
}
public static void reload() {
ModConfig.reload();
allowEnchantmentFunInfs.forEach(e -> {
Map<String, List<String>> conf = ModConfig.getInstance();
conf.putAll(e.call());
ModConfig.setInstance(conf);
});
MinecraftServer server = Mod.getServer();
if (server != null) {
server.getPlayerManager().getPlayerList().forEach(ConfigPayload::sendToPlayer);
}
LOGGER.info("reload {} Ok Entity: \n{}", Mod.MOD_ID, ModConfig.getInstance());
}
public Enchantment(String id, String father) {
this.id = id;
this.father = father;
}
public static List<Enchantment> ofId(String id, Map<String, List<String>> listMap) {
final List<Enchantment> 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<String, List<String>> config = ModConfig.getInstance();
List<Enchantment> groupsA = ofId(enchantmentA, config);
List<Enchantment> groupsB = ofId(enchantmentB, config);
if (groupsA.isEmpty() && groupsB.isEmpty()) {
return true;
}
for (Enchantment groupA : groupsA) {
for (Enchantment groupB : groupsB) {
if (groupA.getFather().equals(groupB.getFather())) {
return true;
}
}
}
return null;
}
abstract String getId();
public String getId() {
return id;
}
public static Enchantment ofId(String id) {
DamageEnchantment damageEnchantment = DamageEnchantment.ofId(id);
if (damageEnchantment != null) {
return damageEnchantment;
}
InfinityEnchantment infinityEnchantment = InfinityEnchantment.ofId(id);
if (infinityEnchantment != null) {
return infinityEnchantment;
}
PiercingEnchantment piercingEnchantment = PiercingEnchantment.ofId(id);
if (piercingEnchantment != null) {
return piercingEnchantment;
}
return ProtectionEnchantment.ofId(id);
public String getFather() {
return father;
}
}

View File

@ -1,37 +0,0 @@
package com.mingliqiye.minecraft.enchantment.conflict.enchantment;
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
import java.util.Arrays;
import java.util.List;
public class InfinityEnchantment extends Enchantment {
private final String id;
private InfinityEnchantment(String id) {
this.id = id;
}
private static final List<String> enchantmentIds =
Arrays.asList("minecraft:infinity","minecraft:mending");
public static InfinityEnchantment ofId(String id) {
if (enchantmentIds.contains(id)) {
return new InfinityEnchantment(id);
}
return null;
}
@Override
public Boolean canBeCombined(Enchantment enchantment) {
if (enchantment instanceof InfinityEnchantment) {
return ModConfig.getInstance().isAllowInfinityEnchantment();
}
return super.canBeCombined(enchantment);
}
@Override
public String getId() {
return id;
}
}

View File

@ -1,37 +0,0 @@
package com.mingliqiye.minecraft.enchantment.conflict.enchantment;
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
import java.util.Arrays;
import java.util.List;
public class PiercingEnchantment extends Enchantment {
private final String id;
private PiercingEnchantment(String id) {
this.id = id;
}
private static final List<String> enchantmentIds =
Arrays.asList("minecraft:piercing","minecraft:multishot");
public static PiercingEnchantment ofId(String id) {
if (enchantmentIds.contains(id)) {
return new PiercingEnchantment(id);
}
return null;
}
@Override
public Boolean canBeCombined(Enchantment enchantment) {
if (enchantment instanceof PiercingEnchantment) {
return ModConfig.getInstance().isAllowPiercingEnchantment();
}
return super.canBeCombined(enchantment);
}
@Override
public String getId() {
return id;
}
}

View File

@ -1,42 +0,0 @@
package com.mingliqiye.minecraft.enchantment.conflict.enchantment;
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
import java.util.Arrays;
import java.util.List;
public class ProtectionEnchantment extends Enchantment {
private final String id;
private ProtectionEnchantment(String id) {
this.id = id;
}
private static final List<String> enchantmentIds =
Arrays.asList(
"minecraft:protection",
"minecraft:fire_protection",
"minecraft:blast_protection",
"minecraft:projectile_protection"
);
public static ProtectionEnchantment ofId(String id) {
if (enchantmentIds.contains(id)) {
return new ProtectionEnchantment(id);
}
return null;
}
@Override
public Boolean canBeCombined(Enchantment enchantment) {
if (enchantment instanceof ProtectionEnchantment) {
return ModConfig.getInstance().isAllowProtectionEnchantment();
}
return super.canBeCombined(enchantment);
}
@Override
public String getId() {
return id;
}
}

View File

@ -1,33 +1,25 @@
package com.mingliqiye.minecraft.enchantment.conflict.mixin;
import com.mojang.logging.LogUtils;
import net.minecraft.enchantment.Enchantment;
import com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment;
import net.minecraft.registry.entry.RegistryEntry;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(Enchantment.class)
@Mixin(net.minecraft.enchantment.Enchantment.class)
public abstract class EnchantmentMixin {
@Unique
private static final Logger LOGGER = LogUtils.getLogger();
@Inject(method = "canBeCombined", at = @At("HEAD"), cancellable = true)
private static void canBeCombined(
RegistryEntry<Enchantment> first, RegistryEntry<Enchantment> second,
CallbackInfoReturnable<Boolean> cir
RegistryEntry<net.minecraft.enchantment.Enchantment> first,
RegistryEntry<net.minecraft.enchantment.Enchantment> second, CallbackInfoReturnable<Boolean> cir
) {
com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment firste =
com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment.ofId(first.getIdAsString());
com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment seconde =
com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment.ofId(second.getIdAsString());
if (firste == null || seconde == null) {
Boolean cbc = Enchantment.canBeCombined(first.getIdAsString(), second.getIdAsString());
if (cbc == null) {
return;
}
cir.setReturnValue(firste.canBeCombined(seconde));
cir.setReturnValue(cbc);
}
}

View File

@ -3,6 +3,7 @@ package com.mingliqiye.minecraft.enchantment.conflict.network;
import com.google.gson.Gson;
import com.mingliqiye.minecraft.enchantment.conflict.Mod;
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
import com.mingliqiye.minecraft.enchantment.conflict.enchantment.Enchantment;
import com.mojang.logging.LogUtils;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
@ -14,9 +15,12 @@ import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import java.util.List;
import java.util.Map;
import static com.mingliqiye.minecraft.enchantment.conflict.Mod.MOD_ID;
public record ConfigPayload(ModConfig data) implements CustomPayload {
public record ConfigPayload(Map<String, List<String>> data) implements CustomPayload {
private static final Logger LOGGER = LogUtils.getLogger();
public static final CustomPayload.Id<ConfigPayload> TYPE =
@ -27,7 +31,7 @@ public record ConfigPayload(ModConfig data) implements CustomPayload {
public static final PacketCodec<PacketByteBuf, ConfigPayload> CODEC =
PacketCodec.of((payload, buf) -> buf.writeString(GSON.toJson(payload.data())),
buf -> new ConfigPayload(GSON.fromJson(buf.readString(), ModConfig.class)));
buf -> new ConfigPayload(GSON.fromJson(buf.readString(), ModConfig.TYPE)));
@Override
public Id<? extends CustomPayload> getId() {

View File

@ -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"
]
}