Useful Snippets

Discussion in 'Resources' started by KeybordPiano459, Oct 6, 2012.

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

    KeybordPiano459

    I'm making a list of snippets! Please post some in the comments, ill put them up here.

    KeybordPiano459
    StringBuilder (Make all args one string) (open)
    StringBuilder str = new StringBuilder(args[0]);
    for(int i = 1; i < args.length; i++) {
    str.append(' ').append(args);
    }

    Update Notifier (Notify whenever there's an update) (open)
    Replace the comments accordingly, and also in the URL it should be a .txt file and the only thing that should be in it is the version, such as 1.0.4 or 1.3.
    try {
    URL url = new URL("THE-URL");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String string;
    while ((string = in.readLine()) != null) {
    int current = Integer.parseInt(Bukkit.getServer().getPluginManager().getPlugin("YOUR-PLUGIN").getDescription().getVersion().replace(".", ""));
    int new = Integer.parseInt(string.replace(".", ""));
    if (current < new) {
    // NEW VERSION!
    } else {
    // THERE ISNT A NEW VERSION!
    }
    }
    in.close();
    } catch (MalformedURLException e) {
    // URL IS INVALID!
    } catch (IOException e) {
    // BAD READ/WRITE!
    }


    Lolmewn
    MySQL Update to MySQL Insert (open)
     
  2. Offline

    KeybordPiano459

    Reserved for the most awesome snippet :D
     
  3. Offline

    Lolmewn

    Rewriting an MySQL UPDATE statement into a MySQL INSERT statement:
    http://pastebin.com/ks3FeDGK
    Not sure if useful to anyone, but cool anyway :p
     
  4. Offline

    KeybordPiano459

    Lolmewn
    Nice! Do you mind if you or I put it in a BukkitDev paste?
     
  5. Offline

    Lolmewn

    Oh, I don't mind if you do ^^
     
  6. Offline

    KeybordPiano459

    Update notifier ^^
    Do whatever you want with it, such as on player login send them a message, or anything else :)
     
  7. Offline

    Cirno

    For those out there who use custom files:
    Code:JAVA
    1. public void initializeFile() throws IOException{
    2. File f = new File(yourjavapluginclass.getDataFolder().getAbsoluteFile(), "/somefilename.somefilextension");
    3. File folder = new File(yourjavapluginclass.getDataFolder().getAbsoluteFile().getAbsolutePath());
    4. if(!folder.exists()){
    5. folder.mkdir();
    6. }
    7. if(!f.exists()){
    8. f.createNewFile();
    9. }
    10. }
     
    hawkfalcon likes this.
  8. Offline

    KeybordPiano459

    Oops. Well I got it from my plugin and not your thread :) I can even show you the source code I got it from, it's on Github.
     
  9. Ok, I believe you haha
     
  10. Want to only trigger move events when players move to a new block and not just twitch/look around?
    Code:
    public void onPlayerMove(PlayerMoveEvent event) {
            if(event.isCancelled()==false &&
                    (event.getTo().getBlockX() != event.getFrom().getBlockX() ||
                    event.getTo().getBlockY() != event.getFrom().getBlockY() ||
                    event.getTo().getBlockZ() != event.getFrom().getBlockZ())){
           
            //CODE GOES HERE
            }
    }
     
    kroltan likes this.
  11. Offline

    stirante

    Method which will change skin of Skull:
    Code:java
    1.  
    2. public ItemStack setSkin(ItemStack item, String nick){
    3. CraftItemStack craftStack = null;
    4. net.minecraft.server.ItemStack itemStack = null;
    5. if (item instanceof CraftItemStack) {
    6. craftStack = (CraftItemStack) item;
    7. itemStack = craftStack.getHandle();
    8. }
    9. else if (item instanceof ItemStack) {
    10. craftStack = new CraftItemStack(item);
    11. itemStack = craftStack.getHandle();
    12. }
    13. NBTTagCompound tag = itemStack.tag;
    14. if (tag == null) {
    15. tag = new NBTTagCompound();
    16. }
    17. tag.setString("SkullOwner", nick);
    18. itemStack.tag.setCompound("display", tag);
    19. itemStack.tag = tag;
    20. return craftStack;
    21. }
     
Thread Status:
Not open for further replies.

Share This Page