PlayerItemConsumeEvent Issue(I am a noobie)

Discussion in 'Plugin Development' started by mittle, Apr 22, 2016.

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

    mittle

    public class Cheese extends JavaPlugin implements EventListener{

    public void onEnable(){

    }
    @SuppressWarnings("null")
    public void onPlayerConsume(PlayerItemConsumeEvent e){
    Player sender = null;
    Player player = (Player) sender;
    Cheese c = (Cheese) e.getPlayer();
    if(c.equals(Material.GOLDEN_APPLE));
    player.sendMessage("Good job!");

    So I've recently starting doing eclipse and trying to learn Events and things that aren't just commands.
    I can never seem to figure out how to write things correctly. If someone could help me with this or help my out by sending me a video or link that would help a noobie out like me it would be very appreciated.
     
  2. 1. Change 'EventListener' to Listener.
    2. In 'public void onEnable()' do:
    public void onEnable(){
    getServer().getPluginManager().registerEvents(this, this);
    }

    3. Replace 'Player sender = null' to 'Player (whatever) = e.getPlayer();
    ^
    I think there is a getPlayer. Otherwise it's getKiller();, if there is non, just say.
    4. Delete 'Player player = (Player) sender;'
    5. Replace;
    'Cheese c = (Cheese) e.getPlayer();
    if(c.equals(Material.GOLDEN_APPLE));
    player.sendMessage("Good job!");'
    With
    'ItemStack goldApple = new ItemStack(Material.GOLDEN_APPLE);
    if(player.getItemInHand() == goldApple){
    player.sendMessage("Good job!);
    }
    6. Add '@EventHandler' on top of 'public void onPlayerConsume(PlayerItemConsumeEvent e){'


    XTRA:
    1. You NEVER add ';' after the parentheses of a if statement, either add a Curley bracket '{' or just keep it on the same line.
    2. I suggest you watch YouTube tutorials based on how much was wrong.
    3. I only gave you steps instead of code to help you better understand.
     
  3. Offline

    Konato_K

    @mittle
    You need to register your events
    What are you doing with the "sender" in the event? If you want a player you need to get it from the event, there is no sender in there
    Why would you cast a player into a Cheese? (JavaPlugin) That will throw an error
    A Cheese will NEVER be a Material

    To make plugins you need to know Java, if you don't you're going to have a really hard time trying to get things working
     
  4. Offline

    mittle

    cheese is the name of the class and the java project name not an item... lol like i said im new no need to roast me >.< @Konato_K but thanks for the input
     
  5. Offline

    Zombie_Striker

    @mittle
    "Roasting" is a different thing than pointing out everything that can go wrong. Although, he could have worded it to make it indirect (I.e not asking "why would you do that") @Konato_K .

    BTW: You really should learn Java before working on Bukkit. Java is a requirement if you want to make plugins.

    1. Variable names and class names should be descriptive, and explain what each item does.
    2. You should never have to suppress warnings (warnings are there for a reason)
    3. There is no reason why you would need a sender for a event, as events do not have any senders.
    4. Sender is null, you are casting a Null value to a Player, and setting player equal to that null value. That means player is null. Use Event#getPlayer() to return the player.
    5. You never registerd your event, and for got to add the "@EventHandler" tag to the top of events. Each class that has events in it also needs to be registering, and events NEED @EventHandler above each method in order to work.
    6. [the rest that the members above have pointed out]
     
    mine-care likes this.
  6. Offline

    mittle

    @Zombie_Striker so I should go learn java to what extent, it would make this easier? if you didn't mind, if you have any good tutorials that you would be willing to send that would be much appreciated. :)
     
  7. Offline

    mittle

  8. Offline

    mine-care

    @mittle Please mark the thread as 'Solved' if your question was answered .
     
  9. Offline

    mcdorli

    Maybe oracle's official java tutorial https://docs.oracle.com/javase/tutorial/
     
Thread Status:
Not open for further replies.

Share This Page