Skip to content

Commit

Permalink
修复插件模式加载的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
0XPYEX0 committed Jan 8, 2024
1 parent 890bcea commit 391c12d
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# CnUsername

Allow player to use Chinese chars in username of Minecraft

允许玩家使用中文名甚至特殊字符进入服务器
Expand Down
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = 'me.xpyex'
version = '1.0.4'
version = '1.0.5'

repositories {
mavenCentral()
Expand Down Expand Up @@ -59,6 +59,9 @@ processResources {
filesMatching('plugin.yml') {
expand props
}
filesMatching('bungee.yml') {
expand props
}
}

shadowJar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.xpyex.model.cnusername;
package me.xpyex.moduel.cnusername;

import java.io.File;
import java.io.IOException;
Expand All @@ -8,6 +8,9 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.ProtectionDomain;
import me.xpyex.moduel.cnusername.bungee.ClassVisitorAllowedCharacters;
import me.xpyex.moduel.cnusername.minecraft.ClassVisitorLoginListener;
import me.xpyex.moduel.cnusername.mojang.ClassVisitorStringReader;
import net.md_5.bungee.api.ProxyServer;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -20,15 +23,15 @@ public class CnUsername {
public static final String CLASS_PATH_LOGIN_MCP = "net/minecraft/server/network/ServerLoginPacketListenerImpl";
public static final String CLASS_PATH_LOGIN_YARN = "net/minecraft/server/network/ServerLoginNetworkHandler";
public static final String CLASS_PATH_STRING = "com/mojang/brigadier/StringReader";
public static final String CLASS_PATH_BUNGEE = "net/md_5/bungee/util/AllowedCharacters";
public static final File MODULE_FOLDER = new File("CnUsername");
public static final boolean DEBUG;
public static final String CLASS_PATH_BUNGEE = "net/md_5/bungee/util/AllowedCharacters";

static {
boolean debugResult;
try {
if (MODULE_FOLDER.exists() && MODULE_FOLDER.isFile()) {
throw new IllegalStateException("错误服务端根目录下已存在CnUsername文件,且非文件夹");
throw new IllegalStateException("错误: 服务端根目录下已存在CnUsername文件,且非文件夹");
}
if (!MODULE_FOLDER.exists()) {
MODULE_FOLDER.mkdirs();
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/me/xpyex/moduel/cnusername/CnUsernamePlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.xpyex.moduel.cnusername;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

public interface CnUsernamePlugin {
public default String readPluginPattern() {
try {
File f = new File(getDataFolder(), "pattern.txt");
File folder = f.getParentFile();
if (folder.exists() && folder.isFile()) {
Logging.info("错误: 插件目录下已存在CnUsername文件,且非文件夹");
return null;
}
if (!f.exists()) {
folder.mkdirs();
f.createNewFile();
}
List<String> content = Files.readAllLines(f.toPath(), StandardCharsets.UTF_8);
return content.isEmpty() ? null : content.get(0);
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}

public File getDataFolder();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.xpyex.model.cnusername;
package me.xpyex.moduel.cnusername;

import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.xpyex.model.cnusername;
package me.xpyex.moduel.cnusername.bungee;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import me.xpyex.moduel.cnusername.minecraft.ClassVisitorLoginListener;
import me.xpyex.moduel.cnusername.Logging;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package me.xpyex.model.cnusername;
package me.xpyex.moduel.cnusername.minecraft;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import me.xpyex.moduel.cnusername.Logging;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.xpyex.model.cnusername;
package me.xpyex.moduel.cnusername.mojang;

import me.xpyex.moduel.cnusername.Logging;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
Expand All @@ -9,7 +10,7 @@ public class ClassVisitorStringReader extends ClassVisitor {
public static final String METHOD_NAME = "isAllowedInUnquotedString";
private final String className;

protected ClassVisitorStringReader(String className, ClassVisitor classVisitor) {
public ClassVisitorStringReader(String className, ClassVisitor classVisitor) {
super(Opcodes.ASM9, classVisitor);
this.className = className;
}
Expand Down
27 changes: 6 additions & 21 deletions src/main/java/me/xpyex/plugin/cnusername/bukkit/CnUsernameBK.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package me.xpyex.plugin.cnusername.bukkit;

import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import me.xpyex.model.cnusername.ClassVisitorLoginListener;
import me.xpyex.model.cnusername.CnUsername;
import me.xpyex.model.cnusername.Logging;
import me.xpyex.moduel.cnusername.minecraft.ClassVisitorLoginListener;
import me.xpyex.moduel.cnusername.CnUsername;
import me.xpyex.moduel.cnusername.CnUsernamePlugin;
import me.xpyex.moduel.cnusername.Logging;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import sun.misc.Unsafe;

public final class CnUsernameBK extends JavaPlugin {
public final class CnUsernameBK extends JavaPlugin implements CnUsernamePlugin {
private final static MethodHandle DEFINE_CLASS_METHOD;

static {
try {
Unsafe unsafeInstance;
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
unsafeInstance = (Unsafe) unsafeField.get(null);
unsafeInstance = (Unsafe) unsafeField.get(null); //Unsafe.theUnsafe静态变量

Field lookupField = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
Object lookupBase = unsafeInstance.staticFieldBase(lookupField);
Expand Down Expand Up @@ -107,17 +105,4 @@ public void onEnable() {
Logging.warning("修改失败");
}
}

public String readPluginPattern() {
try {
File f = new File(getDataFolder(), "pattern.txt");
if (!f.exists()) {
f.createNewFile();
}
return Files.readAllLines(f.toPath(), StandardCharsets.UTF_8).get(0);
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
}
25 changes: 5 additions & 20 deletions src/main/java/me/xpyex/plugin/cnusername/bungee/CnUsernameBC.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package me.xpyex.plugin.cnusername.bungee;

import java.io.File;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import me.xpyex.model.cnusername.ClassVisitorAllowedCharacters;
import me.xpyex.model.cnusername.CnUsername;
import me.xpyex.model.cnusername.Logging;
import me.xpyex.moduel.cnusername.bungee.ClassVisitorAllowedCharacters;
import me.xpyex.moduel.cnusername.CnUsername;
import me.xpyex.moduel.cnusername.CnUsernamePlugin;
import me.xpyex.moduel.cnusername.Logging;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import sun.misc.Unsafe;

public class CnUsernameBC extends Plugin {
public class CnUsernameBC extends Plugin implements CnUsernamePlugin {
private final static MethodHandle DEFINE_CLASS_METHOD;

static {
Expand Down Expand Up @@ -84,17 +82,4 @@ public void onDisable() {
Logging.info("已卸载");
//
}

public String readPluginPattern() {
try {
File f = new File(getDataFolder(), "pattern.txt");
if (!f.exists()) {
f.createNewFile();
}
return Files.readAllLines(f.toPath(), StandardCharsets.UTF_8).get(0);
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CnUsername
main: me.xpyex.plugin.cnusername.bungee.CnUsernameBC
version: '1.0.4'
version: '${version}'
author: XPYEX

0 comments on commit 391c12d

Please sign in to comment.