Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mw;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.util.List;
- import java.util.logging.Logger;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.GameMode;
- import org.bukkit.Location;
- import org.bukkit.World;
- import org.bukkit.WorldCreator;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.event.Event;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- public class WorldLauncher extends JavaPlugin{
- public WorldLauncher plugin;
- public final Logger logger = Logger.getLogger("Minecraft");
- public World PVP_WORLD;
- public World SURVIVAL_WORLD;
- public final ServerBlockListener blockListener = new ServerBlockListener(this); //Not used
- public final ServerPlayerListener playerListener = new ServerPlayerListener(this);
- public void onDisable() {
- PluginDescriptionFile pdfFile = this.getDescription();
- this.logger.info(pdfFile.getName() + " is now disabled.");
- }
- public void onEnable() {
- PluginManager pm = getServer().getPluginManager();
- pm.registerEvent(Event.Type.PLAYER_INTERACT, this.playerListener, Event.Priority.Highest, this);
- PluginDescriptionFile pdfFile = this.getDescription();
- this.logger.info(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
- try{
- // Load worlds
- BufferedReader in = new BufferedReader(new FileReader(new File("worlds.txt")));
- String line;
- while((line = in.readLine()) != null){
- line = line.replaceAll("\\\r\\\n", "");
- Bukkit.createWorld(WorldCreator.name(line));
- }
- in.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- try{
- // Figure out PVP and Survival worlds (Used in game mode)
- BufferedReader in = new BufferedReader(new FileReader(new File("plugins/MultiWorld/specworlds.txt")));
- String line;
- while((line = in.readLine()) != null){
- if(line.startsWith("PVP:")){
- String parts[] = line.replaceAll("\\\n\\\r", "").split(":");
- PVP_WORLD = Bukkit.getWorld(parts[parts.length-1]);
- }else if(line.startsWith("SURVIVAL:")){
- String parts[] = line.replaceAll("\\\n\\\r", "").split(":");
- SURVIVAL_WORLD = Bukkit.getWorld(parts[parts.length-1]);
- }
- }
- in.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- }
- public boolean onCommand(CommandSender sender, Command cmdcmd, String cmd, String[] args){
- Player player = (Player) sender;
- List<World> worlds = Bukkit.getWorlds();
- if(cmd.equalsIgnoreCase("warp")){ // Warps to a world
- String gotoWorld = getAlias(args[0]);
- port(gotoWorld, player);
- }else{
- if(player.isOp() && isAdmin(player)){ //Everything else requires admin rank
- if(cmd.equalsIgnoreCase("create")){ // Create world
- String worldName = args[0];
- if(worlds.contains(worldName)){ // Already exists?
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.RED + "World name already in use!");
- }else{ // ... Not yet, create it
- World created = Bukkit.getServer().createWorld(WorldCreator.name(worldName).seed(System.currentTimeMillis()));
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + created.getName() + " has been created.");
- try{
- // For loading (reload/onEnable)
- BufferedWriter out = new BufferedWriter(new FileWriter(new File("worlds.txt"), true));
- out.write(worldName+"\r\n");
- out.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- }
- }else if(cmd.equalsIgnoreCase("w")){ // Simple command, can serve more purpose
- if(args.length > 0){
- if(args[0].equalsIgnoreCase("interact")){ // Create an interaction point (eg sign)
- String world = player.getWorld().getName();
- String toWorld = args[1];
- String locationString = "";
- Location target = player.getTargetBlock(null, 100).getLocation();
- locationString = target.getX()+".."+target.getY()+".."+target.getZ()+".."+world;
- try{
- // Add point to file
- File worldFile = new File("plugins/MultiWorld/"+toWorld+".PORTAL");
- BufferedWriter out = new BufferedWriter(new FileWriter(worldFile, true));
- out.write(locationString+"\r\n");
- out.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.AQUA + "The interact location has been set.");
- }
- }else{
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "Help Menu: ");
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/warp <world> - Warp to a world");
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/create <worldname> - Create a world");
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/w interact <world> - Create an interaction point for <world>");
- }
- }else{
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "Help Menu: ");
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/warp <world> - Warp to a world");
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/create <worldname> - Create a world");
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.GREEN + "/w interact <world> - Create an interaction point for <world>");
- }
- }else{
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.RED + "You must be an OP to do that.");
- }
- }
- return false; //onCommand needs this
- }
- public boolean isAdmin(Player player){ //Returns tru|false respectfully
- boolean admin = false;
- try{
- File f = new File("admins.txt"); //Not the most secure...
- BufferedReader in = new BufferedReader(new FileReader(f));
- String line;
- while ((line = in.readLine()) != null){
- if(line.toLowerCase().startsWith(player.getName().toLowerCase())){
- admin = true;
- break;
- }
- }
- in.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- return admin;
- }
- public String getAlias(String command){ //For easier typing of worlds
- String world = command;
- try{
- BufferedReader in = new BufferedReader(new FileReader(new File("plugins/MultiWorld/alias.txt")));
- String line;
- while((line = in.readLine()) != null){
- line = line.replaceAll("\\\r\\\n", "");
- String parts[] = line.split("\\.");
- if(parts[1].equalsIgnoreCase(command)){
- world = parts[0];
- break;
- }
- }
- in.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- return world;
- }
- public void port(String gotoWorld, Player player){ // Teleport
- boolean found = false;
- String fromWorld = player.getWorld().getName();
- gotoWorld = getAlias(gotoWorld); // Get alias (from easy-to-type -> actual name)
- checkGameMode(player, gotoWorld); // Some worlds restrict Survival and Creative modes
- if(!gotoWorld.equals("") && gotoWorld != null){ // Did you pass me a world name?
- List<World> worlds = Bukkit.getWorlds();
- for(World w : worlds){ // See if there IS a world with that name
- if(w.getName().equalsIgnoreCase(gotoWorld)){
- player.teleport(w.getSpawnLocation());
- found = true;
- }
- }
- if(!found){
- player.sendMessage(ChatColor.GOLD + "[WORLDS] " + ChatColor.RED + gotoWorld + " is not a world.");
- }else{ // World exists (after teleport)
- inventory(player, gotoWorld, fromWorld); // Change inventory (ERROR HERE)
- }
- }
- }
- public void checkGameMode(Player player, String world){ // Game mode check
- if(world.equalsIgnoreCase(PVP_WORLD.getName()) || world.equalsIgnoreCase(SURVIVAL_WORLD.getName())){
- if(!isBuilder(player)){
- if(!player.isOp()){
- player.setGameMode(GameMode.SURVIVAL); // Put you on the ground
- }
- }
- }
- }
- public boolean isBuilder(Player player){ // Based off another plugin
- boolean builder = false;
- try{
- // Read players and see if they have "GLOBAL" rights, making them a Builder
- BufferedReader in = new BufferedReader(new FileReader(new File("plugins/AMS_GrantedPlayers/"+player.getName()+".PLAYER")));
- String line;
- while((line = in.readLine()) != null){
- if(line.toLowerCase().startsWith("global")){
- builder = true;
- break;
- }
- }
- in.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- return builder;
- }
- // The error is somewhere here
- public void inventory(Player player, String world, String fromWorld){
- File inventoryFile = new File("plugins/Inventory/"+player.getName()+"_"+world+".INVENTORY");
- if(!inventoryFile.exists()){ //Check for inventory file
- try{
- //Populate it (if it doesn't exist)
- BufferedWriter out = new BufferedWriter(new FileWriter(inventoryFile, false));
- for(int i=0;i<36;i++){
- out.write(i+"..0..0..0\r\n");
- }
- out.write("BOOTS..0..0..0\r\n");
- out.write("CHEST..0..0..0\r\n");
- out.write("LEGS..0..0..0\r\n");
- out.write("HELM..0..0..0\r\n");
- out.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- }
- try{
- // Load current inventory into file
- BufferedWriter out = new BufferedWriter(new FileWriter(new File("plugins/Inventory/"+player.getName()+"_"+fromWorld+".INVENTORY"), false));
- for(int i=0;i<36;i++){
- ItemStack item = player.getInventory().getItem(i);
- int slot = i;
- int ID = 0;
- int amount = 0;
- int durability = 0;
- if(item!=null){
- ID = item.getTypeId();
- amount = item.getAmount();
- durability = item.getDurability();
- }
- out.write(slot+".."+ID+".."+amount+".."+durability+"\r\n");
- }
- // Do armor
- ItemStack item = player.getInventory().getBoots();
- out.write("BOOTS.."+item.getTypeId()+".."+item.getAmount()+".."+item.getDurability()+"\r\n");
- item = player.getInventory().getChestplate();
- out.write("CHEST.."+item.getTypeId()+".."+item.getAmount()+".."+item.getDurability()+"\r\n");
- item = player.getInventory().getLeggings();
- out.write("LEGS.."+item.getTypeId()+".."+item.getAmount()+".."+item.getDurability()+"\r\n");
- item = player.getInventory().getHelmet();
- out.write("HELM.."+item.getTypeId()+".."+item.getAmount()+".."+item.getDurability()+"\r\n");
- out.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- // Wipe inventory (for my purposes)
- for(int i=0;i<36;i++){
- player.getInventory().setItem(i, new ItemStack(0, 0));
- }
- // Wipe armor off
- player.getInventory().setBoots(new ItemStack(0, 0));
- player.getInventory().setChestplate(new ItemStack(0, 0));
- player.getInventory().setLeggings(new ItemStack(0, 0));
- player.getInventory().setHelmet(new ItemStack(0, 0));
- try{
- // Load new inventory
- BufferedReader in = new BufferedReader(new FileReader(inventoryFile));
- String line;
- while((line = in.readLine()) != null){
- // Split line to it's parts
- String item[] = line.replaceAll("\\\r\\\n", "").split("\\.\\.");
- if(!item[0].equalsIgnoreCase("boots") &&
- !item[0].equalsIgnoreCase("chest") &&
- !item[0].equalsIgnoreCase("legs") &&
- !item[0].equalsIgnoreCase("helm")){ //EndIfCall
- //Is Item...
- int slot = Integer.parseInt(item[0]);
- int ID = Integer.parseInt(item[1]);
- int amount = Integer.parseInt(item[2]);
- short durability = Short.parseShort(item[3]);
- ItemStack invItem = new ItemStack(ID, amount);
- invItem.setDurability(durability);
- player.getInventory().setItem(slot, invItem);
- }else{ //Is armor
- if(item[0].equalsIgnoreCase("boots")){
- int ID = Integer.parseInt(item[1]);
- int amount = Integer.parseInt(item[2]);
- short durability = Short.parseShort(item[3]);
- ItemStack invItem = new ItemStack(ID, amount);
- invItem.setDurability(durability);
- player.getInventory().setBoots(invItem);
- }else if(item[0].equalsIgnoreCase("chest")){
- int ID = Integer.parseInt(item[1]);
- int amount = Integer.parseInt(item[2]);
- short durability = Short.parseShort(item[3]);
- ItemStack invItem = new ItemStack(ID, amount);
- invItem.setDurability(durability);
- player.getInventory().setChestplate(invItem);
- }else if(item[0].equalsIgnoreCase("legs")){
- int ID = Integer.parseInt(item[1]);
- int amount = Integer.parseInt(item[2]);
- short durability = Short.parseShort(item[3]);
- ItemStack invItem = new ItemStack(ID, amount);
- invItem.setDurability(durability);
- player.getInventory().setLeggings(invItem);
- }else if(item[0].equalsIgnoreCase("helm")){
- int ID = Integer.parseInt(item[1]);
- int amount = Integer.parseInt(item[2]);
- short durability = Short.parseShort(item[3]);
- ItemStack invItem = new ItemStack(ID, amount);
- invItem.setDurability(durability);
- player.getInventory().setHelmet(invItem);
- }
- }
- }
- in.close();
- }catch (Exception e){
- ChatLog.log_error(e.getMessage());
- }
- //End inventory,
- /*
- 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.
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement