Skip to content

Commit

Permalink
[6.2.0][dev] 数据包底层优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Sep 21, 2024
1 parent e539657 commit 7f1ce11
Show file tree
Hide file tree
Showing 24 changed files with 284 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package taboolib.common.platform.function

import taboolib.common.PrimitiveIO
import taboolib.common.io.isDevelopmentMode
import taboolib.common.io.isDebugMode
import taboolib.common.io.newFile
import taboolib.common.io.runningResources
import taboolib.common.platform.PlatformFactory
Expand All @@ -18,14 +18,12 @@ fun <T> server(): T {
}

/**
* 打印开发者日志
* 打印调试信息
*
* @param message 日志内容
*/
fun dev(vararg message: Any?) {
if (isDevelopmentMode) {
message.filterNotNull().forEach { PrimitiveIO.debug(it) }
}
fun debug(vararg message: Any?) {
if (isDebugMode) message.filterNotNull().forEach { PrimitiveIO.debug(it) }
}

/**
Expand Down
2 changes: 2 additions & 0 deletions common-util/src/main/java/taboolib/common/OpenAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ public class OpenAPI {

@NotNull
public static OpenResult call(String name, Object[] data) {
// PrimitiveIO.debug("OpenAPI call {0}", name);
for (Map.Entry<String, Object> entry : TabooLib.getAwakenedClasses().entrySet()) {
if (entry.getValue() instanceof OpenListener) {
OpenResult result = ((OpenListener) entry.getValue()).call(name, data);
if (result.isSuccessful()) {
// PrimitiveIO.debug(" Handle by {0}", entry.getKey());
return result;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public interface OpenContainer {
* @param args 参数
*/
@NotNull
OpenResult call(String name, @NotNull Object[] args);
OpenResult call(String name, Object[] args);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ public interface OpenListener {
* @param data 数据
* @return boolean 是否注册成功
*/
OpenResult call(@NotNull String name, @NotNull Object[] data);
OpenResult call(@NotNull String name, Object[] data);
}
6 changes: 4 additions & 2 deletions common-util/src/main/java/taboolib/common/OpenResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tabooproject.reflex.AnalyseMode;
import org.tabooproject.reflex.Reflex;

public class OpenResult {
Expand Down Expand Up @@ -74,7 +73,10 @@ public static OpenResult failed() {
return new OpenResult(false, null);
}

public static OpenResult deserialize(Object source) {
/**
* 从其他插件的 OpenResult 转换为当前插件的 OpenResult
*/
public static OpenResult cast(Object source) {
Object successful = Reflex.Companion.getLocalProperty(source, "successful");
Object value = Reflex.Companion.getLocalProperty(source, "value");
return new OpenResult(Boolean.TRUE.equals(successful), value);
Expand Down
2 changes: 2 additions & 0 deletions common-util/src/main/kotlin/taboolib/common/io/ProjectInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ val taboolibPath: String

/**
* 特定类的组名
* 传入:org.tabooproject.taboolib.platform.BukkitPlugin
* 传出:org.tabooproject
*/
val Class<*>.groupId: String
get() = name.substringBefore(taboolibId).dropLast(1)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=taboolib
version=6.2.0-beta10
version=6.2.0-beta11
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.caching.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import taboolib.common.PrimitiveIO;
import taboolib.common.reflect.ClassHelper;

import java.io.Closeable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
Expand All @@ -61,7 +62,7 @@
*
* @author fren_gor
*/
public abstract class LightInjector {
public abstract class LightInjector implements Closeable {

private static final Class<?> SERVER_CLASS = getNMSClass("MinecraftServer", "server");
private static final Class<?> SERVER_CONNECTION_CLASS = getNMSClass("ServerConnection", "server.network");
Expand Down Expand Up @@ -337,6 +338,7 @@ public final void receivePacket(@NotNull Channel channel, @NotNull Object packet
* If this injector is already closed then invoking this method has no effect.
* 如果此注入器已经关闭,则调用此方法无效。
*/
@Override
public final void close() {
if (closed.getAndSet(true)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,37 @@ public LightInjectorImpl(@NotNull Plugin plugin) {
protected @Nullable Object onPacketReceiveAsync(@Nullable Player sender, @NotNull Channel channel, @NotNull Object packet) {
if (sender != null) {
PacketReceiveEvent event = new PacketReceiveEvent(sender, new PacketImpl(packet));
event.call();
return event.getPacket().getSource();
if (event.callIf()) {
return ProtocolHandler.INSTANCE.handlePacket(ProtocolHandler.PACKET_RECEIVE, sender, channel, event.getPacket().getSource());
} else {
return null;
}
} else {
PacketReceiveEvent.Handshake event = new PacketReceiveEvent.Handshake(channel, new PacketImpl(packet));
event.call();
return event.getPacket().getSource();
if (event.callIf()) {
return ProtocolHandler.INSTANCE.handlePacket(ProtocolHandler.PACKET_RECEIVE, null, channel, event.getPacket().getSource());
} else {
return null;
}
}
}

@Override
protected @Nullable Object onPacketSendAsync(@Nullable Player receiver, @NotNull Channel channel, @NotNull Object packet) {
if (receiver != null) {
PacketSendEvent event = new PacketSendEvent(receiver, new PacketImpl(packet));
event.call();
return event.getPacket().getSource();
if (event.callIf()) {
return ProtocolHandler.INSTANCE.handlePacket(ProtocolHandler.PACKET_SEND, receiver, channel, event.getPacket().getSource());
} else {
return null;
}
} else {
PacketSendEvent.Handshake event = new PacketSendEvent.Handshake(channel, new PacketImpl(packet));
event.call();
return event.getPacket().getSource();
if (event.callIf()) {
return ProtocolHandler.INSTANCE.handlePacket(ProtocolHandler.PACKET_SEND, null, channel, event.getPacket().getSource());
} else {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ import java.util.logging.Logger
@Suppress("UNCHECKED_CAST")
object Exchanges {

// Minecraft 语言文件缓存
const val MINECRAFT_LANGUAGE = "minecraft_language"

// Spigot 映射表
const val MAPPING_SPIGOT = "mapping_spigot"

// Paper 映射表
const val MAPPING_PAPER = "mapping_paper"

private val map: MutableMap<String, Any>
Expand Down Expand Up @@ -53,6 +58,13 @@ object Exchanges {
return map[key] as T
}

/**
* 读取数据,如果不存在则写入默认值
*/
fun <T> getOrPut(key: String, defaultValue: () -> T): T {
return map.getOrPut(key) { defaultValue()!! } as T
}

/**
* 写入数据
*/
Expand Down Expand Up @@ -151,7 +163,7 @@ object Exchanges {
}

override fun getName(): String {
return "TabooLibExchange"
return "TabooLibExchange/v1"
}
}
// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ fun nmsClass(name: String): Class<*> {
}
}

/**
* 禁用数据包监听器
*/
fun disablePacketListener() {
ProtocolHandler.disable()
}

@Synchronized
fun <T> nmsProxy(clazz: Class<T>, bind: String = "{name}Impl", vararg parameter: Any): T {
return nmsProxy(clazz, bind, emptyList(), *parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ class PacketImpl(override var source: Any) : Packet() {

companion object {

val spigotNameCache = ConcurrentHashMap<String, Optional<String>>()
val spigotNameCache = Exchanges.getOrPut("packet_spigot_name_cache") { ConcurrentHashMap<String, Optional<String>>() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import org.bukkit.entity.Player
import taboolib.common.event.CancelableInternalEvent

/**
* TabooLib
* taboolib.module.nms.PacketReceiveEvent
*
* @author sky
* @since 2021/6/24 5:38 下午
* 数据包接收事件,可用于拦截数据包
* 自 6.2 版本起,不能放行已被拦截的数据包(即使用 isCancelled = false)
*/
class PacketReceiveEvent(val player: Player, val packet: Packet) : CancelableInternalEvent() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import org.bukkit.entity.Player
import taboolib.common.event.CancelableInternalEvent

/**
* TabooLib
* taboolib.module.nms.PacketSendEvent
*
* @author sky
* @since 2021/6/24 5:38 下午
* 数据包发送事件,可用于拦截数据包
* 自 6.2 版本起,不能放行已被拦截的数据包(即使用 isCancelled = false)
*/
class PacketSendEvent(val player: Player, val packet: Packet) : CancelableInternalEvent() {

Expand Down
Loading

0 comments on commit 7f1ce11

Please sign in to comment.