1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hi there Guest! You should join our Minecraft server @ meepcraft.com
  3. We also have a Discord server that you can join @ https://discord.gg/nfZ5zbnVxE
  4. Purchase a rank upgrade and get it instantly in-game! Upgrade Now

Helpful Java Stuff

Discussion in 'Unofficial Guides' started by MrJoe223, Aug 15, 2013.

?

java?

  1. green

    28.6%
  2. purple because aliens don't wear pizzas

    71.4%
Multiple votes are allowed.
  1. MrJoe223

    MrJoe223 Popular Meeper

    Offline
    Messages:
    324
    Likes Received:
    302
    I wrote this for an associate of mine who's trying to get into Bukkit programming yesterday, but I thought you guys'd like it too. It was a common methods/stuff you need to know kinda deal.

    //Inventory manipulation:

    //you need to get the inventory before you can do anything with it
    Inventory i = player.getInventory();
    //add an item; you add ItemStacks.
    ItemStack it = new ItemStack(Material.TNT);
    //add item/s
    i.addItem(it); OR i.addItem(new ItemStack(Material.TNT, 1)); OR i.addItem(new ItemStack[] { new ItemStack(Material.TNT, 1), it});
    //remove items
    i.remove(it);, etc

    //sending messages
    player.sendMessage(string);
    sender.sendMessage(string);
    Bukkit.getServer().broadcastMessage(string);


    //teleportation
    player.teleport(world, x, y, z);
    event.setRespawnLocation(world, x, y, z);
    You can get worlds by doing Bukkit.getServer().getWorld(String worldname);

    //hashmaps
    These are the go-to thing for player-specific values.
    The way hashmaps work, is that for every key(HashMap<STRING, Integer>), there is an associated value(HashMap<String, INTEGER>).
    Do hashmap.put(playername, integer), to put the key and the value.
    The best thing to do, is get the player's name, instead of the player object. Player objects tend to leak memory.
    HashMap<String, Integer> hash = new HashMap<String, Integer>();

    //arraylists
    These are good things to use for teams, and stuff.
    ArrayList<String> redteam = new ArrayList<String>();
    redteam.add(player.getName());
    redteam.remove(player.getName());

    //events && listeners
    You *must* register your listener classes in the onEnable().
    public void onEnable() {

    Bukkit.getServer().getPluginManager().registerEvents(new PluginListenerClass(this), this);

    }

    In your class declaration, it must implement Listener.
    It must have a constructor, through which you pass the main class.
    You must annotate it with @EventHandler.
    You can name the method anything, it only looks for the event.

    private MainClass plugin;

    public PluginListenerClass(MainClass plugin) {
    this.plugin = plugin;
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {

    e.kickPlayer("teehee");

    }

    //commands

    IF YOU'RE USING A SEPARATE EXECUTOR CLASS: As with listeners, you must put something in the onEnable(), but, for this, it's the command you're executing.

    onEnable() {

    getCommand("testcommand").setExecutor(pluginexecutor(this));

    }

    You must also pass the main class through the constructor.

    the executor class must

    Unlike listeners, the command method has a exact format and name =

    @Override
    public void onCommand(CommandSender s, String command, String label(//this is the alias used for the command, if there is one), String[] arguments) {
    if(command.getName().equalsIgnoreCase("testcommand") {
    sender.sendMessage("you ran the test command!");
    }

    }

    //permissions
    Permissions are really simple.
    You can either do if(player.hasPermission("plugin.random.permission"),
    or you can put stuff in the plugin.yml, as follows:

    //plugin.yml
    You *have* to have a plugin.yml in your plugin, otherwise bukkit will not recognize your plugin.
    The three required fields are name, main, and version. Name(obvious), main(the qualified main class of the plugin), and version.
    YAML files are based on a space hierarchy.
    DO NOT TAB. YAML does not recognize it. space twice for each level.
    You must register each command and permission in the .yml.
    Example .yml is below:

    name: Plugin
    main: pluginpackage.mainclass
    version: 1.0
    commands:
    weedle:
    usage: /weedle
    description: weedle leedle
    permission: plugin.weedle
    permission-message: You don't have permission!
    permissions:
    plugin.*:
    children:
    plugin.weedle: true
    default: op
    plugin.weedle:
    default: op
    description: Permission

    EDIT: Damn Xenforo, messing up mah spaces.

    Etc.

    If you have any questions, feel free to ask me or Kedalion, we'll be happy to help! :D
     
    Tsong-Jien and DancingCactus like this.
  2. FluffbearRCT

    FluffbearRCT Popular Meeper

    Offline
    Messages:
    1,619
    Likes Received:
    2,171
    Shouldn't this go in the coding section?
     
  3. MrJoe223

    MrJoe223 Popular Meeper

    Offline
    Messages:
    324
    Likes Received:
    302
    Maybe. However, it *is* a guide, although it pertains to programming.

    I put it here because this subforum gets more attention. >.>
     
    DancingCactus likes this.
  4. RuSiaN_PeDoBeaRAppeal

    RuSiaN_PeDoBeaRAppeal Meeper

    Offline
    Messages:
    2,555
    Likes Received:
    61
  5. EpicnessMJB137

    EpicnessMJB137 Popular Meeper

    Offline
    Messages:
    122
    Likes Received:
    44
    Code:
    Use this it helps 
     

Share This Page