Open
Conversation
Member
Author
|
测试喵 Code review喵 我的测试用例加载后所有玩家都能使用F3+F4切换模式( package io.github.lumine1909;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.leavesmc.leaves.bytebuf.PacketType;
import org.leavesmc.leaves.event.bytebuf.PacketInEvent;
import org.leavesmc.leaves.event.bytebuf.PacketOutEvent;
public class DemoPlugin extends JavaPlugin implements Listener {
public static DemoPlugin plugin;
@Override
public void onEnable() {
plugin = this;
Bukkit.getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void onPacketOut(PacketOutEvent e) {
if (e.getPacketType() != PacketType.Clientbound.ENTITY_EVENT) {
return;
}
e.getBytebuf().writerIndex(4).writeByte(28);
//e.setBytebuf(Bytebuf.buf().writeInt(e.getBytebuf().readInt()).writeByte(28));
}
@EventHandler
public void onPacketIn(PacketInEvent e) {
if (e.getPacketType() != PacketType.Serverbound.CHANGE_GAME_MODE) {
return;
}
e.setCancelled(true);
Player player = e.getAudience().getPlayer();
if (player == null) {
return;
}
GameMode gameMode = switch (e.getBytebuf().readByte()) {
case 0 -> GameMode.SURVIVAL;
case 1 -> GameMode.CREATIVE;
case 2 -> GameMode.ADVENTURE;
case 3 -> GameMode.SPECTATOR;
default -> throw new IllegalStateException();
};
Bukkit.getScheduler().runTask(plugin, () -> player.setGameMode(gameMode));
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
由于Paper重写了Bukkit内部处理事件的逻辑 通过创建匿名类+static final modifier内联插件中事件处理方法的MethodHandler 使用Bukkit事件系统传递PacketEvent将不再产生由于反射导致的额外开销
我们可以重新设计一部分这个API 以提升性能和便捷性