Résolu Aide Plugin 1.19.4

Zpazod

Bucheron
1 Août 2021
15
0
11
23
Bonjour, bonsoir, j'aurai ici un problème de plugin, en effet, le projet est de faire une boss bar qui s'actualise a chaque fois qu'une commande est éxécutée, or, quand j'éxécute la commande, rien ne se passe. Voici le fichier main :
package fr.zpazod.speedrunpluginre;

import java.io.File;
import java.io.IOException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.Bukkit;

public final class main extends JavaPlugin {

private File pointsFile;
private FileConfiguration pointsConfig;
public PointsBossBar pointsBossBar;

@Override
public void onEnable() {
// Plugin startup logic
getLogger().info("SpeedrunPlugin has been enabled.");

PointsTab pointsTab = new PointsTab(this);
getServer().getPluginManager().registerEvents(pointsTab, this);

pointsBossBar = new PointsBossBar(this);
getServer().getPluginManager().registerEvents(pointsBossBar, this);

PointsCommand pointsCommand = new PointsCommand(this);
getCommand("points").setExecutor(pointsCommand);

VanishCommand vanishCommand = new VanishCommand();
getCommand("vanish").setExecutor(new VanishCommand());




if (!Bukkit.getServer().getPluginManager().getPlugin("SpeedrunPluginRe").getDataFolder().exists()) {
Bukkit.getServer().getPluginManager().getPlugin("SpeedrunPluginRe").getDataFolder().mkdir();
}
pointsFile = new File(Bukkit.getServer().getPluginManager().getPlugin("SpeedrunPluginRe").getDataFolder(), "points.yml");
if (!pointsFile.exists()) {
try {
pointsFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
pointsConfig = YamlConfiguration.loadConfiguration(pointsFile);




}


@Override
public void onDisable() {
// Plugin shutdown logic
getLogger().info("SpeedrunPlugin has been disabled.");
}
}

Voici le fichier de la commande :
package fr.zpazod.speedrunpluginre;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
import java.io.IOException;

public class PointsCommand implements CommandExecutor {
private main plugin;
private File pointsFile;
private FileConfiguration pointsConfig;

public PointsCommand(main plugin) {
this.plugin = plugin;
pointsFile = new File(Bukkit.getServer().getPluginManager().getPlugin("SpeedrunPluginRe").getDataFolder(), "points.yml");
pointsConfig = YamlConfiguration.loadConfiguration(pointsFile);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use this command.");
return true;
}
Player player = (Player) sender;
if (!player.hasPermission("points.give")) {
player.sendMessage("You do not have permission to use this command.");
return true;
}
if (args.length != 3) {
player.sendMessage("Usage: /points give <player> <points>");
return true;
}
Player targetPlayer = Bukkit.getPlayer(args[1]);
if (targetPlayer == null) {
player.sendMessage("Player not found.");
return true;
}
int points;
try {
points = Integer.parseInt(args[2]);
} catch (NumberFormatException e) {
player.sendMessage("Invalid number of points.");
return true;
}
if (points < 0) {
player.sendMessage("Points must be a positive number.");
return true;
}
int newPoints = pointsConfig.getInt(targetPlayer.getName()) + points;
pointsConfig.set(targetPlayer.getName(), newPoints);
try {
pointsConfig.save(pointsFile);
} catch (IOException e) {
e.printStackTrace();
}

plugin.pointsBossBar.updateBossBar();

targetPlayer.setPlayerListName(targetPlayer.getName() + " - " + newPoints + " points");

player.sendMessage("You have given " + targetPlayer.getName() + " " + points + " points.");
return true;
}
}
Et voici le fichier gérant la boss bar :
package fr.zpazod.speedrunpluginre;

import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;

import java.io.File;

public class PointsBossBar implements Listener {
private File pointsFile;
private FileConfiguration pointsConfig;
private BossBar bossBar;

public PointsBossBar(Plugin plugin) {
pointsFile = new File(plugin.getDataFolder(), "points.yml");
pointsConfig = YamlConfiguration.loadConfiguration(pointsFile);
bossBar = Bukkit.createBossBar("", BarColor.GREEN, BarStyle.SEGMENTED_10);
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
updateBossBar();
bossBar.addPlayer(player);
}

public void updateBossBar() {
String topPlayerName = "";
int topPlayerPoints = 0;
for (String playerName : pointsConfig.getKeys(false)) {
int playerPoints = pointsConfig.getInt(playerName);
if (playerPoints > topPlayerPoints) {
topPlayerName = playerName;
topPlayerPoints = playerPoints;
}
}
bossBar.setTitle(topPlayerName + " - " + topPlayerPoints + " points");
double progress = topPlayerPoints / 100.0;
bossBar.setProgress(progress);
}
}
En éspérant trouver de l'aide
 

ShE3py

Enbogueuse
Support
26 Septembre 2015
4 139
163
464
247
21
Mìlhüsa
Bonsoir,

Java:
public final class main extends JavaPlugin {
  • En Java, les classes commencent toujours par une majuscule.
  • Main est trop ambiguë, préfère choisir le nom du plugin (SpeedrunPluginRe).

Java:
if (!Bukkit.getServer().getPluginManager().getPlugin("SpeedrunPluginRe").getDataFolder().exists()) {
    Bukkit.getServer().getPluginManager().getPlugin("SpeedrunPluginRe").getDataFolder().mkdir();
}
  • Tu es le plugin, pas besoin de le récupérer je-ne-sais-où.
  • Pas besoin de vérifier si le dossier existe, mkdir() sera sans effet.
Java:
this.getDataFolder().mkdir();


Java:
public PointsCommand(main plugin) {
    this.plugin = plugin;
    pointsFile = new File(Bukkit.getServer().getPluginManager().getPlugin("SpeedrunPluginRe").getDataFolder(), "points.yml");
    pointsConfig = YamlConfiguration.loadConfiguration(pointsFile);
}
Tu as déjà le plugin en paramètre ;
Java:
public PointsCommand(SpeedrunPluginRe plugin) {
    this.plugin = plugin;
    this.config = YamlConfiguration.loadConfiguration(this.plugin.getDataFolder(), "points.yml"));
}


Java:
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use this command.");
           return true;
       }
