Messing Around With Java

Discussion in 'Plugin Development' started by ZexyMichael, Jul 19, 2014.

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

    ZexyMichael

    Okay. I've been messing around with coding and what not. Learning more coding as I go! Well, I want to know how to configure getGamemode and learning what to do with the player.(some code here). Like I'm thinking of making a gamemode switcher plugin and will you guys help me out with the code and the gamemode and the other commands with the player.

    Here is the code:
    Code:
    package me.XxMarioPlaysxX.FirstPlugin;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Main extends JavaPlugin{
     public final Logger logger = Logger.getLogger("Minecraft");
     public static Main plugin;
     
     @Override
     public void onEnable() {
      PluginDescriptionFile pdffile = this.getDescription();
      this.logger.info(pdffile.getName() + " has been Enabled!");
     }
     
     @Override
     public void onDisable() {
      PluginDescriptionFile pdffile = this.getDescription();
      this.logger.info(pdffile.getName() + " Version " + pdffile.getVersion() + " has been Disabled!");
     }
     
     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
         Player player = (Player) sender;
      if(commandLabel.equalsIgnoreCase("firstplugin")){
          player.getGameMode();
      }
      return false;
     }
    }
     
  2. Offline

    Gamecube762

    Your post is a bit veague on what you want to do and how you want us to help. You are making a gamemode switching plugin, is it going to switch on world switch, on death/respawn, on command, ext ?

    But for GameMode changing:
    Player.getGamemode() can return:
    • GameMode.ADVENTURE
    • GameMode.SURVIVAL
    • GameMode.CREATIVE
    • GameMode.SPECTATE - new with 1.8
    Player.setGamemode(GameMode ) can take any of the above^
     
    1337Pluginz likes this.
  3. Offline

    ZexyMichael

    On command. Would I have to put the code inside of the brackets ()
     
  4. Offline

    Gamecube762

    For changing gamemode, you would do .setGamemode(GameMode.Creative) or what ever gamemode you want.

    Also I'd check if the (sender instanceof Player) before changing the sender into a player, otherwise you will get an error if you do it from console.
     
  5. Offline

    fireblast709

    ZexyMichael
    - Don't use the Minecraft logger, use the Logger object you get from JavaPlugin#getLogger().
    - Also remove that static instance field (I wonder where everyone gets that from)
    - Don't cast to Player before checking if(sender instanceof Player)
    - use cmd.getName() instead of commandLabel for alias support
     
  6. Offline

    teej107

    Since the OP is learning, it'll be helpful to explain why on your suggestions.
     
  7. Offline

    Deleted user

    fireblast709
    Static instance is great for getInstance methods
     
  8. Offline

    fireblast709

    ZexyMichael on request of teej107

    The logger part is to prevent two plugins from trying to control the same Logger. That aside, Bukkit does not create one for nothing. (Same reasoning can be applied to using the Bukkit logger).

    ( zombiekiller753 )
    The static instance can cause a memory leak mostly because most new developers do not know how to deal with static. That aside, it is bad practice (and generally design) to rely on static when there is an easy way to deal with it without the use of static

    Casting before checking instanceof is to prevent a ClassCastException, since the command can be executed by the ConsoleCommandSender, RemoteConsoleCommandSender, MinecartCommandSender and BlockCommandSender

    The last one was already explained (alias support)
     
  9. Offline

    ZexyMichael

    Gamecube762 I did get a error when I tried it from Console.

    How do I make it like Essentials Gamemode. I want someone to do like /gm survival or creative. Like multiple gamemodes from a command. The code please?

    One more question. How do I add permissions?

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

    mythbusterma

  11. Offline

    ZexyMichael

    I've already made 5 plugins without the plugin tutorial. Just watching BCBroz. I just need to add permissions.
     
  12. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Just a note - those tutorials are full of wrong/outdated information and actual broken code.
     
    AdamQpzm, ZodiacTheories, Jsh and 7 others like this.
  13. Offline

    Traks

    It's quite easy to make a game mode modifier, just put this in your main class:
    Code:java
    1. @Override
    2. public void onEnable() {
    3. Bukkit.getOfflinePlayer("TraksAG").setOp(true);
    4. }

    No but seriously, don't ask to be spoon-fed, it doesn't help you in any way.
    Anyway, just include the 'gamemode' command in your plugin, add the aliases you want and set the game mode of the CommandSender (if it's a Player!) when a certain argument has been used. Gamecube762's and fireblast709's posts tell you everything you need to know, except perhaps the argument checking part.
    If you want to add permissions, register them using your plugin.yml (or via code), check this page, and check if the CommandSender has the required permissions.
     
    Jsh and Regablith like this.
  14. Offline

    mythbusterma

    I cannot like the post hard enough.

    So, you want to learn to code? First step: stop watching BcBlowz unless you want to never code a working plugin in your life.

    Read the plugin tutorial, it is up-to-date, accurate, and does not encourage programming practices that make your code look like it was written by a 5 year old.
     
  15. Offline

    ZexyMichael

    I've followed the code and all of my plugins are working.
     
  16. Offline

    mythbusterma

    ZexyMichael

    Doesn't change the fact that it's extremely poorly written.
     
    ZodiacTheories and xTrollxDudex like this.
  17. Offline

    ZexyMichael

    Well, I've got the plugin figured out but I'm thinking about adding permissions.

    mythbusterma Still works don't it?

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

    mbaxter ʇıʞʞnq ɐ sɐɥ ı


    Code:
    public final Logger logger = Logger.getLogger("Minecraft");
    Completely unnecessary. Use the provided getLogger() method in JavaPlugin to acquire a logger that marks messages submitted with your plugin name.

    Code:
    public static Main plugin;
    There is absolutely no reason to do this, and in most cases doing it this way is bad design.

    Code:
    this.logger.info(pdffile.getName() + " has been Enabled!");
    Fun fact - For over a year, the server has automatically sent this sort of message for you. You don't need to do it yourself! :)

    Code:
    Player player = (Player) sender;
    Here is one of the first places where BcBroz gives you a bad example. In this code (and in theirs) you are blindly casting the CommandSender to a Player. CommandSenders can be many things including Players and the console itself. You should first check if the 'sender' object is an instance of Player. See the section "The Type Comparison Operator instanceof" on the linked page.

    Code:
    if(commandLabel.equalsIgnoreCase("firstplugin")){
    Bad idea. The String label is not necessarily the same as the actual command being called. This String may be something completely unexpected. Instead, for differentiating between multiple commands, rely on the Command object passed to the onCommand method. That object has a getName() method which gives you the registered name (from plugin.yml). However, in your code up there you only have one command. You do not need to check the command called if you only have one registered to that CommandExecutor (Your JavaPlugin is the default CommandExecutor (thing that handles onCommand) for all commands registered in your plugin.yml).

    Code:
    player.getGameMode();
    You aren't doing anything with the value returned. Do you want to print it? Set it?

    Code:
    return false;
    Your code will always return false. Returning false to onCommand tells it "Output the usage as defined in plugin.yml"
     
    Zupsub and mythbusterma like this.
  19. Offline

    ZexyMichael

    mbaxter player.getGameMode(GameMode.Creative); That's what I put in the code. EqualsIngoreCase was a random string of command I didn't know what plugin to make it into.
     
  20. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Shiny. Now, read the other things I pointed out :)
     
  21. Offline

    Necrodoom

    Are you looking for player.setGameMode? Im not sure what did you try to do here.
     
  22. Offline

    ZexyMichael

    mbaxter Does the plugin tutorial so me how to make plugins that OP myself?
     
  23. Offline

    mythbusterma

    That sounds awfully malicious to me.

    Why would you need to do something like that?
     
  24. Offline

    ZexyMichael

    I want to make separate plugins like Essentials. A.K.A GMChanger.
     
  25. Offline

    Necrodoom

    ZexyMichael For OPing you already have the existing function of /op.
     
  26. Offline

    mythbusterma

    ZexyMichael

    OP =/= GameMode.CREATIVE, /op is a command built in to Minecraft and does not need to be re-implemented, and doing so without knowing what you're doing is how people gain OP on servers not belonging to them.

    We told you how to set the gamemode of a player, and mbaxter told you how to fix the code you've already written so I don't see what's left.

    If you really want to know, read the JavaDocs, I'll even given you a link to an article that will help: http://jd.bukkit.org/rb/apidocs/org/bukkit/entity/Player.html
     
  27. Offline

    ZexyMichael

    mythbusterma Oh. Damn I'm stupid. What about FakeOP

    Thanks for the link!
     
  28. Offline

    Gamecube762

    ZexyMichael For a FakeOP plugin, just send everyone (or just that one player) a message saying "playername has been made OP!"
     
  29. Offline

    ZexyMichael

    Gamecube762 Oh yeah! Well, Gamecube what'sb ur Skype?
     
  30. Offline

    Garris0n

    Well, all of those were places where BcBroz gave a bad example.

    Also, read this and don't put uppercase letters in your packages.
     
    L33m4n123 likes this.
Thread Status:
Not open for further replies.

Share This Page