Code Snippets/Classes

Discussion in 'Mod Development' started by Strikingwolf, Sep 19, 2014.

  1. Democretes

    Democretes New Member

    Generally when I use ForgeDirections, I'm comparing them, iterating through the list of directions, getting the opposite direction. When I use colors, I make them into strings using specific ones. I've never used them to compare colors or remove them. There's just very little use for it. I don't need an enum of colors to iterate through. I just need pre-defined colors as Strings. We're just going to have to agree to disagree here.
     
  2. squeek502

    squeek502 New Member

    Ok. Just to clarify my stance, I agree that there is a use case for some sort of FormattingHelper-type class that makes applying formatting codes easier. I'm not sure String constants are the best solution to that (though I recognize that they help a bit), but as long as EnumChatFormatting is used to define the String constants, I'm happy.
     
  3. Alexiy

    Alexiy Well-Known Member

    I've got a whole library but I don't think it is useful to put something here as the post is very big already. It might be interesting to have a modding wiki, though.
     
  4. Strikingwolf

    Strikingwolf New Member

    Yeah we do need a better way of handling this :p. However I think a community-driven wiki would be better than the minecraft-forge one. How about this, could you make a post in this section with your library in it and I will make a libraries section with links ;)
     
  5. Strikingwolf

    Strikingwolf New Member

    BTW can I have blanket permission to everything here? I was thinking of making say a core mod ;) And yes this may or may not have been a way to collect things people use for said core mod :p
     
  6. chbachman

    chbachman Guest

    You mean a library? Core mods change vanilla minecraft's code.
     
    SatanicSanta likes this.
  7. Strikingwolf

    Strikingwolf New Member

    Like CofhCore :p You get what I mean
     
  8. Lumaceon

    Lumaceon Popular Member

    You're probably thinking of iChunUtil.
     
  9. Gideonseymour

    Gideonseymour Relatable Gamer Trusted User Retired Staff

    Or CoFHlib
     
  10. SoraZodia

    SoraZodia Forum Ghost Third Party Pack Team Mod Developer

    For my code, sure
     
  11. Strikingwolf

    Strikingwolf New Member

    Shhh you know I don't know names of things :p
     
  12. Gideonseymour

    Gideonseymour Relatable Gamer Trusted User Retired Staff

    At least you know the difference between int and String
     
    SatanicSanta likes this.
  13. Strikingwolf

    Strikingwolf New Member

    Amen.[DOUBLEPOST=1413408449,1412913407][/DOUBLEPOST]Quoting for pings
    May I have the power to directly copy this code into a mod? Same for everyone else
     
    GreatOrator likes this.
  14. squeek502

    squeek502 New Member

  15. Strikingwolf

    Strikingwolf New Member

    GreatOrator likes this.
  16. ljfa

    ljfa New Member

    One thing about the LogHelper:
    The FMLLog.log method takes a string and a vararg list of objects and uses String.format internally. Why not make use of it since sometimes you want to print numbers and stuff into the log? My own LogHelper looks like this:
    Code:
    public class LogHelper {
       public static void log(Level logLevel, String format, Object... data) {
         FMLLog.log(Reference.MODNAME, logLevel, format, data);
       }
      
       public static void trace(String format, Object... data) {
         log(Level.TRACE, format, data);
       }
      
       public static void debug(String format, Object... data) {
         log(Level.DEBUG, format, data);
       }
      
       public static void info(String format, Object... data) {
         log(Level.INFO, format, data);
       }
      
       public static void warn(String format, Object... data) {
         log(Level.WARN, format, data);
       }
      
       public static void error(String format, Object... data) {
         log(Level.ERROR, format, data);
       }
      
       public static void fatal(String format, Object... data) {
         log(Level.FATAL, format, data);
       }
    }
    (public domain)
    And you can still print other objects into the log by using "%s" as format string. %s can actually take any Object as parameter.
     
  17. VapourDrive

    VapourDrive New Member

    These ideas of lib mods seem great but who really wants to use code they didn't write? I would much rather write code myself even if just for the experience and learning.
     
    SatanicSanta likes this.
  18. Strikingwolf

    Strikingwolf New Member

    Then why do frameworks exist?
     
  19. VapourDrive

    VapourDrive New Member

    Well its like the difference behind saying "I want to make my own gocart instead of buying one" and saying "I want to build my own car to drive". The go cart you throw together and enjoy the fields behind your house, but a car needs to meet intense criteria and you aren't the only person it may effect while on the road.
     
    immibis and SatanicSanta like this.
  20. ratchet freak

    ratchet freak Well-Known Member

    this is a better NBTUpgradeList Class
    Code:
    public class NBTList<E extends INBTAble> extends AbstractList<E> implements RandomAccess {
       public final NBTTagList list;
       
       public NBTList() {
         this.list = new NBTTagList();
       }
       
       public NBTList(NBTTagList list) {
         this.list = list;
       }
       
       public NBTList(NBTTagCompound nbt) {
         this.list = nbt.getTagList("List", Constants.NBT.TAG_COMPOUND);
       }
       
       public NBTTagCompound addNBTTagCompound(NBTTagCompound nbt) {
         nbt.setTag("UpgradeList", this.list);
         return nbt;
       }
       
       @Override
       public int size() {
         return this.list.tagCount();
       }
       
       private E getUpgrade(NBTTagCompound nbt) {
         try {
           // E upgrade = //TODO: add getFromNBTMethodHERE
           // return upgrade;
         } catch(IndexOutOfBoundsException e) {
           return null;
         }
       }
       
       @Override
       public E get(int index) {
         NBTTagCompound nbt = this.list.getCompoundTagAt(index);
         if(nbt == null) {
           return null;
         }
         E upgrade = this.getUpgrade(nbt);
         if(upgrade == null) {
           this.list.removeTag(index);
           return this.get(index);
         }
         return upgrade;
       }
       
       @Override
       public boolean add(int index, E upgrade) {
         this.list.appendTag(upgrade.getNBT());
         return true;
       }
       
       @Override
       public E set(int index, E element) {
         this.list.func_150304_a(index, element.getNBT());
         return element;
       }
       
       @Override
       public E remove(int index) {
         E upgrade = this.get(index);
         this.list.removeTag(index);
         return upgrade;
       }
    }
    

    Extends from the skeleton AbstractList<E> so only the get, set, remove and add methods need to be implemented
     
    chbachman likes this.

Share This Page