[Solved] Nothing happens during command. No errors either.

Discussion in 'Plugin Development' started by ZeusAllMighty11, May 27, 2012.

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

    r0306

    ZeusAllMighty11
    The item in hand will never be null. Instead, check if the player is holding air.

    Change this:
    Code:
    if (inHand != null) { // checks if item in hand is null
      return;
    }
    to this:
    Code:
    if (inHand.getType() != Material.AIR) { // checks if item in hand is null
      return;
    }
    
     
    ZeusAllMighty11 likes this.
  2. Offline

    travja

    Post Both classes and I'll help you in like 30 mins
     
  3. Offline

    ZeusAllMighty11


    Did that, but still nothing.

    colorSheep class:
    Code:
    package TransformCreature;
     
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Sheep;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.SheepDyeWoolEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class colorSheep extends JavaPlugin implements Listener{
     
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if(cmd.getName().equalsIgnoreCase("colorsheep")){ // If typed command is colorsheep
          System.out.println("Got this far");
          Player s = (Player) sender;
                if(true) {
                    s.getInventory().addItem(new ItemStack(Material.STICK, 1));
                    s.sendMessage(ChatColor.GOLD + ("[TransformCreature]") + ChatColor.GREEN + ("Added wand item 'stick' into your inventory.")); // sends msg
                    s.sendMessage(ChatColor.GOLD + ("[TransformCreature]") + ChatColor.GREEN + ("Right click a sheep to change it's colour.")); // sends msg 
    }
    }
       return true;
       }
     
    // Time to check on things
       
       @EventHandler
    public void sheepListener(PlayerInteractEntityEvent e) { // player and entity interaction
     
    Player p = (Player) e.getPlayer(); // gets the player variable
    ItemStack inHand = p.getItemInHand(); // gets the iteminhand variable
    Sheep Sheep1;
     
     
    if (inHand.getType() != Material.AIR) { // checks if item in hand is null
     return;
    }
     
    if (inHand.getType() == Material.STICK){ // checks for stick in hand
    return;
           }
     
     
    if (!(e.getPlayer() instanceof Player)) { // checks if instance is not player
       return;
       }
     
     
    if (e.getRightClicked() instanceof Sheep) {
    Sheep1 = (Sheep)e.getRightClicked();
     
    Sheep1.setColor(DyeColor.BLUE);
    }
    }
     
    }
    

    main class
    Code:
    package TransformCreature;
     
    import java.lang.reflect.Constructor;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Sheep;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class main extends JavaPlugin implements Listener{
     
    Logger log = Logger.getLogger("Minecraft"); // define logger as 'log' for console
     
    private colorSheep colorsheep;
     
    public void onEnable(){
     colorsheep = new colorSheep();
     getCommand("colorsheep").setExecutor(colorsheep);
     
     getServer().getPluginManager().registerEvents(new colorSheep(), this);
     
     
     this.log.info("[TransformCreature] Detected and enabled! <3 "); // Print that the plugin is ENABLED to console
    }
     
    public void onDisable(){
    this.log.info("[TransformCreature] Plugin disabled! D; "); // Print that the plugin has been DISABLED to console
    }
     
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    Player s = (Player)sender;
     
    if(command.getName().equalsIgnoreCase("transformcreature")){ // If typed command is transformcreature
    s.sendMessage(ChatColor.GOLD + ("[TransformCreature] ") + ChatColor.GREEN + ("Version: v0.1"));
    }
    return true;
    }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  4. Offline

    r0306

    ZeusAllMighty11
    Try printing out messages to the console inside your event handler and see which part is causing the problem.
     
  5. Offline

    ZeusAllMighty11

    r0306

    I ran it, it goes through the checks, but when it's time to set the wool color, nothing happens. It will not make it there.

    I even tried setting the sheep to baby just in case color was bugged.

    Console says so, and I inserted checks in the right spots btw.
     
  6. Offline

    r0306

  7. Offline

    ZeusAllMighty11

    r0306

    It has now way of checking if the player truly is not a player, since we are a player. o.0 if that makes sense.
    I might've set that check up wrong.

     
  8. Offline

    r0306

    ZeusAllMighty11
    You should remove that whole check right there as e.getPlayer() gets the player anyways. No point in doing so.
     
  9. Offline

    ZeusAllMighty11

    Okay, that's gone.

    The problem is reaching the line where it sets the dye color
     
  10. Offline

    travja

    Add this in your listener:
    Code:java
    1. public main plugin;
    2. public colorSheep(main instance){
    3. this.plugin = instance;
    4. }


    Put that at the top of your listener and you should be good
     
  11. Offline

    ZeusAllMighty11


    I did, didn't work.
     
  12. Offline

    travja

    Now instead of private colorSheep colorsheep do:
    public Listener colorSheep = new colorSheep(this);
    public CommandExecutor SheepCommand = new colorSheep(this);
     
  13. Offline

    r0306

    travja
    The problem's not caused by the Listener. We know it worked because it printed messages out to the console when a sheep is right clicked.
     
  14. Offline

    travja

    ok... So the sheep just isn't appearing a different color?
     
  15. Offline

    ZeusAllMighty11

    Mkay, still nothing though
     
  16. Offline

    travja

    r0306 Maybe you have to update the sheep.... I know changing blocks you have to do event.getBlock().getState().update(); after you've made the changes
     
  17. Offline

    ZeusAllMighty11


    How can I do that if this is a PlayerInteractEntity event?
     
  18. Offline

    travja

    Here's what I'm thinking...
    Code:java
    1. if(p.getItemInHand().getType== Material.STICK){
    2. Entity sheep = event.getRightClicked();
    3. if(sheep instanceof Entity.SHEEP){
    4. sheep = Entity.SHEEP;
    5. sheep.setColor(DyeColor.BLUE);
    6. }
    7. }


    That's the only thing I can think of, however, it looks like that's what you're using.

    PS: I haven't tested this.
     
  19. Offline

    r0306

    travja
    I believe living entities automatically update so there is no need to do so. There aren't any methods in the api either. :C

    ZeusAllMighty11
    Does the listener pass the check for instanceof Sheep?

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

    travja

    You still need to check if then entity is colorable

    K, I tested this, this works for me:
    Code:java
    1. @EventHandler
    2. public void Sheep(PlayerInteractEntityEvent event){
    3. Player p = event.getPlayer();
    4. if(p.getItemInHand().getType()== Material.STICK){
    5. Entity sheep = event.getRightClicked();
    6. if(sheep instanceof org.bukkit.entity.Sheep){
    7. ((Colorable) sheep).setColor(DyeColor.BLUE);
    8. }
    9.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
    ZeusAllMighty11 likes this.
  21. Offline

    ZeusAllMighty11


    Nope, it doesn't check. I tried it on both a cow and a sheep to make sure it wasn't just me, and it didn't pass.

    r0306
    travja

    Thanks!!! Solved, Travja's solution worked.


    For some reason, I feel like I should've known that.

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

    travja

    Then you can go through the spectrum by checking the color, so after you check to see if it's a sheep you can check so:
    Code:java
    1. Colorable Sheep = (Colorable) sheep;
    2. if(Sheep.getColor()== DyeColor.WHITE){
    3. Sheep.setColor(DyeColor.RED);
    4. }else if(Sheep.getColor()== DyeColor.RED){
    5. Sheep.setColor(DyeColor.ORANGE);
    6. }else if(Sheep.getColor()== DyeColor.ORANGE){
    7. Sheep.setColor(DyeColor.YELLOW);
    8. }//And so on!
    9.  
    10. [quote="ZeusAllMighty11, post: 1139893, member: 90610"][USER=103742]r0306[/USER]
    11. [USER=90628520]travja[/USER]
    12.  
    13. Thanks!!! Solved, Travja's solution worked.
    14.  
    15.  
    16. For some reason, I feel like I should've known that.[/quote]
    17. Glad I could help you!
    18.  
    19. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
    ZeusAllMighty11 likes this.
  23. Offline

    ZeusAllMighty11

    One more question, I went through the spectrum (not in specific order) and got all the colours I wanted.

    But it says can not refrence field before it's defined. Not sure why, since it's defined above.
     
  24. Offline

    travja

    It does that to me if I haven't saved/finished typing/other random things
     
  25. Offline

    ZeusAllMighty11

    Should I just export anyway?
     
  26. Offline

    travja

    I'd try it, Show me the whole @EventHandler block
     
  27. Offline

    ZeusAllMighty11


    Edit: my bad wrong code
    Code:
    @EventHandler
    public void Sheep(PlayerInteractEntityEvent event){
    Player p = event.getPlayer();
    if(p.getItemInHand().getType()== Material.STICK){
    Entity sheep = event.getRightClicked();
    if(sheep instanceof org.bukkit.entity.Sheep){
    ((Colorable) sheep).setColor(DyeColor.BLUE);
    }
    }
    }
     
    Colorable Sheep = (Colorable) Sheep; // defines sheep
    {
    if(Sheep.getColor()== DyeColor.RED){
    Sheep.setColor(DyeColor.RED);
    }else if(Sheep.getColor()== DyeColor.RED){
    Sheep.setColor(DyeColor.ORANGE);
    }else if(Sheep.getColor()== DyeColor.ORANGE){
    Sheep.setColor(DyeColor.YELLOW);
    }else if(Sheep.getColor()== DyeColor.YELLOW){
    Sheep.setColor(DyeColor.GREEN);
    }else if(Sheep.getColor()== DyeColor.GREEN){
    Sheep.setColor(DyeColor.LIME);
    }else if(Sheep.getColor()== DyeColor.LIME){
    Sheep.setColor(DyeColor.LIGHT_BLUE);
    }else if(Sheep.getColor()== DyeColor.LIGHT_BLUE){
    Sheep.setColor(DyeColor.BLUE);
    }else if(Sheep.getColor()== DyeColor.BLUE){
    Sheep.setColor(DyeColor.MAGENTA);
    }else if(Sheep.getColor()== DyeColor.MAGENTA){
    Sheep.setColor(DyeColor.PURPLE);
    }else if(Sheep.getColor()== DyeColor.PURPLE){
    Sheep.setColor(DyeColor.PINK);
    }else if(Sheep.getColor()== DyeColor.PINK){
    Sheep.setColor(DyeColor.BROWN);
    }else if(Sheep.getColor()== DyeColor.BROWN){
    Sheep.setColor(DyeColor.BLACK);
    }else if(Sheep.getColor()== DyeColor.BLACK){
    Sheep.setColor(DyeColor.GRAY);
    }else if(Sheep.getColor()== DyeColor.GRAY){
    Sheep.setColor(DyeColor.SILVER);
    }else if(Sheep.getColor()== DyeColor.SILVER){
    Sheep.setColor(DyeColor.WHITE);
    }else if(Sheep.getColor()== DyeColor.WHITE){
    Sheep.setColor(DyeColor.CYAN);
    }else if(Sheep.getColor()== DyeColor.CYAN){
    Sheep.setColor(DyeColor.RED);
    }// hi
    }
    }
    [code]
     
  28. Offline

    travja

    ???
    How are you doing the coloring? Post the code about the coloring.
     
  29. Offline

    ZeusAllMighty11

    edited, my bad read again.
     
  30. Offline

    travja

    What's the error and where?

    First thing I see is that if the sheep is red it dyes it red.

    ZeusAllMighty11 OH! When you're defining Sheep you have Sheep = (Colorable) Sheep; This should be sheep, lowercase s

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

Share This Page