mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-23 17:09:19 +00:00
improve mod
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 404 B |
@@ -26,6 +26,7 @@ import org.bukkit.event.EventPriority;
|
|||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
import org.bukkit.event.player.PlayerLoginEvent;
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
import org.bukkit.event.player.PlayerQuitEvent;
|
||||||
import org.bukkit.event.player.PlayerRegisterChannelEvent;
|
import org.bukkit.event.player.PlayerRegisterChannelEvent;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|||||||
@@ -104,5 +104,6 @@ java {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tasks.build {
|
tasks.build {
|
||||||
|
dependsOn(tasks.clean)
|
||||||
dependsOn(tasks.shadowJar)
|
dependsOn(tasks.shadowJar)
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ package net.momirealms.craftengine.fabric.client;
|
|||||||
import net.fabricmc.api.ClientModInitializer;
|
import net.fabricmc.api.ClientModInitializer;
|
||||||
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
|
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
|
||||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||||
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
||||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||||
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
|
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
|
||||||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
|
||||||
@@ -13,6 +14,7 @@ import net.minecraft.client.render.RenderLayer;
|
|||||||
import net.minecraft.registry.Registries;
|
import net.minecraft.registry.Registries;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
import net.minecraft.util.crash.CrashReport;
|
||||||
import net.minecraft.world.biome.FoliageColors;
|
import net.minecraft.world.biome.FoliageColors;
|
||||||
import net.momirealms.craftengine.fabric.client.config.ModConfig;
|
import net.momirealms.craftengine.fabric.client.config.ModConfig;
|
||||||
import net.momirealms.craftengine.fabric.client.network.CraftEnginePayload;
|
import net.momirealms.craftengine.fabric.client.network.CraftEnginePayload;
|
||||||
@@ -40,21 +42,17 @@ public class CraftEngineFabricModClient implements ClientModInitializer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ClientPlayNetworking.registerGlobalReceiver(CraftEnginePayload.ID, (payload, context) -> {
|
ClientPlayNetworking.registerGlobalReceiver(CraftEnginePayload.ID, (payload, context) -> {
|
||||||
try {
|
byte[] data = payload.data();
|
||||||
byte[] data = payload.data();
|
String decoded = new String(data, StandardCharsets.UTF_8);
|
||||||
String decoded = new String(data, StandardCharsets.UTF_8);
|
if (decoded.startsWith("cp:")) {
|
||||||
if (decoded.startsWith("cp:")) {
|
int blockRegistrySize = Integer.parseInt(decoded.substring(3));
|
||||||
int blockRegistrySize = Integer.parseInt(decoded.substring(3));
|
if (Block.STATE_IDS.size() != blockRegistrySize) {
|
||||||
if (Block.STATE_IDS.size() != blockRegistrySize) {
|
client.disconnect(new DisconnectedScreen(
|
||||||
client.disconnect(new DisconnectedScreen(
|
client.currentScreen,
|
||||||
client.currentScreen,
|
Text.translatable("disconnect.craftengine.title"),
|
||||||
Text.translatable("disconnect.craftengine.title"),
|
Text.translatable("disconnect.craftengine.block_registry_mismatch", Block.STATE_IDS.size(), blockRegistrySize))
|
||||||
Text.translatable("disconnect.craftengine.block_registry_mismatch"))
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -26,12 +26,10 @@ public class CraftEngineFabricMod implements ModInitializer {
|
|||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
loadConfig();
|
loadConfig();
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(this::saveConfig));
|
|
||||||
LoggerFilter.filter();
|
LoggerFilter.filter();
|
||||||
try {
|
try {
|
||||||
YamlUtils.ensureConfigFile("additional-real-blocks.yml");
|
YamlUtils.saveDefaultResource("additional-real-blocks.yml");
|
||||||
YamlUtils.ensureConfigFile("mappings.yml");
|
YamlUtils.saveDefaultResource("mappings.yml");
|
||||||
YamlUtils.ensureConfigFile("config.yml");
|
|
||||||
Map<Identifier, Integer> map = YamlUtils.loadMappingsAndAdditionalBlocks();
|
Map<Identifier, Integer> map = YamlUtils.loadMappingsAndAdditionalBlocks();
|
||||||
for (Map.Entry<Identifier, Integer> entry : map.entrySet()) {
|
for (Map.Entry<Identifier, Integer> entry : map.entrySet()) {
|
||||||
Identifier replacedBlockId = entry.getKey();
|
Identifier replacedBlockId = entry.getKey();
|
||||||
@@ -46,15 +44,15 @@ public class CraftEngineFabricMod implements ModInitializer {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(this::saveConfig));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void loadConfig() {
|
private void loadConfig() {
|
||||||
if (!Files.exists(CONFIG_PATH)) {
|
if (!Files.exists(CONFIG_PATH)) {
|
||||||
saveConfig();
|
ModConfig.enableNetwork = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try (InputStream inputStream = Files.newInputStream(CONFIG_PATH)) {
|
try (InputStream inputStream = Files.newInputStream(CONFIG_PATH)) {
|
||||||
Yaml yaml = new Yaml();
|
Yaml yaml = new Yaml();
|
||||||
var config = yaml.loadAs(inputStream, java.util.Map.class);
|
var config = yaml.loadAs(inputStream, java.util.Map.class);
|
||||||
@@ -68,10 +66,8 @@ public class CraftEngineFabricMod implements ModInitializer {
|
|||||||
DumperOptions options = new DumperOptions();
|
DumperOptions options = new DumperOptions();
|
||||||
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||||
Yaml yaml = new Yaml(options);
|
Yaml yaml = new Yaml(options);
|
||||||
|
|
||||||
var data = new java.util.HashMap<String, Object>();
|
var data = new java.util.HashMap<String, Object>();
|
||||||
data.put("enable-network", ModConfig.enableNetwork);
|
data.put("enable-network", ModConfig.enableNetwork);
|
||||||
|
|
||||||
try (Writer writer = Files.newBufferedWriter(CONFIG_PATH)) {
|
try (Writer writer = Files.newBufferedWriter(CONFIG_PATH)) {
|
||||||
yaml.dump(data, writer);
|
yaml.dump(data, writer);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package net.momirealms.craftengine.fabric.util;
|
||||||
|
|
||||||
|
import org.yaml.snakeyaml.LoaderOptions;
|
||||||
|
import org.yaml.snakeyaml.constructor.SafeConstructor;
|
||||||
|
import org.yaml.snakeyaml.nodes.MappingNode;
|
||||||
|
import org.yaml.snakeyaml.nodes.Node;
|
||||||
|
import org.yaml.snakeyaml.nodes.NodeTuple;
|
||||||
|
import org.yaml.snakeyaml.nodes.ScalarNode;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class StringKeyConstructor extends SafeConstructor {
|
||||||
|
|
||||||
|
public StringKeyConstructor(LoaderOptions loaderOptions) {
|
||||||
|
super(loaderOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Map<Object, Object> constructMapping(MappingNode node) {
|
||||||
|
Map<Object, Object> map = new LinkedHashMap<>();
|
||||||
|
for (NodeTuple tuple : node.getValue()) {
|
||||||
|
Node keyNode = tuple.getKeyNode();
|
||||||
|
Node valueNode = tuple.getValueNode();
|
||||||
|
String key = constructScalar((ScalarNode) keyNode);
|
||||||
|
Object value = constructObject(valueNode);
|
||||||
|
map.put(key, value);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,8 +9,10 @@ import net.minecraft.registry.Registries;
|
|||||||
import net.minecraft.registry.RegistryKeys;
|
import net.minecraft.registry.RegistryKeys;
|
||||||
import net.minecraft.registry.RegistryWrapper;
|
import net.minecraft.registry.RegistryWrapper;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
import org.yaml.snakeyaml.LoaderOptions;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -21,23 +23,25 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class YamlUtils {
|
public class YamlUtils {
|
||||||
public static final String CONFIG_DIR = "config/craft-engine-fabric-mod/";
|
public static final Path CONFIG_DIR = Path.of("config/craft-engine-fabric-mod/");
|
||||||
private static final Yaml yaml = new Yaml();
|
|
||||||
private static final RegistryWrapper<Block> registryWrapper = BuiltinRegistries.createWrapperLookup().getOrThrow(RegistryKeys.BLOCK);
|
private static final RegistryWrapper<Block> registryWrapper = BuiltinRegistries.createWrapperLookup().getOrThrow(RegistryKeys.BLOCK);
|
||||||
|
|
||||||
public static <T> T loadConfig(Path filePath) throws IOException {
|
public static <T> T loadConfig(Path filePath) throws IOException {
|
||||||
|
if (!Files.exists(filePath)) {
|
||||||
|
throw new FileNotFoundException(filePath.toString());
|
||||||
|
}
|
||||||
try (InputStream inputStream = Files.newInputStream(filePath)) {
|
try (InputStream inputStream = Files.newInputStream(filePath)) {
|
||||||
|
Yaml yaml = new Yaml(new StringKeyConstructor(new LoaderOptions()));
|
||||||
return yaml.load(inputStream);
|
return yaml.load(inputStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ensureConfigFile(String fileName) throws IOException {
|
public static void saveDefaultResource(String fileName) throws IOException {
|
||||||
Path configDir = Path.of(CONFIG_DIR);
|
if (!Files.exists(CONFIG_DIR)) {
|
||||||
if (!Files.exists(configDir)) {
|
Files.createDirectories(CONFIG_DIR);
|
||||||
Files.createDirectories(configDir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Path targetPath = configDir.resolve(fileName);
|
Path targetPath = CONFIG_DIR.resolve(fileName);
|
||||||
if (Files.exists(targetPath)) return;
|
if (Files.exists(targetPath)) return;
|
||||||
String resourcePath = "assets/craft-engine-fabric-mod/config/" + fileName;
|
String resourcePath = "assets/craft-engine-fabric-mod/config/" + fileName;
|
||||||
try (InputStream inputStream = YamlUtils.class.getClassLoader().getResourceAsStream(resourcePath)) {
|
try (InputStream inputStream = YamlUtils.class.getClassLoader().getResourceAsStream(resourcePath)) {
|
||||||
@@ -48,10 +52,9 @@ public class YamlUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Map<Identifier, Integer> loadMappingsAndAdditionalBlocks() throws IOException {
|
public static Map<Identifier, Integer> loadMappingsAndAdditionalBlocks() throws IOException {
|
||||||
Path mappingPath = Path.of(CONFIG_DIR + "mappings.yml");
|
Path mappingPath = CONFIG_DIR.resolve("mappings.yml");
|
||||||
Path additionalYamlPath = Path.of(CONFIG_DIR + "additional-real-blocks.yml");
|
Path additionalYamlPath = CONFIG_DIR.resolve("additional-real-blocks.yml");
|
||||||
Map<String, String> blockStateMappings = loadConfig(mappingPath);
|
Map<String, String> blockStateMappings = loadConfig(mappingPath);
|
||||||
validateBlockStateMappings(blockStateMappings);
|
validateBlockStateMappings(blockStateMappings);
|
||||||
Map<Identifier, Integer> blockTypeCounter = new LinkedHashMap<>();
|
Map<Identifier, Integer> blockTypeCounter = new LinkedHashMap<>();
|
||||||
@@ -63,7 +66,6 @@ public class YamlUtils {
|
|||||||
return buildRegisteredRealBlockSlots(blockTypeCounter, additionalYaml);
|
return buildRegisteredRealBlockSlots(blockTypeCounter, additionalYaml);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static void validateBlockStateMappings(Map<String, String> blockStateMappings) {
|
private static void validateBlockStateMappings(Map<String, String> blockStateMappings) {
|
||||||
Map<String, String> temp = new HashMap<>(blockStateMappings);
|
Map<String, String> temp = new HashMap<>(blockStateMappings);
|
||||||
for (Map.Entry<String, String> entry : temp.entrySet()) {
|
for (Map.Entry<String, String> entry : temp.entrySet()) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"title.craftengine.config": "CraftEngine Settings",
|
"title.craftengine.config": "CraftEngine Settings",
|
||||||
"category.craftengine.general": "General",
|
"category.craftengine.general": "General",
|
||||||
"option.craftengine.enable_network": "Enable custom blocks in server",
|
"option.craftengine.enable_network": "Enable custom blocks in server",
|
||||||
"tooltip.craftengine.enable_network": "You need to re-enter the server to take effect",
|
"tooltip.craftengine.enable_network": "Changes requires re-entering the server to take effect",
|
||||||
"disconnect.craftengine.block_registry_mismatch": "Block registry size mismatch!",
|
"disconnect.craftengine.block_registry_mismatch": "Block registry size mismatch. Current: %s. Expected: %s. \n 1. Make sure that the configs are the same as the server's. \n 2. Do not use any mod that might register new block. \n 3. Do not install ViaVersion.",
|
||||||
"disconnect.craftengine.title": "CraftEngine Connection Error"
|
"disconnect.craftengine.title": "CraftEngine Connection Error"
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
"title.craftengine.config": "CraftEngine设置",
|
"title.craftengine.config": "CraftEngine设置",
|
||||||
"category.craftengine.general": "通用",
|
"category.craftengine.general": "通用",
|
||||||
"option.craftengine.enable_network": "启用服务器内自定义方块",
|
"option.craftengine.enable_network": "启用服务器内自定义方块",
|
||||||
"tooltip.craftengine.enable_network": "需要重新进入服务器即可生效",
|
"tooltip.craftengine.enable_network": "需要重新进入服务器以应用更改",
|
||||||
"disconnect.craftengine.block_registry_mismatch": "方块注册表大小不匹配!",
|
"disconnect.craftengine.block_registry_mismatch": "方块注册表大小不匹配. 当前: %s. 预期: %s \n 1. 确保客户端mod配置与服务端配置一致. \n 2. 不要安装任何会注册新方块的mod. \n 3. 不要使用ViaVersion.",
|
||||||
"disconnect.craftengine.title": "CraftEngine连接错误"
|
"disconnect.craftengine.title": "CraftEngine连接错误"
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user