Skip to content

Commit

Permalink
Ajout de la commande /exploderandom (#77)
Browse files Browse the repository at this point in the history
*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
Xernas78 authored Jul 7, 2024
2 parents ea3163f + c2a0078 commit b51a316
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,6 @@ gradle-app.setting

### Project ###

/builds/
/builds/

bin/
2 changes: 2 additions & 0 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public void onEnable() {
this.getCommand("link").setExecutor(new LinkCommand(linkerAPI));
this.getCommand("manuallink").setExecutor(new ManualLinkCommand(linkerAPI));
this.getCommand("credit").setExecutor(new CreditCommand());
this.getCommand("exploderandom").setExecutor(new ExplodeRandomCommand());
this.getCommand("rtp").setExecutor(new RTPCommand(this));
this.getCommand("feed").setExecutor(new FeedCommand(this));
this.getCommand("money").setExecutor(new MoneyCommand(economyManager));
Expand Down Expand Up @@ -157,6 +158,7 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new AntiTrampling(),this);
getServer().getPluginManager().registerEvents(new RTPWand(this), this);
getServer().getPluginManager().registerEvents(onPlayers, this);
getServer().getPluginManager().registerEvents(new ExplosionListener(), this);
//getServer().getPluginManager().registerEvents(new SleepListener(),this);
getServer().getPluginManager().registerEvents(new ChatListener(this, discordWebhook), this);
getServer().getPluginManager().registerEvents(new FreezeListener(this), this);
Expand Down
131 changes: 131 additions & 0 deletions src/main/java/fr/communaywen/core/commands/ExplodeRandomCommand.java
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 src/main/java/fr/communaywen/core/listeners/ExplosionListener.java
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());
}
}
}
1 change: 1 addition & 0 deletions src/main/java/fr/communaywen/core/tpa/CommandTPA.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
Player receiver = Bukkit.getPlayerExact(args[0]);
if (receiver == null) {
player.sendMessage("Impossible de trouver le joueur «"+args[0]+"»");

return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ commands:
usage: /money [transfer <player> <amount>]
credit:
description: Affiche les crédits du plugin
exploderandom:
description: Fait exploser un joueur aléatoire, côute 32 diamants
link:
description: Lie un compte Minecraft à Discord
manuallink:
Expand Down

0 comments on commit b51a316

Please sign in to comment.