Skocz do zawartości
  • 0

getName()


rykker

Pytanie

Cześć, to znowu ja :D. Nie wiem dlaczego, ale nie działa mi getName()? jak tego użyć? Pisane na wersji 1.15
błąd:
 

The method getName() is undefined for the type Inventory
if(e.getInventory().getName().equals(ChatColor.AQUA + "Custom GUI")){

Cały kod:
 

package GUI;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import GUI.GUICommand;
import Core.Main;

public class GUIMoveItem extends JavaPlugin implements Listener{
 
    @EventHandler
    public void clickEvent(InventoryClickEvent e){
 
        //Check to see if its the GUI menu
        if(e.getInventory().getName().equals(ChatColor.AQUA + "Custom GUI")){
            Player player = (Player) e.getWhoClicked();
            switch(e.getCurrentItem().getType()){
                case TNT:
                    player.closeInventory();
                    player.setHealth(0.0);
                    player.sendMessage("1");
                    break;
                case BREAD:
                    player.closeInventory();
                    player.setFoodLevel(20);
                    player.sendMessage("2");
                    break;
                case DIAMOND_SWORD:
                    player.closeInventory();
                    player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
                    player.sendMessage("");
                    break;
            }
 
 
            e.setCancelled(true);
        }
    }

    
}

 

Odnośnik do komentarza
https://skript.pl/temat/40059-getname/
Udostępnij na innych stronach

11 odpowiedzi na to pytanie

Rekomendowane odpowiedzi

  • 0
46 minut temu, rykker napisał:

Cześć, to znowu ja :D. Nie wiem dlaczego, ale nie działa mi getName()? jak tego użyć? Pisane na wersji 1.15
błąd:
 


The method getName() is undefined for the type Inventory

if(e.getInventory().getName().equalsIgnoreCase(ChatColor.AQUA+"Custom GUI"){

Cały kod:
 


package GUI;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import GUI.GUICommand;
import Core.Main;

public class GUIMoveItem extends JavaPlugin implements Listener{
 
    @EventHandler
    public void clickEvent(InventoryClickEvent e){
 
        //Check to see if its the GUI menu
        if(e.getInventory().getName().equalsIgnoreCase(ChatColor.AQUA + "Custom GUI")){
            Player player = (Player) e.getWhoClicked();
            switch(e.getCurrentItem().getType()){
                case TNT:
                    player.closeInventory();
                    player.setHealth(0.0);
                    player.sendMessage("1");
                    break;
                case BREAD:
                    player.closeInventory();
                    player.setFoodLevel(20);
                    player.sendMessage("2");
                    break;
                case DIAMOND_SWORD:
                    player.closeInventory();
                    player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
                    player.sendMessage("");
                    break;
            }
 
 
            e.setCancelled(true);
        }
    }

    
}

 

Spróbuj teraz - zmieniłem z equals() na equalsIgnoreCase()

Edytowane przez CountingDogs
Odnośnik do komentarza
https://skript.pl/temat/40059-getname/#findComment-250569
Udostępnij na innych stronach

  • 0
Godzinę temu, CountingDogs napisał:

Spróbuj teraz - zmieniłem z equals() na equalsIgnoreCase()

przecież to to samo, tylko z ignorowaniem wielkości znaków, dodatkowo nie jest problem z tym tylko z getName

5 minut temu, CountingDogs napisał:

Zamień getName() na getTitle()

samo to nic nie da

 

do autora, zmień metodę: 

e.getView().getTitle().equals("fsdfsdsf")

 

Odnośnik do komentarza
https://skript.pl/temat/40059-getname/#findComment-250579
Udostępnij na innych stronach

  • 0
15 minut temu, kerpson napisał:

przecież to to samo, tylko z ignorowaniem wielkości znaków, dodatkowo nie jest problem z tym tylko z getName

samo to nic nie da

 

do autora, zmień metodę: 


e.getView().getTitle().equals("fsdfsdsf")

 

Błędu już nie ma, ale można wyciągać itemki z GUI. mam ostrzeżenie przy:

switch(e.getCurrentItem().getType())

 

Ostrzeżenie:

The enum constant NETHER_BRICK_FENCE needs a corresponding case label in this enum switch on Material

Jest takich kolejnych 99 ostrzeżeń z innymi materiałami.

Odnośnik do komentarza
https://skript.pl/temat/40059-getname/#findComment-250582
Udostępnij na innych stronach

  • 0
14 minut temu, kerpson napisał:

switch jest niepotrzebne, zrób np. tak


Material item = e.getCurrentItem().getType();
if (item == Material.TNT) {
	cos

 

Brak błędów w kodzie, nadal da się wyjąć itemki

KOD:

package GUI;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

public class GUIMoveItem extends JavaPlugin implements Listener{
 
    @EventHandler
    public void clickEvent(InventoryClickEvent e){
 
        //Check to see if its the GUI menu
        if(e.getView().getTitle().equals(ChatColor.AQUA + "Custom GUI")){
            Player player = (Player) e.getWhoClicked();
            Material item = e.getCurrentItem().getType();
            if (item == Material.TNT) {
            	player.closeInventory();
                player.setHealth(0.0);
                player.sendMessage("1");
                e.setCancelled(true);
            }
            if (item == Material.BREAD) {
            	player.closeInventory();
                player.setFoodLevel(20);
                player.sendMessage("2");
                e.setCancelled(true);
            }
            if (item == Material.DIAMOND_SWORD) {
            	player.closeInventory();
                player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
                player.sendMessage("3");
                e.setCancelled(true);
            }
        }
    }

    
}

BŁĄD Z KONSOLI:
 

[21:36:54] [Server thread/INFO]: [RKRplugin] Enabling RKRplugin v1.0
[21:36:54] [Server thread/ERROR]: Error occurred while enabling RKRplugin v1.0 (Is it up to date?)
java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:178) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:52) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at GUI.GUIMoveItem.<init>(GUIMoveItem.java:12) ~[?:?]
        at Core.Main.onEnable(Main.java:19) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:462) [spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:376) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:457) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:267) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:784) [spigot.jar:git-Spigot-800b93f-8160e29]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:181) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:52) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at Core.Main.<init>(Main.java:11) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_231]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_231]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_231]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:72) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:334) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:253) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.loadPlugins(CraftServer.java:351) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:203) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        ... 2 more

 

