API RulesInventory API

Discussion in 'Resources' started by Th3Gam3rz, Jul 15, 2015.

?

Was this API useful to you?

  1. YES! I HAVE BEEN LOOKING FOR THIS.

    0 vote(s)
    0.0%
  2. Yeah, I suppose so.

    0 vote(s)
    0.0%
  3. Kind of...

    0 vote(s)
    0.0%
  4. Not Really.

    0 vote(s)
    0.0%
  5. No, Not at all.

    1 vote(s)
    25.0%
  6. Not me, but possibly others.

    3 vote(s)
    75.0%
Thread Status:
Not open for further replies.
  1. Offline

    Th3Gam3rz

    Hi,

    This is my first API that I have coded so I would be grateful if you could point out any improvements or faults in the comments!

    The Rules Inventory API
    Download Link: https://github.com/Th3Gam3rz/RulesInventory-API
    License: DWYWCIDCJGC (Do what you want 'cause I don't care, just give credit)
    Important: Please see the bottom of this post for the new methods introduced in 1.1!

    What is it?

    The RulesInventory API is a simplified way of creating a visual representation of your server's rules. With a few lines of code, you can have a snazzy looking GUI for you server's guidelines that will probably wow your players.

    How does it work?

    Below, you can find a tutorial for how to use the API and start creating stylish and sleek inventory menus for your rules.

    Step 1 - Setting up your Plugin

    To add the API to your plugin, all you need to do is go over to this Github repo and download the zip.
    Copy across the 2 .java files into your package, adjust the package names and you are good to go!

    Step 2 - Creating a Rules Inventory

    Now you can start to actually create your Rules Inventory.

    Code:
    RulesInventory inv = new RulesInventory("Your Server's Name's Rules", 9);
    This will create a new Rules Inventory with room for 9 rules titled "Your Server's Name's Rules". You could display this to the player right now if you wanted to, but you have no rules in it yet. Adding rules is just as simple!

    As of Version 1.1, there is a new way to create inventories. Please see the bottom of this post to find out how to.

    Step 3 - Adding Rules

    There are two different ways to add rules. One is more simplified than the other. I will show you how to do both.

    Way 1

    First, you need to create your rule.

    Code:
    Rule rule1 = new Rule(ChatColor.GOLD + "Play Fair");
    This will create a rule with a gold title labelled "Play Fair". Now, you can add different attributes to the rule.

    Code:
    //Sets the Item
    //Argument is a Material: Item's Type
    rule1.setItem(Material.ANVIL);
    
    //Sets the Lore
    //Argument is a String List: Item's Lore
    List<String> lore = new ArrayList<String>();
    lore.add(ChatColor.RED + "You should not cheat");
    lore.add(ChatColor.RED + "or use any hacks.");
    rule1.setDescription(lore);
                   
    //Update Name if Necessary
    //Argument is a String: Rule Name
    rule1.setName(ChatColor.GOLD + "New Rule Name");


    Way 2

    This way, you create a rule with 1 line of code!

    Code:
    Rule rule2 = new Rule(ChatColor.GOLD + "Play Fair").setItem(Material.ANVIL).addToDescription(ChatColor.RED + "You should not cheat").addToDescription(ChatColor.RED + "or use any hacks.").setName(ChatColor.GOLD + "New Rule Name");
    Both ways work well, however I personally find the latter to be easier.

    Step 4 - Adding the Rules to the Inventory

    Now, you can add the rules to the Inventory. Again, there are two ways to do this. One is aimed at adding rules one at a time, whereas the other is designed for mass-adding rules.


    Way 1
    Code:
    //Adds Rules to Inventory
    //Argument is Rule to Add
    inv.addRule(rule1);
    inv.addRule(rule2);
    Way 2

    Code:
    //Adds Rules to Inventory
    //Argument is List of Rules to Add
    List<Rule> rules = new ArrayList<Rule>();
    rules.add(rule1);
    rules.add(rule2);
    inv.addMultipleRules(rules);


    Although this way uses more code, it allows you to add multiple rules at once. Example: You could add your rules to a list as you initialize them and then use one line to add the whole list at the end of your code.

    Final Step - Displaying the Inventory

    You can use one line of code to show the player the rules.

    Code:
    inv.openFor(p);
    Thank you for reading through this long tutorial. Before people say that this way seems a lot longer than using Bukkit's Inventory API, I created this to aid new developers who are new to the whole Bukkit API.

    Version 1.1 - New Ways to Create an Inventory

    Due to demand, I have made the RulesInventory a "builder" so that you can now make an inventory with one command:

    Code:
    RulesInventory inv = new RulesInventory("Inventory Name").addRule(new Rule("Rule Name").setItem(Material.APPLE).addDescription("Rule Description Line 1")).openFor(getServer().getOnlinePlayers().toArray(new Player[getServer().getOnlinePlayers().size()]));
    This will create:
    • An Inventory called Inventory Name
    • With 1 Rule called Rule Name
    • The Rule's Icon is an Apple
    • The Rule's Description is Rule Description Line 1
    • All Players on the Server will Open the Inventory
    Please see below for the new methods.

    New Methods in 1.1

    Code:
    .openFor(Player[] players);
    Now, you can have multiple players open the inventory with just one line of code!

    Also, the
    Code:
    .addRule()
    and
    Code:
    .addMultipleRules()
    methods both return the Inventory so you can create a chain of methods like the one showed in the previous section!

    License

    DWYWCIDCJGC (Do what you want 'cause I don't care, just give credit)

     
    Last edited: Jul 16, 2015
  2. Offline

    Zombie_Striker

    @Th3Gam3rz
    This is a really neat idea.

    One thought though is "why can't you just use a folder full of .txt files/ lines in a config to store the rules?" I seems weird having one plugin set up an inventory, and another to actually add the rules.

    You can use the same idea that I had for my two plugins (Music and LobbyAPI), where inside the plugins datafolder, you create a sub folder that will store .txt files. In this case, the name of the .txt file would be the name of the rule, and the text in the file would be lore for the item.

    This was just my idea. It just feels weird to have to create your own, separate plugin in order to have the other plugin work.
     
  3. Offline

    Th3Gam3rz

    I really like this idea. I will have to look into possibilities of using text files or ymls and configuration sections. Expect an update to be out in a week or so.
     
  4. Offline

    chasechocolate

    @Th3Gam3rz an easier option (for the user at least) would be to make a builder class: Rule.builder("Rule Name").desc("Line 1", "Line 2").build().
     
  5. Offline

    Zombie_Striker

    @chasechocolate
    Well, for that, all he needs to do is turn all of his methods from voids to a method that returns whatever class he currently has. This isn't really that hard, and will save the amount of lines you would need to put down:

    @Th3Gam3rz
     
  6. Offline

    Th3Gam3rz

    Haven't I already done that with the Rule builder:
    Code:
    Rule rule = new Rule("Rule Name").setItem(Material.APPLE).addDescription("Lore Line 1").addDescription("Lore Line 2");
    @Zombie_Striker

    Sorry for double post but wanted announcement to stand out.

    Version 1.1 has been released for RulesInventory API.

    It contains new methods and also makes it easier to now create inventories!

    Please see the original post for new methods and tutorials.

    EDIT: Version 1.2 Should be out in the Next Few Days! is now out!

    It contains lots of new methods, some new classes, a custom exception and also allows you to pass conditions in the .openFor(Player[]) method!

    I have not tested it thoroughly though, so I recommend that you test your plugins that use it well before deploying any plugins.

    All of the methods have changed so the current tutorial is useless.

    I have uploaded it to Github but I do not have the time to update the tutorial, I will have the tutorial updated by 17/07/15


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page