|
| 1 | +package fr.communaywen.core.event; |
| 2 | + |
| 3 | +import fr.communaywen.core.AywenCraftPlugin; |
| 4 | +import net.kyori.adventure.text.Component; |
| 5 | +import org.bukkit.*; |
| 6 | +import org.bukkit.block.Block; |
| 7 | +import org.bukkit.block.BlockFace; |
| 8 | +import org.bukkit.boss.BarColor; |
| 9 | +import org.bukkit.boss.BarFlag; |
| 10 | +import org.bukkit.boss.BarStyle; |
| 11 | +import org.bukkit.boss.BossBar; |
| 12 | +import org.bukkit.entity.EntityType; |
| 13 | +import org.bukkit.entity.LivingEntity; |
| 14 | +import org.bukkit.entity.Player; |
| 15 | +import org.bukkit.entity.Skeleton; |
| 16 | +import org.bukkit.inventory.ItemStack; |
| 17 | +import org.bukkit.potion.PotionEffect; |
| 18 | +import org.bukkit.potion.PotionEffectType; |
| 19 | +import org.bukkit.scheduler.BukkitRunnable; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Random; |
| 24 | + |
| 25 | +import static fr.communaywen.core.event.CubeListener.isCubeBlock; |
| 26 | + |
| 27 | +public class CubeManager { |
| 28 | + |
| 29 | + private static AywenCraftPlugin plugin; |
| 30 | + public static Location currentLocation; |
| 31 | + public static int CUBE_SIZE = 5; |
| 32 | + public static Material CUBE_MATERIAL = Material.LAPIS_BLOCK; |
| 33 | + private final int MOVE_DELAY = 18000; // 15 minutes |
| 34 | + private final Material CORRUPTION_MATERIAL = Material.WARPED_NYLIUM; |
| 35 | + private final int CORRUPTION_RADIUS = 15; |
| 36 | + private final Random random = new Random(); |
| 37 | + private BossBar bossBar; |
| 38 | + |
| 39 | + public CubeManager(AywenCraftPlugin plugin) { |
| 40 | + this.plugin = plugin; |
| 41 | + World world = Bukkit.getWorld("world"); |
| 42 | + if (world == null) return; |
| 43 | + |
| 44 | + int startX = plugin.getManagers().getKevinConfig().getInt("posX", -126); |
| 45 | + int startZ = plugin.getManagers().getKevinConfig().getInt("posZ", -87); |
| 46 | + int startY = world.getHighestBlockYAt(startX, startZ); |
| 47 | + |
| 48 | + currentLocation = new Location(world, startX, startY, startZ); |
| 49 | + |
| 50 | + bossBar = Bukkit.createBossBar("Cube d'OpenMC", BarColor.BLUE, BarStyle.SOLID, BarFlag.CREATE_FOG, BarFlag.DARKEN_SKY); |
| 51 | + bossBar.setVisible(true); |
| 52 | + |
| 53 | + new BukkitRunnable() { |
| 54 | + |
| 55 | + @Override |
| 56 | + public void run() { |
| 57 | + moveCube(); |
| 58 | + } |
| 59 | + }.runTaskTimer(plugin, 0, MOVE_DELAY); |
| 60 | + |
| 61 | + startBossBarUpdater(); |
| 62 | + |
| 63 | + startMobSpawnTask(); |
| 64 | + } |
| 65 | + |
| 66 | + private void startBossBarUpdater() { |
| 67 | + new BukkitRunnable() { |
| 68 | + @Override |
| 69 | + public void run() { |
| 70 | + for (Player player : Bukkit.getOnlinePlayers()) { |
| 71 | + if (player.getWorld().equals(currentLocation.getWorld())) { |
| 72 | + double distance = player.getLocation().distance(currentLocation); |
| 73 | + |
| 74 | + if (distance <= 100) { |
| 75 | + if (!bossBar.getPlayers().contains(player)) { |
| 76 | + bossBar.addPlayer(player); |
| 77 | + } |
| 78 | + } else { |
| 79 | + bossBar.removePlayer(player); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + }.runTaskTimer(plugin, 0, 20); |
| 85 | + } |
| 86 | + |
| 87 | + private void startMobSpawnTask() { |
| 88 | + new BukkitRunnable() { |
| 89 | + @Override |
| 90 | + public void run() { |
| 91 | + spawnMobsAroundCube(); |
| 92 | + } |
| 93 | + }.runTaskTimer(plugin, 0, 6000); // 6000 ticks = 5 minutes |
| 94 | + } |
| 95 | + |
| 96 | + private void spawnMobsAroundCube() { |
| 97 | + World world = currentLocation.getWorld(); |
| 98 | + int baseX = currentLocation.getBlockX(); |
| 99 | + int baseY = currentLocation.getBlockY(); |
| 100 | + int baseZ = currentLocation.getBlockZ(); |
| 101 | + |
| 102 | + int mobsToSpawn = random.nextInt(5) + 15; |
| 103 | + |
| 104 | + for (int i = 0; i < mobsToSpawn; i++) { |
| 105 | + int x = baseX + random.nextInt(CORRUPTION_RADIUS * 2) - CORRUPTION_RADIUS; |
| 106 | + int z = baseZ + random.nextInt(CORRUPTION_RADIUS * 2) - CORRUPTION_RADIUS; |
| 107 | + int y = world.getHighestBlockYAt(x, z); |
| 108 | + |
| 109 | + Location spawnLocation = new Location(world, x, y, z); |
| 110 | + |
| 111 | + if (spawnLocation.distance(currentLocation) <= CORRUPTION_RADIUS) { |
| 112 | + EntityType mobType = random.nextBoolean() ? EntityType.ZOMBIE : EntityType.SKELETON; |
| 113 | + LivingEntity entity = (LivingEntity) world.spawnEntity(spawnLocation, mobType); |
| 114 | + |
| 115 | + entity.getEquipment().setHelmet(new ItemStack(Material.LAPIS_BLOCK)); |
| 116 | + entity.getEquipment().setHelmetDropChance(0); |
| 117 | + |
| 118 | + entity.addPotionEffect(new PotionEffect(PotionEffectType.STRENGTH, Integer.MAX_VALUE, 1)); |
| 119 | + entity.addPotionEffect(new PotionEffect(PotionEffectType.RESISTANCE, Integer.MAX_VALUE, 1)); |
| 120 | + entity.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, Integer.MAX_VALUE, 1)); |
| 121 | + entity.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, Integer.MAX_VALUE, 0)); |
| 122 | + |
| 123 | + entity.getEquipment().setChestplate(new ItemStack(Material.IRON_CHESTPLATE)); |
| 124 | + entity.getEquipment().setChestplateDropChance(0); |
| 125 | + entity.getEquipment().setBoots(new ItemStack(Material.IRON_BOOTS)); |
| 126 | + entity.getEquipment().setBootsDropChance(0); |
| 127 | + |
| 128 | + if (mobType == EntityType.SKELETON) { |
| 129 | + Skeleton skeleton = (Skeleton) entity; |
| 130 | + skeleton.getEquipment().setItemInMainHand(new ItemStack(Material.BOW)); |
| 131 | + skeleton.getEquipment().setItemInMainHandDropChance(0); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void createCube(Location location) { |
| 138 | + for (int x = 0; x < CUBE_SIZE; x++) { |
| 139 | + for (int y = 0; y < CUBE_SIZE; y++) { |
| 140 | + for (int z = 0; z < CUBE_SIZE; z++) { |
| 141 | + location.clone().add(x, y, z).getBlock().setType(CUBE_MATERIAL); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private void clearCube(Location location) { |
| 148 | + for (int x = 0; x < CUBE_SIZE; x++) { |
| 149 | + for (int y = 0; y < CUBE_SIZE; y++) { |
| 150 | + for (int z = 0; z < CUBE_SIZE; z++) { |
| 151 | + location.clone().add(x, y, z).getBlock().setType(Material.AIR); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private int lastDirection = -1; |
| 158 | + |
| 159 | + private void moveCube() { |
| 160 | + clearCube(currentLocation); |
| 161 | + |
| 162 | + int direction; |
| 163 | + do { |
| 164 | + direction = random.nextInt(4); |
| 165 | + } while ((lastDirection == 0 && direction == 1) || (lastDirection == 1 && direction == 0) || |
| 166 | + (lastDirection == 2 && direction == 3) || (lastDirection == 3 && direction == 2)); |
| 167 | + |
| 168 | + lastDirection = direction; |
| 169 | + |
| 170 | + int newX = currentLocation.getBlockX(); |
| 171 | + int newZ = currentLocation.getBlockZ(); |
| 172 | + |
| 173 | + switch (direction) { |
| 174 | + case 0 -> newX += CUBE_SIZE; |
| 175 | + case 1 -> newX -= CUBE_SIZE; |
| 176 | + case 2 -> newZ += CUBE_SIZE; |
| 177 | + case 3 -> newZ -= CUBE_SIZE; |
| 178 | + } |
| 179 | + |
| 180 | + World world = currentLocation.getWorld(); |
| 181 | + int newY = world.getHighestBlockYAt(newX, newZ); |
| 182 | + |
| 183 | + currentLocation.setX(newX); |
| 184 | + currentLocation.setY(newY+1); |
| 185 | + currentLocation.setZ(newZ); |
| 186 | + |
| 187 | + world.playSound(currentLocation, Sound.BLOCK_BEACON_ACTIVATE, 100, 1.0f); |
| 188 | + |
| 189 | + Bukkit.broadcast(Component.text("§9Le Cube §7s'est déplacé en §f" + currentLocation.x() + " " + currentLocation.y() + " " + currentLocation.z())); |
| 190 | + createCube(currentLocation); |
| 191 | + corruptAreaAroundCube(); |
| 192 | + } |
| 193 | + |
| 194 | + private void corruptAreaAroundCube() { |
| 195 | + World world = currentLocation.getWorld(); |
| 196 | + int baseX = currentLocation.getBlockX(); |
| 197 | + int baseY = currentLocation.getBlockY(); |
| 198 | + int baseZ = currentLocation.getBlockZ(); |
| 199 | + |
| 200 | + int corruptionStartX = baseX - CORRUPTION_RADIUS; |
| 201 | + int corruptionEndX = baseX + CUBE_SIZE + CORRUPTION_RADIUS; |
| 202 | + int corruptionStartZ = baseZ - CORRUPTION_RADIUS; |
| 203 | + int corruptionEndZ = baseZ + CUBE_SIZE + CORRUPTION_RADIUS; |
| 204 | + |
| 205 | + for (int x = corruptionStartX; x < corruptionEndX; x++) { |
| 206 | + for (int z = corruptionStartZ; z < corruptionEndZ; z++) { |
| 207 | + for (int yOffset = -1; yOffset <= 5; yOffset++) { |
| 208 | + Location targetLocation = new Location(world, x, baseY + yOffset, z); |
| 209 | + |
| 210 | + if (targetLocation.getBlock().getType().isSolid() && !isCubeBlock(targetLocation) && random.nextDouble() < 0.4) { |
| 211 | + targetLocation.getBlock().setType(CORRUPTION_MATERIAL); |
| 212 | + |
| 213 | + if (targetLocation.getBlock().getType() == CORRUPTION_MATERIAL) { |
| 214 | + applyBoneMealEffect(targetLocation); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + private void applyBoneMealEffect(Location location) { |
| 223 | + Block block = location.getBlock(); |
| 224 | + |
| 225 | + |
| 226 | + if (block.getType() == Material.WARPED_NYLIUM || block.getType() == Material.CRIMSON_NYLIUM) { |
| 227 | + block.applyBoneMeal(BlockFace.UP); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + public static void clearCube() { |
| 232 | + for (int x = 0; x < CUBE_SIZE; x++) { |
| 233 | + for (int y = 0; y < CUBE_SIZE; y++) { |
| 234 | + for (int z = 0; z < CUBE_SIZE; z++) { |
| 235 | + currentLocation.clone().add(x, y, z).getBlock().setType(Material.AIR); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + public static void saveCubeLocation() { |
| 242 | + plugin.getManagers().getKevinConfig().set("posX", currentLocation.getBlockX()); |
| 243 | + plugin.getManagers().getKevinConfig().set("posY", currentLocation.getBlockY()); |
| 244 | + plugin.getManagers().getKevinConfig().set("posZ", currentLocation.getBlockZ()); |
| 245 | + try { |
| 246 | + plugin.getManagers().getKevinConfig().save(new File(plugin.getDataFolder(), "cube.yml")); |
| 247 | + } catch (IOException e) { |
| 248 | + throw new RuntimeException(e); |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | +} |
0 commit comments