Username Change

Discussion in 'Plugin Development' started by Royal_Soda, Aug 9, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Royal_Soda

    Hey,

    I've been trying to make a plugin that checks if a user has a blue/red wool on their head, and if they do, then it would change the username to be red/blue color accordingly. The red/blue wool is put on the player's head by the plugin War when they join a game. I have attempted this in various ways, but here is my current source code. It doesn't work, but it doesn't have any errors at all. I'm not sure what's wrong, it may be that War overrides the plugin or that I'm using the wrong event. Please help.

    Main class.
    Code:
    package voxela.warnames;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Master extends JavaPlugin {
     
        public final HelmetListener hListener = new HelmetListener();
       
        public void onEnable() {
           
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(this.hListener, this);
           
            getLogger().info("WarNames has booted.");
     
        }
     
        public void onDisable() {
           
            getLogger().info("WarNames is shutting down.");
       
        }
       
    }
    Listener.
    Code:
    package voxela.warnames;
     
    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.player.AsyncPlayerChatEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class HelmetListener implements Listener {
       
        @EventHandler
        public void playerInventoryEvent(AsyncPlayerChatEvent event) {
           
            Player player = event.getPlayer();
            String lastName = player.getDisplayName();
            String lastNamez = player.getPlayerListName();
            ItemStack redwool = new ItemStack(Material.WOOL, 1, (byte)14);
            ItemStack bluewool = new ItemStack(Material.WOOL, 1, (byte)11);
           
            if (player.getInventory().getHelmet() == redwool) {
           
                player.setPlayerListName(ChatColor.DARK_RED + lastNamez);
                player.setDisplayName(ChatColor.DARK_RED + lastName);
           
            }
           
            if (player.getInventory().getHelmet() == bluewool) {
               
                player.setPlayerListName(ChatColor.DARK_BLUE + lastNamez);
                player.setDisplayName(ChatColor.DARK_BLUE + lastName);
               
            }
           
            if (player.getInventory().getHelmet() != redwool && player.getInventory().getHelmet() != bluewool) {
               
                player.setPlayerListName(lastNamez);
                player.setDisplayName(lastName);
               
            }
           
        }
    }
    Plugin.YML
    Code:
    name: WarNames
    version: 1.0
    description: This plugin changes a player's name depending on what they're wearing as a helmet.
    author: Royal_Soda
    authors: [Royal_Soda, Dawsonator27]
    website: http://www.voxela.com/
     
    main: voxela.warnames.Master
    database: false
    Please help!
     
  2. Offline

    rjVapes

    Instead of checking to see if they have your new instance of wool blocks on their like:
    Code:
    if (player.getInventory().getHelmet() == redwool) {
    try
    Code:
    if (player.getInventory().getHelmet().getType() == Material.WOOL && player.getInventory().getHelmet().getData().getData() == (byte)14) {
    It might be comparing the references, not actual values of the references, .equals may also work, I just haven't messed with it to try.
     
  3. Offline

    Royal_Soda

    rjVapes I'll try it in a few minutes and get back to you with what happens.

    rjVapes
    I've done what you said, now I have an error in the console, yay! (lol, please help.)
    Code:
    20:05:07 [SEVERE] Could not load 'plugins/WarNames.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.UnsupportedClassVersionError: voxela/warnames/Master : Unsupported major.minor version 51.0
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:155)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:222)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:198)
            at net.minecraft.server.ServerConfigurationManagerAbstract.<init>(ServerConfigurationManagerAbstract.java:50)
            at net.minecraft.server.ServerConfigurationManager.<init>(SourceFile:11)
            at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:105)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:380)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.UnsupportedClassVersionError: voxela/warnames/Master : Unsupported major.minor version 51.0
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:41)
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:29)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:264)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:144)
            ... 9 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  4. Offline

    rjVapes

    It looks like you're linking against a version of something (bukkit? jdk?) that's newer than the version your server is using.
     
  5. Offline

    ZeusAllMighty11

    Unsupported major minor error thing means you are exporting in one version of java, and using it in another. If your server runs java 7, export in java 7. If it exports in 6, run the server with 6...
     
  6. Offline

    Royal_Soda

    rjVapes
    ZeusAllMighty11
    hmm.. What's up with this?
    Code:
    21:58:54 [SEVERE] Could not pass event AsyncPlayerChatEvent to WarNames
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:332)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:830)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.NetworkManager.i(NetworkManager.java:216)
            at net.minecraft.server.NetworkManager.c(NetworkManager.java:331)
            at net.minecraft.server.NetworkReaderThread.run(SourceFile:93)
    Caused by: java.lang.NullPointerException
            at voxela.warnames.HelmetListener.playerInventoryEvent(HelmetListener.java:18)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:616)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:330)
            ... 9 more
    
     
  7. Offline

    ZeusAllMighty11

    Whatever is on line 18 of your class HelmetListener.. it's returning null

    Since it's an inventoryevent, make sure you check for null slots (air)
     
  8. Offline

    Royal_Soda

    ZeusAllMighty11
    Here's my current listener class. Right now, I can wear either red or blue wool and my name will be a different color. But, if I switch wool, the color will not switch, and if I remove the wool, the name doesn't switch. If from the start, I didn't have a red/blue wool in my helmet slot, everything is white, chat and message, also the error pops up.
    Code:
    package voxela.warnames;
     
    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.player.AsyncPlayerChatEvent;
     
    public class HelmetListener implements Listener {
       
        @EventHandler
        public void playerInventoryEvent(AsyncPlayerChatEvent event) {
     
            Player player = event.getPlayer();
            String lastName = player.getDisplayName();
     
            if (player.getInventory().getHelmet().getType() == Material.WOOL && player.getInventory().getHelmet().getData().getData() == (byte)14) {
           
                player.setDisplayName(ChatColor.DARK_RED + lastName + ChatColor.GRAY);
               
            }
           
            else if (player.getInventory().getHelmet().getType() == Material.WOOL && player.getInventory().getHelmet().getData().getData() == (byte)11) {
               
                player.setDisplayName(ChatColor.DARK_BLUE + lastName + ChatColor.GRAY);
           
            }
           
            else {
               
                player.setDisplayName(ChatColor.WHITE + lastName + ChatColor.GRAY);
               
            }
           
        }
    }
     
  9. Offline

    Firefly

    Code:
    player.getInventory().getHelmet().getType()
    If your helmet is empty, you'll get a Null Pointer. Check if getHelmet() returns null first.
     
  10. Offline

    Morgannah

    This would actually be an interesting mod.
     
  11. Offline

    Royal_Soda

    Testing in a sec, will respond with what happened.
     
  12. Offline

    RingOfStorms

    Are you talkign abotu teh username above the head or just in chat? The display name will only change the chat color one, and if there are any other plugins that do anything with chat (even some perms do this) they will over write your color iwth their own prefix colors for display names.

    If you're planning on doing color above their player, they WILL lose their skins and be the default, but if they are wearing armor then it is a cool affect to add.
     
  13. Offline

    Royal_Soda

    Nope, I'm talking about their in-chat names. I'm aware many plugins do this, but this is meant for a specific use.

    Firefly
    Ok, if I put on a piece of wool, it changes the name to a said color, but then if I switch hats or remove hats, the name stays in the same color. Also, I did what you said but when I talk with AIR on my head, I still get this error:
    Code:
    01:19:34 [SEVERE] Could not pass event AsyncPlayerChatEvent to WarNames
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:332)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
            at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:830)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:807)
            at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.NetworkManager.i(NetworkManager.java:216)
            at net.minecraft.server.NetworkManager.c(NetworkManager.java:331)
            at net.minecraft.server.NetworkReaderThread.run(SourceFile:93)
    Caused by: java.lang.NullPointerException
            at voxela.warnames.HelmetListener.playerInventoryEvent(HelmetListener.java:18)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:616)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:330)
            ... 9 more
    Please help. Furthermore, should I try to test for air, instead of null?

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  14. Offline

    Morthis

    Show the code again after you added the null check.
     
  15. Offline

    Royal_Soda

    Morthis
    Ok!
    Master class:
    Code:
    package voxela.warnames;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Master extends JavaPlugin {
     
        public final HelmetListener hListener = new HelmetListener();
       
        public void onEnable() {
           
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(this.hListener, this);
           
            getLogger().info("WarNames has booted.");
     
        }
     
        public void onDisable() {
           
            getLogger().info("WarNames is shutting down.");
       
        }
       
    }
    Listener:
    Code:
    package voxela.warnames;
     
    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.player.AsyncPlayerChatEvent;
     
    public class HelmetListener implements Listener {
       
        @EventHandler
        public void playerInventoryEvent(AsyncPlayerChatEvent event) {
     
            Player player = event.getPlayer();
            String lastName = player.getDisplayName();
     
            if (player.getInventory().getHelmet().getType() == null) {
               
                player.setDisplayName(ChatColor.WHITE + lastName + ChatColor.GRAY);
               
            }
           
            else if (player.getInventory().getHelmet().getType() == Material.WOOL && player.getInventory().getHelmet().getData().getData() == (byte)14) {
           
                player.setDisplayName(ChatColor.DARK_RED + lastName + ChatColor.GRAY);
               
            }
           
            else if (player.getInventory().getHelmet().getType() == Material.WOOL && player.getInventory().getHelmet().getData().getData() == (byte)11) {
               
                player.setDisplayName(ChatColor.DARK_BLUE + lastName + ChatColor.GRAY);
           
            }
           
            else {
               
                player.setDisplayName(ChatColor.WHITE + lastName + ChatColor.GRAY);
               
            }
           
        }
    }
     
  16. Offline

    Morthis

    Code:
    if (player.getInventory().getHelmet().getType() == null) {
    Change to

    Code:
    if (player.getInventory().getHelmet() == null) {
     
  17. Offline

    Royal_Soda

    Morthis
    Ok, now there are no errors. But if you're assigned to a specific color, then switch hats, it doesn't change the color to the new hat's color. Please help.
     
  18. Offline

    Firefly

    Code:
    if (player.getInventory().getHelmet() == null) {
    I said to do this lol...
     
  19. Offline

    Royal_Soda

    I did, I miss interpreted in the beginning though. Anyways, can someone help me out?
     
  20. Offline

    Firefly

    If you have a blue wool on, your text is blue, but if you change it to red and chat it isn't red..?
     
  21. Offline

    Royal_Soda

    Then it stays blue.
     
  22. Offline

    Firefly

    Add a debug message after you check what their helmet is like: "Your helmet is blue wool" or "Your helmet is red wool"
     
  23. Offline

    Royal_Soda

    How does that fix the problem? :S
     
  24. Offline

    Firefly

    To let you know what might be wrong with your code. Don't expect me to pull a magical solution out of my hat. I need to poke around at your code's logic before I can make an assessment of your problem.

    Anyways, the first thing anyone should do before posting on these forums is to add debug messages to their code (and read stacktraces) to figure out what might be wrong with their code. If you come on here after and still haven't solved it, at least you can say that you tried debug messages and it still didn't work; but here's what you think might be the problem. Then someone can help you.
     
Thread Status:
Not open for further replies.

Share This Page