diff --git a/pom.xml b/pom.xml index 30b8abf..1fd4253 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ huihui.crsc ClickRailSpawnCar - 1.0 + 2.0 jar ClickRailSpawnCar @@ -67,7 +67,7 @@ org.spigotmc spigot-api - 1.16.5-R0.1-SNAPSHOT + 1.13-R0.1-SNAPSHOT provided diff --git a/src/main/java/huihui/crsc/ClickRailSpawnCar.java b/src/main/java/huihui/crsc/ClickRailSpawnCar.java index 0ef54c2..d6b8ccd 100644 --- a/src/main/java/huihui/crsc/ClickRailSpawnCar.java +++ b/src/main/java/huihui/crsc/ClickRailSpawnCar.java @@ -1,32 +1,32 @@ package huihui.crsc; -import huihui.crsc.minecars.ScanPlayerEvents; +import huihui.crsc.events.ScanPlayerEvents; import org.bukkit.ChatColor; -import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; -import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitTask; -import java.io.Console; +import java.util.HashMap; +import java.util.Map; public final class ClickRailSpawnCar extends JavaPlugin { + // 新建一个Map,存放玩家的坐车时长,reload或重启服务器刷新。 + public static Map playerCarTimeCount = new HashMap<>(); + // normal + public static String normal = "[Crsc]"; + public static ClickRailSpawnCar instance; @Override public void onEnable() { // Plugin startup logic getServer().getPluginManager().registerEvents(new ScanPlayerEvents(), this); - getConfig().options().copyDefaults(); - saveDefaultConfig(); - Plugin plugin = huihui.crsc.ClickRailSpawnCar.getPlugin(huihui.crsc.ClickRailSpawnCar.class); + instance = this; System.out.println(ChatColor.RED + "[crsc]插件已启动"); - int tick = plugin.getConfig().getInt("WaitTime") * 20 * 60; - BukkitTask Clean = new huihui.crsc.minecars.CleanMinecars().runTaskTimer(this, 0, tick); } @Override public void onDisable() { // Plugin shutdown logic + playerCarTimeCount.clear(); System.out.println(ChatColor.RED + "[crsc]插件已注销"); } } diff --git a/src/main/java/huihui/crsc/events/ScanPlayerEvents.java b/src/main/java/huihui/crsc/events/ScanPlayerEvents.java new file mode 100644 index 0000000..2493768 --- /dev/null +++ b/src/main/java/huihui/crsc/events/ScanPlayerEvents.java @@ -0,0 +1,120 @@ +package huihui.crsc.events; + +import huihui.crsc.utils.PlayerUtils; +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.vehicle.VehicleEnterEvent; +import org.bukkit.event.vehicle.VehicleExitEvent; +import org.bukkit.inventory.EquipmentSlot; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import static huihui.crsc.ClickRailSpawnCar.playerCarTimeCount; + + +public class ScanPlayerEvents implements Listener { + + // 检测玩家点击 + @EventHandler + public void onClickBlock(PlayerInteractEvent event) { + Block block = event.getClickedBlock(); + Player player = event.getPlayer(); + if (block == null){ + return; + } + if (event.getAction() == Action.LEFT_CLICK_BLOCK){ + return; + } + if (event.getHand() == EquipmentSlot.OFF_HAND){ + return; + } + double x = block.getX(); + double y = block.getY() + 1; + double z = block.getZ(); + + Material clickType = block.getType(); // 获取方块的类型 + + if (clickType == Material.RAIL) { + spawnCar(x, y, z, player); + } + else if (clickType == Material.POWERED_RAIL){ + spawnCar(x, y, z, player); + } + else if (clickType == Material.ACTIVATOR_RAIL){ + spawnCar(x, y, z, player); + } + else if (clickType == Material.DETECTOR_RAIL){ + spawnCar(x, y, z, player); + } + + } + + // 检测玩家上车 + @EventHandler + public void onEnterCar(VehicleEnterEvent event){ + // 获取进入到矿车的玩家 + if (event.getVehicle().getType().equals(EntityType.MINECART)){ + // 防止动物进入,报错 + if (event.getEntered() instanceof Player){ + Player player = (Player) event.getEntered(); + Long enterTime = System.currentTimeMillis(); + // 给玩家计时 + playerCarTimeCount.put(player, enterTime); + PlayerUtils.send(player, "#AQUA#欢迎乘坐NameFlying第%d号列车,已开始计时。", randomCarNum()); + } + } + + } + + // 检测玩家下车 + @EventHandler + public void onCarUnuseful(VehicleExitEvent event){ + if (event.getVehicle().getType().equals(EntityType.MINECART)){ + // 防止动物进入,报错 + if (event.getExited() instanceof Player){ + Player player = (Player) event.getExited(); + // 停止计时,并告知玩家 + if (playerCarTimeCount.containsKey(player)){ + Long enterTime = playerCarTimeCount.get(player); + Long stopTime = System.currentTimeMillis(); + double spendTime = (double) (stopTime - enterTime) / 1000; + PlayerUtils.send(player, "#AQUA#本次旅程共花费:#GREEN#" + spendTime + " #AQUA#秒,感谢乘坐NameFlying此次列车。"); + // 删除Map中的玩家 + playerCarTimeCount.remove(player); + } + // 将车子销毁 + event.getVehicle().remove(); + } + } + } + + public void spawnCar(double x, double y, double z, Player player){ + // console报告 + System.out.println("玩家:" + player.getName() + "召唤了一个矿车 坐标:(" + x + ", " + y + ", " + z + ")"); + // 获取玩家所在world + World world = player.getWorld(); + // 定义矿车位置 + Location location = new Location(world, x, y, z); + // 生成矿车并让玩家坐上去 + world.spawnEntity(location, EntityType.MINECART).addPassenger(player); + } + + public int randomCarNum(){ + List numList = new ArrayList<>(); + numList.add(114); + numList.add(514); + numList.add(1919); + numList.add(810); + Random random = new Random(); + return numList.get(random.nextInt(4)); + } + +} diff --git a/src/main/java/huihui/crsc/utils/ChatUtils.java b/src/main/java/huihui/crsc/utils/ChatUtils.java new file mode 100644 index 0000000..a5364cd --- /dev/null +++ b/src/main/java/huihui/crsc/utils/ChatUtils.java @@ -0,0 +1,59 @@ +package huihui.crsc.utils; + +import huihui.crsc.ClickRailSpawnCar; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Chat工具类 + * @author WindLeaf_qwq + */ +public class ChatUtils { + // 存放颜色代码对应的中文 + /** + * 将带特殊颜色代码的文本转为带颜色代码的文本 + *

换行 + * 例如:"#RED#你输了!" -> "§c你输了!" + * @param string 带特殊颜色代码的文本 + * @return 转换后的文本 + */ + public static String translateColor(Object string) { + // 正则表达式替换 + String result = (String) string; // #RED#[红队] + Pattern regex = Pattern.compile("#[A-Z_-]+#"); + Matcher matcher = regex.matcher(result); + while (matcher.find()) { + String code = matcher.group(); // #RED# + result = result.replaceFirst( + code, // #RED# + "§" + ChatColor.valueOf(code.replaceAll("#", "")).getChar() // §c + ); + } + return result; + } + + /** + * 将带特殊颜色代码的文本转为带颜色代码的文本 (带占位符) + *

+ * 例如:"#RED#你输了!" -> "§c你输了!" + * @param string 带特殊颜色代码的文本 + * @param args 占位符替换 + * @return 转换后的文本 + */ + public static String translateColor(Object string, Object... args) { + return translateColor(String.format((String) string, args)); + } + + /** + * 全服公告消息(带占位符) + * @param message 要公告的消息 + * @param args 占位符替换 + */ + public static void broadcast(String message, Object... args) { + Bukkit.getOnlinePlayers().forEach(it -> PlayerUtils.send(it, ClickRailSpawnCar.normal + message, args)); + } + +} diff --git a/src/main/java/huihui/crsc/utils/LogUtils.java b/src/main/java/huihui/crsc/utils/LogUtils.java new file mode 100644 index 0000000..1810c7d --- /dev/null +++ b/src/main/java/huihui/crsc/utils/LogUtils.java @@ -0,0 +1,47 @@ +package huihui.crsc.utils; + + +import huihui.crsc.ClickRailSpawnCar; + +/** + * 日志工具库 + * @author X_huihui + */ +public class LogUtils { + // 定义instance + static ClickRailSpawnCar instance = ClickRailSpawnCar.instance; + + /** + * LogInfo + * @param log 要输出的info + */ + public static void info(Object log){ + instance.getLogger().info(ChatUtils.translateColor(log.toString())); + } + + /** + * LogInfo(WithArguments) + * @param log 要输出的info + * @param args 占位符 + */ + public static void info(Object log, Object... args){ + instance.getLogger().info(ChatUtils.translateColor(log.toString(), args)); + } + + /** + * LogWarning + * @param log 要输出的warning + */ + public static void warning(Object log){ + instance.getLogger().warning(ChatUtils.translateColor(log.toString())); + } + + /** + * LogWarning(WithArguments) + * @param log 要输出的warning + * @param args 占位符 + */ + public static void warning(Object log, Object... args){ + instance.getLogger().warning(ChatUtils.translateColor(log.toString(), args)); + } +} diff --git a/src/main/java/huihui/crsc/utils/PlayerUtils.java b/src/main/java/huihui/crsc/utils/PlayerUtils.java new file mode 100644 index 0000000..6684868 --- /dev/null +++ b/src/main/java/huihui/crsc/utils/PlayerUtils.java @@ -0,0 +1,33 @@ +package huihui.crsc.utils; + +import org.bukkit.command.CommandSender; + +import java.util.Collections; + +/** + * Player工具类 + * @author WindLeaf_qwq + */ +public class PlayerUtils { + + /** + * 给玩家发送信息 + * @param sender 玩家或控制台 + * @param message 要发送的消息 + */ + public static void send(CommandSender sender, Object message) { + StringBuilder sb = new StringBuilder(); + Collections.singletonList(message).forEach(it -> sb.append(it).append(" ")); + sender.sendMessage(ChatUtils.translateColor(sb.toString())); + } + + /** + * 给玩家发送消息(带占位符) + * @param sender 玩家或控制台 + * @param message 要发送的信息 + * @param args 占位符替换 + */ + public static void send(CommandSender sender, Object message, Object... args) { + send(sender, String.format((String) message, args)); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8a87159..4a22b60 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,4 @@ name: ClickRailSpawnCar version: '${project.version}' main: huihui.crsc.ClickRailSpawnCar -api-version: 1.16 +api-version: 1.13