Solved question about fishing event

Discussion in 'Plugin Development' started by flaxeneel2, Aug 17, 2020.

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

    flaxeneel2

    So, I am really new to java and coding minecraft plugins directly in java (I started yesterday). I wanted to make something similar to the Saico fishing plugin, where the fish you cast in are all modified and special, you can also do this in skript quite easily using skript-mirror

    example:
    Code:
    import:
        org.bukkit.event.player.PlayerFishEvent as fishevent
     
    on fishevent:
        set {_item} to raw fish
        enchant {_item} with unbreaking 1
        event.getcaught({_item})
    
    I wanted to replicate this in java, i tried giving it a go myself, but i was unable to do it, so i hit the old forums, but got no help there either and am therefore making this forum post

    Here is my code:

    Code:
    package io.github.flaxeneel2.testarea.listeners
    
    import io.github.flaxeneel2.testarea.Main
    import org.bukkit.ChatColor
    import org.bukkit.entity.Item
    import org.bukkit.entity.Player
    import org.bukkit.event.EventHandler
    import org.bukkit.event.EventPriority
    import org.bukkit.event.Listener
    import org.bukkit.event.player.PlayerFishEvent
    import org.bukkit.inventory.ItemStack
    import static org.bukkit.Material.RAW_FISH
    
    public class Fishing implements Listener {
    
    @SuppressWarnings("Unused")
    
    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onPlayerFishEvent(PlayerFishEvent e) {
    if(e.getCaught() instanceof Item){
    Item item = (Item) e.getCaught(); 
    if(item.getItemStack().getType().equals(RAW_FISH)){
    Player p = e.getPlayer();
    ItemStack is = item.getItemStack();
    is.getItemMeta().setDisplayName(ChatColor.GOLD + "ok so test");
    item.setItemStack(is);
    p.sendMessage("ITS WORKINGGGGGGGGGGGGGGGGGG");}
    }
    }
    
    }
    //this dont work this dont work this dont work this dont work AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    

    it does send me the "its working" message, but the fish I get is still unedited, please tell me how I can edit the catch, also please point out any other unrelated mistakes I did, because since I just started, I can use any and all help


    EDIT: i forgot to preview my post, and now I see that the indentation is all messed up, I mean java isn't really indentation sensitive but now it just looks bad xD
     
    Last edited: Aug 17, 2020
  2. Offline

    KarimAKL

    @flaxeneel2 It's not getting updated because the ItemMeta gotten from ItemStack#getItemMeta() is a copy so, you can't modify it directly. You need to use the ItemStack#setItemMeta(ItemMeta) method after modifying.
    Code:Java
    1. ItemMeta meta = itemStack.getItemMeta();
    2. meta.setDisplayName("Some name");
    3. itemStack.setItemMeta(meta);


    Some notes:
    1. Don't just suppress warnings for no reason, they're there for a reason. (Talking about your @SuppressWarnings("Unused"))
    2. Compare constants with "==" instead of "equals()". (In this case, that's your Material enum from ItemStack#getType())
    3. I believe the Skript version and the Java version are doing different things; the Skript version is setting the entity to an ItemStack with the Material being RAW_FISH but, the Java version is giving RAW_FISH a custom name.

    Here's a link to the Javadocs for Spigot in case you don't know where to find them: https://hub.spigotmc.org/javadocs/spigot/index.html?overview-summary.html
     
  3. Offline

    flaxeneel2


    Thank you, it worked perfectly. Also I'll keep your notes in mind
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page