Solved Commands and Events doesn't work how i wan't it .

Discussion in 'Plugin Development' started by FightManiac, Jan 25, 2014.

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

    FightManiac

    Code:java
    1. @EventHandler
    2.  
    3. public void onPlayerCommandWrite(PlayerCommandPreprocessEvent event) {
    4. Player player = event.getPlayer(); {
    5. if (player.performCommand("before")) {
    6.  
    7. }
    8. {
    9. }
    10. event.getPlayer().getInventory().contains(new ItemStack(Material.GOLD_BLOCK));
    11. event.getPlayer().getInventory().removeItem(new ItemStack[] { new ItemStack(Material.GOLD_BLOCK, 1) });
    12. event.getPlayer().getInventory().addItem(new ItemStack[] { new ItemStack(Material.GOLD_INGOT, 9) });
    13. event.getPlayer().sendMessage(ChatColor.DARK_GREEN + "TEST MESSAGE"); }
    14.  
    15. }
    16.  
    17.  
    18. }


    I made a plugin when you /before it woudl change 1 gold into 9 gold ingots everything works but it works on every command out there.

    For an example if i write /before it works how it suppose to be
    but if I write /day ,/spawn every existing command this will still work and give the player the 9 gold ingots even if he dont have the gold block he will get the it any help?
     
  2. Offline

    L33m4n123

    You do not check IF he has a Gold Block. its not in an if-clause.. you just remove it without checking if it is there. you give them items without checking if the 2 before were true.. you do that stuff no matter what they use as command

    So go ahaed and check your brackets and use more if statements
     
  3. Offline

    FightManiac

    L33m4n123 I'm a noob in this can you give a example ?
     
  4. Offline

    Gater12

    FightManiac
    If statements are the basics of Java.... Which you SHOULD by now know already.
     
  5. Offline

    LukesComputers

    You need to implement a if statement that "checks" if he has a gold block in his inventory. In addition, there are several other mistakes inside of this code. I will disregard this mistakes as if you are a newbie to this. Here is the code rewritten:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String args){
    2. if(Sender instanceof Player){
    3. Player player = (Player) sender;
    4.  
    5. if(commandLabel.equalsIgnoreCase("before"){
    6. Inventory playerInventory = player.getInventory();
    7.  
    8. if(playerInventory.contains(MATERIAL.GOLD_BLOCK){
    9.  
    10. playerInventory.removeItem(MATERIAL.GOLD_BLOCK, 1);
    11. playerInventory.addItem(MATERIAL.GOLD_INGOT, 9);
    12. }
    13. }
    14. }
    15. return false;
    16. }


    I apologize in advanced I was not able to do this in my compiler I did it in notepad. The "adding" & "removing" of the items may be off. Also, you should watch some Java Tutorials instead of Bukkit Programming Tutorials because they lack to give you good base of the basics of Java.
     
  6. Offline

    FightManiac

    I know i will now start learning Java but it's not working what I did wrong?
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String args){
    2. if(sender instanceof Player){
    3. Player player = (Player) sender;
    4.  
    5. if(commandLabel.equalsIgnoreCase("before")){
    6. Inventory playerInventory = player.getInventory();
    7.  
    8. if(player.getInventory().contains(Material.GOLD_BLOCK)){
    9.  
    10. player.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.GOLD_BLOCK, 1) });
    11. player.getInventory().addItem(new ItemStack[] { new ItemStack(Material.GOLD_INGOT, 9) });
    12. }
    13. }
    14. }
    15. return false;
    16. }
    17. }
     
  7. Offline

    LukesComputers

    Here are your problems:
    • Your way of having the console recognize your command is quite abstract. (Not really a problem but it bothers me)
    • You obviously do not understand how a stand a "IF" statement works, which means you do not understand the BASIS of any programming language.
     
  8. Offline

    FightManiac

    Well i admit it. Il start learning Java after this but can anyone help me?
    If this plugin will work i will understand how to do better things

    xTrollxDudex My friend told your a good dev maybe you can help?


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

    xTrollxDudex

  10. Offline

    FightManiac

    xTrollxDudex when i write the command its told there no command like this.
    and there is an error in this line:
    PlayerInventory Inventory= player.getInventory();
    That i dont use the "inventory"

    Note i have my command writed in plugin.yml

    Code:java
    1. name: Slifuoti
    2. author: FightManiac
    3. version: 1.5
    4. main: as.FightManiac.Slifuoju.Slifuoti
    5. description: TEST.
    6. commands:
    7. before:
    8. description: blahblah


    My plugin.yml

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

    xTrollxDudex

    FightManiac
    Does the plugin load?

    The variable isn't used because you didn't use it, you only continued to use player.getInventory() instead of playerIntventory.
     
  12. Offline

    FightManiac

    xTrollxDudex Okay i did this but when i write the command nothing is done

    Code:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String commandLabel, String args){
    2. if(sender instanceof Player){
    3. Player player = (Player) sender;
    4.  
    5. if(commandLabel.equalsIgnoreCase("before")){
    6. Inventory playerInventory = player.getInventory();
    7. Player p = (Player) sender;
    8.  
    9.  
    10. if(playerInventory.contains(Material.GOLD_BLOCK)){
    11.  
    12. playerInventory.removeItem(new ItemStack[] { new ItemStack(Material.GOLD_BLOCK, 1) });
    13. playerInventory.addItem(new ItemStack[] { new ItemStack(Material.GOLD_INGOT, 9) });
    14. p.sendMessage("My Message");
    15. }
    16. }
    17. }
    18. return false;
    19. }
    20. }


    My plugin.yml
    Code:java
    1. name: Slifuoti
    2. author: FightManiac
    3. version: 1.5
    4. main: as.FightManiac.Slifuoju.Slifuoti
    5. description: derp
    6. commands:
    7. before:
    8. description: any message here is good


    Why is it not working?

    Solved It.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page