优化代码
This commit is contained in:
parent
a512e6de59
commit
e18ff9d26d
@ -3,11 +3,11 @@ plugins {
|
|||||||
id 'maven-publish'
|
id 'maven-publish'
|
||||||
}
|
}
|
||||||
|
|
||||||
version = project.mod_version
|
version = "${project.minecraft_version}-${project.mod_version}"
|
||||||
group = project.maven_group
|
group = project.maven_group
|
||||||
|
|
||||||
base {
|
base {
|
||||||
archivesName = "${project.archives_base_name}-${project.minecraft_version}"
|
archivesName = project.archives_base_name
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
@ -7,24 +7,7 @@ import net.minecraft.server.MinecraftServer;
|
|||||||
public class Mod implements ModInitializer {
|
public class Mod implements ModInitializer {
|
||||||
|
|
||||||
public static String MOD_ID = "enchantmentdoesnotconflict";
|
public static String MOD_ID = "enchantmentdoesnotconflict";
|
||||||
public static final String PROTOCOL_VERSION = "1.0";
|
|
||||||
private static MinecraftServer MINECRAFTSERVER;
|
|
||||||
|
|
||||||
public static MinecraftServer getMinecraftServer(){
|
|
||||||
return MINECRAFTSERVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean isClient(){
|
|
||||||
return MINECRAFTSERVER.getOverworld().isClient();
|
|
||||||
}
|
|
||||||
public static boolean isServer(){
|
|
||||||
return !MINECRAFTSERVER.getOverworld().isClient();
|
|
||||||
}
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
ServerLifecycleEvents.SERVER_STARTED.register(
|
|
||||||
(server) -> {
|
|
||||||
MINECRAFTSERVER = server;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package com.mingliqiye.minecraft.enchantment.conflict.client;
|
package com.mingliqiye.minecraft.enchantment.conflict.client;
|
||||||
|
|
||||||
import com.mingliqiye.minecraft.enchantment.conflict.network.ClientNetworkHandler;
|
import com.mingliqiye.minecraft.enchantment.conflict.network.NetworkHandler;
|
||||||
import net.fabricmc.api.ClientModInitializer;
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
|
|
||||||
public class ModClient implements ClientModInitializer {
|
public class ModClient implements ClientModInitializer {
|
||||||
@Override
|
@Override
|
||||||
public void onInitializeClient() {
|
public void onInitializeClient() {
|
||||||
ClientNetworkHandler.registerReceivers();
|
NetworkHandler.registerReceivers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ public class ModConfig {
|
|||||||
private boolean allowInfinityEnchantment = true;
|
private boolean allowInfinityEnchantment = true;
|
||||||
private boolean allowPiercingEnchantment = true;
|
private boolean allowPiercingEnchantment = true;
|
||||||
private boolean allowProtectionEnchantmentMixin = true;
|
private boolean allowProtectionEnchantmentMixin = true;
|
||||||
private static final Logger LOGGER = LogUtils.getLogger();
|
|
||||||
|
|
||||||
public ModConfig() {
|
public ModConfig() {
|
||||||
}
|
}
|
||||||
@ -34,7 +33,6 @@ public class ModConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void setInstance(ModConfig instance) {
|
public static void setInstance(ModConfig instance) {
|
||||||
LOGGER.info("Load Server {} ModConfig {}", MOD_ID, instance);
|
|
||||||
ModConfig.instance = instance;
|
ModConfig.instance = instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package com.mingliqiye.minecraft.enchantment.conflict.mixin;
|
package com.mingliqiye.minecraft.enchantment.conflict.mixin;
|
||||||
|
|
||||||
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
|
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
|
||||||
import com.mingliqiye.minecraft.enchantment.conflict.network.ServerNetWorkSender;
|
import com.mingliqiye.minecraft.enchantment.conflict.network.NetworkHandler;
|
||||||
import net.minecraft.network.ClientConnection;
|
import net.minecraft.network.ClientConnection;
|
||||||
import net.minecraft.server.PlayerManager;
|
import net.minecraft.server.PlayerManager;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
@ -18,6 +18,6 @@ public abstract class PlayerManagerMixin {
|
|||||||
ClientConnection connection, ServerPlayerEntity player,
|
ClientConnection connection, ServerPlayerEntity player,
|
||||||
CallbackInfo ci
|
CallbackInfo ci
|
||||||
){
|
){
|
||||||
ServerNetWorkSender.sendConfigToClient(player, ModConfig.getInstance());
|
NetworkHandler.sendConfigToClient(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
package com.mingliqiye.minecraft.enchantment.conflict.network;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonParser;
|
|
||||||
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
|
|
||||||
import net.fabricmc.api.EnvType;
|
|
||||||
import net.fabricmc.api.Environment;
|
|
||||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
|
||||||
|
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
|
||||||
public class ClientNetworkHandler {
|
|
||||||
|
|
||||||
public static void registerReceivers() {
|
|
||||||
ClientPlayNetworking.registerGlobalReceiver(
|
|
||||||
NetworkHandler.CONFIG_PACKET_ID,
|
|
||||||
(client, handler, buf, responseSender) -> {
|
|
||||||
JsonElement configData =
|
|
||||||
JsonParser.parseString(buf.readString());
|
|
||||||
client.execute(() -> handleConfigPacket(configData));
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void handleConfigPacket(JsonElement configData) {
|
|
||||||
ModConfig config = new Gson().fromJson(configData, ModConfig.class);
|
|
||||||
ModConfig.setInstance(config);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +1,40 @@
|
|||||||
package com.mingliqiye.minecraft.enchantment.conflict.network;
|
package com.mingliqiye.minecraft.enchantment.conflict.network;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
|
||||||
|
import com.mojang.logging.LogUtils;
|
||||||
|
import io.netty.buffer.Unpooled;
|
||||||
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||||
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||||
|
import net.minecraft.network.PacketByteBuf;
|
||||||
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
import com.mingliqiye.minecraft.enchantment.conflict.Mod;
|
import com.mingliqiye.minecraft.enchantment.conflict.Mod;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
public class NetworkHandler {
|
public class NetworkHandler {
|
||||||
public static final Identifier
|
public static final Identifier CONFIG_PACKET_ID = new Identifier(Mod.MOD_ID, "config_packet");
|
||||||
CONFIG_PACKET_ID = new Identifier(Mod.MOD_ID, "config_packet");
|
public static final Gson GSON = new Gson();
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LogUtils.getLogger();
|
||||||
|
|
||||||
|
public static void sendConfigToClient(ServerPlayerEntity player) {
|
||||||
|
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
|
||||||
|
buf.writeString(GSON.toJson(ModConfig.getInstance()));
|
||||||
|
ServerPlayNetworking.send(player, NetworkHandler.CONFIG_PACKET_ID, buf);
|
||||||
|
LOGGER.info("Send Player({}) Server {} config {}", player.getName().getString(), Mod.MOD_ID,
|
||||||
|
ModConfig.getInstance());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void registerReceivers() {
|
||||||
|
ClientPlayNetworking.registerGlobalReceiver(NetworkHandler.CONFIG_PACKET_ID,
|
||||||
|
(client, handler, buf, responseSender) -> {
|
||||||
|
ModConfig configData = GSON.fromJson(buf.readString(), ModConfig.class);
|
||||||
|
client.execute(() -> {
|
||||||
|
ModConfig.setInstance(configData);
|
||||||
|
LOGGER.info("Load Server {} config {}", Mod.MOD_ID, configData);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
package com.mingliqiye.minecraft.enchantment.conflict.network;
|
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.mingliqiye.minecraft.enchantment.conflict.config.ModConfig;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import net.fabricmc.api.EnvType;
|
|
||||||
import net.fabricmc.api.Environment;
|
|
||||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
|
||||||
import net.minecraft.network.PacketByteBuf;
|
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
|
||||||
|
|
||||||
@Environment(EnvType.SERVER)
|
|
||||||
public class ServerNetWorkSender {
|
|
||||||
public static void sendConfigToClient(ServerPlayerEntity player, ModConfig config) {
|
|
||||||
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
|
|
||||||
buf.writeString(new Gson().toJson(config));
|
|
||||||
ServerPlayNetworking.send(player, NetworkHandler.CONFIG_PACKET_ID, buf);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user