测试版

This commit is contained in:
yaohunya 2024-10-30 20:39:15 +08:00
commit c1b5c1bfd6
18 changed files with 1197 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target/
/out/
/.idea/

52
pom.xml Normal file
View File

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>WheatWar</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>public-rpg</id>
<url>https://repo.aurora-pixels.com/repository/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>net.md5.bungee</groupId>
<artifactId>BungeeCord</artifactId>
<version>1.12.2</version>
</dependency>
<dependency>
<groupId>com.fastasyncworldedit.bukkit</groupId>
<artifactId>FastAsyncWorldEdit</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>de.tr7zw.nbtapi</groupId>
<artifactId>NbtAPI</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.yaohun.teleport-API</groupId>
<artifactId>JGTeleportSpigot</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,56 @@
package com.yaohun.wheatwar;
import com.yaohun.wheatwar.effect.SummonTheVillager;
import com.yaohun.wheatwar.game.Game;
import com.yaohun.wheatwar.listener.GameEffectListener;
import com.yaohun.wheatwar.listener.GameListener;
import com.yaohun.wheatwar.listener.GamePotect;
import com.yaohun.wheatwar.listener.JoinListener;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import tools.GameUtil;
public class WheatWar extends JavaPlugin {
private static WheatWar instance;
private static Game game;
@Override
public void onEnable() {
instance = this;
game = new Game();
getServer().getPluginManager().registerEvents(new GameEffectListener(),this);
getServer().getPluginManager().registerEvents(new GameListener(),this);
getServer().getPluginManager().registerEvents(new GamePotect(),this);
getServer().getPluginManager().registerEvents(new JoinListener(),this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String Command, String[] args) {
if (Command.equalsIgnoreCase("livegift")) {
// RepairGiftGui.OpenGui((Player) sender);
return true;
}
if (Command.equalsIgnoreCase("gameedit")) {
// GameEditGui.OpenGui((Player) sender);
return true;
}
if (Command.equalsIgnoreCase("game")) {
if (args.length == 0) {
sender.sendMessage("/game start - 开始游戏");
return true;
}
if(args.length == 1 && args[0].equalsIgnoreCase("test1")){
SummonTheVillager.apply(getGame(),(Player) sender,"1111",1);
sender.sendMessage("执行成功.");
}
}
return false;
}
public static WheatWar inst(){return instance;}
public static Game getGame() {return game;}
}

View File

@ -0,0 +1,82 @@
package com.yaohun.wheatwar.effect;
import com.yaohun.wheatwar.WheatWar;
import com.yaohun.wheatwar.game.Game;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.bukkit.util.Vector;
import tools.CDTimeAPI;
import java.util.UUID;
public class SummonTheVillager {
public static void apply(Game game, Player zhubo, String userName, int amount) {
if (!game.isStarted()) {
return;
}
// 获取主播的uuid
UUID uuid = zhubo.getUniqueId();
// 计时器key
String timepieceKey = "SummonVillagerEffect";
startTimepieceEvent(game,zhubo,userName,timepieceKey);
if(CDTimeAPI.isCD(uuid,timepieceKey)){
long newTime = CDTimeAPI.getCD(uuid,timepieceKey) + (245L * amount);
CDTimeAPI.setPlayerCD(uuid,timepieceKey,newTime);
} else {
CDTimeAPI.setPlayerCD(uuid,timepieceKey,245L * amount);
}
}
public static void startTimepieceEvent(Game game, Player zhubo, String userName, String timepieceKey) {
if (CDTimeAPI.isCD(zhubo.getUniqueId(), timepieceKey)) {
return;
}
BukkitTask task = new BukkitRunnable() {
@Override
public void run() {
if (!CDTimeAPI.isCD(zhubo.getUniqueId(), timepieceKey)) {
cancel();
return;
}
spawnMobs(game,userName);
}
}.runTaskTimer(WheatWar.inst(), 0L, 5L);
game.addTasks(task);
}
public static void spawnMobs(Game game,String userName) {
Location location = game.getFarmlandRandom().clone().add(0,1,0);
Villager entity = (Villager) location.getWorld().spawnEntity(location, EntityType.VILLAGER);
entity.setProfession(Villager.Profession.FARMER);
entity.setCustomNameVisible(true);
entity.setCustomName("§a" + userName);
Location centralPoint = game.getCentralPoint();
// 计算方向
Vector direction = centralPoint.toVector().subtract(location.toVector()).normalize();
// 设置初速度
double initialVerticalVelocity = 0.4; // 向上抛的速度
double horizontalVelocity = 1.35; // 控制水平抛出的力度
// 创建一个向量包括水平和垂直方向的速度
Vector velocity = direction.multiply(horizontalVelocity).setY(initialVerticalVelocity);
entity.setVelocity(velocity);
new BukkitRunnable() {
private int i = 0;
@Override
public void run() {
if(i >= 15){
cancel();
return;
}
game.getWorld().spawnParticle(Particle.ELECTRIC_SPARK,entity.getLocation().clone().add(0,1,0),2);
//game.getWorld().spawnParticle(Particle.VILLAGER_HAPPY,entity.getLocation().clone().add(0,1,0),2);
i++;
}
}.runTaskTimer(WheatWar.inst(),0,1L);
}
}

View File

@ -0,0 +1,107 @@
package com.yaohun.wheatwar.game;
import com.yaohun.wheatwar.WheatWar;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import tools.BossBarUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Game {
private World world;
private Region region;
private BossBar bossBar1;
private BossBar bossBar2;
private boolean started = false;
private Location centralPoint;
private List<Location> wheatList = new ArrayList<>();
private List<BukkitTask> tasks = new ArrayList<>();
public Game(){
this.world = Bukkit.getWorld("world");
this.region = new Region(world,new Point(-30,68,-33),new Point(32,68,29));
this.centralPoint = new Location(world,1,72,-2);
this.bossBar1 = Bukkit.createBossBar("今日挑战进度", BarColor.WHITE, BarStyle.SEGMENTED_10);
this.bossBar2 = Bukkit.createBossBar("补种进度", BarColor.GREEN, BarStyle.SEGMENTED_20);
loadGameMapFarmland();
}
private void updateCompleteWheatGoalBossBar() {
int cNow = GameSite.getMatureWheatAmount();
int cMax = wheatList.size();
double d = (double) cNow / (double) cMax;
String bossTitle = "§6成熟小麦数量§c: §f"+cNow+" §b("+String.format("%.2f",(d*100))+"%)";
bossBar2.setTitle(bossTitle);
BossBarUtil.setBarColor(bossBar2,d);
BossBarUtil.setBarProgress(bossBar2,d);
}
public void start(){
if (started) {
return;
}
this.started = true;
tasks.add(new BukkitRunnable() {
@Override
public void run() {
updateCompleteWheatGoalBossBar();
}
}.runTaskTimer(WheatWar.inst(), 0L, 5L));
}
public void initPlayerData(Player player) {
player.setArrowCooldown(6000);
player.setGameMode(GameMode.SURVIVAL);
player.setAllowFlight(true);
// bossBar1.addPlayer(player);
bossBar2.addPlayer(player);
}
public Location getFarmlandRandom(){
List<Location> locationList = getWheatList();
Collections.shuffle(locationList);
Random random = new Random();
return locationList.get(random.nextInt(locationList.size()));
}
public World getWorld() {return world;}
public List<Location> getWheatList() {return wheatList;}
public Region getRegion() {return region;}
public Location getCentralPoint() {return centralPoint;}
public boolean isStarted() {return started;}
public void addTasks(BukkitTask task){
this.tasks.add(task);
}
public void loadGameMapFarmland(){
List<Location> locationList = new ArrayList<>();
for (int y = (int) Math.floor(67); y <= 67; y++) {
for (int x = (int) Math.floor(region.getMin().getX()); x <= region.getMax().getX(); x++) {
for (int z = (int) Math.floor(region.getMin().getZ()); z <= region.getMax().getZ(); z++) {
Block block = region.getWorld().getBlockAt(x, y, z);
if (!block.getType().equals(Material.AIR)) {
if(block.getType().equals(Material.FARMLAND)){
locationList.add(block.getLocation().clone().add(0,1,0));
}
}
}
}
}
this.wheatList = locationList;
Bukkit.getConsoleSender().sendMessage("[日志 - 保卫小麦] 游戏地图:");
Bukkit.getConsoleSender().sendMessage(" - "+this.wheatList.size()+"块小麦");
}
}

View File

@ -0,0 +1,36 @@
package com.yaohun.wheatwar.game;
import com.yaohun.wheatwar.WheatWar;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.BlockData;
import java.util.ArrayList;
import java.util.List;
public class GameSite {
// 获取已成熟小麦数量
public static int getMatureWheatAmount(){
Game game = WheatWar.getGame();
World world = game.getWorld();
List<Location> locationList = game.getWheatList();
int i = 0;
for (Location location : locationList){
Block block = world.getBlockAt(location);
if (!block.getType().equals(Material.AIR)) {
BlockData blockData = block.getBlockData();
if (blockData instanceof Ageable ageable) {
if (ageable.getAge() == 7) {
i++;
}
}
}
}
return i;
}
}

View File

@ -0,0 +1,94 @@
package com.yaohun.wheatwar.game;
import com.sk89q.worldedit.math.BlockVector3;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Objects;
public class Point {
private double x;
private double y;
private double z;
public Point(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public void setZ(double z) {
this.z = z;
}
public Point clone() {
return new Point(x, y, z);
}
public Location toLocation(World world) {
return new Location(world, x, y, z, 0, 0);
}
public static Point of(Location location) {
return new Point(location.getX(), location.getY(), location.getZ());
}
public static Point deserialize(ConfigurationSection section) {
return new Point(section.getDouble("x"), section.getDouble("y"), section.getDouble("z"));
}
public BlockVector3 toBlockVector3() {
return BlockVector3.at(x, y, z);
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
", z=" + z +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return Double.compare(point.x, x) == 0 && Double.compare(point.y, y) == 0 && Double.compare(point.z, z) == 0;
}
@Override
public int hashCode() {
return Objects.hash(x, y, z);
}
}

View File

@ -0,0 +1,69 @@
package com.yaohun.wheatwar.game;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
public class Region {
private World world;
private Point min;
private Point max;
public Region(World world, Point min, Point max) {
this.world = world;
this.min = min;
this.max = max;
}
public World getWorld() {
return world;
}
public Point getMin() {
return min;
}
public Point getMax() {
return max;
}
public void setMax(Point max) {
this.max = max;
}
public void setMin(Point min) {
this.min = min;
}
public static Region deserialize(ConfigurationSection section) {
World world1 = Bukkit.getWorld(section.getString("world"));
Point point1 = Point.deserialize(section.getConfigurationSection("min"));
Point point2 = Point.deserialize(section.getConfigurationSection("max"));
return new Region(world1, point1, point2);
}
public boolean isInRegion(Location location) {
if (!location.getWorld().getName().equalsIgnoreCase(world.getName())) {
return false;
}
return (location.getBlockX() >= this.min.getX()
&& location.getBlockX() <= this.max.getX()
&& location.getBlockY() >= this.min.getY()
&& location.getBlockY() <= this.max.getY()
&& location.getBlockZ() >= this.min.getZ()
&& location.getBlockZ() <= this.max.getZ());
}
public boolean isInRegionPlayer(Location location) {
if (!location.getWorld().getName().equalsIgnoreCase(world.getName())) {
return false;
}
return (location.getBlockX() >= this.min.getX()-1
&& location.getBlockX() <= this.max.getX()+1
&& location.getBlockZ() >= this.min.getZ()-1
&& location.getBlockZ() <= this.max.getZ()+1);
}
}

View File

@ -0,0 +1,352 @@
package com.yaohun.wheatwar.listener;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.type.Farmland;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.TNTPrimed;
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.player.PlayerMoveEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import tools.CDTimeAPI;
import tools.RandomUtil;
import java.util.UUID;
public class GameEffectListener implements Listener {
/*
* 当玩家手持物品右键指定方块时触发效果
* */
@EventHandler
public void onPotion_drop_Main(PlayerInteractEvent e) {
Player p = e.getPlayer();
ItemStack item = p.getInventory().getItemInMainHand();
if (item.getType() != Material.AIR && item.getType() == Material.SPLASH_POTION) {
Block block = e.getClickedBlock();
if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
boolean sound_butt = false;
Location location = block.getLocation();
// 检测 3x2x3范围中的方块
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
// 判断方块是否是泥土
if (newBlock.getType() == Material.FARMLAND) {
Farmland fdata = (Farmland) newBlock.getBlockData();
if (fdata.getMoisture() < 7) {
sound_butt = true;
fdata.setMoisture(fdata.getMaximumMoisture());
newBlock.setBlockData(fdata);
location.getWorld().spawnParticle(Particle.CRIMSON_SPORE, newBlock.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
}
}
}
}
}
if (sound_butt) {
p.playSound(p.getLocation(), Sound.ITEM_BUCKET_EMPTY, 1, 1);
}
}
}
}
@EventHandler
public void onPotion_drop_Off(PlayerInteractEvent e) {
Player p = e.getPlayer();
ItemStack item = p.getInventory().getItemInOffHand();
if (item.getType() != Material.AIR && item.getType() == Material.SPLASH_POTION) {
Block block = e.getClickedBlock();
if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
boolean sound_butt = false;
Location location = block.getLocation();
// 检测 3x2x3范围中的方块
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
// 判断方块是否是泥土
if (newBlock.getType() == Material.FARMLAND) {
Farmland fdata = (Farmland) newBlock.getBlockData();
if (fdata.getMoisture() < 7) {
sound_butt = true;
fdata.setMoisture(fdata.getMaximumMoisture());
newBlock.setBlockData(fdata);
location.getWorld().spawnParticle(Particle.CRIMSON_SPORE, newBlock.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
}
}
}
}
}
if (sound_butt) {
p.playSound(p.getLocation(), Sound.ITEM_BUCKET_EMPTY, 1, 1);
}
}
}
}
@EventHandler
public void onBone_meal_Main(PlayerInteractEvent e) {
Player p = e.getPlayer();
Block block = e.getClickedBlock();
if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
ItemStack item = p.getInventory().getItemInMainHand();
// 检测手持物品是否是骨粉
if (item.getType() == Material.BONE_MEAL) {
e.setCancelled(true);
boolean sound_butt = false;
Location location = block.getLocation();
// 检测 3x2x3范围中的方块
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
if(newBlock.getType() == Material.WHEAT){
if(newBlock.getBlockData() instanceof Ageable) {
Ageable ageable = (Ageable) newBlock.getBlockData();
if (ageable.getAge() < 7) {
sound_butt = true;
ageable.setAge(ageable.getMaximumAge());
newBlock.setBlockData(ageable);
block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, newBlock.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
}
}
}
}
}
}
if(sound_butt){
p.playSound(p.getLocation(),Sound.BLOCK_CROP_BREAK,1,1);
}
}
}
}
@EventHandler
public void onBone_meal_Off(PlayerInteractEvent e) {
Player p = e.getPlayer();
Block block = e.getClickedBlock();
if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
ItemStack item = p.getInventory().getItemInOffHand();
// 检测手持物品是否是骨粉
if (item.getType() == Material.BONE_MEAL) {
e.setCancelled(true);
boolean sound_butt = false;
Location location = block.getLocation();
// 检测 3x2x3范围中的方块
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
if(newBlock.getType() == Material.WHEAT){
if(newBlock.getBlockData() instanceof Ageable) {
Ageable ageable = (Ageable) newBlock.getBlockData();
if (ageable.getAge() < 7) {
sound_butt = true;
ageable.setAge(ageable.getMaximumAge());
newBlock.setBlockData(ageable);
block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, newBlock.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
}
}
}
}
}
}
if(sound_butt){
p.playSound(p.getLocation(),Sound.BLOCK_CROP_BREAK,1,1);
}
}
}
}
@EventHandler
public void onWheat_Seeds(PlayerInteractEvent e){
Player p = e.getPlayer();
Block block = e.getClickedBlock();
if(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
ItemStack item = p.getInventory().getItemInMainHand();
// 检测手持物品是否是小麦种子
if (item.getType() == Material.WHEAT_SEEDS) {
boolean sound_butt = false;
// 获取玩家点击方块的坐标
Location location = block.getLocation();
if (p.isSneaking()) {
// 检索到泥土方块后判断方块是否是湿润状态
if (block.getBlockData() instanceof Farmland) {
Farmland farmland = (Farmland) block.getBlockData();
// 如果没有打湿则跳过
if (farmland.getMoisture() != 7) {
return;
}
// 获取泥土方块上方是否有小麦
Block blockWheat = block.getRelative(0, 1, 0);
if (blockWheat.getType() == Material.AIR) {
blockWheat.setType(Material.WHEAT);
if (blockWheat.getBlockData() instanceof Ageable) {
// 将小麦设置为刚种下时的阶段
Ageable wheat = (Ageable) blockWheat.getBlockData();
wheat.setAge(3);
blockWheat.setBlockData(wheat);
p.playSound(p.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_PLACE, 1, 1);
block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
}
}
}
} else {
if(block.getType() == Material.FARMLAND){
if (block.getBlockData() instanceof Farmland) {
Farmland farmland = (Farmland) block.getBlockData();
if (farmland.getMoisture() != 7) {
e.setCancelled(true);
}
}
}
// 检测 3x2x3范围中的方块
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 0; y++) {
for (int z = -1; z <= 1; z++) {
Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
// 检索到泥土方块后判断方块是否是湿润状态
if (newBlock.getBlockData() instanceof Farmland) {
Farmland farmland = (Farmland) newBlock.getBlockData();
// 如果没有打湿则跳过遍历
if (farmland.getMoisture() != 7) {
continue;
}
// 获取泥土方块上方是否有小麦
Block blockWheat = newBlock.getRelative(0, 1, 0);
if (blockWheat.getType() == Material.AIR) {
blockWheat.setType(Material.WHEAT);
if (blockWheat.getBlockData() instanceof Ageable) {
// 将小麦设置为刚种下时的阶段
Ageable wheat = (Ageable) blockWheat.getBlockData();
wheat.setAge(3);
blockWheat.setBlockData(wheat);
block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
sound_butt = true;
}
}else if (block.getType() == Material.WHEAT) {
if (block.getBlockData() instanceof Ageable) {
// 将小麦设置为刚种下时的阶段
Ageable ageable = (Ageable) block.getBlockData();
if (ageable.getAge() < 3) {
ageable.setAge(3);
block.setBlockData(ageable);
block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
sound_butt = true;
}
}
}
}
}
}
}
if(sound_butt){
p.playSound(p.getLocation(),Sound.BLOCK_CROP_BREAK,1,1);
}
}
}
}
}
@EventHandler
public void onWheat_Seeds_off(PlayerInteractEvent e){
Player p = e.getPlayer();
Block block = e.getClickedBlock();
if(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) {
ItemStack item = p.getInventory().getItemInOffHand();
// 检测手持物品是否是小麦种子
if (item.getType() == Material.WHEAT_SEEDS) {
boolean sound_butt = false;
// 获取玩家点击方块的坐标
Location location = block.getLocation();
if (p.isSneaking()) {
// 检索到泥土方块后判断方块是否是湿润状态
if (block.getBlockData() instanceof Farmland) {
Farmland farmland = (Farmland) block.getBlockData();
// 如果没有打湿则跳过
if (farmland.getMoisture() != 7) {
return;
}
// 获取泥土方块上方是否有小麦
Block blockWheat = block.getRelative(0, 1, 0);
if (blockWheat.getType() == Material.AIR) {
blockWheat.setType(Material.WHEAT);
if (blockWheat.getBlockData() instanceof Ageable) {
// 将小麦设置为刚种下时的阶段
Ageable wheat = (Ageable) blockWheat.getBlockData();
wheat.setAge(3);
blockWheat.setBlockData(wheat);
p.playSound(p.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_PLACE, 1, 1);
block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
}
}
}
} else {
if(block.getType() == Material.FARMLAND){
if (block.getBlockData() instanceof Farmland) {
Farmland farmland = (Farmland) block.getBlockData();
if (farmland.getMoisture() != 7) {
e.setCancelled(true);
}
}
}
// 检测 3x2x3范围中的方块
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 0; y++) {
for (int z = -1; z <= 1; z++) {
Block newBlock = location.getWorld().getBlockAt(location.clone().add(x, y, z));
// 检索到泥土方块后判断方块是否是湿润状态
if (newBlock.getBlockData() instanceof Farmland) {
Farmland farmland = (Farmland) newBlock.getBlockData();
// 如果没有打湿则跳过遍历
if (farmland.getMoisture() != 7) {
continue;
}
// 获取泥土方块上方是否有小麦
Block blockWheat = newBlock.getRelative(0, 1, 0);
if (blockWheat.getType() == Material.AIR) {
blockWheat.setType(Material.WHEAT);
if (blockWheat.getBlockData() instanceof Ageable) {
// 将小麦设置为刚种下时的阶段
Ageable wheat = (Ageable) blockWheat.getBlockData();
wheat.setAge(3);
blockWheat.setBlockData(wheat);
block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
sound_butt = true;
}
}else if (block.getType() == Material.WHEAT) {
if (block.getBlockData() instanceof Ageable) {
// 将小麦设置为刚种下时的阶段
Ageable ageable = (Ageable) block.getBlockData();
if (ageable.getAge() < 3) {
ageable.setAge(3);
block.setBlockData(ageable);
block.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, blockWheat.getLocation().add(0.5, 0.5, 0.5), 3, 0.5, 0.5, 0.5, 0.1);
sound_butt = true;
}
}
}
}
}
}
}
if(sound_butt){
p.playSound(p.getLocation(),Sound.BLOCK_CROP_BREAK,1,1);
}
}
}
}
}
}

