Skocz do zawartości
  • 0

Problem z pluginem


4RNI

Pytanie

Witam po wpisaniu komendy wyskakuje mi blad "An internal error occured while attempting to perform this command", zamiast wiadomosci z klasy Lang. Co zrobic?

Kod pluginu:

package pl.unixhc.tools.commands;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import pl.unixhc.tools.config.Lang;
import pl.unixhc.tools.managers.CommandManager;
import pl.unixhc.tools.utils.Util;

public class InvseeCommand extends CommandManager{
  public InvseeCommand(){
    super("invsee", true, "unixhc.invsee", "Otwiera ekwipunek gracza", "/invsee [nick]", new String[0]);
  }
  
  public boolean exe(CommandSender sender, String command, String[] args){
    if (args.length < 1){
      Util.sendUsage(sender, getUsage());
      return false;
    }
    Player playerInv = Bukkit.getPlayer(args[0]);
    if (playerInv == null){
      sender.sendMessage(Lang.getInst().PLAYER_NOT_EXISTS);
      return false;
    }
    Player player = (Player)sender;
    player.openInventory(playerInv.getInventory());
    return true;
  }
}

Main:

package pl.unixhc.tools;

import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import pl.unixhc.tools.commands.*;
import pl.unixhc.tools.listeners.*;

public class Main extends JavaPlugin{
	  private static Main inst;
	public void onEnable(){
	    registerCommands();
	    registerListeners();	    
		saveDefaultConfig();
	}
	public static Main getInst(){
		return inst;
	}
	  
	public void registerCommands(){
	    new InvseeCommand().register();
	}
	public void registerListeners(){
		PluginManager pm = Bukkit.getPluginManager();
		pm.registerEvents(new ProtectPluginsListener(), this);
		pm.registerEvents(new AsyncPlayerChatListener(), this);;
		pm.registerEvents(new BlockPlaceListener(), this);
		pm.registerEvents(new SignColorListener(), this);
	}
}

Lang:

package pl.unixhc.tools.config;

public class Lang{
	  private static Lang inst;
	  public String USAGE;
	  public String PERMISSION;
	  public String PLAYER_NOT_EXISTS;
	  public String INVENTORY_OPENED;
	  public String COMMAND_PLAYER;
	  
	  public static Lang getInst(){
	    if(inst == null){
	    	new Lang();
	    }
	    return inst;
	  }
	  
	  public Lang(){
		  this.PERMISSION = "&4Nie masz uprawnien.";
		  this.PLAYER_NOT_EXISTS = "&4Ten gracz jest offline.";
		  this.INVENTORY_OPENED = "&aOtworzono ekwipunek gracza {PLAYER}";
		  this.USAGE = "&cPoprawne uzycie: {USAGE}.";
		  this.COMMAND_PLAYER = "&4Ta komenda jest dostepna tylko dla gracza.";
	  }
}

Util:

package pl.unixhc.tools.utils;

import org.bukkit.command.CommandSender;

import pl.unixhc.tools.config.Lang;

public class Util {
	  public static boolean sendMessage(CommandSender sender, String message)
	  {
	    sender.sendMessage(message);
	    return true;
	  }
	  public static boolean sendPermission(CommandSender sender, String permission)
	  {
	    sender.sendMessage(Lang.getInst().PERMISSION.replace("{PERM}", permission));
	    return true;
	  }
	  public static boolean sendUsage(CommandSender sender, String usage)
	  {
	    sender.sendMessage(Lang.getInst().USAGE.replace("{USAGE}", usage));
	    return true;
	  }
}

CommandManager:

package pl.unixhc.tools.managers;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import pl.unixhc.tools.config.Lang;
import pl.unixhc.tools.utils.Util;

public abstract class CommandManager
  extends org.bukkit.command.Command
{
  private String name;
  private String description;
  private String usage;
  private String permission;
  private List<String> aliases;
  private boolean player;
  
  public CommandManager(String name, boolean player, String permission, String description, String usage, String... aliases)
  {
    super(name, description, usage, Arrays.asList(aliases));
    this.name = name;
    this.description = description;
    this.usage = usage;
    this.permission = permission;
    this.aliases = Arrays.asList(aliases);
    this.player = player;
  }
  
  public boolean execute(CommandSender sender, String command, String[] args)
  {
    if ((isPlayer()) && (!(sender instanceof Player))) {
      return Util.sendMessage(sender, Lang.getInst().COMMAND_PLAYER);
    }
    if ((getPermission() != null) && (!sender.hasPermission(getPermission()))) {
      return Util.sendPermission(sender, getPermission());
    }
    return exe(sender, command, args);
  }
  
  public abstract boolean exe(CommandSender paramCommandSender, String paramString, String[] paramArrayOfString);
  
  public String getName()
  {
    return this.name;
  }
  
  public String getDescription()
  {
    return this.description;
  }
  
  public String getUsage()
  {
    return this.usage;
  }
  
  public String getPermission()
  {
    return this.permission;
  }
  
  public List<String> getAliases()
  {
    return this.aliases;
  }
  
  public boolean isPlayer()
  {
    return this.player;
  }
  
  public CommandManager setDescription(String description)
  {
    this.description = description;
    return this;
  }
  
  public CommandManager setUsage(String usage)
  {
    this.usage = usage;
    return this;
  }
  
  public void setPermission(String permission)
  {
    this.permission = permission;
  }
  
  public CommandManager setAliases(List<String> aliases)
  {
    this.aliases = aliases;
    return this;
  }
  
  public void setPlayer(boolean player)
  {
    this.player = player;
  }
  
  public void register()
  {
    try
    {
      if (getName() == null) {
        return;
      }
      Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
      f.setAccessible(true);
      CommandMap cmap = (CommandMap)f.get(Bukkit.getServer());
      cmap.register(getName(), this);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  
  public boolean onExecute(CommandSender sender, String[] args)
  {
    return false;
  }
}

Log z konsoli: 

null
org.bukkit.command.CommandException: Unhandled exception executing 'invsee' in pl.unixhc.tools.commands.InvseeCommand(invsee)
	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:148) ~[Silnik.jar:git-Spigot-db6de12-18fbb24]
	at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_161]
	at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_161]
	at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [Silnik.jar:git-Spigot-db6de12-18fbb24]
	at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
Caused by: java.lang.NullPointerException
	at pl.unixhc.tools.utils.Util.sendPermission(Util.java:20) ~[?:?]
	at pl.unixhc.tools.managers.CommandManager.execute(CommandManager.java:41) ~[?:?]
	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[Silnik.jar:git-Spigot-db6de12-18fbb24]
	... 14 more

 

Edytowane przez 4RNI
Odnośnik do komentarza
Udostępnij na innych stronach

1 odpowiedź na to pytanie

Rekomendowane odpowiedzi

  • 0
private static Lang inst;

[...]

public static Lang getInst(){
    if(inst == null){
        new Lang();
    }
    return inst;
}

inst będzie zawsze nullem. Musicie definiować kiedyś inst. Proponuje to zrobić w linijce new Lang(), czyli będzie ostatecznie wyglądała

inst = new Lang();

 

Używasz tutaj singleton, który w mojej opinii (chyba większości z tej branży) uważa za anty-wzorzec, który budują złą architekturę ze względu na użycie zmiennych globalnych, które są modyfikowalne (mutable). https://stormit.pl/singleton/#singleton-jako-antywzorzec

Zmienne nazywamy z małej litery!

Odnośnik do komentarza
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ę...