Edytowane przez rykker
Odnośnik do komentarza
https://skript.pl/temat/40059-getname/#findComment-250587
Udostępnij na innych stronach

  • 0
4 minuty temu, rykker napisał:

Brak błędów w kodzie, nadal da się wyjąć itemki

KOD:


package GUI;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

public class GUIMoveItem extends JavaPlugin implements Listener{
 
    @EventHandler
    public void clickEvent(InventoryClickEvent e){
 
        //Check to see if its the GUI menu
        if(e.getView().getTitle().equals(ChatColor.AQUA + "Custom GUI")){
            Player player = (Player) e.getWhoClicked();
            Material item = e.getCurrentItem().getType();
            if (item == Material.TNT) {
            	player.closeInventory();
                player.setHealth(0.0);
                player.sendMessage("1");
                e.setCancelled(true);
            }
            if (item == Material.BREAD) {
            	player.closeInventory();
                player.setFoodLevel(20);
                player.sendMessage("2");
                e.setCancelled(true);
            }
            if (item == Material.DIAMOND_SWORD) {
            	player.closeInventory();
                player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD));
                player.sendMessage("3");
                e.setCancelled(true);
            }
        }
    }

    
}

BŁĄD Z KONSOLI:
 


[21:36:54] [Server thread/INFO]: [RKRplugin] Enabling RKRplugin v1.0
[21:36:54] [Server thread/ERROR]: Error occurred while enabling RKRplugin v1.0 (Is it up to date?)
java.lang.IllegalArgumentException: Plugin already initialized!
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:178) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:52) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at GUI.GUIMoveItem.<init>(GUIMoveItem.java:12) ~[?:?]
        at Core.Main.onEnable(Main.java:19) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:417) [spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:462) [spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:376) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:457) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:267) [spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:784) [spigot.jar:git-Spigot-800b93f-8160e29]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
Caused by: java.lang.IllegalStateException: Initial initialization
        at org.bukkit.plugin.java.PluginClassLoader.initialize(PluginClassLoader.java:181) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.java.JavaPlugin.<init>(JavaPlugin.java:52) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at Core.Main.<init>(Main.java:11) ~[?:?]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_231]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_231]
        at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_231]
        at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_231]
        at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:72) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:334) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:253) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at org.bukkit.craftbukkit.v1_15_R1.CraftServer.loadPlugins(CraftServer.java:351) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:203) ~[spigot.jar:git-Spigot-800b93f-8160e29]
        ... 2 more

 

Po co dajesz "extends JavaPlugin" ? Przecież z tego co widzę, to nie jest klasa główna tylko Listener, wystarczy samo "implements Listener", a w klasie glównej Bukkit.getPluginManager().registerEvents(new NazwaListenera(), this);

Odnośnik do komentarza
https://skript.pl/temat/40059-getname/#findComment-250588
Udostępnij na innych stronach

  • 0
15 godzin temu, kerpson napisał:

Po co dajesz "extends JavaPlugin" ? Przecież z tego co widzę, to nie jest klasa główna tylko Listener, wystarczy samo "implements Listener", a w klasie glównej Bukkit.getPluginManager().registerEvents(new NazwaListenera(), this);

Poprawiłem, mam zapisane listener w klasie głównej. Ale to nic nie daje.

Edytowane przez rykker
Odnośnik do komentarza
https://skript.pl/temat/40059-getname/#findComment-250589
Udostępnij na innych stronach

Nieaktywny
Ten temat został zamknięty. Brak możliwości dodania odpowiedzi.
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...