View File

@ -0,0 +1,6 @@
package com.yaohun.wheatwar.listener;
import org.bukkit.event.Listener;
public class GameListener implements Listener {
}

View File

@ -0,0 +1,62 @@
package com.yaohun.wheatwar.listener;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.entity.EntityCombustEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
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 onJumpFarm(PlayerInteractEvent e){
if (e.isCancelled()) {return;}
if(e.getAction() == Action.PHYSICAL){
Block block = e.getClickedBlock();
if(block.getType() == Material.FARMLAND){
e.setCancelled(true);
}
}
}
@EventHandler /*保护农作物免踩踏*/
public void onMobsFarm(EntityInteractEvent e){
if (e.isCancelled()) {return;}
if(e.getEntityType() != EntityType.PLAYER){
Block block = e.getBlock();
if(block.getType() == Material.FARMLAND){
e.setCancelled(true);
}
}
}
}

View File

@ -0,0 +1,25 @@
package com.yaohun.wheatwar.listener;
import com.yaohun.wheatwar.WheatWar;
import com.yaohun.wheatwar.game.Game;
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 {
@EventHandler
public void onJoin(PlayerJoinEvent e){
Game game = WheatWar.getGame();
new BukkitRunnable() {
@Override
public void run() {
if(!game.isStarted()) {
game.start();
}
game.initPlayerData(e.getPlayer());
}
}.runTaskLater(WheatWar.inst(),10L);
}
}

