Initial commit

This commit is contained in:
2022-10-24 02:43:46 +08:00
commit 0bdc4d0cd8
59 changed files with 3199 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
version = '1.0.0'
setArchivesBaseName("HamsterBall-Bukkit")
evaluationDependsOn(':hamster-ball-common')
dependencies {
apiShade(project(":hamster-ball-common")) {
exclude group: "*"
}
compileOnly 'org.spigotmc:spigot-api:1.12.2-R0.1-SNAPSHOT'
}
processResources {
inputs.property "version", project.version
filesMatching("plugin.yml") {
expand "version": project.version
}
duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
}
tasks.compileJava.dependsOn(":hamster-ball-common:build")
tasks.create("shadowJar", Jar) {
dependsOn("jar")
from([
tasks.jar.outputs.files.collect {
it.isDirectory() ? it : zipTree(it)
},
configurations.shade.collect {
it.isDirectory() ? it : zipTree(it)
},
configurations.apiShade.collect {
it.isDirectory() ? it : zipTree(it)
},
configurations.implementationShade.collect {
it.isDirectory() ? it : zipTree(it)
}
])
destinationDir(rootProject.buildDir)
}
tasks.build.dependsOn(shadowJar)

View File

@@ -0,0 +1,42 @@
package cn.hamster3.mc.plugin.ball.bukkit;
import cn.hamster3.mc.plugin.ball.bukkit.api.BallBukkitAPI;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Logger;
public class HamsterBallPlugin extends JavaPlugin {
private static HamsterBallPlugin instance;
public static HamsterBallPlugin getInstance() {
return instance;
}
public static void sync(Runnable runnable) {
Bukkit.getScheduler().runTask(instance, runnable);
}
@Override
public void onLoad() {
instance = this;
Logger logger = getLogger();
BallBukkitAPI.init();
logger.info("BallBukkitAPI 已初始化.");
try {
BallBukkitAPI.getInstance().enable();
} catch (Exception e) {
e.printStackTrace();
sync(Bukkit::shutdown);
}
}
@Override
public void onDisable() {
try {
BallBukkitAPI.getInstance().disable();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,59 @@
package cn.hamster3.mc.plugin.ball.bukkit.api;
import cn.hamster3.mc.plugin.ball.bukkit.HamsterBallPlugin;
import cn.hamster3.mc.plugin.ball.bukkit.listener.BallBukkitListener;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.config.BallConfig;
import cn.hamster3.mc.plugin.ball.common.entity.ServerInfo;
import cn.hamster3.mc.plugin.ball.common.entity.ServerType;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import java.sql.SQLException;
public class BallBukkitAPI extends BallAPI {
public BallBukkitAPI(@NotNull BallConfig config) {
super(config);
}
public static BallBukkitAPI getInstance() {
return (BallBukkitAPI) instance;
}
public static void init() {
if (instance != null) {
return;
}
HamsterBallPlugin plugin = HamsterBallPlugin.getInstance();
plugin.saveDefaultConfig();
FileConfiguration pluginConfig = plugin.getConfig();
String host = pluginConfig.getString("server-info.name.host", Bukkit.getIp());
BallConfig config = new BallConfig(
new ServerInfo(
pluginConfig.getString("server-info.id"),
pluginConfig.getString("server-info.name"),
ServerType.GAME,
host.isEmpty() ? "127.0.0.1" : host,
pluginConfig.getInt("server-info.name.port", Bukkit.getPort())
),
pluginConfig.getString("ball-server.host"),
pluginConfig.getInt("ball-server.port"),
pluginConfig.getInt("ball-server.nio-thread")
);
instance = new BallBukkitAPI(config);
instance.addListener(BallBukkitListener.INSTANCE);
}
@Override
public void enable() throws SQLException, InterruptedException {
super.enable();
}
@Override
public void disable() throws SQLException, InterruptedException {
super.disable();
}
}

View File

@@ -0,0 +1,54 @@
package cn.hamster3.mc.plugin.ball.bukkit.data;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.data.ServiceBlockPos;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("unused")
public class BukkitBlockPos extends ServiceBlockPos {
public BukkitBlockPos(@NotNull String serverID, @NotNull String worldName, int x, int y, int z) {
super(serverID, worldName, x, y, z);
}
public BukkitBlockPos(@NotNull Entity player) {
this(player.getLocation());
}
public BukkitBlockPos(@NotNull Block block) {
this(block.getLocation());
}
public BukkitBlockPos(@NotNull Location location) {
super(
BallAPI.getInstance().getLocalServerId(),
location.getWorld().getName(),
location.getBlockX(),
location.getBlockY(),
location.getBlockZ()
);
}
public BukkitBlockPos(@NotNull ServiceBlockPos location) {
super(
BallAPI.getInstance().getLocalServerId(),
location.getWorldName(),
location.getX(),
location.getY(),
location.getZ()
);
}
@NotNull
public Location toBukkitLocation() {
return new Location(Bukkit.getWorld(getWorldName()), getX(), getY(), getZ(), 0, 0);
}
@NotNull
public BukkitLocation toServiceLocation() {
return new BukkitLocation(getServerID(), getWorldName(), getX(), getY(), getZ());
}
}

View File

@@ -0,0 +1,62 @@
package cn.hamster3.mc.plugin.ball.bukkit.data;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.data.ServiceLocation;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("unused")
public class BukkitLocation extends ServiceLocation {
public BukkitLocation(@NotNull String serverID, @NotNull String worldName, double x, double y, double z) {
super(serverID, worldName, x, y, z, 0, 0);
}
public BukkitLocation(@NotNull String serverID, @NotNull String worldName, double x, double y, double z, float yaw, float pitch) {
super(serverID, worldName, x, y, z, yaw, pitch);
}
public BukkitLocation(@NotNull Entity player) {
this(player.getLocation());
}
public BukkitLocation(@NotNull Block block) {
this(block.getLocation());
}
public BukkitLocation(@NotNull Location location) {
super(
BallAPI.getInstance().getLocalServerId(),
location.getWorld().getName(),
location.getX(),
location.getY(),
location.getZ(),
location.getYaw(),
location.getPitch()
);
}
public BukkitLocation(@NotNull ServiceLocation location) {
super(
location.getServerID(),
location.getWorldName(),
location.getX(),
location.getY(),
location.getZ(),
location.getYaw(),
location.getPitch()
);
}
@NotNull
public Location toBukkitLocation() {
return new Location(Bukkit.getWorld(getWorldName()), getX(), getY(), getZ(), getYaw(), getPitch());
}
@NotNull
public BukkitBlockPos toBukkitBlockPos() {
return new BukkitBlockPos(getServerID(), getWorldName(), getBlockX(), getBlockY(), getBlockZ());
}
}

View File

@@ -0,0 +1,87 @@
package cn.hamster3.mc.plugin.ball.bukkit.listener;
import cn.hamster3.mc.plugin.ball.bukkit.data.BukkitLocation;
import cn.hamster3.mc.plugin.ball.common.api.BallAPI;
import cn.hamster3.mc.plugin.ball.common.data.ServiceLocation;
import cn.hamster3.mc.plugin.ball.common.entity.ServerType;
import cn.hamster3.mc.plugin.ball.common.event.operate.DispatchConsoleCommandEvent;
import cn.hamster3.mc.plugin.ball.common.event.operate.DispatchPlayerCommandEvent;
import cn.hamster3.mc.plugin.ball.common.event.operate.SendPlayerToLocationEvent;
import cn.hamster3.mc.plugin.ball.common.event.operate.SendPlayerToPlayerEvent;
import cn.hamster3.mc.plugin.ball.common.listener.BallListener;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.UUID;
public class BallBukkitListener extends BallListener implements Listener {
public static final BallBukkitListener INSTANCE = new BallBukkitListener();
private final HashMap<UUID, Location> playerToLocation = new HashMap<>();
@Override
public void onDispatchConsoleCommand(@NotNull DispatchConsoleCommandEvent event) {
if (event.getType() != null && event.getType() != ServerType.GAME) {
return;
}
if (event.getServerID() != null && !BallAPI.getInstance().isLocalServer(event.getServerID())) {
return;
}
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), event.getCommand());
}
@Override
public void onDispatchGamePlayerCommand(@NotNull DispatchPlayerCommandEvent event) {
if (event.getType() != null && event.getType() != ServerType.GAME) {
return;
}
if (event.getUuid() != null && Bukkit.getPlayer(event.getUuid()) == null) {
return;
}
Bukkit.dispatchCommand(Bukkit.getPlayer(event.getUuid()), event.getCommand());
}
@Override
public void onSendPlayerToLocation(@NotNull SendPlayerToLocationEvent event) {
ServiceLocation location = event.getLocation();
if (!BallAPI.getInstance().isLocalServer(location.getServerID())) {
return;
}
for (UUID uuid : event.getSendPlayerUUID()) {
playerToLocation.put(uuid, new BukkitLocation(location).toBukkitLocation());
}
}
@Override
public void onSendPlayerToPlayer(@NotNull SendPlayerToPlayerEvent event) {
Player player = Bukkit.getPlayer(event.getToPlayerUUID());
if (player == null) {
return;
}
Location location = player.getLocation();
for (UUID uuid : event.getSendPlayerUUID()) {
playerToLocation.put(uuid, location);
}
}
@Override
public void onReconnectFailed() {
Bukkit.shutdown();
}
@EventHandler(ignoreCancelled = true)
public void onPlayerLogin(PlayerLoginEvent event) {
Player player = event.getPlayer();
Location location = playerToLocation.remove(player.getUniqueId());
if (location != null) {
player.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
}
}
}

View File

@@ -0,0 +1,24 @@
ball-server:
host: "ball.hamster3.cn"
port: 58888
nio-thread: 10
server-info:
# 服务器唯一识别码,最长 32 字符
# 推荐格式:全小写英文+横杠+数字尾号
# 例如:
# survival-1, survival-2生存1区生存2区
# plot-1, plot-2地皮1区地皮2区
# resource-1, resource-2资源1区资源2区
id: "test-1"
# 服务端名称,用于展示给玩家看
name: "测试1区"
# 当前子服的内网地址
# 不填则自动获取 server.properties 文件中的设置
# 若都为空,则自动设定为 127.0.0.1
# 连接上 HamsterBall 之后BC 端的插件会自动将这个服务器的地址和端口加入到列表
# 因此只需在这里填上无需每次都手动更改 BC 的配置文件
# host: "127.0.0.1"
# 当前子服的内网端口
# 不填则自动获取 server.properties 文件中的设置
# port: 25577

View File

@@ -0,0 +1,10 @@
name: HamsterBall
main: cn.hamster3.mc.plugin.ball.bukkit.HamsterBallPlugin
version: ${version}
author: MiniDay
website: https://github.com/MiniDay/hamster-ball
description: 仓鼠球:一个基于 Netty 的 Minecraft 服务端通用消息中间件原HamsterService
depend:
- HamsterCore