Advertisement
turt2live

WorldLauncher.java

Nov 5th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.41 KB | None | 0 0
  1. package mw;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.util.List;
  9. import java.util.logging.Logger;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.GameMode;
  14. import org.bukkit.Location;
  15. import org.bukkit.World;
  16. import org.bukkit.WorldCreator;
  17. import org.bukkit.command.Command;
  18. import org.bukkit.command.CommandSender;
  19. import org.bukkit.entity.Player;
  20. import org.bukkit.event.Event;
  21. import org.bukkit.inventory.ItemStack;
  22. import org.bukkit.plugin.PluginDescriptionFile;
  23. import org.bukkit.plugin.PluginManager;
  24. import org.bukkit.plugin.java.JavaPlugin;
  25.  
  26. public class WorldLauncher extends JavaPlugin{
  27.     public WorldLauncher plugin;
  28.     public final Logger logger = Logger.getLogger("Minecraft");
  29.    
  30.     public World PVP_WORLD;
  31.     public World SURVIVAL_WORLD;
  32.    
  33.     public final ServerBlockListener blockListener = new ServerBlockListener(this); //Not used
  34.     public final ServerPlayerListener playerListener = new ServerPlayerListener(this);
  35.    
  36.     public void onDisable() {
  37.         PluginDescriptionFile pdfFile = this.getDescription();
  38.         this.logger.info(pdfFile.getName() + " is now disabled.");
  39.     }
  40.  
  41.     public void onEnable() {
  42.         PluginManager pm = getServer().getPluginManager();
  43.         pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Highest, this);
  44.         PluginDescriptionFile pdfFile = this.getDescription();
  45.         this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
  46.         try{
  47.             // Load worlds
  48.             BufferedReader in = new BufferedReader(new FileReader(new File("worlds.txt")));
  49.             String line;
  50.             while((line = in.readLine()) != null){
  51.                 line = line.replaceAll("\\\r\\\n", "");
  52.                 Bukkit.createWorld(WorldCreator.name(line));
  53.             }
  54.             in.close();
  55.         }catch (Exception e){
  56.             ChatLog.log_error(e.getMessage());
  57.         }
  58.         try{
  59.             // Figure out PVP and Survival worlds (Used in game mode)
  60.             BufferedReader in = new BufferedReader(new FileReader(new File("plugins/MultiWorld/specworlds.txt")));
  61.             String line;
  62.             while((line = in.readLine()) != null){
  63.                 if(line.startsWith("PVP:")){
  64.                     String parts[] = line.replaceAll("\\\n\\\r", "").split(":");
  65.                     PVP_WORLD = Bukkit.getWorld(parts[parts.length-1]);
  66.                 }else if(line.startsWith("SURVIVAL:")){
  67.                     String parts[] = line.replaceAll("\\\n\\\r", "").split(":");
  68.                     SURVIVAL_WORLD = Bukkit.getWorld(parts[parts.length-1]);
  69.                 }
  70.             }
  71.             in.close();
  72.         }catch (Exception e){
  73.             ChatLog.log_error(e.getMessage());
  74.         }
  75.     }
  76.    
  77.     public boolean onCommand(CommandSender sender, Command cmdcmd, String cmd, String[] args){
  78.         Player player = (Player) sender;
  79.         List<World> worlds = Bukkit.getWorlds();
  80.         if(cmd.equalsIgnoreCase("warp")){  // Warps to a world
  81.             String gotoWorld = getAlias(args[0]);
  82.             port(gotoWorld, player);
  83.         }else{
  84.             if(player.isOp() && isAdmin(player)){ //Everything else requires admin rank
  85.                 if(cmd.equalsIgnoreCase("create")){  // Create world
  86.                     String worldName = args[0];
  87.                     if(worlds.contains(worldName)){  // Already exists?
  88.                         player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.RED + "World name already in use!");
  89.                     }else{  // ... Not yet, create it
  90.                         World created = Bukkit.getServer().createWorld(WorldCreator.name(worldName).seed(System.currentTimeMillis()));
  91.                         player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + created.getName() + " has been created.");
  92.                         try{
  93.                             // For loading (reload/onEnable)
  94.                             BufferedWriter out = new BufferedWriter(new FileWriter(new File("worlds.txt"), true));
  95.                             out.write(worldName+"\r\n");
  96.                             out.close();
  97.                         }catch (Exception e){
  98.                             ChatLog.log_error(e.getMessage());
  99.                         }
  100.                     }
  101.                 }else if(cmd.equalsIgnoreCase("w")){  // Simple command, can serve more purpose
  102.                         if(args.length > 0){
  103.                             if(args[0].equalsIgnoreCase("interact")){  // Create an interaction point (eg sign)
  104.                                 String world = player.getWorld().getName();
  105.                                 String toWorld = args[1];
  106.                                 String locationString = "";
  107.                                 Location target = player.getTargetBlock(null, 100).getLocation();
  108.                                 locationString = target.getX()+".."+target.getY()+".."+target.getZ()+".."+world;
  109.                                 try{
  110.                                     // Add point to file
  111.                                     File worldFile = new File("plugins/MultiWorld/"+toWorld+".PORTAL");
  112.                                     BufferedWriter out = new BufferedWriter(new FileWriter(worldFile, true));
  113.                                     out.write(locationString+"\r\n");
  114.                                     out.close();
  115.                                 }catch (Exception e){
  116.                                     ChatLog.log_error(e.getMessage());
  117.                                 }
  118.                                 player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.AQUA + "The interact location has been set.");
  119.                             }
  120.                         }else{
  121.                             player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "Help Menu: ");
  122.                             player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/warp <world>  - Warp to a world");
  123.                             player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/create <worldname>  - Create a world");
  124.                             player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/w interact <world>  - Create an interaction point for <world>");
  125.                         }
  126.                 }else{
  127.                     player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "Help Menu: ");
  128.                     player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/warp <world>  - Warp to a world");
  129.                     player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/create <worldname>  - Create a world");
  130.                     player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/w interact <world>  - Create an interaction point for <world>");
  131.                 }
  132.             }else{
  133.                 player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.RED + "You must be an OP to do that.");
  134.             }
  135.         }
  136.         return false;  //onCommand needs this
  137.     }
  138.    
  139.     public boolean isAdmin(Player player){  //Returns tru|false respectfully
  140.         boolean admin = false;
  141.         try{
  142.             File f = new File("admins.txt"); //Not the most secure...
  143.             BufferedReader in = new BufferedReader(new FileReader(f));
  144.             String line;
  145.             while ((line = in.readLine()) != null){
  146.                 if(line.toLowerCase().startsWith(player.getName().toLowerCase())){
  147.                     admin = true;
  148.                     break;
  149.                 }
  150.             }
  151.             in.close();
  152.         }catch (Exception e){
  153.             ChatLog.log_error(e.getMessage());
  154.         }
  155.         return admin;
  156.     }
  157.    
  158.     public String getAlias(String command){  //For easier typing of worlds
  159.         String world = command;
  160.         try{
  161.             BufferedReader in = new BufferedReader(new FileReader(new File("plugins/MultiWorld/alias.txt")));
  162.             String line;
  163.             while((line = in.readLine()) != null){
  164.                 line = line.replaceAll("\\\r\\\n", "");
  165.                 String parts[] = line.split("\\.");
  166.                 if(parts[1].equalsIgnoreCase(command)){
  167.                     world = parts[0];
  168.                     break;
  169.                 }
  170.             }
  171.             in.close();
  172.         }catch (Exception e){
  173.             ChatLog.log_error(e.getMessage());
  174.         }
  175.         return world;
  176.     }
  177.    
  178.     public void port(String gotoWorld, Player player){  // Teleport
  179.         boolean found = false;
  180.         String fromWorld = player.getWorld().getName();
  181.         gotoWorld = getAlias(gotoWorld);  // Get alias (from easy-to-type -> actual name)
  182.         checkGameMode(player, gotoWorld);  // Some worlds restrict Survival and Creative modes
  183.         if(!gotoWorld.equals("") && gotoWorld != null){  // Did you pass me a world name?
  184.             List<World> worlds = Bukkit.getWorlds();
  185.             for(World w : worlds){ // See if there IS a world with that name
  186.                 if(w.getName().equalsIgnoreCase(gotoWorld)){
  187.                     player.teleport(w.getSpawnLocation());
  188.                     found = true;
  189.                 }
  190.             }
  191.             if(!found){
  192.                 player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.RED + gotoWorld + " is not a world.");
  193.             }else{  // World exists (after teleport)
  194.                 inventory(player, gotoWorld, fromWorld);  // Change inventory (ERROR HERE)
  195.             }
  196.         }
  197.     }
  198.    
  199.     public void checkGameMode(Player player, String world){  // Game mode check
  200.         if(world.equalsIgnoreCase(PVP_WORLD.getName()) || world.equalsIgnoreCase(SURVIVAL_WORLD.getName())){
  201.             if(!isBuilder(player)){
  202.                 if(!player.isOp()){
  203.                     player.setGameMode(GameMode.SURVIVAL);  // Put you on the ground
  204.                 }
  205.             }
  206.         }
  207.     }
  208.    
  209.     public boolean isBuilder(Player player){  // Based off another plugin
  210.         boolean builder = false;
  211.         try{
  212.             // Read players and see if they have "GLOBAL" rights, making them a Builder
  213.             BufferedReader in = new BufferedReader(new FileReader(new File("plugins/AMS_GrantedPlayers/"+player.getName()+".PLAYER")));
  214.             String line;
  215.             while((line = in.readLine()) != null){
  216.                 if(line.toLowerCase().startsWith("global")){
  217.                     builder = true;
  218.                     break;
  219.                 }
  220.             }
  221.             in.close();
  222.         }catch (Exception e){
  223.             ChatLog.log_error(e.getMessage());
  224.         }
  225.         return builder;
  226.     }
  227.  
  228.     // The error is somewhere here
  229.     public void inventory(Player player, String world, String fromWorld){
  230.         File inventoryFile = new File("plugins/Inventory/"+player.getName()+"_"+world+".INVENTORY");
  231.         if(!inventoryFile.exists()){ //Check for inventory file
  232.             try{
  233.                 //Populate it (if it doesn't exist)
  234.                 BufferedWriter out = new BufferedWriter(new FileWriter(inventoryFile, false));
  235.                 for(int i=0;i<36;i++){
  236.                     out.write(i+"..0..0..0\r\n");
  237.                 }
  238.                 out.write("BOOTS..0..0..0\r\n");
  239.                 out.write("CHEST..0..0..0\r\n");
  240.                 out.write("LEGS..0..0..0\r\n");
  241.                 out.write("HELM..0..0..0\r\n");
  242.                 out.close();
  243.             }catch (Exception e){
  244.                 ChatLog.log_error(e.getMessage());
  245.             }
  246.         }
  247.         try{
  248.             // Load current inventory into file
  249.             BufferedWriter out = new BufferedWriter(new FileWriter(new File("plugins/Inventory/"+player.getName()+"_"+fromWorld+".INVENTORY"), false));
  250.             for(int i=0;i<36;i++){
  251.                 ItemStack item = player.getInventory().getItem(i);
  252.                 int slot = i;
  253.                 int ID = 0;
  254.                 int amount = 0;
  255.                 int durability = 0;
  256.                 if(item!=null){
  257.                     ID = item.getTypeId();
  258.                     amount = item.getAmount();
  259.                     durability = item.getDurability();
  260.                 }
  261.                 out.write(slot+".."+ID+".."+amount+".."+durability+"\r\n");
  262.             }
  263.             // Do armor
  264.             ItemStack item = player.getInventory().getBoots();
  265.             out.write("BOOTS.."+item.getTypeId()+".."+item.getAmount()+".."+item.getDurability()+"\r\n");
  266.             item = player.getInventory().getChestplate();
  267.             out.write("CHEST.."+item.getTypeId()+".."+item.getAmount()+".."+item.getDurability()+"\r\n");
  268.             item = player.getInventory().getLeggings();
  269.             out.write("LEGS.."+item.getTypeId()+".."+item.getAmount()+".."+item.getDurability()+"\r\n");
  270.             item = player.getInventory().getHelmet();
  271.             out.write("HELM.."+item.getTypeId()+".."+item.getAmount()+".."+item.getDurability()+"\r\n");
  272.             out.close();
  273.         }catch (Exception e){
  274.             ChatLog.log_error(e.getMessage());
  275.         }
  276.         // Wipe inventory (for my purposes)
  277.         for(int i=0;i<36;i++){
  278.             player.getInventory().setItem(i, new ItemStack(0, 0));
  279.         }
  280.         // Wipe armor off
  281.         player.getInventory().setBoots(new ItemStack(0, 0));
  282.         player.getInventory().setChestplate(new ItemStack(0, 0));
  283.         player.getInventory().setLeggings(new ItemStack(0, 0));
  284.         player.getInventory().setHelmet(new ItemStack(0, 0));
  285.         try{
  286.             // Load new inventory
  287.             BufferedReader in = new BufferedReader(new FileReader(inventoryFile));
  288.             String line;
  289.             while((line = in.readLine()) != null){
  290.                 // Split line to it's parts
  291.                 String item[] = line.replaceAll("\\\r\\\n", "").split("\\.\\.");
  292.                 if(!item[0].equalsIgnoreCase("boots") &&
  293.                     !item[0].equalsIgnoreCase("chest") &&
  294.                     !item[0].equalsIgnoreCase("legs") &&
  295.                     !item[0].equalsIgnoreCase("helm")){ //EndIfCall
  296.                     //Is Item...
  297.                     int slot = Integer.parseInt(item[0]);
  298.                     int ID = Integer.parseInt(item[1]);
  299.                     int amount = Integer.parseInt(item[2]);
  300.                     short durability = Short.parseShort(item[3]);
  301.                     ItemStack invItem = new ItemStack(ID, amount);
  302.                     invItem.setDurability(durability);
  303.                     player.getInventory().setItem(slot, invItem);  
  304.                 }else{ //Is armor
  305.                     if(item[0].equalsIgnoreCase("boots")){
  306.                         int ID = Integer.parseInt(item[1]);
  307.                         int amount = Integer.parseInt(item[2]);
  308.                         short durability = Short.parseShort(item[3]);
  309.                         ItemStack invItem = new ItemStack(ID, amount);
  310.                         invItem.setDurability(durability);
  311.                         player.getInventory().setBoots(invItem);
  312.                     }else if(item[0].equalsIgnoreCase("chest")){
  313.                         int ID = Integer.parseInt(item[1]);
  314.                         int amount = Integer.parseInt(item[2]);
  315.                         short durability = Short.parseShort(item[3]);
  316.                         ItemStack invItem = new ItemStack(ID, amount);
  317.                         invItem.setDurability(durability);
  318.                         player.getInventory().setChestplate(invItem);
  319.                     }else if(item[0].equalsIgnoreCase("legs")){
  320.                         int ID = Integer.parseInt(item[1]);
  321.                         int amount = Integer.parseInt(item[2]);
  322.                         short durability = Short.parseShort(item[3]);
  323.                         ItemStack invItem = new ItemStack(ID, amount);
  324.                         invItem.setDurability(durability);
  325.                         player.getInventory().setLeggings(invItem);
  326.                     }else if(item[0].equalsIgnoreCase("helm")){
  327.                         int ID = Integer.parseInt(item[1]);
  328.                         int amount = Integer.parseInt(item[2]);
  329.                         short durability = Short.parseShort(item[3]);
  330.                         ItemStack invItem = new ItemStack(ID, amount);
  331.                         invItem.setDurability(durability);
  332.                         player.getInventory().setHelmet(invItem);
  333.                     }
  334.                 }
  335.             }
  336.             in.close();
  337.         }catch (Exception e){
  338.             ChatLog.log_error(e.getMessage());
  339.         }
  340.         //End inventory,
  341.         /*
  342.             The code can reach all of the code just fine, but does not give the right inventory (in fact it stays wiped). The code also throws no errors if I were to spam this thing with debug statements.
  343.         */
  344.     }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement