Skip to content

Commit

Permalink
Remake the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuihui1022 authored Aug 4, 2023
1 parent 99b2972 commit d0fc832
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>huihui.crsc</groupId>
<artifactId>ClickRailSpawnCar</artifactId>
<version>1.0</version>
<version>2.0</version>
<packaging>jar</packaging>

<name>ClickRailSpawnCar</name>
Expand Down Expand Up @@ -67,7 +67,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<version>1.13-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/huihui/crsc/ClickRailSpawnCar.java
Original file line number Diff line number Diff line change
@@ -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<Player, Long> 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]插件已注销");
}
}
120 changes: 120 additions & 0 deletions src/main/java/huihui/crsc/events/ScanPlayerEvents.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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));
}

}
59 changes: 59 additions & 0 deletions src/main/java/huihui/crsc/utils/ChatUtils.java
Original file line number Diff line number Diff line change
@@ -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 {
// 存放颜色代码对应的中文
/**
* 将带特殊颜色代码的文本转为带颜色代码的文本
* <p>换行
* 例如:"#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;
}

/**
* 将带特殊颜色代码的文本转为带颜色代码的文本 (带占位符)
* <p>
* 例如:"#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));
}

}
47 changes: 47 additions & 0 deletions src/main/java/huihui/crsc/utils/LogUtils.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
33 changes: 33 additions & 0 deletions src/main/java/huihui/crsc/utils/PlayerUtils.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ClickRailSpawnCar
version: '${project.version}'
main: huihui.crsc.ClickRailSpawnCar
api-version: 1.16
api-version: 1.13

0 comments on commit d0fc832

Please sign in to comment.