Skip to content
This repository was archived by the owner on Oct 12, 2025. It is now read-only.

Commit 96b099c

Browse files
authored
[EVENT] Le Cube (#485)
*Avez vous lu le [Code de Conduite](https://github.com/Margouta/PluginOpenMC/blob/main/CODE_OF_CONDUCT.md)?*: oui *Votre code se compile t-il en local ?*: oui *Avez-vous supprimez les imports inutilisés ?*: oui ## Decrivez vos changements *Clairement et avec des screenshots si nécessaires* ![image](https://github.com/user-attachments/assets/b87cabf5-dcba-4209-9604-8be67237633e) Un cube qui bouge initialement tout les 15 minutes
2 parents fc957a5 + f2b378d commit 96b099c

File tree

5 files changed

+341
-0
lines changed

5 files changed

+341
-0
lines changed

src/main/java/fr/communaywen/core/AywenCraftPlugin.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
import fr.communaywen.core.contest.listeners.*;
3737
import fr.communaywen.core.customitems.listeners.*;
38+
import fr.communaywen.core.event.CubeListener;
39+
import fr.communaywen.core.event.CubeManager;
3840
import fr.communaywen.core.listeners.*;
3941

4042
import fr.communaywen.core.contest.managers.ContestManager;
@@ -375,6 +377,7 @@ public void run() {
375377

376378
/* LISTENERS */
377379
registerEvents(
380+
new CubeListener(),
378381
new ParticleListener(this),
379382
new HeadListener(this),
380383
new JumpListener(this, jumpManager),
@@ -441,6 +444,7 @@ public void run() {
441444
ClaimConfigDataBase.processStoredClaimData();
442445
new BandageRecipe();
443446

447+
new CubeManager(this);
444448

445449
// BETTER SPAWN
446450
// - Leaderboard
@@ -481,6 +485,9 @@ public void onDisable() {
481485
jumpManager.removeDisplayJumpStart();
482486
jumpManager.removeDisplayJumpEnd();
483487

488+
CubeManager.saveCubeLocation();
489+
CubeManager.clearCube();
490+
484491
for (Player player : Bukkit.getOnlinePlayers()) {
485492
PlayerQuests pq = QuestsManager.getPlayerQuests(player.getUniqueId()); // Load quest progress
486493
QuestsManager.savePlayerQuestProgress(player, pq); // Save quest progress

src/main/java/fr/communaywen/core/Managers.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public class Managers {
8484
private FileConfiguration levelsConfig;
8585
private FileConfiguration quizzesConfig;
8686
private FileConfiguration customItemsConfig;
87+
private FileConfiguration kevinConfig;
8788

8889
public void initConfig(AywenCraftPlugin plugin) {
8990
plugin.saveDefaultConfig();
@@ -93,6 +94,7 @@ public void initConfig(AywenCraftPlugin plugin) {
9394
levelsConfig = ConfigUtils.loadConfig(plugin, "levels.yml");
9495
quizzesConfig = ConfigUtils.loadConfig(plugin, "quizzes.yml");
9596
customItemsConfig = ConfigUtils.loadConfig(plugin, "customitems.yml");
97+
kevinConfig = ConfigUtils.loadConfig(plugin, "cube.yml");
9698
}
9799

98100
public void init(AywenCraftPlugin plugin) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package fr.communaywen.core.event;
2+
3+
import org.bukkit.Location;
4+
import org.bukkit.Sound;
5+
import org.bukkit.entity.Player;
6+
import org.bukkit.event.EventHandler;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.block.BlockBreakEvent;
9+
import org.bukkit.event.entity.EntityDamageByEntityEvent;
10+
import org.bukkit.event.player.PlayerInteractEvent;
11+
import org.bukkit.event.player.PlayerMoveEvent;
12+
import org.bukkit.util.Vector;
13+
14+
import static fr.communaywen.core.event.CubeManager.*;
15+
16+
public class CubeListener implements Listener {
17+
@EventHandler
18+
public void onPlayerInteract(PlayerInteractEvent event) {
19+
Location clickedBlock = event.getClickedBlock() != null ? event.getClickedBlock().getLocation() : null;
20+
if (clickedBlock != null && isCubeBlock(clickedBlock)) {
21+
repulsePlayer(event.getPlayer());
22+
}
23+
}
24+
25+
@EventHandler
26+
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
27+
if (event.getDamager() instanceof Player player && isCubeBlock(event.getEntity().getLocation())) {
28+
repulsePlayer(player);
29+
}
30+
}
31+
32+
@EventHandler
33+
public void onBlockBreak(BlockBreakEvent event) {
34+
if (isCubeBlock(event.getBlock().getLocation())) {
35+
event.setCancelled(true);
36+
}
37+
}
38+
39+
@EventHandler
40+
public void onPlayerMove(PlayerMoveEvent event) {
41+
Player player = event.getPlayer();
42+
Location belowPlayer = player.getLocation().clone().subtract(0, 1, 0);
43+
44+
if (isCubeBlock(belowPlayer)) {
45+
Vector velocity = player.getVelocity();
46+
velocity.setY(1.0);
47+
player.setVelocity(velocity);
48+
49+
player.playSound(player.getLocation(), Sound.BLOCK_BEACON_POWER_SELECT, 1.0f, 2.0f);
50+
}
51+
}
52+
53+
54+
public static boolean isCubeBlock(Location blockLocation) {
55+
int x = blockLocation.getBlockX();
56+
int y = blockLocation.getBlockY();
57+
int z = blockLocation.getBlockZ();
58+
59+
int cubeX = currentLocation.getBlockX();
60+
int cubeY = currentLocation.getBlockY();
61+
int cubeZ = currentLocation.getBlockZ();
62+
63+
return x >= cubeX && x < cubeX + CUBE_SIZE &&
64+
y >= cubeY && y < cubeY + CUBE_SIZE &&
65+
z >= cubeZ && z < cubeZ + CUBE_SIZE &&
66+
blockLocation.getBlock().getType() == CUBE_MATERIAL;
67+
}
68+
69+
private void repulsePlayer(Player player) {
70+
Vector direction = player.getLocation().toVector().subtract(currentLocation.toVector()).normalize();
71+
direction.multiply(3);
72+
direction.setY(1);
73+
player.setVelocity(direction);
74+
75+
player.playSound(player.getLocation(), Sound.BLOCK_BEACON_POWER_SELECT, 1.0f, 2.0f);
76+
}
77+
}
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
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+
}

src/main/resources/cube.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
posX: -126
2+
posY: 0
3+
posZ: -87

0 commit comments

Comments
 (0)