Item on world switch

Discussion in 'Plugin Development' started by FreakyPear5, Feb 11, 2020.

  1. Offline

    FreakyPear5

    Hi everyone!

    I'm trying to develop a simple little plugin so that when a player switches to a certain world, it gives them a certain item. I know how to do this (theoretically) however it isnt working..

    How would I get this to work?
    I haven't made a plugin like this in a while and have forgotten how to do it :p
    At the moment, I am using the PlayerChangedWorld event but it doesn't like it at all and nothing is happening. No console errors either. How else could I do it?

    Thank you and regards,

    Freaky
     
  2. Offline

    caderapee

  3. Offline

    FreakyPear5

    nope :p thanks :D
     
  4. Offline

    FreakyPear5

    @caderapee how do i do that? I've completely forgotten and nothing I can find on the Internet works.
     
  5. Offline

    CraftCreeper6

    @FreakyPear5
    getServer()#getPluginManager()#registerEvents(event, this); (this assumes it's in your main class)
     
  6. Offline

    caderapee

  7. Offline

    Strahan

    For future reference, when you have a problem with your code it helps to post the code :)
     
  8. Offline

    FreakyPear5

    Sorry Strahan :p

    I put that in my onEnable() but it still isn't running the event when I join. I added a PlayerJoinEvent with a player.sendMessage() on it to test but nothing happens in join. Might I have a buggy JAR file or is it something else?
     
  9. Online

    timtower Moderator Moderator

    And that is exactly where the "post your code" comes in handy :p
    So: please post your code. Full class.
     
  10. Offline

    FreakyPear5

    Code:
    package dev.FreakyPear5.WorldCompass;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import dev.FreakyPear5.WorldCompass.Commands.EventCommand;
    
    public class Main extends JavaPlugin implements Listener {
      
        // STATICS //
        public static Inventory worldsel = Bukkit.createInventory(null, 9, "World Selection Menu");
      
        // ITEMSTACKS FOR INV //
        ItemStack hub = new ItemStack(Material.GLASS, 1);
        ItemStack kp = new ItemStack(Material.GOLD_BLOCK, 1);
        ItemStack sb = new ItemStack(Material.GRASS, 1);
      
        // ITEMMETAS FOR INV //
        ItemMeta chub = hub.getItemMeta();
        ItemMeta ckp = kp.getItemMeta();
        ItemMeta csb = sb.getItemMeta();
      
        // ENABLE / DISABLE //
        @Override
        public void onEnable() {
            // CONFIGS //
            this.saveConfig();
            // CMDS //
            this.getCommand("event").setExecutor((CommandExecutor)new EventCommand());
            // EVENTS //
            getServer().getPluginManager().registerEvents(this, this);
            // BOOT MESSAGES //
            System.out.println("[WorldCompass] Enabling");
            System.out.println("[WorldCompass] Plugin built by FreakyPear5");
            System.out.println("[WorldCompass] Copyright 2020 TRC FreakyPear5©");
        }
        public void onDisable() {
            System.out.println("[WorldCompass] Disabling");
            System.out.println("[WorldCompass] Plugin built by FreakyPear5");
            System.out.println("[WorldCompass] Copyright 2020 TRC FreakyPear5©");
        }
        // EVENTS //
        @EventHandler
        public void onWorldSwitch(PlayerChangedWorldEvent event) {
            Player player = event.getPlayer();
            System.out.println("Test: EVENT WORKS");
            if (player.getLocation().getWorld().getName() == "build") {
                ItemStack compass = new ItemStack(Material.COMPASS, 1);
                ItemMeta cmeta = compass.getItemMeta();
                cmeta.setDisplayName(ChatColor.RED + "World Selection Menu");
                compass.setItemMeta(cmeta);
                player.getInventory().addItem(compass);
            }
        }
        public void rightClick(PlayerInteractEvent event) {
            Action action = event.getAction();
            Player player = event.getPlayer();
            if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK) {
                if (player.getItemInHand().getItemMeta().getDisplayName() == ChatColor.RED + "World Selection Menu" && player.getItemInHand().getType().equals(Material.COMPASS)) {
                  
                }
            }
        }
        public void playerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            player.sendMessage("test");
            }
    
    }
     
    Last edited by a moderator: Feb 13, 2020
  11. Online

    timtower Moderator Moderator

    @FreakyPear5 Strings are compared with .equals, not with ==
     
  12. Offline

    Strahan

    Yea, you seem to have them backwards. Like how in your PlayerInteractEvent you compare a string with == and an enum with .equals. Typically people do that the opposite.

    Also:
    • Speaking of that event, you shouldn't make compound statements with elements that can fail.

      I.E. if the player has no item in hand, hanging .getItemMeta() off of it will crash the plugin with NullPointerException. Also if you are using 1.9+, you are not getting item in hand right. It's Player#getInventory()#getItemInMainHand().
    • There is no need to have ,1 when making an ItemStack. 1 is the default amount.
    • There is no need to cast the class to CommandExecutor when building the setExecutor argument
    • You should be using the built in logger, not println
    • There is no need to send enable/disable messages; Spigot already does that
     
  13. Offline

    FreakyPear5

    @Strahan Ok thanks I'll edit what you said, however my events still aren't running. Nothing happens on join, but it should and I don't know why it isn't. Can you tell me how to fix that?

    Thanks :)
     
  14. Offline

    caderapee

    @FreakyPear5 youy didn not put the annotation eventhandler for the player join event and interact event
     
    Strahan likes this.
  15. Offline

    FreakyPear5

    oh my god why didnt i see that thank you so much i was so annoyed
     

Share This Page