bspkrs

mod_startingInventory.java

Oct 1st, 2012
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.25 KB | None | 0 0
  1. package net.minecraft.src;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.net.URL;
  7. import java.util.*;
  8. import java.util.logging.Level;
  9.  
  10. import net.minecraft.client.Minecraft;
  11.  
  12. public class mod_startingInventory extends BaseMod
  13. {
  14.     @MLProp(info="Set to true to allow checking for mod updates, false to disable")
  15.     public static boolean allowUpdateCheck = true;
  16.     @MLProp(info="The length of time in game time ticks (1/20th of a second) that items are allowed to spawn in a fresh world.  If you are on a slower machine and are having trouble with items not spawning, try setting this a little higher\n\n**ONLY EDIT WHAT IS BELOW THIS**")
  17.     public static int tickWindow = 100;
  18.    
  19.     private boolean checkUpdate;
  20.     boolean canGiveItems;
  21.     String fileName;
  22.     File mcdir;
  23.     File file;
  24.     private Scanner scan;
  25.     private List list;
  26.  
  27.     public mod_startingInventory()
  28.     {
  29.         fileName = "startingInventory";
  30.         mcdir = Minecraft.getMinecraftDir();
  31.         file = new File(mcdir, (new StringBuilder()).append("/mods/daftpvf/").append(fileName).append(".txt").toString());
  32.         list = new ArrayList();
  33.         list.clear();
  34.         if(!file.exists() || scan == null)
  35.         {
  36.             createFile();
  37.         }
  38.         readItems();
  39.         checkUpdate = allowUpdateCheck;
  40.     }
  41.  
  42.     @Override
  43.     public String getVersion()
  44.     {
  45.         return "1.3.2.b modified by bspkrs";
  46.     }
  47.  
  48.     @Override
  49.     public void load()
  50.     {
  51.         if(!isCurrentVersion(ModLoader.getMinecraftInstance()))
  52.             ModLoader.getLogger().log(Level.INFO, "Your version of StartingInventory is out of date! Visit http://www.minecraftforum.net/topic/1009577- for the latest version.");
  53.     }
  54.  
  55.     @Override
  56.     public boolean onTickInGame(float f, Minecraft mc)
  57.     {
  58.         if(canGiveItems && mc.isSingleplayer() && isFreshWorld(mc) && isPlayerInventoryEmpty(mc.thePlayer))
  59.             addItems();
  60.         canGiveItems = false;
  61.        
  62.         if(checkUpdate)
  63.         {
  64.             if(!isCurrentVersion(mc))
  65.             {
  66.                 mc.thePlayer.addChatMessage("\247cYour version of StartingInventory is out of date!");
  67.                 mc.thePlayer.addChatMessage("\247cVisit http://www.minecraftforum.net/topic/1009577- for the latest version.");
  68.             }
  69.             checkUpdate = false;
  70.         }
  71.            
  72.         return false;
  73.     }
  74.  
  75.     @Override
  76.     public void clientConnect(NetClientHandler nch)
  77.     {
  78.         ModLoader.setInGameHook(this, true, true);
  79.         canGiveItems = true;
  80.     }
  81.    
  82.     private boolean isCurrentVersion(Minecraft minecraft)
  83.     {
  84.         try
  85.         {
  86.             String[] version = loadTextFromURL(new URL("https://dl.dropbox.com/u/20748481/Minecraft/1.3.1/startingInventory.version"));
  87.             return version[0].equalsIgnoreCase(getVersion());
  88.         }
  89.         catch(Throwable e)
  90.         {
  91.             ModLoader.getLogger().log(Level.WARNING, "Error getting current version info: " + e.getStackTrace());
  92.             return true;
  93.         }
  94.     }
  95.  
  96.     private String[] loadTextFromURL(URL url)
  97.     {
  98.         ArrayList arraylist = new ArrayList();
  99.         Scanner scanner = null;
  100.         try
  101.         {
  102.             scanner = new Scanner(url.openStream(), "UTF-8");
  103.         }
  104.         catch(Throwable e) {
  105.             ModLoader.getLogger().log(Level.WARNING, "Error getting current version info: " + e.getStackTrace());
  106.             return new String[] {""};
  107.         }
  108.         while(scanner.hasNextLine())
  109.         {
  110.             arraylist.add(scanner.nextLine());
  111.         }
  112.         scanner.close();
  113.         return (String[])arraylist.toArray(new String[arraylist.size()]);
  114.     }
  115.    
  116.     public boolean isFreshWorld(Minecraft mc)
  117.     {
  118.         try
  119.         {
  120.             return (int)mc.theWorld.worldInfo.getWorldTime() < tickWindow;
  121.         }
  122.         catch(Exception e)
  123.         {
  124.             return false;
  125.         }
  126.     }
  127.    
  128.     public boolean isPlayerInventoryEmpty(EntityClientPlayerMP player)
  129.     {
  130.         try
  131.         {
  132.             for(int i = 0; i < player.inventory.mainInventory.length; i++)
  133.                 if(player.inventory.mainInventory[i] != null)
  134.                     return false;
  135.             return true;
  136.         }
  137.         catch(Exception e)
  138.         {
  139.             return false;
  140.         }
  141.     }
  142.  
  143.     public void addItems()
  144.     {
  145.         for(int i = 0; i < list.size(); i++)
  146.         {
  147.             addItem((String)list.get(i));
  148.         }
  149.  
  150.     }
  151.  
  152.     private void addItem(String s)
  153.     {
  154.         int j = 1;
  155.         int k = 0;
  156.         int l = s.indexOf(',');
  157.         int i1 = s.indexOf(',', l + 1);
  158.         int i;
  159.         if(l != -1)
  160.         {
  161.             i = parseInt(s.substring(0, l));
  162.             if(i1 != -1)
  163.             {
  164.                 j = parseInt(s.substring(l + 1, i1));
  165.                 k = parseInt(s.substring(i1 + 1));
  166.             } else
  167.             {
  168.                 j = parseInt(s.substring(l + 1));
  169.             }
  170.         } else
  171.         {
  172.             i = parseInt(s);
  173.         }
  174.         if(Item.itemsList[i] == null)
  175.         {
  176.             return;
  177.         } else
  178.         {
  179.             Minecraft mc = ModLoader.getMinecraftInstance();
  180.             //ModLoader.getMinecraftInstance().thePlayer.inventory.addItemStackToInventory(new ItemStack(i, j, k));
  181.             mc.getIntegratedServer().executeCommand("give " + mc.thePlayer.username + " " + i + " " + j + " " + k);
  182.             return;
  183.         }
  184.     }
  185.  
  186.     public void readItems()
  187.     {
  188.         if(scan != null)
  189.         {
  190.             for(; scan.hasNextLine(); list.add(scan.nextLine())) { }
  191.         }
  192.         scan.close();
  193.     }
  194.  
  195.     public int parseInt(String s)
  196.     {
  197.         try
  198.         {
  199.             return Integer.parseInt(s.trim());
  200.         }
  201.         catch(NumberFormatException numberformatexception)
  202.         {
  203.             return 0;
  204.         }
  205.     }
  206.  
  207.     public void createFile()
  208.     {
  209.         File file1 = new File(mcdir, "/mods/daftpvf/");
  210.         if(!file1.exists() && file1.mkdir())
  211.         {
  212.             file = new File(file1, (new StringBuilder()).append(fileName).append(".txt").toString());
  213.         }
  214.         else
  215.         {
  216.             file = new File(file1, (new StringBuilder()).append(fileName).append(".txt").toString());
  217.         }
  218.         try
  219.         {
  220.             file.createNewFile();
  221.             scan = new Scanner(file);
  222.         }
  223.         catch(Exception exception) { }
  224.     }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment