diff --git a/src/main/java/me/Demon/BlockWars/Game/GameData.java b/src/main/java/me/Demon/BlockWars/Game/GameData.java index 568aa87..849ee8d 100644 --- a/src/main/java/me/Demon/BlockWars/Game/GameData.java +++ b/src/main/java/me/Demon/BlockWars/Game/GameData.java @@ -8,6 +8,7 @@ import me.Demon.BlockWars.Util.BossBarUtil; import me.Demon.BlockWars.Util.ComputeBlock; import me.Demon.BlockWars.Util.GameUtil; import org.bukkit.*; +import org.bukkit.block.Block; import org.bukkit.boss.BarColor; import org.bukkit.boss.BarStyle; import org.bukkit.boss.BossBar; @@ -28,6 +29,7 @@ import java.util.Random; public class GameData { + public static final Material[] BLOCK_TYPES = new Material[] {Material.IRON_BLOCK, Material.GOLD_BLOCK, Material.DIAMOND_BLOCK, Material.EMERALD_BLOCK}; private Region region; private World world; private static boolean started; @@ -222,6 +224,51 @@ public class GameData { this.blockRegionList = blockRegions; } + public void updateBlockType(Block block) { + if (!region.isInRegion(block.getLocation())) { + return; + } + int y = block.getY(); + int maxY = (int) region.getMax().getY();// 获取最高y + int minY = (int) region.getMin().getY();// 获取最低y + int range = maxY - minY; // 总范围 + double partSize = (double) range / 4; // 每个部分的大小 + int level = 3; // 初始 level + if (y < minY || y > maxY) { + level = 0; + } else { + // 计算 y 在哪个部分 + if (y < minY + partSize) { + level = 0; + } else if (y < minY + 2 * partSize) { + level = 1; + } else if (y < minY + 3 * partSize) { + level = 2; + } + } + Material material = BLOCK_TYPES[level]; + block.setType(material); + } + public Material updateBlockType(int y) { + int maxY = (int) region.getMax().getY();// 获取最高y + int minY = (int) region.getMin().getY();// 获取最低y + int range = maxY - minY; // 总范围 + double partSize = (double) range / 4; // 每个部分的大小 + int level = 0; // 初始 level + if (y < minY || y > maxY) { + level = 0; + } else { + if (y < minY + 2 * partSize) { + level = 1; + } else if (y < minY + 3 * partSize) { + level = 2; + } else { + level = 3; + } + } + return BLOCK_TYPES[level]; + } + public List getBlockRegionList() { return blockRegionList; } diff --git a/src/main/java/me/Demon/BlockWars/Listener/GameListener.java b/src/main/java/me/Demon/BlockWars/Listener/GameListener.java index 3f8ebf6..57746a3 100644 --- a/src/main/java/me/Demon/BlockWars/Listener/GameListener.java +++ b/src/main/java/me/Demon/BlockWars/Listener/GameListener.java @@ -10,6 +10,7 @@ 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.block.BlockBreakEvent; import org.bukkit.event.block.BlockExplodeEvent; import org.bukkit.event.block.BlockPlaceEvent; @@ -106,6 +107,43 @@ public class GameListener implements Listener { } } + @EventHandler //*禁止在游戏区外放置方块*// + public void onUsePlace(PlayerInteractEvent e) { + Player p = e.getPlayer(); + Block block = e.getClickedBlock(); + if(e.getAction() == Action.RIGHT_CLICK_BLOCK) { + if (Main.gameData.isStarted()) { + if(Main.configYml.isGameSettings("speedplace")) { + Region region = Main.gameData.getRegion(); + // 检测方块是否在游戏场地 + if (region.isInRegion(block.getLocation())) { + // 检测方块下方是否存在空缺 + Location location = block.getLocation(); + int indexY = location.getBlockY(); + for (int y = (int) region.getMin().getY(); y < indexY; y++) { + Block block1 = location.getWorld().getBlockAt(location.getBlockX(), y, location.getBlockZ()); + if (block1.getType().equals(Material.AIR)) { + indexY = y; + break; + } + } + if(location.getY() == indexY){ + return; + } + e.setCancelled(true); + Block block1 = location.getWorld().getBlockAt(location.getBlockX(), indexY, location.getBlockZ()); + Main.gameData.updateBlockType(block1); + GameUtil.refreshPlayerHandStack(p); + boolean sounds_butt = Main.configYml.isGameSettings("placesounds"); + if(sounds_butt) { + p.playSound(p.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_PLACE, 1, 1); + } + } + } + } + } + } + /* * 启动快速搭建方块 * */ @@ -121,6 +159,7 @@ public class GameListener implements Listener { if(Main.configYml.isGameSettings("speedplace")) { if (!isPlaceNextBlock(block)) { GameUtil.refreshPlayerHandStack(p); + Main.gameData.updateBlockType(block); if(sounds_butt) { p.playSound(p.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_PLACE, 1.0F, 1.0F); } @@ -137,13 +176,14 @@ public class GameListener implements Listener { } event.setCancelled(true); int a = 1; - for (int place_y = indexY; place_y <= (indexY+2); place_y++) { + for (int place_y = indexY; place_y <= (indexY+3); place_y++) { int finalPlace_y = place_y; new BukkitRunnable() { @Override public void run() { Block block1 = world.getBlockAt(location.getBlockX(), finalPlace_y, location.getBlockZ()); if (block1.getType().equals(Material.AIR)) { + Main.gameData.updateBlockType(block1); if (sounds_butt) { p.playSound(p.getLocation(), Sound.BLOCK_METAL_PLACE, 1.0F, 1.0F); p.playSound(p.getLocation(), Sound.BLOCK_AMETHYST_BLOCK_PLACE, 1.0F, 1.0F);