Cool Code Snippets =3

Discussion in 'Resources' started by KeybordPiano459, Feb 7, 2013.

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

    KeybordPiano459

    As a little bukkitteer (about seven months ago), when I started developing bukkit plugins, there were many secrets that I could never figure out, but know how to use really well now. If anyone has other snippets that could go here, just comment below and I'll work something out :) By the way, better title... anyone?

    1. Converting '&' symbols to ChatColors
    This happened to be so easy that I felt really dumb the moment I figured this one out...
    Code:java
    1. String colorstr = "&4Hi!";
    2. ChatColor.translateAlternateColorCodes('&', colorstr);


    2. Getting comments in between the lines in my config.yml
    For this one, I just used basic java functions, opening up to my config.yml with a FileWriter, and writing the file line by line. If you want, you can replace the methods (getConfig(), reloadConfig()) with these by adding them, and putting these methods in there, and then putting the @Override annotation above those methods.
    http://pastebin.com/MfXGareh

    3. Check if a player is within two locations
    Thanks to Ugleh for this one, it seems like something I could've used awhile ago :)
    http://pastebin.com/PxfuD2sM

    4. Iterate through blocks between two locations
    Thanks again to Ugleh for this one, it's kinda like the thing they use in worldedit to set and replace a bunch of blocks with the wand...
    http://pastebin.com/ZGBjzJCs



    Is there anything else that someone feels should go here? If so, just comment :)

    Reserved

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

    Ugleh

    Just title it "Code Snippets"
    Here is one.
    Check if player is within 2 locations
    First we turn the 2 locations into a CubedArea and then we do the check,
    Code:java
    1.  
    2. public boolean playerWithin(Location l1, Location l2, Location pLoc) {
    3. int x1 = Math.min(l1.getBlockX(), l2.getBlockX());
    4. int y1 = Math.min(l1.getBlockY(), l2.getBlockY());
    5. int z1 = Math.min(l1.getBlockZ(), l2.getBlockZ());
    6. int x2 = Math.max(l1.getBlockX(), l2.getBlockX());
    7. int y2 = Math.max(l1.getBlockY(), l2.getBlockY());
    8. int z2 = Math.max(l1.getBlockZ(), l2.getBlockZ());
    9. l1 = new Location(l1.getWorld(), x1, y1, z1);
    10. l2 = new Location(l2.getWorld(), x2, y2, z2);
    11.  
    12. return pLoc.getBlockX() >= l1.getBlockX()
    13. && pLoc.getBlockX() <= l2.getBlockX()
    14. && pLoc.getBlockY() >= l1.getBlockY()
    15. && pLoc.getBlockY() <= l2.getBlockY()
    16. && pLoc.getBlockZ() >= l1.getBlockZ()
    17. && pLoc.getBlockZ() <= l2.getBlockZ();
    18. }
    19. }
     
  3. Offline

    KeybordPiano459

    Thanks for the snippet, I added it :D
     
  4. Offline

    chasechocolate

    Cool, hope to use it in one of my plugins!
     
  5. Offline

    Ugleh

    Iterate through blocks between 2 locations.
    Code:
    public void loopThrough(Location loc1, Location loc2, World w) {
     
    int minx = Math.min(loc1.getBlockX(), loc2.getBlockX()),
    miny = Math.min(loc1.getBlockY(), loc2.getBlockY()),
    minz = Math.min(loc1.getBlockZ(), loc2.getBlockZ()),
    maxx = Math.max(loc1.getBlockX(), loc2.getBlockX()),
    maxy = Math.max(loc1.getBlockY(), loc2.getBlockY()),
    maxz = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
    for(int x = minx; x<=maxx;x++){
    for(int y = miny; y<=maxy;y++){
    for(int z = minz; z<=maxz;z++){
    Block b = w.getBlockAt(x, y, z);
    //do somthing productive with b...
    }
    }
    }
    }
     
    KeybordPiano459 likes this.
  6. Offline

    chasechocolate

    Ugleh No need for the world parameter, you can get it with:
    Code:java
    1. World world = loc<1/2>.getWorld();
     
  7. Offline

    Ugleh

    what does this do?
    Code:
    loc<1/2>
    do you just mean loc1 or loc2 or is that an actual syntax for something?
     
    finalblade1234 likes this.
  8. Offline

    chasechocolate

    No I meant either:
    Code:java
    1. World world = loc1.getWorld();
    2. //Or:
    3. World world = loc2.getWorld();
     
  9. Offline

    KeybordPiano459

    That one is really cool, I feel like I should use it! :p Thanks, it should be added shortly...
     
Thread Status:
Not open for further replies.

Share This Page