View File

@ -0,0 +1,43 @@
package tools;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
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<BossBar> bossBarList){
for (BossBar bossBar : bossBarList) {
bossBar.addPlayer(player);
}
}
public static void removeBossBar(Player player,List<BossBar> bossBarList){
for (BossBar bossBar : bossBarList) {
bossBar.removePlayer(player);
}
}
}

View File

@ -0,0 +1,66 @@
package tools;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;
public class CDTimeAPI {
private static HashSet<CDData> 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<String, Long> 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);
}
public HashMap<String, Long> getCdTime() {
return cdTime;
}
}
public static HashSet<CDData> getCdData() {
return cdData;
}
}

View File

@ -0,0 +1,83 @@
package tools;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.math.BlockVector3;
import com.yaohun.wheatwar.WheatWar;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import java.io.File;
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);
}
}
}
public static void sendAllTitle(String title,String subTitle,int t1,int t2,int t3) {
for (Player player : Bukkit.getOnlinePlayers()) {
player.sendTitle(title,subTitle,t1,t2,t3);
}
}
public static Vector getRandomVector(double radius){
double x = RandomUtil.getRandomDouble(0-radius, 0+radius, 1);
double y = RandomUtil.getRandomDouble(0, 0.5, 1);
double z = RandomUtil.getRandomDouble(0-radius, 0+radius, 1);
return new Vector(x, y, z);
}
public static Vector getRandomVector(double radius,double radiusY){
double x = RandomUtil.getRandomDouble(0-radius, 0+radius, 1);
double y = RandomUtil.getRandomDouble(0+radiusY, 0.5+radiusY, 1);
double z = RandomUtil.getRandomDouble(0-radius, 0+radius, 1);
return new Vector(x, y, z);
}
public static void loadSchematics(String fileName) {
World world = Bukkit.getWorld("world");
File schema_file = new File("plugins/FastAsyncWorldEdit/schematics",fileName+".schem");
Location location = new Location(world, -233,74,258);
EditSession editSession = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(world));
Bukkit.getScheduler().runTaskAsynchronously(
WheatWar.inst(), () -> {
try {
Clipboard clipboard = ClipboardFormats.findByFile(schema_file).load(schema_file);
clipboard.paste(BukkitAdapter.adapt(world), BlockVector3.at(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
clipboard.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
editSession.close();
}
}
);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,17 @@
package tools;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
import java.util.ArrayList;
import java.util.List;
public class StackUtil {
}

View File

@ -0,0 +1,8 @@
name: WheatWar
main: com.yaohun.wheatwar.WheatWar
version: 1.0
api-version: '1.18'
commands:
game:
gameedit:
livegift: