Plugin command not working (It still loads though)

Discussion in 'Plugin Development' started by BrushPainter, Mar 8, 2014.

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

    BrushPainter

    Hey guys, my plugin command "spbshop", stands for "spawn paintball shop" isn't working, it's supposed to spawn a villager named Zinc - The Paintballer at the location of the command sender, but when I type it nothing at all happens. Please help.. Here's my code for the command:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2.  
    3. if(cmd.getName().equalsIgnoreCase("spbshop")){
    4. Player p = (Player) sender;
    5.  
    6. Villager cv = p.getWorld().spawn(p.getLocation(), Villager.class);
    7.  
    8. cv.setBreed(true);
    9. cv.setMaxHealth(100000000);
    10. cv.setHealth(100000000);
    11. cv.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 6));
    12. cv.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, -6));
    13. cv.setCustomName(ChatColor.BLUE + "" + ChatColor.BOLD + "Zinc - The Paintballer");
    14. cv.getEquipment().getItemInHand().setType(Material.GOLD_BARDING);
    15.  
    16. }
    17. return false;
    18. }
    19. }
     
  2. Offline

    MrInspector

    I don't believe this is necessary or not, but try cv.setCustomNameVisible(true);
     
  3. Offline

    ShadowLAX

    acecheesecr14 label is the command's name, so going cmd.getName() is exactly the same as using label. It has no effect what so ever.

    BrushPainter Use the spawnEntity() method instead of the spawn() method.
     
  4. Offline

    Garris0n

    You should use command.getName() because it will return the command even if an alias is used while the commandLabel will not.
     
    acecheesecr14 likes this.
  5. Offline

    acecheesecr14

    That makes sense xD
     
  6. Offline

    BrushPainter

    ShadowLAX I changed my code to:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    2.  
    3. if (cmd.getName().equalsIgnoreCase("spbshop")) {
    4. Player p = (Player) sender;
    5.  
    6. Villager cv = p.getWorld().spawnEntity(p.getLocation(), Villager.class);
    7.  
    8. cv.setBreed(true);
    9. cv.setMaxHealth(100000000);
    10. cv.setHealth(100000000);
    11. cv.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 6));
    12. cv.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, -6));
    13. cv.setCustomName(ChatColor.BLUE + "" + ChatColor.BOLD + "Zinc - The Paintballer");
    14. cv.setCustomNameVisible(true);
    15. cv.getEquipment().getItemInHand().setType(Material.GOLD_BARDING);
    16.  
    17. }
    18. return false;
    19. }
    20. }

    And I'm getting the error:

    "The method spawnEntity(Location, EntityType) in the type World is not applicable for the arguments (Location, Class<Villager>)"
    MrInspector
    I did that, should fix a problem before I even get to notice it, haha.
     
    MrInspector likes this.
  7. Offline

    ShadowLAX

    BrushPainter The second parameter isn't a class, it's an EntityType.
     
  8. Offline

    Wizehh

    I reccomend you check before you cast Player to sender.
     
  9. Offline

    BrushPainter

    ShadowLAX Not too sure what you mean, please give me an example? (I know this sounds super nooby but I started using Java a bit more than a week ago)

    Wizehh Done, thanks for pointing that out. :)
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    2.  
    3. if (cmd.getName().equalsIgnoreCase("spbshop")) {
    4. if (sender instanceof Player) {
    5. Player p = (Player) sender;
    6.  
    7. Villager cv = p.getWorld().spawnEntity(p.getLocation(), Villager.class);
    8.  
    9. cv.setBreed(true);
    10. cv.setMaxHealth(100000000);
    11. cv.setHealth(100000000);
    12. cv.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 6));
    13. cv.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, -6));
    14. cv.setCustomName(ChatColor.BLUE + "" + ChatColor.BOLD + "Zinc - The Paintballer");
    15. cv.setCustomNameVisible(true);
    16. cv.getEquipment().getItemInHand().setType(Material.GOLD_BARDING);
    17.  
    18. }
    19. return false;
    20. }
    21. }
    22. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    acecheesecr14

    Change Villager.class to EntityType.VILLAGER
    Just going by what shadow said xD
     
  11. Offline

    ShadowLAX

    BrushPainter EntityType is a Bukkit Enum that stores information for entities. Just replace Villager.class, which is a class that extends the Villager itself, to EntityType.VILLAGER.
     
    BrushPainter likes this.
  12. Offline

    BrushPainter

    acecheesecr14 ShadowLAX Wizehh Ok but now I am getting this error on lines (9, 10, 11, 12, 13, 14, 15, 16):

    "The method (MethodNamesHere) is undefined for the type Entity"

    After I changed my code to this:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    2.  
    3. if (cmd.getName().equalsIgnoreCase("spbshop")) {
    4. if (sender instanceof Player) {
    5. Player p = (Player) sender;
    6.  
    7. Entity e = p.getWorld().spawnEntity(p.getLocation(), EntityType.VILLAGER);
    8.  
    9. e.setBreed(true);
    10. e.setMaxHealth(1000000);
    11. e.setHealth(1000000);
    12. e.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 6));
    13. e.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, -6));
    14. e.setCustomName(ChatColor.BLUE + "" + ChatColor.BOLD + "Zinc - The Paintballer");
    15. e.setCustomNameVisible(true);
    16. e.getEquipment().getItemInHand().setType(Material.GOLD_BARDING);
    17.  
    18. }
    19. return false;
    20. }
    21. }
    22. }


    ShadowLAX acecheesecr14 Wizehh Also, will making setMaxHealth(1000000); allow the Villager not to be hit? If not how can I prevent players from killing it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  13. Offline

    ShadowLAX

    BrushPainter That is because you changed the Villager declarable field to a generic Entity, which could be anything. Not everything has those methods, so it's a sub method of the Villager interface.
     
  14. Offline

    Rainy37

    You have to make a Villager instead of the "Entity"

    change line 7 to this:

    Code:
    Villager e = (Villager) p.getWorld().spawnEntity(p.getLocation(), EntityType.VILLAGER);
    By the way, post your plugin.yml, did you register the command "spbshop"?
     
  15. Offline

    ShadowLAX

    No, that would just make the Villager have extremely high health. To cancel damage, use the EntityDamageByEntityEvent and cancel it if the entity is a villager.
     
  16. Offline

    BrushPainter

    ShadowLAX Rainy37 acecheesecr14 Wizehh MrInspector It's still not working for some reason... :confused:

    Code:java
    1. 08.03 23:26:12 [Server] INFO ... 9 more
    2. 08.03 23:26:12 [Server] INFO Caused by: java.io.FileNotFoundException: Jar does not contain plugin.yml
    3. 08.03 23:26:12 [Server] INFO at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    4. 08.03 23:26:12 [Server] INFO at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:424) [craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    5. 08.03 23:26:12 [Server] INFO at net.minecraft.server.v1_7_R1.DedicatedServer.init(DedicatedServer.java:126) [craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    6. 08.03 23:26:12 [Server] INFO at net.minecraft.server.v1_7_R1.DedicatedPlayerList.<init>(SourceFile:14) [craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    7. 08.03 23:26:12 [Server] INFO at net.minecraft.server.v1_7_R1.PlayerList.<init>(PlayerList.java:63) [craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    8. 08.03 23:26:12 [Server] INFO at org.bukkit.craftbukkit.v1_7_R1.CraftServer.<init>(CraftServer.java:235) [craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    9. 08.03 23:26:12 [Server] INFO at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:257) [craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    10. 08.03 23:26:12 [Server] INFO at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:133) [craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    11. 08.03 23:26:12 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader.getPluginDescription(JavaPluginLoader.java:176) ~[craftbukkit_dev_2995.jar:git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks]
    12. 08.03 23:26:12 [Server] INFO org.bukkit.plugin.InvalidDescriptionException: Invalid plugin.yml
    13. 08.03 23:26:12 [Server] ERROR Could not load 'plugins/MenuServiceAPI.jar' in folder 'plugins'
    14. 08.03 23:26:12 [Server] INFO This server is running CraftBukkit version git-Bukkit-1.7.2-R0.2-15-g2f3dbd3-b2995jnks (MC: 1.7.2) (Implementing API version 1.7.2-R0.3-SNAPSHOT)
    15. 08.03 23:26:12 [Server] INFO Starting Minecraft server on 198.12.123.231:25565
    16. 08.03 23:26:12 [Server] INFO Generating keypair
    17. 08.03 23:26:12 [Server] INFO Default game type: ADVENTURE
    18. 08.03 23:26:12 [Server] INFO Loading properties
    19. 08.03 23:26:12 [Server] INFO Starting minecraft server version 1.7.2
    20. 08.03 23:26:07 [Server] INFO Loading libraries, please wait...
    21. 08.03 23:26:07 [Multicraft] Loaded config for "Mod: CraftBukkit Dev 1.7.2-R0.3 (Build #2995)"
    22. 08.03 23:26:07 [Multicraft] Starting server!
    23. 08.03 23:26:07 [Multicraft] Loading server properties
    24.  


    I'm getting this error in the console (again) on starting even though it loads and the command is there, just doesn't do anything when I type /spbshop.

    My plugin.yml export and setup:
    Setup:
    Code:
    name: PBGunShop
    version: 1.0.1
    main: me.BrushPainter.PBGunShop.Main
     
    commands:
      spbshop:
        permission: pbgunshop.spbshop.spawn
     
    permissions:
      pbgunshop.buy.threeburst:
        description: Allows you to buy a threeburst pb gun.
        default: not op
     
      pbgunshop.buy.fullyauto:
        description: Allows you to buy a fullyauto pb gun.
        default: not op
     
      pbgunshop.spbshop.spawn:
        description: Allows you to spawn a PB shop.
        default: op
    Export:
    [​IMG]

    ShadowLAX Also, will this work to prevent damage to Zinc, the villager?

    Code:java
    1. ArrayList<String> Damagers = new ArrayList<String>();
    2. @EventHandler
    3. public void onEntityDamage(EntityDamageByEntityEvent event) {
    4.  
    5. Player player = (Player) event.getDamager();
    6. LivingEntity entity = (LivingEntity)event.getEntity();
    7. Villager zinc = (Villager) entity;
    8.  
    9. if (entity.getLastDamageCause().getEntity() instanceof Player) {
    10. event.setCancelled(true);
    11. } else if(zinc.getCustomName() != null && zinc.getCustomName().equals(ChatColor.BLUE + "" + ChatColor.BOLD + "Zinc - The Paintballer")){
    12. event.setCancelled(true);
    13. } else {
    14. if(Damagers.contains(player.getName())) {
    15. event.setCancelled(true);
    16. }
    17. }
    18. }
     
  17. Offline

    MrInspector

    I believe you're supposed to add a description: to the plugin yml? or am I incorrect. I probably am.
     
  18. Offline

    BrushPainter

  19. Offline

    MrInspector




    Can you post your full code?
     
  20. Offline

    BrushPainter

    Ok.
    Code:java
    1. package me.BrushPainter.PBGunShop;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Entity;
    11. import org.bukkit.entity.EntityType;
    12. import org.bukkit.entity.LivingEntity;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.entity.Villager;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    18. import org.bukkit.event.inventory.InventoryClickEvent;
    19. import org.bukkit.event.player.PlayerInteractEntityEvent;
    20. import org.bukkit.inventory.Inventory;
    21. import org.bukkit.inventory.ItemStack;
    22. import org.bukkit.inventory.meta.ItemMeta;
    23. import org.bukkit.plugin.java.JavaPlugin;
    24. import org.bukkit.potion.PotionEffect;
    25. import org.bukkit.potion.PotionEffectType;
    26.  
    27. public class Main extends JavaPlugin implements Listener{
    28.  
    29. public void onEnable() {
    30.  
    31. Bukkit.getPluginManager().registerEvents(this, this);
    32.  
    33. getLogger().info("PBGunShop Enabled");
    34.  
    35. }
    36.  
    37. public void onDisable() {
    38.  
    39. getLogger().info("PBGunShop Disabled");
    40.  
    41. }
    42. public static Inventory PaintballGunShop = Bukkit.createInventory(null, 9, "Inventory");
    43.  
    44. @EventHandler
    45. public void onInventoryClick(InventoryClickEvent event) {
    46. Player player = (Player) event.getWhoClicked();
    47. Player buyer = (Player) event.getWhoClicked();
    48. ItemStack clicked = event.getCurrentItem();
    49. Inventory inventory = event.getInventory();
    50. ItemStack is = new ItemStack(Material.WOOL );
    51. is.setDurability((byte) 14);
    52. ItemMeta im = is.getItemMeta();
    53. im.setDisplayName(ChatColor.DARK_RED + "Close");
    54. ArrayList<String> lore = new ArrayList<String>();
    55. lore.add(ChatColor.DARK_GRAY + "Closes the PB Shop.");
    56. im.setLore(lore);
    57. is.setItemMeta(im);
    58. if (inventory.getName().equals(PaintballGunShop.getName())) {
    59. if (clicked.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.DARK_RED + "Close")) {
    60. event.setCancelled(true);
    61. player.closeInventory();
    62. }
    63.  
    64. ItemStack fullyauto = new ItemStack(Material.DIAMOND_BARDING, 1);
    65. ItemMeta im1 = fullyauto.getItemMeta();
    66. im1.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Fully-Automatic Paintball Gun");
    67. fullyauto.setItemMeta(im1);
    68. lore.add(ChatColor.WHITE + "500 Coins");
    69. im1.setLore(lore);
    70. PaintballGunShop.setItem(0,fullyauto);
    71. if (inventory.getName().equals(PaintballGunShop.getName())) {
    72. if (clicked.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "" + ChatColor.BOLD + "Fully-Automatic Paintball Gun")) {
    73. event.setCancelled(true);
    74. player.closeInventory();
    75. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manuaddp " + buyer + " paintballguns.getgun.fullyauto");
    76. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manuaddp " + buyer + " -pbgunshop.buy.fullyauto");
    77. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco take " + buyer + " 500");
    78. buyer.sendMessage(ChatColor.YELLOW + "Congratulations! You purchased the fully-auto paintball gun!");
    79. buyer.sendMessage(ChatColor.YELLOW + "You can now use /fullyauto to get your gun!");
    80. }
    81.  
    82. ItemStack threeburst = new ItemStack(Material.GOLD_BARDING, 1);
    83. ItemMeta im2 = fullyauto.getItemMeta();
    84. im2.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "3-Burst Paintball Gun");
    85. threeburst.setItemMeta(im2);
    86. lore.add(ChatColor.WHITE + "1000 Coins");
    87. im2.setLore(lore);
    88. PaintballGunShop.setItem(8,threeburst);
    89. if (inventory.getName().equals(PaintballGunShop.getName())) {
    90. if (clicked.getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GOLD + "" + ChatColor.BOLD + "3-Burst Paintball Gun")) {
    91. event.setCancelled(true);
    92. player.closeInventory();
    93. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manuaddp " + buyer + " paintballguns.getgun.threeburst");
    94. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "manuaddp " + buyer + " -pbgunshop.buy.threeburst");
    95. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco take " + buyer + " 1000");
    96. buyer.sendMessage(ChatColor.YELLOW + "Congratulations! You purchased the three-burst paintball gun!");
    97. buyer.sendMessage(ChatColor.YELLOW + "You can now use /threeburst to get your gun!");
    98. }
    99. }
    100. }
    101. }
    102. }
    103.  
    104. ArrayList<String> Damagers = new ArrayList<String>();
    105. @EventHandler
    106. public void onEntityDamage(EntityDamageByEntityEvent event) {
    107.  
    108. Player player = (Player) event.getDamager();
    109. LivingEntity entity = (LivingEntity)event.getEntity();
    110. Villager zinc = (Villager) entity;
    111.  
    112. if (entity.getLastDamageCause().getEntity() instanceof Player) {
    113. event.setCancelled(true);
    114. } else if(zinc.getCustomName() != null && zinc.getCustomName().equals(ChatColor.BLUE + "" + ChatColor.BOLD + "Zinc - The Paintballer")){
    115. event.setCancelled(true);
    116. } else {
    117. if(Damagers.contains(player.getName())) {
    118. event.setCancelled(true);
    119. }
    120. }
    121. }
    122.  
    123. public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
    124. Entity entity = event.getRightClicked();
    125. Villager zinc = (Villager) entity;
    126. Player player = event.getPlayer();
    127.  
    128. if(zinc.getCustomName() != null && zinc.getCustomName().equals(ChatColor.BLUE + "" + ChatColor.BOLD + "Zinc - The Paintballer")){
    129.  
    130.  
    131. player.openInventory(PaintballGunShop);
    132. }
    133. else{
    134.  
    135. }
    136. }
    137.  
    138. public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args){
    139.  
    140. if (cmd.getName().equalsIgnoreCase("spbshop")) {
    141. if (sender instanceof Player) {
    142. Player p = (Player) sender;
    143.  
    144. Villager e = (Villager) p.getWorld().spawnEntity(p.getLocation(), EntityType.VILLAGER);
    145.  
    146. e.setBreed(true);
    147. e.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 6));
    148. e.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, -6));
    149. e.setCustomName(ChatColor.BLUE + "" + ChatColor.BOLD + "Zinc - The Paintballer");
    150. e.setCustomNameVisible(true);
    151. e.getEquipment().getItemInHand().setType(Material.GOLD_BARDING);
    152.  
    153. }
    154. }
    155. return false;
    156. }
    157. }
     
  21. Offline

    Rainy37

    When you make your plugin.yml, you need to right click the "src" folder which is under PBGunShop if you hit the drop down arrow. then create file plugin.yml ... It should work if you do that
     
Thread Status:
Not open for further replies.

Share This Page