Développement plugin Minecraft probleme cannot access org.bukkit.entity.Player

Screen_V

Architecte en herbe
10 Avril 2016
117
2
125
28
Bonjour je développe actuellement mon premier plugin minecraft, mais je rencontre un probleme. Je développe sur IntelliJ et sous maven.

Voici la structure de mon projet

Code:
AdminPlugin/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── me/
│   │   │       └── jonathan/
│   │   │           └── adminplugin/
│   │   │               ├── Main.java            (Point d'entrée principal)
│   │   │               ├──InventoryManager.java     (Classe principale du plugin)
│   │   │               ├── CommandAdmin.java    (Commande /admin)
│   │   │               └── EventListener.java   (Écouteur d'événements)
│   │   └── resources/
│   │       └── plugin.yml                       (Fichier de configuration du plugin)
└── pom.xml                                      (Fichier de configuration Maven)


Dans mon pom.xml voici ce qu'il y a:

Code:
package me.jonathan.adminplugin;

import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;


public class Main extends JavaPlugin {
@Override
    public void onEnable() {
PluginCommand adminCommand = getCommand("admin");
        if (adminCommand != null) {
adminCommand.setExecutor(new CommandAdmin()); // Passer une référence de Main
        } else {
getLogger().severe("Command 'admin' not found in plugin.yml");
        }

getServer().getPluginManager().registerEvents(new EventListener(this), this); // Passer une référence de Main
    }

@Override
    public void onDisable() {
// Logique pour gérer la désactivation du plugin si nécessaire
    }
}


Dans mon main.jave:

Code:
package me.jonathan.adminplugin;

import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;


public class Main extends JavaPlugin {
@Override
    public void onEnable() {
PluginCommand adminCommand = getCommand("admin");
        if (adminCommand != null) {
adminCommand.setExecutor(new CommandAdmin()); // Passer une référence de Main
        } else {
getLogger().severe("Command 'admin' not found in plugin.yml");
        }

getServer().getPluginManager().registerEvents(new EventListener(this), this); // Passer une référence de Main
    }

@Override
    public void onDisable() {
// Logique pour gérer la désactivation du plugin si nécessaire
    }
}


Dans mon InventoryManager:

Code:
package me.jonathan.adminplugin;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Material;
import org.bukkit.GameMode;

import java.util.HashMap;
import java.util.UUID;

public class InventoryManager {

private static final HashMap<UUID, ItemStack[]> inventories = new HashMap<>();
    private static final HashMap<UUID, GameMode> gameModes = new HashMap<>();

    public static void saveInventory(Player player) {
inventories.put(player.getUniqueId(), player.getInventory().getContents());
        gameModes.put(player.getUniqueId(), player.getGameMode());
        player.getInventory().clear();
    }

public static void restoreInventory(Player player) {
if (inventories.containsKey(player.getUniqueId())) {
player.getInventory().setContents(inventories.get(player.getUniqueId()));
            inventories.remove(player.getUniqueId());
        }
if (gameModes.containsKey(player.getUniqueId())) {
player.setGameMode(gameModes.get(player.getUniqueId()));
            gameModes.remove(player.getUniqueId());
        }
    }

public static void giveAdminCompass(Player player) {
ItemStack compass = new ItemStack(Material.COMPASS);
        player.getInventory().setItem(4, compass);
    }
}

Dans mon commandadmin.java:

Code:
package me.jonathan.adminplugin;

import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class CommandAdmin implements CommandExecutor {

public CommandAdmin() {
    }

@Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
            if (args.length == 1) {
if (args[0].equalsIgnoreCase("on")) {
InventoryManager.saveInventory(player);
                    player.setGameMode(GameMode.SPECTATOR);
                    InventoryManager.giveAdminCompass(player);
                    player.sendMessage("Admin mode enabled.");
                    return true;
                } else if (args[0].equalsIgnoreCase("off")) {
InventoryManager.restoreInventory(player);
                    player.sendMessage("Admin mode disabled.");
                    return true;
                }
            }
        }
return false;
    }
}

Dans mon EventListener:

Code:
package me.jonathan.adminplugin;

import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class EventListener implements Listener {

private final Main plugin;

    public EventListener(Main plugin) {
this.plugin = plugin;
    }

@EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
// Votre logique de gestion des interactions de joueur ici
        if (event.hasItem()) {
ItemStack item = event.getItem();
            if (item != null && item.getType().equals(Material.COMPASS)) {
Inventory adminMenu = plugin.getServer().createInventory(null, 27, "Admin Menu");
                event.getPlayer().openInventory(adminMenu);
            }
        }
    }
}


Et le message d'erreur est cannot access org.bukkit.entity.Player.
Et je n'arrive pas a régler cette erreur.

Merci de votre aide.
 
Bonjour,

Il faudrait l'erreur complète (ou du moins la ligne), tu peux l'obtenir en faisant un maven compile (tu devrais avoir un panneau latéral à droite dans IntelliJ pour Maven).

Cordialement,
ShE3py
 
Bonjour,

