-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajout de la commande /exploderandom (#77)
*Avez vous lu le [Code de Conduite](https://github.com/Margouta/PluginOpenMC/blob/main/CODE_OF_CONDUCT.md)?* Oui ## Decrivez vos changements - Ajout de la commande /exploderandom qui fait spawn une tnt 5 blocks au dessus d'un joueur aléatoire pour le prix d'un diamant - Les tnt ne font pas de dégats au terrain, seulement aux entités
- Loading branch information
Showing
6 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,4 +137,6 @@ gradle-app.setting | |
|
||
### Project ### | ||
|
||
/builds/ | ||
/builds/ | ||
|
||
bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/main/java/fr/communaywen/core/commands/ExplodeRandomCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package fr.communaywen.core.commands; | ||
|
||
import org.bukkit.GameMode; | ||
import org.bukkit.Material; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.EntityType; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.entity.TNTPrimed; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.PlayerInventory; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.*; | ||
|
||
public final class ExplodeRandomCommand implements CommandExecutor { | ||
|
||
private final HashMap<UUID, Long> cooldowns = new HashMap<>(); | ||
private final HashMap<UUID, Long> lastExploded = new HashMap<>(); | ||
private static final int COOLDOWN_TIME = 300; // 5 minutes in seconds | ||
|
||
private static final int IMMUNITY_TIME = 60 * 60; // 1 hour in seconds | ||
|
||
public static final HashSet<TNTPrimed> preventedExplosvies = new HashSet<>(); | ||
|
||
public static final int COST = 32; | ||
|
||
@Override | ||
public boolean onCommand(final @NotNull CommandSender sender, | ||
final @NotNull Command command, | ||
final @NotNull String label, | ||
final @NotNull String[] args) { | ||
if (sender instanceof Player player) { | ||
try { | ||
UUID playerId = player.getUniqueId(); | ||
long currentTime = System.currentTimeMillis() / 1000; | ||
|
||
if (cooldowns.containsKey(playerId)) { | ||
long lastUsed = cooldowns.get(playerId); | ||
long timeSinceLastUse = currentTime - lastUsed; | ||
|
||
if (timeSinceLastUse < COOLDOWN_TIME) { | ||
long timeLeft = COOLDOWN_TIME - timeSinceLastUse; | ||
player.sendMessage("Vous devez attendre encore " + timeLeft + " secondes avant d'utiliser cette commande à nouveau."); | ||
return true; | ||
} | ||
} | ||
|
||
List<Player> players = player.getWorld().getPlayers(); | ||
players.remove(player); | ||
|
||
List<Player> untouchedPlayers = player.getWorld().getPlayers(); | ||
untouchedPlayers.remove(player); | ||
|
||
for (Player currentPlayer : untouchedPlayers) { | ||
if (lastExploded.containsKey(playerId)) { | ||
long lastUsed = lastExploded.get(playerId); | ||
long timeSinceLastUse = currentTime - lastUsed; | ||
|
||
if (timeSinceLastUse < IMMUNITY_TIME) { | ||
players.remove(currentPlayer); | ||
} | ||
} | ||
} | ||
|
||
if (players.isEmpty()) { | ||
player.sendMessage("Aucun joueur n'as été trouvée..."); | ||
|
||
return true; | ||
} | ||
|
||
PlayerInventory inventory = player.getInventory(); | ||
|
||
if (player.getGameMode() != GameMode.CREATIVE) { | ||
int totalDiamonds = Arrays.stream(inventory.getContents()) | ||
.filter(stack -> stack != null && stack.getType() == Material.DIAMOND) //make the stream only be of gold stacks | ||
.mapToInt(stack -> stack.getAmount()) //turn stacks into their amounts | ||
.sum(); //add them up | ||
|
||
if (totalDiamonds < COST) { | ||
player.sendMessage("Il vous manque " + (COST - totalDiamonds) + " diamants"); | ||
|
||
return true; | ||
} | ||
|
||
List<ItemStack> diamondStacks = Arrays.stream(inventory.getContents()) | ||
.filter(stack -> stack != null && stack.getType() == Material.DIAMOND).toList(); | ||
|
||
int remaining = COST; | ||
|
||
for (ItemStack stack : diamondStacks) { | ||
int ammount = stack.getAmount(); | ||
|
||
if (ammount < remaining) { | ||
remaining -= ammount; | ||
stack.setAmount(0); | ||
continue; | ||
} | ||
|
||
stack.setAmount(ammount - remaining); | ||
|
||
break; | ||
} | ||
} | ||
|
||
int random = new Random().nextInt(players.size()); | ||
|
||
Player chosenPlayer = players.get(random); | ||
chosenPlayer.sendMessage("Tu as été désigné."); | ||
chosenPlayer.sendMessage("REGARDE AU DESSUS DE TOI!"); | ||
|
||
player.sendMessage(chosenPlayer.getDisplayName() + " va reçevoir un cadeau explosif"); | ||
|
||
TNTPrimed tnt = (TNTPrimed) chosenPlayer.getWorld().spawnEntity(chosenPlayer.getLocation().add(0, 5, 0), EntityType.TNT); | ||
|
||
tnt.setFuseTicks(15); | ||
|
||
preventedExplosvies.add(tnt); | ||
|
||
lastExploded.put(playerId, currentTime); | ||
|
||
cooldowns.put(playerId, currentTime); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/fr/communaywen/core/listeners/ExplosionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fr.communaywen.core.listeners; | ||
|
||
import fr.communaywen.core.commands.ExplodeRandomCommand; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.entity.EntityExplodeEvent; | ||
|
||
public class ExplosionListener implements Listener { | ||
@EventHandler | ||
public void onPrimedTNTExplosion(EntityExplodeEvent event) { | ||
if (ExplodeRandomCommand.preventedExplosvies.contains(event.getEntity())) { | ||
event.blockList().clear(); | ||
ExplodeRandomCommand.preventedExplosvies.remove(event.getEntity()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters