Ir para conteúdo
  • 0

[Resolvido] Verificar inventário/armadura.


DEVKEWI

Pergunta

Salve,

Está tentando verificar o inventário do jogador com este código:

if (p.getInventory().getContents().length == 0) {}

Pelo que eu entendi ele vai ver a quantidade de itens no inventário, então o  == 0 seria se o inventário estivesse vazio. E com isso iria fazer retornar uma mensagem.

 

 

E para verificar a armadura do jogador (no caso o elmo) eu fiz isso:

if (p.getInventory().getHelmet() != null && p.getInventory().getHelmet().getTypeId() != 310) {}

Agradeço ao @MrPowerGamerBR, e ao @TzMarcio pelo código a cima.

 

O problema é que está deixando o jogador executar o comando sendo que o inventário está vazio.

E se estiver sem armadura da erro na linha de verificar a armadura, como mostra a print abaixo:

I2FN3J0.png

 

OBS: Se o jogador não possuir elmo/não possuir elmo de diamante retorna uma mensagem! (é isso que tentei fazer =/ )

Editado por Kewilleen G.
Link para o comentário
Compartilhar em outros sites

9 respostass a esta questão

Posts Recomendados

for(ItemStack item : p.getInventory().getContents()){

    if(item != null){

        p.sendMessage("§cEsvazie o inventario");

        return true;

    }

}

 

 

eu uso isso para verificar se o inventario ta vazio

Link para o comentário
Compartilhar em outros sites

 

for(ItemStack item : p.getInventory().getContents()){
    if(item != null){
        p.sendMessage("§cEsvazie o inventario");
        return true;
    }
}
 
 
eu uso isso para verificar se o inventario ta vazio

 

Só mudei para == null porque quero que ele tenha algo no inventário :p

Link para o comentário
Compartilhar em outros sites

Utilize este método:

public boolean InvVazio(Player p){
    for(ItemStack item : p.getInventory().getContents()){
        if(item != null && item.getType()!=Material.AIR)
        return false;
    }
    for(ItemStack item : p.getInventory().getArmorContents()){
        if(item != null && item.getType()!=Material.AIR)
        return false;
    }
    return true;
}

if(InvVazio(p)){
    // Está com o inv vazio
    // Incluindo armadura
}
Link para o comentário
Compartilhar em outros sites

 

Utilize este método:

public boolean InvVazio(Player p){
    for(ItemStack item : p.getInventory().getContents()){
        if(item == null && item.getType()==Material.AIR)
        return false;
    }
    for(ItemStack item : p.getInventory().getArmorContents()){
        if(item != null && item.getType()!=Material.AIR)
        return false;
    }
    return true;
}

if(InvVazio(p)){
    // Está com o inv vazio
    // Incluindo armadura
}

Já usei este método ;-;

 

Tentei usar esse:

public static boolean inv(Player p) {
        for (ItemStack item : p.getInventory().getContents()) {
           if(item != null && item.getType()!=Material.AIR)
                p.sendMessage(KwMetodos.getReplaced("Arena.Sem_Itens"));

            return true;
        }
        for (ItemStack item : p.getInventory().getArmorContents()) {
            if (item == null && item.getType() == Material.AIR)
                p.sendMessage(KwMetodos.getReplaced("Arena.Sem_Armadura"));
            return false;
        }
        return true;
    }

Mas como eu deixei == null

Ele da esse pequeno erro:

2016-04-19 14:15:23 [SEVERE] null
org.bukkit.command.CommandException: Unhandled exception executing command 'arena' in plugin MomentArena v1.0
	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
	at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:546)
	at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:985)
	at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:901)
	at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:846)
	at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
	at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:215)
	at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:115)
	at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
	at org.spigotmc.MultiplexingServerConnection.b(MultiplexingServerConnection.java:72)
	at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:583)
	at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:227)
	at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:472)
	at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:404)
	at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
Caused by: java.lang.NullPointerException
	at momentarena.metodos.KwMetodos.inv(KwMetodos.java:141)
	at momentarena.comandos.KwArena.onCommand(KwArena.java:21)
	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
	... 15 more

A linha 141:

hpHflxt.png

Editado por Kewilleen G.
Link para o comentário
Compartilhar em outros sites

Se tu tiver usando java8

 



public boolean isEmpty( Player player )
{
  return Steam.of( player.getArmorContents() ).allMatch( Objects::isNull ) &&
Steam.of( player.getInventory().getContents() ).allMatch( Objects::isNull )
}


 

Java 7

 



public boolean isEmpty( Player player )
{
  for ( ItemStack is : player.getArmorContents() )
    if (is != null) return false;


  for ( ItemStack is : player.getInventory().getContents() )
    if (is != null) return false;

return true;
}

Editado por leonardosc
Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novos posts.
×
×
  • Criar Novo...