Il faudrait l'erreur complète (ou du moins la ligne), tu peux l'obtenir en faisant un maven compile (tu devrais avoir un panneau latéral à droite dans IntelliJ pour Maven).

Cordialement,
ShE3py
Bonjour, merci de ta réponse.
voici l'erreur, et la ligne
Capture d'écran 2024-07-08 162006.png
Capture d'écran 2024-07-08 162129.png
 
C'est étrange que t'as pas des erreurs dans tous les fichiers, il doit s'arrêter à la 1re.

Pourrais-tu envoyer le pom.xml ? Tu l'as mal envoyé ; tu peux d'ailleurs faire une coloration syntaxique en précisant le langage :
Code:
```xml
le contenu du fichier
```
 
C'est étrange que t'as pas des erreurs dans tous les fichiers, il doit s'arrêter à la 1re.

Pourrais-tu envoyer le pom.xml ? Tu l'as mal envoyé ; tu peux d'ailleurs faire une coloration syntaxique en précisant le langage :
Code:
```xml
le contenu du fichier
```
Voila tout ce qu'il y a dans pom.xml

XML:
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>me.jonathan</groupId>
    <artifactId>adminplugin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>AdminPlugin</name>
    <description>Plugin d'administration pour Minecraft</description>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <repositories>
        <repository>
            <id>spigot-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.spigotmc</groupId>
            <artifactId>spigot-api</artifactId>
            <version>1.21-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>24.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
 
Possible que tu envoies tout ton projet zippé ? Je sais pas vraiment quelle mouche le pique :confused:
 
Ça compile chez moi, possible que tu envoies toutes les logs de Maven ?


XML:
C:\Users\joj93\.jdks\corretto-1.8.0_412\bin\java.exe -Dmaven.multiModuleProjectDirectory=C:\Users\joj93\IdeaProjects\AdminPlugin -Djansi.passthrough=true "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA 2024.1.4\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA 2024.1.4\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA 2024.1.4\plugins\maven\lib\maven-event-listener.jar" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.1.4\lib\idea_rt.jar=57871:C:\Program Files\JetBrains\IntelliJ IDEA 2024.1.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2024.1.4\plugins\maven\lib\maven3\boot\plexus-classworlds-2.7.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2024.1.4\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2024.1.4 compile
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< me.jonathan:adminplugin >-----------------------
[INFO] Building AdminPlugin 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- resources:3.3.1:resources (default-resources) @ adminplugin ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource from src\main\resources to target\classes
[INFO] 
[INFO] --- compiler:3.8.1:compile (default-compile) @ adminplugin ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 4 source files to C:\Users\joj93\IdeaProjects\AdminPlugin\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Users/joj93/IdeaProjects/AdminPlugin/src/main/java/me/jonathan/adminplugin/InventoryManager.java:[3,25] cannot access org.bukkit.entity.Player
  bad class file: C:\Users\joj93\.m2\repository\org\spigotmc\spigot-api\1.21-R0.1-SNAPSHOT\spigot-api-1.21-R0.1-SNAPSHOT.jar(org/bukkit/entity/Player.class)
    class file has wrong version 61.0, should be 52.0
    Please remove or make sure it appears in the correct subdirectory of the classpath.
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.929 s
[INFO] Finished at: 2024-07-08T16:19:06+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project adminplugin: Compilation failure
[ERROR] /C:/Users/joj93/IdeaProjects/AdminPlugin/src/main/java/me/jonathan/adminplugin/InventoryManager.java:[3,25] cannot access org.bukkit.entity.Player
[ERROR]   bad class file: C:\Users\joj93\.m2\repository\org\spigotmc\spigot-api\1.21-R0.1-SNAPSHOT\spigot-api-1.21-R0.1-SNAPSHOT.jar(org/bukkit/entity/Player.class)
[ERROR]     class file has wrong version 61.0, should be 52.0
[ERROR]     Please remove or make sure it appears in the correct subdirectory of the classpath.
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Process finished with exit code 1
 
Tu as Java 8 alors que Spigot a été compilé pour Java 17, met à jour Java.
https://adoptium.net/fr/temurin/releases/?os=windows&arch=x64&package=jre&version=17
(j'ai essayé avec Java 8 mais j'avais l'erreur sur un autre fichier alors bon)

Puis change la version dans IntelliJ (menu hamburger en haut à gauche > Project Structure > Project Settings / Project > SDK > puis prend Java 17 dans la liste déroulante (il devrait le détecter automatiquement)).

Ainsi que le pom.xml en conséquence :
XML:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>me.jonathan</groupId>
    <artifactId>adminplugin</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>AdminPlugin</name>
    <description>Plugin d'administration pour Minecraft</description>
   
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
   
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
               
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spigot-repo</id>
            <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.spigotmc</groupId>
            <artifactId>spigot-api</artifactId>
            <version>1.21-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
       
        <dependency>
            <groupId>org.jetbrains</groupId>
            <artifactId>annotations</artifactId>
            <version>24.1.0</version>
        </dependency>
    </dependencies>
</project>