Guard Plugin - NEED HELP - See forum for details

Discussion in 'Plugin Development' started by ScootTech, Jun 20, 2015.

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

    ScootTech

    Hey guys! I was doing some plugin development, and I was having some troubles.

    First problem, I set it up so that when someone does /onduty it says in chat "{USERNAME} is now on duty!" and the same for /offduty "{USERNAME} is now off duty!", but I'm having some trouble coding it.
    The coding I selected to broadcast this was

    if (cmd.getName().equalsIgnoreCase("onduty") {
    Bukkit.broadcastMessage(ChatColor.RED + player.getName() + ChatColor.AQUA + " is now on duty!"
    if (cmd.getName().equalsIgnoreCase("offduty") {
    Bukkit.broadcastMessage(ChatColor.RED + player.getName() + ChatColor.AQUA + " is now off duty!"

    But here is the issue, "player" in the coding section "player.getName()" has a red underline and I can't figure out why, so if someone could leave a solution in the comments, that would be great!

    Second problem, this is for guarding on a server, so I was gonna give them a Guard kit, but how do I give them the kit on command "/onduty" and then take it away with "/offduty". If someone could leave code for that in the comments, that would be great, thanks guys!
     
  2. Offline

    Creeper674

    What message comes up when you hover over the error?

    Is 'player' a Player variable? Have you cast the sender as a player?
     
  3. Offline

    CyranoTrinity

    @ScootTech
    1. You have missed your close parentheses at the end.
    2. I believe you need to cast player to sender after you check that sender is a player.
     
  4. Offline

    ScootTech

    There are multiple solutions, but if I do it, getName doesn't work, and there is no solution for that.


    Please classify what you mean, I'm new when it comes to this stuff (coding you can provide?).



    {{Posts merged by Myrathi}}
     
    Last edited by a moderator: Jun 20, 2015
  5. Offline

    CyranoTrinity

  6. Offline

    xXCapzXx

    @ScootTech

    You need to put this ")" at the end of your code, it is another word for parentheses in coding.

    Like this

    Code:
    Player player = (Player)sender;
    if (cmd.getName().equalsIgnoreCase("onduty") {
    Bukkit.broadcastMessage(ChatColor.RED + sender.getName() + ChatColor.AQUA + " is now on duty!")
    if (cmd.getName().equalsIgnoreCase("offduty") {
    Bukkit.broadcastMessage(ChatColor.RED + sender.getName() + ChatColor.AQUA + " is now off duty!")
    
    I hope that works, I put in the Player.
    If you are going to put player.getName() you actually need to initialize it.
    Player player = (Player)sender;
    So you should actually do this first, then actually do sender.getName()
    Send a conversation to me and I will explain more.
    And also, can you show your whole class? It would be much easier so I can understand what your doing and if you added player already.
     
    Last edited: Jun 21, 2015
  7. Offline

    ScootTech

    Code:
    package me.scoottech.onduty;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class OnDuty extends JavaPlugin {
    
        public void onEnable() {
            
        }
        
        public void onDisable() {
            
        }
        
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player)sender;
            if (cmd.getName().equalsIgnoreCase("onduty")) {
                Bukkit.broadcastMessage(ChatColor.RED + player.getName() + ChatColor.AQUA + " is now on duty!") ;
            if (cmd.getName().equalsIgnoreCase("offduty")) {
                Bukkit.broadcastMessage(ChatColor.RED + player.getName() + ChatColor.AQUA + " is now off duty!") ;
                
            }
                    
                }
                return true;
                
            }
        }
    
    Does this help?

    Also I need coding to make it so when the sender (player) does /onduty it gives them a kit, and /offduty takes the kit away, anyway I can do that?

     
    Last edited: Jun 21, 2015
  8. Offline

    ark9026

    @ScootTech
    Get the players inventory, then add an item to it.
    Code:
    player.getInventory().addItem(new ItemStack(Material.Blah Blah Blah));
    To clear it, clear the players inventory and optionally set their armor to air.
    I'm not a huge fan of spoonfeeding, but I can see that you need help.
     
    Last edited: Jun 24, 2015
  9. Offline

    ScootTech

    I understand not everyone loves spoonfeeding, but yeah I do need help. Do you know how to enchant a material givin? And I don't understand what you mean by setting their armor to air?

    Okay, so I figured out my last question, my final and only question left is, why is the code "addEnchant", "addUnsafeEnchant" underlined? I go to fix it and it just changes it back and forth from "addEnchant", "addUnsafeEnchant", and I'm confused. I'm assuming it has something to do with the import: net.minecraft.server.v1_7_R4.Enchantment;
    I don't know, I'm a rookie here.

    CODE:
    Code:
    package me.scoottech.onduty;
    
    import net.minecraft.server.v1_7_R4.Enchantment;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class OnDuty extends JavaPlugin {
    
        public void onEnable() {
           
        }
       
        public void onDisable() {
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player)sender;
            if (cmd.getName().equalsIgnoreCase("onduty")) {
                ItemStack helmet = new ItemStack(Material.CHAINMAIL_HELMET, 1);
                helmet.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 4);
                player.getInventory().addItem(helmet);
                ItemStack chestplate = new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1);
                chestplate.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 4);
                player.getInventory().addItem(chestplate);
                ItemStack leggings = new ItemStack(Material.CHAINMAIL_LEGGINGS, 1);
                helmet.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 4);
                player.getInventory().addItem(leggings);
                ItemStack boots = new ItemStack(Material.CHAINMAIL_BOOTS, 1);
                helmet.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 4);
                player.getInventory().addItem(boots);
                ItemStack stick = new ItemStack(Material.STICK, 1);
                stick.addEnchantment(Enchantment.KNOCKBACK, 10);
                player.getInventory().addItem(stick);
                Bukkit.broadcastMessage(ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " is now on duty!") ;
            if (cmd.getName().equalsIgnoreCase("offduty")) {
                player.getInventory().remove(helmet);
                player.getInventory().remove(chestplate);
                player.getInventory().remove(leggings);
                player.getInventory().remove(boots);
                player.getInventory().remove(stick);
                Bukkit.broadcastMessage(ChatColor.YELLOW + player.getName() + ChatColor.RED + " is now off duty") ;
               
            }
                   
                }
                return true;
               
            }
        }
    
    Please comment back, if you have a solution to this very slim issue

    @ark9026 @CyranoTrinity @xXCapzXx
     
    Last edited by a moderator: Jun 24, 2015
  10. @ScootTech Check before casting Player
    Use Bukkit's Enchant not MC's
    You have a command if in a command. You check if the command is /onduty then it checks if the command is /offduty it cannot be /onduty and /offduty you need to put a ending bracket after that first command.
     
  11. Offline

    ScootTech

    @bwfcwalshy like this?

    Code:
    package me.ScootTech.guarding;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Guarding extends JavaPlugin {
    
        public void onEnable() {
            
        }
        
        public void onDisable() {
            
        }
        
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player)sender;
            if (cmd.getName().equals("onduty"));
                Bukkit.broadcastMessage(ChatColor.BLACK + "[" + ChatColor.AQUA + "☆" + ChatColor.BLACK + "]" + ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " is now on duty!") ;
               ItemStack helmet = new ItemStack(Material.CHAINMAIL_HELMET, 1);
               helmet.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 10);
               ItemMeta helmetmeta = helmet.getItemMeta();
               helmetmeta.setDisplayName(ChatColor.GREEN + "Guard Helmet");
               helmet.setItemMeta(helmetmeta);
               player.getInventory().addItem(helmet);
               ItemStack chestplate = new ItemStack(Material.CHAINMAIL_CHESTPLATE, 1);
               chestplate.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 10);
               ItemMeta chestplatemeta = chestplate.getItemMeta();
               chestplatemeta.setDisplayName(ChatColor.GREEN + "Guard Chestplate");
               chestplate.setItemMeta(chestplatemeta);
               player.getInventory().addItem(chestplate);
               ItemStack leggings = new ItemStack(Material.CHAINMAIL_LEGGINGS, 1);
               leggings.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 10);
               ItemMeta leggingsmeta = leggings.getItemMeta();
               leggingsmeta.setDisplayName(ChatColor.GREEN + "Guard Leggings");
               leggings.setItemMeta(leggingsmeta);
               player.getInventory().addItem(leggings);
               ItemStack boots = new ItemStack(Material.CHAINMAIL_BOOTS, 1);
               boots.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 10);
               ItemMeta bootsmeta = boots.getItemMeta();
               bootsmeta.setDisplayName(ChatColor.GREEN + "Guard Boots");
               boots.setItemMeta(bootsmeta);
               player.getInventory().addItem(boots);
               ItemStack stick = new ItemStack(Material.STICK, 1);
               stick.addUnsafeEnchantment(Enchantment.KNOCKBACK, 10);
               ItemMeta stickmeta = stick.getItemMeta();
               stickmeta.setDisplayName(ChatColor.GREEN + "Guard Stick");
               stick.setItemMeta(stickmeta);
               player.getInventory().addItem(stick); {
            }
             if (cmd.getName().equalsIgnoreCase("offduty"));
                 Bukkit.broadcastMessage(ChatColor.BLACK + "[" + ChatColor.AQUA + "☆" + ChatColor.BLACK + "]" + ChatColor.YELLOW + player.getName() + ChatColor.RED + " is no longer on duty!") ;
                 player.getInventory().remove(helmet);
                 player.getInventory().remove(chestplate);
                 player.getInventory().remove(leggings);
                 player.getInventory().remove(boots);
               player.getInventory().remove(stick); {
               
             }
                return true;
                 
       }
    }[/FONT]
    
    [FONT=Trebuchet MS]
    I customized the broadcast, to fit the server name.
     
  12. Offline

    xXCapzXx

    Yeah, that enchantment.
    Not Mc's.
     
  13. Offline

    ark9026

    @ScootTech
    One little error: You forgot your start bracket after offduty, and you have an extra set of brackets that do nothing.
     
  14. Offline

    ScootTech

    @xXCapzXx @bwfcwalshy @ark9026 BIG SHOUTOUT to everyone that spoonfed me, and helped me along the way to help me develop my first plugin! The plugin itself works, now I just need to know how to put the plugin into a server host website. And I tried that, I input the craftbukkit external jar, and this is the error message:
    org.bukkit.plugin.InvalidPluginException: java.lang.UnsupportedClassVersionError: me/ScootTech/guarding/Guarding : Unsupported major.minor version 52.0
    Can anyone tell me what that means or how to fix it? This is indeed the last step of the plugin!
     
    ark9026 likes this.
  15. Online

    timtower Administrator Administrator Moderator

    @ScootTech It means that you are compiling using java 8 and running it with something lower than java 8
     
  16. Offline

    ScootTech

    @timtower What is running it lower than Java 8? The server? Does this mean I have to install a java on the java project lower than 8?
     
    Last edited: Jun 25, 2015
  17. Online

    timtower Administrator Administrator Moderator

    @ScootTech The server is indeed running a lower version.
    And yes, you need to recompile with java 7
     
  18. Offline

    ScootTech

    @timtower So I need to find an older version of java 7 update 52? Or any version of 7?
     
    Last edited: Jun 25, 2015
  19. Offline

    ScootTech

    @bwfcwalshy I did that, and I still get an error :/
    ERROR MSG:
    26.06 13:41:16 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:127) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:40) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at java.lang.Class.forName(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.lang.Class.forName0(Native Method) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:62) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:77) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.7.0_72] 26.06 13:41:16 [Server] INFO Caused by: java.lang.UnsupportedClassVersionError: me/ScootTech/guarding/Guarding : Unsupported major.minor version 52.0 26.06 13:41:16 [Server] INFO at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:436) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedServer.init(DedicatedServer.java:133) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at net.minecraft.server.v1_7_R4.DedicatedPlayerList.<init>(SourceFile:14) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at net.minecraft.server.v1_7_R4.PlayerList.<init>(PlayerList.java:68) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.CraftServer.<init>(CraftServer.java:326) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at org.bukkit.craftbukkit.v1_7_R4.CraftServer.loadPlugins(CraftServer.java:364) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:328) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[craftbukkit-dev.jar:git-Bukkit-1.7.9-R0.2-24-g07d4558-b3116jnks] 26.06 13:41:16 [Server] INFO org.bukkit.plugin.InvalidPluginException: java.lang.UnsupportedClassVersionError: me/ScootTech/guarding/Guarding : Unsupported major.minor version 52.0 26.06 13:41:16 [Server] ERROR Could not load 'plugins/Guarding.jar' in folder 'plugins'

    I know it's a lot of coding and some of it is useless, but I thought I would add all of it, so you guys can help me figure this out, this is the only step, and last step I need figured out. Thank you to all who have gave their time to me and got me through this. We're almost done, just a little bit more!
     
Thread Status:
Not open for further replies.

Share This Page