world change and greetings

Discussion in 'Plugin Development' started by ruwen, May 9, 2012.

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

    ruwen

    Hi, Im trying to make a functions der greetings people when entering a specified world. but it does not working...

    public boolean onPlayerEvent(PlayerEvent event){
    String playerWorld = event.getPlayer().getWorld().getName();
    String playerName = event.getPlayer().getName();
    if(playerWorld == "world") return true;

    event.getPlayer().sendMessage("Welcome to " + playerWorld + "!");
    return true;

    }

    or is there a better way to do this?
     
  2. Offline

    CorrieKay

    First of all, the issue is that youre returning a boolean when you should be returning void. Next problem is that you dont have your @EventHandler annotation above the method.

    also, you need to use a tighter event, use this one instead
    For example:

    Code:
    @EventHandler
    public void onWorldChange(PlayerChangedWorldEvent event){
      event.getPlayer().sendMessage("Welcome to " + event.getPlayer().getWorld().getName()+"!");
    }
     
  3. Offline

    ZachBora

  4. Offline

    ruwen

    First of all thank you for helping me CorrieKay.
    But, when i go to nether, i dont get any massage at all :(
     
  5. Offline

    CorrieKay

    hmm, did you get the message anytime else?
     
  6. Offline

    ruwen

    this is the full code, but i dont get any massage when entering another world. i can chat and use other commands der talk back to me.

    Code:
    package net.madebyruwen.worldgreetings;
     
    import java.util.logging.Logger;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class worldgreetings  extends JavaPlugin implements Listener{
     
        private Logger log = Logger.getLogger("Minecraft");
       
        public void onEnable(){
            this.logMessage("Enabled.");
        }
       
        public void onDisable(){
            this.logMessage("Disable.");
        }
       
     
        @EventHandler
        public void onWorldChange(PlayerChangedWorldEvent event){
          event.getPlayer().sendMessage("Welcome to " + event.getPlayer().getWorld().getName()+"!");
        }
       
     
        public void logMessage(String msg){
            PluginDescriptionFile pdFile = this.getDescription();
            this.log.info(pdFile.getName() + " " + pdFile.getVersion() + ": " + msg);
           
           
        }
    }
    
     
  7. Offline

    CorrieKay

    oh, oh, you forgot to register your events!

    add this line to your onEnable method:
    Bukkit.getPluginManager().registerEvents(this,this);

    Just like that, and it should work fine :3
     
  8. Offline

    ruwen

    Yeah that works. but i have a last questions if you can help me.
    Code:
    String playerWorld = event.getPlayer().getWorld().getName();
    String permWorldName = "greeting." + playerWorld;
    if(event.getPlayer().hasPermission(permWorldName)){
    event.getPlayer().sendMessage("Welcome to " + playerWorld +"!");
    }
    I dont recreive any massage after applying this to the script
     
  9. Offline

    KaiBB

    Just in case anything about Corrie's above post was unclear, seeing as I know plenty of people who do not understand this at all;
    (classname represents the name of your Listener class)
    ((I do not use Bukkit, I use getServer, but either way should work.))
    Code:
    public void onEnable(){
            this.logMessage("Enabled.");
            getServer().getPluginManager().registerEvents(new classname(), this);
        }
     
  10. Offline

    CorrieKay

    First, check and doublecheck their permissions file to see if they have the right permission.

    Next, check if permWorldName has any capitals in the string, permissions hate capitals last time i tried.
     
  11. Offline

    ZachBora

    Permissions are indeed case sensitive.
     
    KaiBB likes this.
  12. Offline

    ruwen

    Awesome..

    can i ask a last question?
    is it possible to teleport the event.getPlayer()? we have a fort in nether we use safezone.. so if it was possible to teleport the user somehow, it would be awesome. but i haven't been able to find any clues about it.
     
  13. Offline

    I_am_not_funny

    event.getPlayer().teleport(new Location(World, double x, double y, double z))
     
  14. Offline

    ruwen

    new Location("World_Nether", double x, double y, double z)
    why do it put a redline under "World_Nether"??
     
  15. Offline

    Sagacious_Zed Bukkit Docs

    Eclipse will tell you why it put a red line under it.
     
  16. Offline

    ZachBora

    You need to pass the world object, not a string.
     
  17. Offline

    KaiBB

    Code:
    @EventHandler
    public void onWorldChange(PlayerChangedWorldEvent event){
      event.getPlayer().sendMessage("Welcome to " + event.getPlayer().getWorld().getName()+"!");
      if(event.getPlayer().getWorld().getName().equalsIgnoreCase("world_nether"){
      World nether = event.getPlayer().getWorld();
    }
    }
    The world object, if you use my code above, would be "nether".

    EDIT: If you are going to make this plugin public, you should make the world name configurable. Because not everyone's Nether is name world_nether.
     
  18. Offline

    ruwen

    I get an error in the console.. (no this plugin is private, as long Im learning how stuffs work.)

    Object WorldLoc = "world_nether";
    Location SafezoneLoc = new Location((World) WorldLoc, -240, 63, 70);
    event.getPlayer().teleport(SafezoneLoc);

    Code:
    21:40:36 [SEVERE] Could not pass event PlayerChangedWorldEvent to net.madebyruwen.greeting
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:303)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
            at net.minecraft.server.ServerConfigurationManager.moveToWorld(ServerConfigurationManager.java:322)
            at net.minecraft.server.ServerConfigurationManager.changeDimension(ServerConfigurationManager.java:394)
            at net.minecraft.server.EntityPlayer.a(EntityPlayer.java:306)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:341)
            at net.minecraft.serverhook.NetServerHandlerProxy.a(NetServerHandlerProxy.java:99)
            at net.minecraft.server.Packet10Flying.handle(SourceFile:126)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.serverhook.NetServerHandlerProxy.a(NetServerHandlerProxy.java:83)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:554)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:452)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:490)
    Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.bukkit.World
            at dk.googus.realmprotection.realmprotection.onWorldChange(realmprotection.java:68)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:616)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301)
            ... 15 more
    
     
  19. Offline

    KaiBB

    You're trying to use a String as an object. Use my code.
     
  20. Offline

    ZachBora

    do :
    event.getPlayer().teleport(new Location(event.getPlayer().getWorld(), -240, 63, 70));
     
  21. Offline

    ruwen

    what if i want to make a command later you can use from main world?
     
  22. Offline

    ZachBora

    I'm not following...
     
  23. Offline

    ruwen

    What I mean is, if i wish to add a command like /netherfort while in normal world. how do I get it to work then so i gets teleported to the nether fort?
    Code:
    event.getPlayer().teleport(new Location(event.getPlayer().getWorld(), -240, 63, 70));
    this is not functional now
     
  24. Offline

    KaiBB

    so, essentially, you want to set a warp?

    Code:
    @Override
        public boolean onCommand(CommandSender arg0, Command arg1, String arg2,
                String[] arg3) {
            Player p = (Player) arg0;
            if(arg1.equals("netherfort")){
                p.teleport(location);
                        return false;
            }
    That goes after your onEnable and onDisable in the main class. Or, if you want to have it a bit more organized, you can make a CommandExecutor.

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

    the_merciless

    Not sure this is the best way but this is how i would do it.

    Make a command to set the forts co-ords in the config

    Code:
    public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, String[] args ){
            if(cmd.getName().equalsIgnoreCase("setfort")){
     
    Player p = (Player)sender;
                double x = p.getLocation().getX();
                double y = p.getLocation().getY();
                double z = p.getLocation().getZ();
                String w = p.getWorld().getName();
                getConfig().set("fort.world", w);
                getConfig().set("fort.x", x);
                getConfig().set("fort.y", y);
                getConfig().set("fort.z", z);
                saveConfig();
            p.sendMessage("You have saved the forts co-ordinates")
    }
    Then have a command to tp you there

    Code:
            if(cmd.getName().equalsIgnoreCase("netherfort")){
    String w = getConfig().getString("fort.world");
                        String x = getConfig().getString("fort.x");
                        String y = getConfig().getString("fort.y");
                        String z = getConfig().getString("fort.z");
                        double xd = Double.parseDouble(x);
                        double yd = Double.parseDouble(y);
                        double zd = Double.parseDouble(z);
                        Location fort = new Location(Bukkit.getWorld(w),xd,yd,zd);
                        p.teleport(fort);
    }
    
     
  26. Offline

    KaiBB

    He said he doesn't need it to be configurable, it is for educational purposes only, and he will not be releasing it to anyone.
     
  27. Offline

    the_merciless

    Sure, like i said its prob not the best way. Im not too good at coding thats just how i would do it as its prob the only way i know without researching anything more.
     
  28. Offline

    KaiBB

    I don't blame you, BukkitAPI requires a lot of critical thinking... Especially in this case, because there aren't too many real examples. :p

    Oh! If you wanted, you could have a warp sort of plugin... On the command setnetherfort you can get Player.getLocation() and set that location to netherfortloc, and on command netherfort you can Player.teleport(netherfortloc). If that makes any sense. :3

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

    ruwen

    it works perfectly now. thanks all for helping me..

    this is not a request, but is it possible to do, so you need to wait 1 min before you can cast the teleport command again?
     
  30. Offline

    KaiBB

    It's already been done. So, yes, it is possible.
     
Thread Status:
Not open for further replies.

Share This Page