From db9bcd13718043fc1472e5ec67d2c7435256ebb5 Mon Sep 17 00:00:00 2001 From: yaohunya <31456652@qq.com> Date: Fri, 13 Sep 2024 06:36:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E6=B8=B8=E6=88=8F=E6=A1=86?= =?UTF-8?q?=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + .../com/yaohun/farmingwar/FarmingWar.java | 59 ++++++++++++++ .../java/com/yaohun/farmingwar/GiftData.java | 65 +++++++++++++++ .../java/com/yaohun/farmingwar/LikeData.java | 41 ++++++++++ .../yaohun/farmingwar/effects/BigWindill.java | 70 ++++++++++++++++ .../farmingwar/effects/SummonTheBee.java | 67 +++++++++++++++ .../java/com/yaohun/farmingwar/game/Game.java | 31 +++++++ .../listener/GameEffectListener.java | 39 +++++++++ .../farmingwar/listener/GameListener.java | 15 ++++ .../farmingwar/listener/GamePotect.java | 51 ++++++++++++ .../farmingwar/listener/JoinListener.java | 34 ++++++++ .../farmingwar/listener/PlayerListener.java | 15 ++++ .../liveevent/GiftEventHandler.java | 27 +++++++ .../farmingwar/liveevent/GiftListener.java | 34 ++++++++ .../farmingwar/liveevent/LikeListener.java | 41 ++++++++++ .../farmingwar/manager/GameManager.java | 81 +++++++++++++++++++ .../com/yaohun/farmingwar/util/GiftUtil.java | 70 ++++++++++++++++ src/main/java/tools/BossBarUtil.java | 45 +++++++++++ src/main/java/tools/CDTimeAPI.java | 58 +++++++++++++ src/main/java/tools/GameUtil.java | 33 ++++++++ src/main/java/tools/MathUtil.java | 29 +++++++ src/main/java/tools/RandomUtil.java | 36 +++++++++ src/main/resources/plugin.yml | 6 ++ 23 files changed, 949 insertions(+) create mode 100644 .gitignore create mode 100644 src/main/java/com/yaohun/farmingwar/FarmingWar.java create mode 100644 src/main/java/com/yaohun/farmingwar/GiftData.java create mode 100644 src/main/java/com/yaohun/farmingwar/LikeData.java create mode 100644 src/main/java/com/yaohun/farmingwar/effects/BigWindill.java create mode 100644 src/main/java/com/yaohun/farmingwar/effects/SummonTheBee.java create mode 100644 src/main/java/com/yaohun/farmingwar/game/Game.java create mode 100644 src/main/java/com/yaohun/farmingwar/listener/GameEffectListener.java create mode 100644 src/main/java/com/yaohun/farmingwar/listener/GameListener.java create mode 100644 src/main/java/com/yaohun/farmingwar/listener/GamePotect.java create mode 100644 src/main/java/com/yaohun/farmingwar/listener/JoinListener.java create mode 100644 src/main/java/com/yaohun/farmingwar/listener/PlayerListener.java create mode 100644 src/main/java/com/yaohun/farmingwar/liveevent/GiftEventHandler.java create mode 100644 src/main/java/com/yaohun/farmingwar/liveevent/GiftListener.java create mode 100644 src/main/java/com/yaohun/farmingwar/liveevent/LikeListener.java create mode 100644 src/main/java/com/yaohun/farmingwar/manager/GameManager.java create mode 100644 src/main/java/com/yaohun/farmingwar/util/GiftUtil.java create mode 100644 src/main/java/tools/BossBarUtil.java create mode 100644 src/main/java/tools/CDTimeAPI.java create mode 100644 src/main/java/tools/GameUtil.java create mode 100644 src/main/java/tools/MathUtil.java create mode 100644 src/main/java/tools/RandomUtil.java create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..37b1922 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea/ +/out/ diff --git a/src/main/java/com/yaohun/farmingwar/FarmingWar.java b/src/main/java/com/yaohun/farmingwar/FarmingWar.java new file mode 100644 index 0000000..07662e5 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/FarmingWar.java @@ -0,0 +1,59 @@ +package com.yaohun.farmingwar; + +import com.yaohun.farmingwar.game.Game; +import com.yaohun.farmingwar.listener.*; +import com.yaohun.farmingwar.liveevent.GiftListener; +import com.yaohun.farmingwar.liveevent.LikeListener; +import com.yaohun.farmingwar.manager.GameManager; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +public class FarmingWar extends JavaPlugin { + + private static FarmingWar instance; + private static Game game; + private static GameManager gameManager; + + @Override + public void onEnable() { + instance = this; + gameManager = new GameManager(); + game = new Game(); + Bukkit.getPluginManager().registerEvents(new GiftListener(), this); + Bukkit.getPluginManager().registerEvents(new LikeListener(gameManager), this); + Bukkit.getPluginManager().registerEvents(new GamePotect(), this); + Bukkit.getPluginManager().registerEvents(new GameEffectListener(), this); + Bukkit.getPluginManager().registerEvents(new GameListener(this), this); + Bukkit.getPluginManager().registerEvents(new PlayerListener(this), this); + Bukkit.getPluginManager().registerEvents(new JoinListener(this), this); + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length == 0) { + sender.sendMessage("/game start - 开始游戏"); + return true; + }else { + String subCommand = args[0]; + if (subCommand.equalsIgnoreCase("start")) { + if (!(sender instanceof Player)) { + sender.sendMessage("非玩家无法开始游戏"); + return true; + } + if (game.isStarted()) { + sender.sendMessage("游戏已开始"); + return true; + } + game.start(); + } + } + return false; + } + + public static Game getGame() {return game;} + public static FarmingWar inst() {return instance;} + public static GameManager getGameManager() {return gameManager;} +} diff --git a/src/main/java/com/yaohun/farmingwar/GiftData.java b/src/main/java/com/yaohun/farmingwar/GiftData.java new file mode 100644 index 0000000..7cae577 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/GiftData.java @@ -0,0 +1,65 @@ +package com.yaohun.farmingwar; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import tools.GameUtil; + +public class GiftData { + private String giftName; + private String event; + private String comple_type; + private String sounds; + private int amount = 0; + + public GiftData(String giftName, FileConfiguration yml){ + String str = "礼物设置."+giftName+"."; + this.giftName = giftName; + this.comple_type = "NULL"; + if(yml.getString(str+"进度") != null){ + String[] stringSp = yml.getString(str+"进度").split("#"); + String tpye = stringSp[0]; + int a = Integer.parseInt(stringSp[1]); + this.comple_type = tpye; + this.amount = a; + } + this.sounds = yml.getString(str+"声音","NULL"); + this.event = yml.getString(str + "效果"); + if (comple_type.equalsIgnoreCase("NULL")) { + Bukkit.getConsoleSender().sendMessage("事件: " + this.event + " 条件: " + giftName+" 声音: "+this.sounds); + }else{ + if(comple_type.equalsIgnoreCase("take")){ + Bukkit.getConsoleSender().sendMessage("事件: " + this.event + " 条件: " + giftName+" 减"+amount+"赢"); + } else if(comple_type.equalsIgnoreCase("add")){ + Bukkit.getConsoleSender().sendMessage("事件: " + this.event + " 条件: " + giftName+" 加"+amount+"赢"); + } + } + } + public String getEvent() { + return event; + } + + public String getComple_Show() { + if(comple_type.equalsIgnoreCase("take")){ + return "§c赢-"+amount; + }else if(comple_type.equalsIgnoreCase("add")){ + return "§b赢+"+amount; + } + return ""; + } + + public void OutCompleEvent(int amount) { + int new_amount = this.amount * amount; + if(comple_type.equalsIgnoreCase("take")){ + // ColorblindWar.getGame().effect_takeComplete(new_amount); + Bukkit.broadcastMessage("§c[系统]§a今日挑战进度: §c-"+new_amount); + } else if(comple_type.equalsIgnoreCase("add")){ + // ColorblindWar.getGame().effect_addComplete(new_amount); + Bukkit.broadcastMessage("§c[系统]§a今日挑战进度: §b+"+new_amount); + } + } + public void OutPlaySoundsEvent() { + if(!sounds.equalsIgnoreCase("NULL")) { + GameUtil.sendAllSound(sounds); + } + } +} diff --git a/src/main/java/com/yaohun/farmingwar/LikeData.java b/src/main/java/com/yaohun/farmingwar/LikeData.java new file mode 100644 index 0000000..039c5b4 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/LikeData.java @@ -0,0 +1,41 @@ +package com.yaohun.farmingwar; + +public class LikeData { + + private String effectKey; + private int needAmount; + private int nowAmount; + + public LikeData(String effectKey, int needAmount){ + this.effectKey = effectKey; + this.needAmount = needAmount; + this.nowAmount = 0; + } + + public String getEffectKey() { + return effectKey; + } + + public boolean meetTheQuantityRequirement(){ + if(nowAmount >= needAmount){ + return true; + } + return false; + } + + public int getNeedAmount() { + return needAmount; + } + + public int getNowAmount() { + return nowAmount; + } + + public void addNowAmount(int amount){ + setNowAmount(getNowAmount()+amount); + } + + public void setNowAmount(int nowAmount) { + this.nowAmount = nowAmount; + } +} diff --git a/src/main/java/com/yaohun/farmingwar/effects/BigWindill.java b/src/main/java/com/yaohun/farmingwar/effects/BigWindill.java new file mode 100644 index 0000000..f0382f9 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/effects/BigWindill.java @@ -0,0 +1,70 @@ +package com.yaohun.farmingwar.effects; + +import com.yaohun.farmingwar.FarmingWar; +import com.yaohun.farmingwar.game.Game; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; +import tools.CDTimeAPI; + +import java.util.UUID; + +public class BigWindill { + + public static void apply(Game game, Player zhubo, int seconds){ + // 检测游戏是否启动 + if(!game.isStarted()){return;} + // 获取主播的uuid + UUID uuid = zhubo.getUniqueId(); + // 计时器key + String timepieceKey = "bigWindill"; + startTimepieceEvent(game,zhubo,timepieceKey); + if(CDTimeAPI.isCD(uuid,timepieceKey)){ + long newTime = CDTimeAPI.getCD(uuid,timepieceKey) + (1000L * seconds); + CDTimeAPI.setPlayerCD(uuid,timepieceKey,newTime); + } else { + CDTimeAPI.setPlayerCD(uuid,timepieceKey,1000L * seconds); + } + } + + public static void startTimepieceEvent(Game game,Player zhubo,String timepieceKey){ + if(CDTimeAPI.isCD(zhubo.getUniqueId(),timepieceKey)){ + return; + } + BukkitTask task = new BukkitRunnable() { + double yaw = 0; + int time = 0; + @Override + public void run() { + if (!CDTimeAPI.isCD(zhubo.getUniqueId(), timepieceKey)) { + cancel(); + return; + } + if(time >= 55){ + time -= 55; + zhubo.getWorld().strikeLightningEffect(zhubo.getLocation()); + if(CDTimeAPI.getCD(zhubo.getUniqueId(),timepieceKey) >= 2000) { + zhubo.playSound(zhubo.getLocation(), "dafengche", 1, 1); + } + } + yaw += 24.0; // 每次旋转2度 + if (yaw >= 360) { + yaw -= 360; + } + // 获取玩家当前位置 + Location loc = zhubo.getLocation(); + // 只设置yaw,不改变位置 + loc.setYaw((float) yaw); + // 设置新的 yaw,pitch 保持不变 + zhubo.setRotation(loc.getYaw(), loc.getPitch()); + zhubo.getWorld().spawnParticle(Particle.CLOUD, zhubo.getLocation(), 2); + // 生成云状粒子 + time++; + } + }.runTaskTimer(FarmingWar.inst(), 0L, 2L); + game.addTasks(task); + } + +} diff --git a/src/main/java/com/yaohun/farmingwar/effects/SummonTheBee.java b/src/main/java/com/yaohun/farmingwar/effects/SummonTheBee.java new file mode 100644 index 0000000..39a7241 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/effects/SummonTheBee.java @@ -0,0 +1,67 @@ +package com.yaohun.farmingwar.effects; + +import com.yaohun.farmingwar.FarmingWar; +import com.yaohun.farmingwar.game.Game; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.entity.Bee; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; +import tools.RandomUtil; + +public class SummonTheBee { + public static void apply(Game game, Player zhubo, String userName, int amount) { + if (!game.isStarted()) {return;} + if(amount >= 200){ + // 获取掉落次数 + int forA = 10 + (amount / 50); + // 获取每次掉落数量 + int forB = (int) (amount / forA * 0.7); + for (int i = 0; i < forA;i++){ + Bukkit.getScheduler().runTaskLater(FarmingWar.inst(), () -> { + for (int a = 0; a < forB;a++) { + spawnMobs(zhubo,userName); + } + zhubo.playSound(zhubo.getLocation(), "huh", 1.0F, 1.0F); + },(long) i*5); + } + }else if(amount >= 120){ + for (int i = 0; i < 20;i++){ + Bukkit.getScheduler().runTaskLater(FarmingWar.inst(), () -> { + for (int ai = 0; ai < 6;ai++) { + spawnMobs(zhubo,userName); + } + zhubo.playSound(zhubo.getLocation(), "huh", 1.0F, 1.0F); + },(long) i*5); + } + }else if(amount >= 30){ + for (int i = 0; i < 10;i++){ + Bukkit.getScheduler().runTaskLater(FarmingWar.inst(), () -> { + for (int ai = 0; ai < 3;ai++) { + spawnMobs(zhubo,userName); + } + zhubo.playSound(zhubo.getLocation(), "huh", 1.0F, 1.0F); + },(long) i*5); + } + }else { + for (int i = 0; i < amount; i++) { + spawnMobs(zhubo,userName); + } + zhubo.playSound(zhubo.getLocation(), "huh", 1.0F, 1.0F); + } + } + + public static void spawnMobs(Player zhubo,String userName){ + Location location = zhubo.getLocation().add(0,3,0); + location.getWorld().spawnParticle(Particle.FIREWORKS_SPARK, location, 2, 1, 1, 1, 0.085); + Bee entity = (Bee) location.getWorld().spawnEntity(location, EntityType.BEE); + entity.setTarget(zhubo); + entity.setHealth(1); + entity.setCustomName("§c" + userName); + entity.setCustomNameVisible(true); + Vector vector = new Vector(RandomUtil.getRandomDouble(-1,1,1),RandomUtil.getRandomDouble(-1,1,1),RandomUtil.getRandomDouble(-1,1,1)); + entity.setVelocity(vector); + } +} diff --git a/src/main/java/com/yaohun/farmingwar/game/Game.java b/src/main/java/com/yaohun/farmingwar/game/Game.java new file mode 100644 index 0000000..911d6d7 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/game/Game.java @@ -0,0 +1,31 @@ +package com.yaohun.farmingwar.game; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; + +import java.util.ArrayList; +import java.util.List; + +public class Game { + + + private boolean started = false; + private List tasks = new ArrayList<>(); + private List entities = new ArrayList<>(); + public Game(){ + + } + + public void start() { + if(started){return;} + } + + public boolean isStarted() {return started;} + public List getTasks() { + return tasks; + } + public void addTasks(BukkitTask task){ + this.tasks.add(task); + } +} diff --git a/src/main/java/com/yaohun/farmingwar/listener/GameEffectListener.java b/src/main/java/com/yaohun/farmingwar/listener/GameEffectListener.java new file mode 100644 index 0000000..0b3a3f4 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/listener/GameEffectListener.java @@ -0,0 +1,39 @@ +package com.yaohun.farmingwar.listener; + +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Color; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import tools.CDTimeAPI; +import tools.RandomUtil; + +import java.util.UUID; + +public class GameEffectListener implements Listener { + + @EventHandler + public void onMove(PlayerMoveEvent e) { + Player p = e.getPlayer(); + UUID uuid = p.getUniqueId(); + Location loc = p.getLocation(); + World world = loc.getWorld(); + if (CDTimeAPI.getCD(uuid, "SummonArrowEffect") >= 1) { + long milliseconds = CDTimeAPI.getCD(uuid, "SummonArrowEffect"); + int seconds = (int) milliseconds / 250; + String message = "§6箭头剩余未发射: §b" + seconds + "支"; + p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message)); + } else if (CDTimeAPI.getCD(uuid, "bigWindill") >= 1) { + long milliseconds = CDTimeAPI.getCD(uuid, "bigWindill"); + double seconds = (double) milliseconds / 1000; + String message = "§6大风车倒计时: §b" + String.format("%.1f", seconds) + "秒"; + p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(message)); + } + } + +} diff --git a/src/main/java/com/yaohun/farmingwar/listener/GameListener.java b/src/main/java/com/yaohun/farmingwar/listener/GameListener.java new file mode 100644 index 0000000..b2eaa24 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/listener/GameListener.java @@ -0,0 +1,15 @@ +package com.yaohun.farmingwar.listener; + +import com.yaohun.farmingwar.FarmingWar; +import com.yaohun.farmingwar.game.Game; +import org.bukkit.event.Listener; + +public class GameListener implements Listener { + + private FarmingWar farmingWar; + private Game game; + public GameListener(FarmingWar farmingWarMainClass){ + farmingWar = farmingWarMainClass; + game = FarmingWar.getGame(); + } +} diff --git a/src/main/java/com/yaohun/farmingwar/listener/GamePotect.java b/src/main/java/com/yaohun/farmingwar/listener/GamePotect.java new file mode 100644 index 0000000..2bf8fc0 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/listener/GamePotect.java @@ -0,0 +1,51 @@ +package com.yaohun.farmingwar.listener; + +import org.bukkit.Sound; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.*; +import org.bukkit.event.entity.EntityCombustEvent; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.FoodLevelChangeEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerPickupItemEvent; +import org.bukkit.event.weather.WeatherChangeEvent; + +public class GamePotect implements Listener { + + @EventHandler // 取消生物燃烧 + public void onEntityCombust(EntityCombustEvent event) { + if (event.getEntity() instanceof LivingEntity) { + event.setCancelled(true); + } + } + @EventHandler // 禁止火焰燃烧方块 + public void onBlockFire(BlockBurnEvent e) { + e.setCancelled(true); + } + @EventHandler// 禁止火焰燃烧物品 + public void onBlockIgnite(BlockIgniteEvent e) { + e.setCancelled(true); + } + @EventHandler(priority= EventPriority.HIGH) + public void onWeatherChange(WeatherChangeEvent e) { + e.setCancelled(true); + } + @EventHandler(priority= EventPriority.HIGH) + public void onFoodChange(FoodLevelChangeEvent e) { + e.setCancelled(true); + } + @EventHandler // 拾取任何物品不进入背包直接清理 + public void onPick(PlayerPickupItemEvent e){ + Player p = e.getPlayer(); + p.playSound(p.getLocation(), Sound.ENTITY_ITEM_PICKUP, 1, 1); + e.setCancelled(true); + e.getItem().remove(); + } + + @EventHandler // 禁止丢弃任何物品 + public void onDrop(PlayerDropItemEvent e){e.setCancelled(true);} +} diff --git a/src/main/java/com/yaohun/farmingwar/listener/JoinListener.java b/src/main/java/com/yaohun/farmingwar/listener/JoinListener.java new file mode 100644 index 0000000..5defdf4 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/listener/JoinListener.java @@ -0,0 +1,34 @@ +package com.yaohun.farmingwar.listener; + +import com.yaohun.farmingwar.FarmingWar; +import com.yaohun.farmingwar.game.Game; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.scheduler.BukkitRunnable; + +public class JoinListener implements Listener { + + private FarmingWar farmingWar; + private Game game; + public JoinListener(FarmingWar farmingWarMainClass){ + farmingWar = farmingWarMainClass; + game = FarmingWar.getGame(); + } + + @EventHandler + public void onJoin(PlayerJoinEvent e){ + Player p = e.getPlayer(); + p.setGameMode(GameMode.ADVENTURE); + new BukkitRunnable() { + @Override + public void run() { + if(!game.isStarted()) { + game.start(); + } + } + }.runTaskLater(farmingWar,10L); + } +} diff --git a/src/main/java/com/yaohun/farmingwar/listener/PlayerListener.java b/src/main/java/com/yaohun/farmingwar/listener/PlayerListener.java new file mode 100644 index 0000000..62a1adf --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/listener/PlayerListener.java @@ -0,0 +1,15 @@ +package com.yaohun.farmingwar.listener; + +import com.yaohun.farmingwar.FarmingWar; +import com.yaohun.farmingwar.game.Game; +import org.bukkit.event.Listener; + +public class PlayerListener implements Listener { + + private FarmingWar farmingWar; + private Game game; + public PlayerListener(FarmingWar farmingWarMainClass){ + farmingWar = farmingWarMainClass; + game = FarmingWar.getGame(); + } +} diff --git a/src/main/java/com/yaohun/farmingwar/liveevent/GiftEventHandler.java b/src/main/java/com/yaohun/farmingwar/liveevent/GiftEventHandler.java new file mode 100644 index 0000000..72fce7d --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/liveevent/GiftEventHandler.java @@ -0,0 +1,27 @@ +package com.yaohun.farmingwar.liveevent; + +import com.yaohun.farmingwar.FarmingWar; +import com.yaohun.farmingwar.effects.BigWindill; +import com.yaohun.farmingwar.effects.SummonTheBee; +import com.yaohun.farmingwar.game.Game; +import org.bukkit.entity.Player; +import tools.GameUtil; + +public class GiftEventHandler { + + public static void SendHandLer(Player zhubo, String userName, String eventName){ + Game game = FarmingWar.getGame(); + userName = GameUtil.hideName(userName); + if (eventName.equalsIgnoreCase("大风车5秒")) { + BigWindill.apply(game,zhubo,5); + } else if (eventName.contains("蜜蜂")) { + int amount = 1; + if(eventName.contains("X") || eventName.contains("x") || eventName.contains("*")) { + eventName = eventName.replace("*","X"); + String[] eventSp = eventName.toUpperCase().split("X"); + amount = Integer.parseInt(eventSp[1]); + } + SummonTheBee.apply(game, zhubo, userName, amount); + } + } +} diff --git a/src/main/java/com/yaohun/farmingwar/liveevent/GiftListener.java b/src/main/java/com/yaohun/farmingwar/liveevent/GiftListener.java new file mode 100644 index 0000000..854ce51 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/liveevent/GiftListener.java @@ -0,0 +1,34 @@ +package com.yaohun.farmingwar.liveevent; + +import com.io.yutian.mclive.event.LiveGiftEvents; +import com.yaohun.farmingwar.util.GiftUtil; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import tools.GameUtil; +import tools.RandomUtil; + +public class GiftListener implements Listener { + + @EventHandler + public void onGfit(LiveGiftEvents e) { + Player zhubo = e.getPlayer(); + String userName = ""+ RandomUtil.getRandomInt(10000,90000); + boolean butt = false; + if(e.getUser().nickName() != null) { + userName = e.getUser().nickName(); + butt = true; + } + String giftName = e.getName(); + int amount = (int) e.getAmount(); + if (amount <= 0) { + amount = 1; + } + String show_userName = GameUtil.hideName(userName); + GiftUtil.simulatedGiftEffect(zhubo,show_userName,giftName,amount); + for (Player player : Bukkit.getOnlinePlayers()){ + player.sendMessage("§a礼物: §e"+ show_userName+" §d送来了 §e"+giftName+"x"+amount); + } + } +} diff --git a/src/main/java/com/yaohun/farmingwar/liveevent/LikeListener.java b/src/main/java/com/yaohun/farmingwar/liveevent/LikeListener.java new file mode 100644 index 0000000..6a1aa3d --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/liveevent/LikeListener.java @@ -0,0 +1,41 @@ +package com.yaohun.farmingwar.liveevent; + +import com.io.yutian.mclive.event.LiveLikeEvents; +import com.yaohun.farmingwar.LikeData; +import com.yaohun.farmingwar.manager.GameManager; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import tools.RandomUtil; + +import java.util.HashMap; + +public class LikeListener implements Listener { + + private GameManager gameManager; + public LikeListener(GameManager manager){ + gameManager = manager; + } + @EventHandler//点赞 + public void onDianZan(LiveLikeEvents e) { + Player zhubo = e.getPlayer(); + String userName = ""+ RandomUtil.getRandomInt(999,10000); + if(e.getUser().nickName() != null) { + userName = e.getUser().nickName(); + } + long add_amount = e.getCount(); + HashMap likeDataMap = gameManager.getLikeDataMap(); + for (String key : likeDataMap.keySet()){ + LikeData likeData = likeDataMap.get(key); + likeData.addNowAmount((int)add_amount); + if(likeData.meetTheQuantityRequirement()){ + String eventName = likeData.getEffectKey(); + String title = "§c"+eventName; + String subtitle = "§9双击屏幕x"+likeData.getNeedAmount(); + zhubo.sendTitle(title, subtitle,0, 30, 10); + GiftEventHandler.SendHandLer(zhubo,userName,eventName); + likeData.setNowAmount(0); + } + } + } +} diff --git a/src/main/java/com/yaohun/farmingwar/manager/GameManager.java b/src/main/java/com/yaohun/farmingwar/manager/GameManager.java new file mode 100644 index 0000000..c5047b9 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/manager/GameManager.java @@ -0,0 +1,81 @@ +package com.yaohun.farmingwar.manager; + +import com.io.yutian.mclive.event.ZhuboAPI; +import com.yaohun.farmingwar.GiftData; +import com.yaohun.farmingwar.LikeData; +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; + +public class GameManager { + + private static boolean isRandomBox; + private HashMap likeDataMap = new HashMap<>(); + private HashMap giftDataMap = new HashMap<>(); + + public GameManager(){ + Bukkit.getConsoleSender().sendMessage("[日志 - 保卫小麦] 工具注册:"); + loadRandomBoxPlugin(); + Bukkit.getConsoleSender().sendMessage("[日志 - 保卫小麦] 事件注册:"); + File file = new File("./plugins/游戏设置","礼物设置.yml"); + FileConfiguration gift_yml = YamlConfiguration.loadConfiguration(file); + ConfigurationSection likeSection = gift_yml.getConfigurationSection("点赞整蛊"); + if (likeSection != null) { + for (String key : likeSection.getKeys(false)) { + String effect = likeSection.getString(key+".效果"); + int likeAmount = likeSection.getInt(key+".点赞次数"); + likeDataMap.put(effect,new LikeData(effect,likeAmount)); + String message = "事件: "+effect+" 条件: 点赞x"+likeAmount; + Bukkit.getConsoleSender().sendMessage(message); + } + } + ConfigurationSection giftSection = gift_yml.getConfigurationSection("礼物设置"); + if(giftSection != null){ + ZhuboAPI.setGameGiftList(new ArrayList<>(giftSection.getKeys(false))); + for (String giftName : giftSection.getKeys(false)) { + giftDataMap.put(giftName, new GiftData(giftName, gift_yml)); + } + } + } + + public void loadRandomBoxPlugin() { + if(Bukkit.getPluginManager().getPlugin("RandomBox") != null){ + isRandomBox = true; + Bukkit.getConsoleSender().sendMessage("- 随机盲盒: 已启用"); + } else { + isRandomBox = false; + Bukkit.getConsoleSender().sendMessage("- 随机盲盒: 未启用"); + } + } + + + public static boolean isIsRandomBox() { + return isRandomBox; + } + // 判断哪个点赞整蛊效果已达到要求可触发 + public HashMap getLikeDataMap() { + return likeDataMap; + } + public boolean isGiftDataExit(String giftName){ + for (String gift : this.giftDataMap.keySet()){ + if(gift.equalsIgnoreCase(giftName)){ + return true; + } + } + return false; + } + public GiftData getGiftData(String giftName){ + if(this.giftDataMap.get(giftName) == null){ + return null; + } + return this.giftDataMap.get(giftName); + } + public HashMap getGiftDataMap() { + return giftDataMap; + } +} diff --git a/src/main/java/com/yaohun/farmingwar/util/GiftUtil.java b/src/main/java/com/yaohun/farmingwar/util/GiftUtil.java new file mode 100644 index 0000000..7927c58 --- /dev/null +++ b/src/main/java/com/yaohun/farmingwar/util/GiftUtil.java @@ -0,0 +1,70 @@ +package com.yaohun.farmingwar.util; + +import com.yaohun.RandomBox.api.RBoxAPI; +import com.yaohun.farmingwar.FarmingWar; +import com.yaohun.farmingwar.GiftData; +import com.yaohun.farmingwar.liveevent.GiftEventHandler; +import com.yaohun.farmingwar.manager.GameManager; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +public class GiftUtil { + public static void simulatedGiftEffect(Player player, String userName, String giftName, int amount){ + GameManager manager = FarmingWar.getGameManager(); + if(!manager.isGiftDataExit(giftName)){ + return; + } + GiftData giftData = manager.getGiftData(giftName); + giftData.OutCompleEvent(amount); + String eventName = giftData.getEvent(); + if(GiftUtil.specialGiftEffectTriggers(player,eventName,userName,amount)){ + giftData.OutPlaySoundsEvent(); + return; + } + String title = "§c" + eventName; + if(amount >= 2) { + title = "§c" + eventName + " x" + amount; + } + String subtitle = "§9" + giftName; + player.sendTitle(title, subtitle,0, 30, 10); + if (amount <= 1) { + giftData.OutPlaySoundsEvent(); + GiftEventHandler.SendHandLer(player, userName, eventName); + } else { + long dadey = 10L; + for (int i = 0; i < amount; i++) { + Bukkit.getScheduler().runTaskLater(FarmingWar.inst(), () -> { + giftData.OutPlaySoundsEvent(); + GiftEventHandler.SendHandLer(player, userName, eventName); + }, (long) i * dadey); + } + } + } + + public static boolean specialGiftEffectTriggers(Player player,String eventName,String show_userName,int amount) { + if (eventName.contains("盲盒")) { + if (eventName.contains("#")) { + String s1 = "love"; + String s2 = "000304"; + String[] eventSp = eventName.split("#"); + String boxKey = eventSp[1]; + int boxAmount = 1; + if (boxKey.contains("X")) { + boxAmount = Integer.parseInt(boxKey.split("X")[1]); + boxKey = s1 + "-" + s2 + "-" + boxKey.split("X")[0]; + } else if (boxKey.contains("x")) { + boxAmount = Integer.parseInt(boxKey.split("x")[1]); + boxKey = s1 + "-" + s2 + "-" + boxKey.split("x")[0]; + } else { + boxKey = s1 + "-" + s2 + "-" + boxKey; + } + amount = (boxAmount * amount); + RBoxAPI.addUserData(player, show_userName, boxKey, amount); + } else { + System.out.println("[错误 - 盲盒] 随机盲盒在礼物设置中配置错误."); + } + return true; + } + return false; + } +} diff --git a/src/main/java/tools/BossBarUtil.java b/src/main/java/tools/BossBarUtil.java new file mode 100644 index 0000000..fe7ab63 --- /dev/null +++ b/src/main/java/tools/BossBarUtil.java @@ -0,0 +1,45 @@ +package tools; + +import org.bukkit.boss.BarColor; +import org.bukkit.boss.BossBar; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class BossBarUtil { + + public static void setBarColor(BossBar bossBar, double percent){ + if (percent >= 0 && percent <= 0.3333) { + bossBar.setColor(BarColor.RED); + } else if (percent <= 0.6666) { + bossBar.setColor(BarColor.YELLOW); + } else { + bossBar.setColor(BarColor.GREEN); + } + } + + public static void setBarProgress(BossBar bossBar,double percent){ + if (percent <= 0) { + percent = 0; + } else if (percent >= 1) { + percent = 1; + } + bossBar.setProgress(percent); + } + + // BossBarUtil.addBossBar(player,new ArrayList<>(Arrays.asList(bossBar1,bossBar3,bossBar2))); + public static void addBossBar(Player player, List bossBarList){ + for (BossBar bossBar : bossBarList) { + bossBar.addPlayer(player); + } + } + + public static void removeBossBar(Player player,List bossBarList){ + for (BossBar bossBar : bossBarList) { + bossBar.removePlayer(player); + } + } + +} diff --git a/src/main/java/tools/CDTimeAPI.java b/src/main/java/tools/CDTimeAPI.java new file mode 100644 index 0000000..ff25147 --- /dev/null +++ b/src/main/java/tools/CDTimeAPI.java @@ -0,0 +1,58 @@ +package tools; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.UUID; + +public class CDTimeAPI { + private static HashSet cdData = new HashSet<>(); + + public static void setPlayerCD(UUID uuid, String key, long mills) { + CDData data = getCDData(uuid); + long now = System.currentTimeMillis(); + data.setCD(key, now + mills); + } + + public static long getCD(UUID uuid, String key) { + CDData data = getCDData(uuid); + long now = System.currentTimeMillis(); + return data.getCD(key) - now; + } + + public static boolean isCD(UUID uuid, String key) { + return (getCD(uuid, key) > 0L); + } + + public static CDData getCDData(UUID uuid) { + for (CDData cDData : cdData) { + if (cDData.getUuid().equals(uuid)) + return cDData; + } + CDData data = new CDData(uuid); + cdData.add(data); + return data; + } + + public static class CDData{ + private final UUID uuid; + + private final HashMap cdTime; + + public CDData(UUID uuid) { + this.uuid = uuid; + this.cdTime = new HashMap<>(); + } + + public UUID getUuid() { + return this.uuid; + } + + public void setCD(String key, long time) { + this.cdTime.put(key, time); + } + + public long getCD(String key) { + return this.cdTime.getOrDefault(key,-1L); + } + } +} diff --git a/src/main/java/tools/GameUtil.java b/src/main/java/tools/GameUtil.java new file mode 100644 index 0000000..7efa7ea --- /dev/null +++ b/src/main/java/tools/GameUtil.java @@ -0,0 +1,33 @@ +package tools; + +import org.bukkit.Bukkit; +import org.bukkit.Sound; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class GameUtil { + public static String hideName(String audience) { + if (audience.length() <= 2) { + return "**"; + } + char firstChar = audience.charAt(0); + char lastChar = audience.charAt(audience.length() - 1); + StringBuilder maskedString = new StringBuilder(); + for (int i = 1; i < audience.length() - 1; i++) { + maskedString.append('*'); + } + return String.valueOf(firstChar) + maskedString + lastChar; + } + public static void sendMessage(CommandSender sender, String message) { + sender.sendMessage("§c[系统]§a" + message); + } + public static void sendAllSound(String sound) { + for (Player player : Bukkit.getOnlinePlayers()) { + if (sound.contains("_")) { + player.playSound(player.getLocation(), Sound.valueOf(sound), 1, 1); + } else { + player.playSound(player.getLocation(), sound, 0.5F, 1); + } + } + } +} diff --git a/src/main/java/tools/MathUtil.java b/src/main/java/tools/MathUtil.java new file mode 100644 index 0000000..7fdd402 --- /dev/null +++ b/src/main/java/tools/MathUtil.java @@ -0,0 +1,29 @@ +package tools; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class MathUtil { + + public static double round(double d) { + return new BigDecimal(Double.toString(d)).setScale(2, RoundingMode.HALF_UP).doubleValue(); + } + + public static double round(double d, int scale) { + return new BigDecimal(Double.toString(d)).setScale(scale, RoundingMode.HALF_UP).doubleValue(); + } + + public static int clamp(int value, int min, int max) { + return Math.max(min, Math.min(max, value)); + } + + public static double clamp(double value, double min, double max) { + return Math.max(min, Math.min(max, value)); + } + + public static float clamp(float value, float min, float max) { + return Math.max(min, Math.min(max, value)); + } + + +} diff --git a/src/main/java/tools/RandomUtil.java b/src/main/java/tools/RandomUtil.java new file mode 100644 index 0000000..14bf75b --- /dev/null +++ b/src/main/java/tools/RandomUtil.java @@ -0,0 +1,36 @@ +package tools; + +import org.bukkit.Location; +import org.bukkit.World; + +import java.util.Random; + +public class RandomUtil { + + public static boolean random(double d) { + return d >= new Random().nextFloat(); + } + + public static int getRandomInt(int min, int max) { + if (min == max) { + return max; + } + Random r = new Random(); + int i = min < max ? min : max; + int a = min < max ? max : min; + return r.nextInt(a - i + 1) + i; + } + + public static double getRandomDouble(double min, double max, int scl) { + int pow = (int) Math.pow(10, scl); + return Math.floor((Math.random() * (max - min) + min) * pow) / pow; + } + + public static Location getRandomLocation(World world, double minX, double maxX, double minY, double maxY, double minZ, double maxZ) { + double rx = getRandomDouble(minX, maxX, 3); + double ry = getRandomDouble(minY, maxY, 3); + double rz = getRandomDouble(minZ, maxZ, 3); + return new Location(world, rx, ry, rz); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..7cc6b15 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,6 @@ +name: FarmingWar +main: com.yaohun.farmingwar.FarmingWar +version: 1.0 +api-version: 1.18.2 +commands: + game: \ No newline at end of file