Solved Issue With PlayerRespawnEvent

Discussion in 'Spigot Plugin Development' started by ItsNutt, Oct 13, 2021.

  1. Code:
    Code (Text):
    public class RespawnListener implements Listener {

        private final PlayerNameManager playerNameManager = new PlayerNameManager();

        private final ChatManager chatManager = new ChatManager();

        @EventHandler
        public void OnRespawn(PlayerRespawnEvent event){

            System.out.println("Player is respawning");

            Player p = event.getPlayer();

            ItemStack mobClass = new ItemStack(Material.GOLD_NUGGET);
            ItemMeta mobClassMeta = mobClass.getItemMeta();
            mobClassMeta.setDisplayName(ChatColor.BLACK + "Mob Classes");
            mobClass.setItemMeta(mobClassMeta);

                System.out.println("Attempting to reset player to default");
                p.setDisplayName(playerNameManager.getPlayerName(p.getUniqueId()));
                chatManager.disablePrefix(p.getUniqueId());
                p.setCanPickupItems(true);
                p.getInventory().addItem(mobClass);
                System.out.println("Success");

        }

    }
    It's pretty simple code. I get no errors, which is why I'm so confused. I've confirmed that the Listener is registered in the OnEnable. When a player respawns, nothing happens in-game, nothing in the console. I'm not sure what's wrong.
     
  2. Can you show your main class?
     
  3. md_5

    Administrator Developer

    If there's nothing in the console it probably isn't registered.

    Also OnEnable is different to onEnable. Make sure it's the latter.
     
  4. Definitely registered in onEnable, "OnEnable" was a typo :p my bad.
     
  5. Code (Text):
    package me.gamernut65.dvztests;

    import org.bukkit.plugin.java.JavaPlugin;


    public final class DvZTests extends JavaPlugin {


        @Override
        public void onEnable() {
            // Plugin startup logic
            getServer().getPluginManager().registerEvents(new ChatListener(),this);
            getServer().getPluginManager().registerEvents(new ClassSelector(), this);
            getServer().getPluginManager().registerEvents(new BannedMobs(), this);
            getServer().getPluginManager().registerEvents(new MobClassSelector(), this);
    //Registered Here
            getServer().getPluginManager().registerEvents(new RespawnListener(), this);
            getServer().getPluginManager().registerEvents(new PortalCancel(), this);
            getServer().getPluginManager().registerEvents(new PotionListener(), this);
            getServer().getPluginManager().registerEvents(new DeathListener(), this);
            getServer().getPluginManager().registerEvents(new JoinListener(), this);
            getServer().getPluginManager().registerEvents(new LeaveListener(),this);




        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }
    }
     
    Apologies for double-post, didn't see this one until after I'd already posted the first reply
     
  6. I can confirm that no other plugins are responsible for this issue. Still have no idea what could be wrong here. The rest of the plugin works as intended, it's just this that doesn't work.
     
  7. Try adding a runnable and execute the code after 1 second from respawn time
     
  8. The Respawn event is called before the player completely respawns, so you need to make a scheduler, so everything gets reset.

    So you should like @adrianpopa1998 said make a scheduler and execute your code after 1 Tick. (No need for 1 second, because it isn’t called async and 1 Tick would be slower than the management of the whole respawn.)
     
  9. So I did this:
    Code (Text):
    public class RespawnListener implements Listener {

        private final PlayerNameManager playerNameManager = new PlayerNameManager();

        private final ChatManager chatManager = new ChatManager();

        private final DvZTests DvZTests = new DvZTests();


        @EventHandler
        public void OnRespawn(PlayerRespawnEvent event){

            Player p = event.getPlayer();

            ItemStack mobClass = new ItemStack(Material.GOLD_NUGGET);
            ItemMeta mobClassMeta = mobClass.getItemMeta();
            mobClassMeta.setDisplayName(ChatColor.BLACK + "Mob Classes");
            mobClass.setItemMeta(mobClassMeta);

            new BukkitRunnable(){
                @Override
                public void run(){
                    System.out.println("Attempting to reset player to default");
                    p.setDisplayName(playerNameManager.getPlayerName(p.getUniqueId()));
                    chatManager.disablePrefix(p.getUniqueId());
                    p.setCanPickupItems(true);
                    p.getInventory().addItem(mobClass);
                    System.out.println("Success");
                }
    //I did try various delay times, no impact on result
            }.runTaskLater(DvZTests,1);

        }

    }
    Result is the same. I'm like 90% sure there's no critical problem with the way I made the runnable, but I suppose I could've messed up there. If that's not the issue, I assume there's a problem with the Listener somehow. I find this very strange, as I had been testing nearly identical code a few weeks ago (without a runnable) and everything worked fine.
     
  10. System.out.println is showing something in console or nothing? And you can try set EventPriority to Highest
     
  11. Nothing in console. All Listener's stopped functioning when I set RespawnListener to highest priority. Still no errors in the console. Very strange. I've never encountered a problem like this before.
     
  12. Please send another class which implements Listener
     
  13. I have no clue why this isn‘t working for you and can‘t test it right now as I‘m on my phone. But why don‘t you create a PluginManager variable in your onEnable method? This would be a lot less code and make it look better overall.
     
    • Useful Useful x 1
  14. Not sure how to do that yet but I'll look into it. Thanks.
    EDIT: Oh my god I'm an idiot. I didn't realize what you were suggesting, my bad.

    If I understand correctly, you want any of my classes that implement Listener.
    Code (Text):
    public class ChatListener implements Listener {

        private final ChatManager chatManager = new ChatManager();


        @EventHandler
        public void OnPlayerChat(AsyncPlayerChatEvent event){
            Player p = event.getPlayer();
            String prefix = chatManager.getPrefix(p.getUniqueId());
            String monPrefix = chatManager.getMonsterPrefix(p.getUniqueId());


            if (p.hasPermission("Op")){
                event.setFormat(ChatColor.DARK_RED + "[Host] " + ChatColor.RESET + "" + ChatColor.GOLD + "%1$s: %2$s");

            }else{if (!chatManager.getPrefix(p.getUniqueId()).equals(null)){
                event.setFormat(ChatColor.BLUE + prefix + ChatColor.RESET + ChatColor.GRAY + " " + "%1$s: %2$s");
            }else{
                    event.setFormat(ChatColor.RED + monPrefix + ChatColor.RESET + ChatColor.GRAY + " " + "%1$s: %2$s");
                }
            }
        }
    }
     
    This is just a class I use to mess around with chat format, which I have confirmed is working. Apologies if I'm misunderstanding.
    Also, I know this one is written rather poorly, but it works fine and I don't wanna spend my energy on making a better one.
     
  15. I saw now, why you are calling a "new" main?

    Edit: create a method getInstance() in your main class, then call it from where you need
     
  16. I see what you're saying, but that's the way I learned to make BukkitRunnable work. I had a feeling I was doing it wrong, but it worked so I didn't care. I did try a few different things the way you described, but my IDE (Intellij) did not like it. I'm an amateur, so I know I'm doing something wrong, but I haven't yet been able to figure out what that is. I believe the issue is with my understanding of Java, so I can't ask about that on this forum.

    Anyway, it seems I have a much bigger problem now: most of my Listeners are no longer functioning. I don't know what I did to make that happen. I didn't even touch them, aside from RespawnListener. I did notice one thing though:
    Code (Text):
    public final class DvZTests extends JavaPlugin {


        @Override
        public void onEnable() {
            // Plugin startup logic

            PluginManager p = getServer().getPluginManager();

            p.registerEvents(new ChatListener(),this);
            p.registerEvents(new ClassSelector(), this);
            p.registerEvents(new BannedMobs(), this);
            p.registerEvents(new MobClassSelector(), this);
            p.registerEvents(new PortalCancel(), this);
            p.registerEvents(new PotionListener(), this);
            p.registerEvents(new DeathListener(), this);
            p.registerEvents(new JoinListener(), this);
            p.registerEvents(new LeaveListener(),this);
            p.registerEvents(new RespawnListener(), this);




        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }
    }
     
    ChatListener, ClassSelector, and BannedMobs all function properly, but everything registered below them does not. This is extremely confusing. I see no obvious issues within each of the non-functional Listeners. ClassSelector and MobClassSelector are practically identical in contents. I would post them both here but they are each over 400 lines long. There are no similarities between ALL the non-functional Listeners that are not also shared by the functional Listeners. My apologies if I wrote that strangely.

    I did try messing around with the order in which they are registered and found that if any of the currently functional Listeners are registered after MobClassSelector, they cease to function. I also tried the opposite (registering other Listeners before MobClassSelector), but none resumed function.

    I have no idea what this means, except that the problem does not lie solely with PlayerRespawnEvent. I'm not sure if I should change or add to the title, or create another post entirely, or do nothing and leave it the way it is.

    Essentially, it seems I misidentified what the problem was. I apologize.
     
  17. You're issue is
    Code (Text):
    private final DvZTests DvZTests = new DvZTests();
    You can't do this, you're trying to initiate another class (your main class which extends JavaPlugin) again and I do not believe there is no error cause this would throw an error, this would be the error
    Code (Text):
    java.lang.IllegalArgumentException: Plugin already initialized!
    Use Dependency Injection
     
    • Useful Useful x 1
  18. Well you are correct, that was the issue, but I did not get a single error in the console at any point during my testing. I assumed it was fine because I got no error. Thank you for explaining it to me.

    Is there a reason why I wouldn't get an error in the console? I could've avoided making this post if had gotten one and I'm feeling pretty ashamed for making one about my very stupid mistake.

    Also, I know I really shouldn't ask this here, but are there any good resources for learning Dependency Injection? I've tried to learn it several times now for various reasons, but every time I try to put what I learn into practice, my IDE says there's a problem with the way I do it or it ultimately doesn't work.

    I know of ways to get around using BukkitRunnable to accomplish my goals, but I feel like putting off learning Dependency Injection is a bad idea.