Player player = (Player) sender;
L'exécuteur n'a pas à être un joueur pour faire /points give.


Java:
int newPoints = pointsConfig.getInt(targetPlayer.getName()) + points;
       pointsConfig.set(targetPlayer.getName(), newPoints);
       try {
pointsConfig.save(pointsFile);
       } catch (IOException e) {
e.printStackTrace();
       }
       plugin.pointsBossBar.updateBossBar();
     
       targetPlayer.setPlayerListName(targetPlayer.getName() + " - " + newPoints + " points");
       player.sendMessage("You have given " + targetPlayer.getName() + " " + points + " points.");
Le joueur peut changer de nom ; utilise son UUID.
Ceci doit être une fonction dédiée ;
Java:
public class SpeedrunPluginRe extends JavaPlugin {
    public void addPoints(UUID playerId, int points) {
        // TODO: add + save + update scoreboard + update bossbar
    }
}

// onKommand()
this.plugin.addPoints(targetPlayer.getUniqueId(), points);


Tu ouvres trois fois points.yml en écriture dans SpeedrunPluginRe, PointsCommand et PointsBossBar. Cependant, FileConfiguration est stockée en mémoire et est écrit sur le disque quand tu fais save() ; tes trois classes utilisent toutes une zone mémoire différente et ne lisent le fichier sur le disque qu'au démarrage : modifier PointsCommand.pointsConfig ne modifiera pas PointsBossBar.pointsConfig.

Le fichier de configuration ne doit être ouvert que dans une seule classe, par ex. SpeedrunPluginRe.

Cordialement,
ShE3py
 

Zpazod

Bucheron
1 Août 2021
15
0
11
23
Yep, j'ai en effet changé l'ouverture de points.yml, et chargé la configuration aux constructeurs des différentes classes, et ça fonctionne, merci bien : )(merci beaucoup aussi pour les autres tips, y'as des trucs que je savait pas, et ça fait toujours du bien t'entendre des conseils) !