Changing player language group

Discussion in 'Spigot Plugin Development' started by Nighterance, Apr 30, 2017.

  1. Hello!
    So I made a plugin while ago, then left it, it was working fine. Basicaly what it does - on command opens a gui, where, on item click you are added to a group, so in playerchat.yml there is Nick group: English ect.
    So everything was working. Now I tested, it does not work anymore and brings me error that I see for first time.
    Maybe you can help me?
    This is error
    java.lang.NoSuchMethodError: me.Nighterance.main.AbstractFile.<init>(Lme/Nighterance/main/ChatRoomPlus;Ljava/lang/String;)V
    at me.Nighterance.main.PlayerChat.<init>(PlayerChat.java:8) ~[?:?]
    at me.Nighterance.main.ChatRoomPlus.onEnable(ChatRoomPlus.java:37) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]

    this is main class
    package me.Nighterance.main;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.UnsupportedEncodingException;

    import java.util.logging.Level;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    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.inventory.InventoryClickEvent;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class ChatRoomPlus extends JavaPlugin implements Listener{
    private PlayerChat pc;
    public void onEnable(){

    getServer().getConsoleSender().sendMessage(ChatColor.GREEN + " \n ----- ChatRoomPlus enabled -----" + ChatColor.YELLOW + " \n Plugin made by Nighterance! \n If you like using it, rate it! \n If you have any suggestions please leave them on comments \n" + ChatColor.GREEN + "----- ChatRoomPlus enabled ---- \n");
    getConfig().options().copyDefaults(true);
    saveDefaultConfig();
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    this.saveResource("messages.yml", false);
    this.pc = new PlayerChat(this);
    if(!getDataFolder().exists()){
    getDataFolder().mkdirs();
    }

    }

    public void onDisable(){

    }






    // This is to make config and messages
    private FileConfiguration customConfig = null;
    private File customConfigFile = null;
    public void reloadCustomConfig() {
    if (customConfigFile == null) {
    customConfigFile = new File(getDataFolder(), "messages.yml");
    }
    customConfig = YamlConfiguration.loadConfiguration(customConfigFile);

    Reader defConfigStream = null;
    try {
    defConfigStream = new InputStreamReader(this.getResource("messages.yml"), "UTF8");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    if (defConfigStream != null) {
    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    customConfig.setDefaults(defConfig);
    }
    }

    public FileConfiguration getCustomConfig() {
    if (customConfig == null) {
    reloadCustomConfig();
    }
    return customConfig;
    }

    public void saveCustomConfig() {
    if (customConfig == null || customConfigFile == null) {
    return;
    }
    try {
    getCustomConfig().save(customConfigFile);
    } catch (IOException ex) {
    getLogger().log(Level.SEVERE, "Could not save config to " + customConfigFile, ex);
    }
    }

    public void saveDefaultCustomConfig() {
    if (customConfigFile == null) {
    customConfigFile = new File(getDataFolder(), "messages.yml");
    }
    if (!customConfigFile.exists()) {
    this.saveResource("messages.yml", false);
    }
    }
    @EventHandler
    public void PlayerChatEvent(AsyncPlayerChatEvent e)
    {
    Player p = e.getPlayer();
    String pname = pc.config.getString(p.getName()+".group");

    for (Player all : Bukkit.getOnlinePlayers()) {
    if(e.getMessage().startsWith("!")){
    e.getMessage().replace("!", "");
    return;
    }
    String allnameonconf = pc.config.getString(all.getName()+".group");
    if(pname.equals("English")){
    if (!allnameonconf.equals("English") && !allnameonconf.equals("Global") && !allnameonconf.equals("AdminChat")) {
    e.getRecipients().remove(all);
    }
    if(allnameonconf.equals("AdminChat")){
    e.getRecipients().remove(all);
    all.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&cEnglishChat&7]&f" + " " + e.getPlayer().getName() + ": " + e.getMessage()));
    }
    }else
    if(pname.equals("Latvian")){
    if (!allnameonconf.equals("Latvian") && !allnameonconf.equals("Global") && !allnameonconf.equals("AdminChat")) {
    e.getRecipients().remove(all);
    }
    if(allnameonconf.equals("AdminChat")){
    e.getRecipients().remove(all);
    all.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&cLatvianChat&7]&f" + " " + e.getPlayer().getName() + ": " + e.getMessage()));
    }
    }else
    if(pname.equals("Global")){
    if (!allnameonconf.equals("Global") && !allnameonconf.equals("AdminChat")) {
    e.getRecipients().remove(all);
    if(allnameonconf.equals("AdminChat")){
    e.getRecipients().remove(all);
    all.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7[&cGlobalChat&7]&f" + " " + e.getPlayer().getName() + ": " + e.getMessage()));
    }

    }}else{
    pc.config.set(p.getName()+".group", "Latvian");
    pc.save();
    }





    }
    }


    @EventHandler
    public void onJoinEvent(PlayerJoinEvent e){
    Player p = (Player) e.getPlayer();
    p.sendMessage(ChatColor.YELLOW + ChatColor.translateAlternateColorCodes('&', getCustomConfig().getString("OnJoinMessage")));
    }





    @EventHandler
    public void OnInventoryClick(InventoryClickEvent e){

    Player p = (Player) e.getWhoClicked();
    Inventory open = e.getClickedInventory();
    ItemStack item = e.getCurrentItem();
    Inventory inv = e.getInventory();
    if(open == null){
    return;
    }
    if(item.equals(null) || !item.hasItemMeta()){
    return;
    }
    if(inv.getName().equals(ChatColor.DARK_BLUE + "ChatRoom changer")){
    if(item.getItemMeta().getDisplayName().equals(ChatColor.RED + ChatColor.translateAlternateColorCodes('&' , getCustomConfig().getString("name1")))){
    pc.config.set(p.getName()+".group", "English");
    p.closeInventory();
    pc.save();
    p.sendMessage(ChatColor.YELLOW + "You are now using English chat");
    }else if(item.getItemMeta().getDisplayName().equals(ChatColor.RED + ChatColor.translateAlternateColorCodes('&' , getCustomConfig().getString("name2")))){
    pc.config.set(p.getName()+".group", "Latvian");
    p.closeInventory();
    pc.save();
    p.sendMessage(ChatColor.YELLOW + "You are now using Latvian chat");
    }else if(item.getItemMeta().getDisplayName().equals(ChatColor.RED + ChatColor.translateAlternateColorCodes('&' , getCustomConfig().getString("name3")))){
    pc.config.set(p.getName()+".group", "Global");
    p.sendMessage(ChatColor.YELLOW + "You are now using Global chat");
    p.closeInventory();
    pc.save();
    }
    e.setCancelled(true);

    // There it ends.

    }}

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player p = (Player) sender;
    if(cmd.getName().equalsIgnoreCase("lang")){
    ChatRoomGUI i = new ChatRoomGUI();
    i.newInventory(p);
    }

    if(cmd.getName().equalsIgnoreCase("adminchat")){

    if(p.hasPermission("ChatRoomPlus.adminchat")){
    pc.config.set(p.getName()+".group", "AdminChat");
    p.sendMessage(ChatColor.RED + "You are now using Admin chat!");
    }else{
    p.sendMessage(ChatColor.RED + "You do not have permission to use admin chat!");
    }

    }

    return true;
    }



    }
    This is Abstract File
    package me.Nighterance.main;

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    public class AbstractFile {



    protected ChatRoomPlus crp;
    private File file;
    protected FileConfiguration config;

    public AbstractFile(ChatRoomPlus crp, String fileName){
    this.crp = crp;
    this.file = new File(crp.getDataFolder(), fileName);
    if(!file.exists()){

    try{
    file.createNewFile();
    }catch (IOException e){
    e.printStackTrace();
    }}

    this.config = YamlConfiguration.loadConfiguration(file);
    }
    public void save(){
    try {
    config.save(file);
    }catch (IOException e){
    e.printStackTrace();
    }
    }

    }

    This is PlayerChat class
    package me.Nighterance.main;


    public class PlayerChat extends AbstractFile{
    public PlayerChat(ChatRoomPlus crp){
    super(crp, "playerchat.yml");
    }

    }
     
  2. The 'NoSuchMethodError' error is thrown when the server cannot locate a specific method in which you have imported. It seems to be 'me.Nighterance.main.AbstractFile'. You extend that in the PlayerChat class you have, however you have not imported it according to your post of it, so ensure you have imported it.
     
  3. imported it

    package me.Nighterance.main;
    import me.Nighterance.main.AbstractFile;

    public class PlayerChat extends AbstractFile{
    public PlayerChat(ChatRoomPlus crp){
    super(crp, "playerchat.yml");
    }

    }


    Still it has that error
     
  4. So you have exported the plugin and loaded it on the server again, is the error the same?
    As this part here shows that method cannot be located in PlayerChat:8 (line 8):
    Code (Text):
    java.lang.NoSuchMethodError: me.Nighterance.main.AbstractFile.<init>(Lme/Nighterance/main/ChatRoomPlus;Ljava/lang/String;)V
    at me.Nighterance.main.PlayerChat.<init>(PlayerChat.java:8) ~[?:?]
     
  5. Yes, i did export that and here is error

    Code (Text):

    java.lang.NoSuchMethodError: me.Nighterance.main.AbstractFile.<init>(Lme/Nighterance/main/ChatRoomPlus;Ljava/lang/String;)V
            at me.Nighterance.main.PlayerChat.<init>(PlayerChat.java:6) ~[?:?]
            at me.Nighterance.main.ChatRoomPlus.onEnable(ChatRoomPlus.java:37) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
     
     
  6. Hello @Nighterance ,

    I have read your code, and copied it over to my own IDE to try to reproduce the issue.
    But the issue doesn't reproduce. An NoSuchMethodError will get thrown when a method called but not existent at runtime.

    The reason why you got that error is likely because you have for example compiled the source to a jar, but haven't replaced the jar into the server. It's a little bit difficult to explain honestly but I hope you'll understand it.

    Furthermore, I noticed an import was not existent in the code, which I fixed myself, and additionally why did you use BungeeCord's ChatColor class while bukkut has it's own class for that?

    If you have any more questions about this subject, feel free to ask.

    Kind regards,
    Ronald ("Azoraqua")