Closing disconnected inventory reset problems. This is a bit of dead code that may be useful in the future (i.e. the save-player-inventories-to-disk fix I should have tried to reproduce before coding).

This commit is contained in:
taoneill 2011-05-15 13:08:42 -04:00
parent 04b917807c
commit 9c3c722645
3 changed files with 164 additions and 38 deletions

View File

@ -38,6 +38,7 @@ import com.tommytony.war.mappers.VolumeMapper;
import com.tommytony.war.mappers.WarMapper;
import com.tommytony.war.mappers.WarzoneMapper;
import com.tommytony.war.utils.ChatFixUtil;
import com.tommytony.war.utils.InventoryStash;
/**
*
@ -61,6 +62,7 @@ public class War extends JavaPlugin {
private final List<Warzone> incompleteZones = new ArrayList<Warzone>();
private final List<String> zoneMakerNames = new ArrayList<String>();
private final List<String> zoneMakersImpersonatingPlayers = new ArrayList<String>();
private HashMap<String, InventoryStash> disconnected = new HashMap<String, InventoryStash>();
private final HashMap<String, String> wandBearers = new HashMap<String, String>(); // playername to zonename
private final HashMap<Integer, ItemStack> defaultLoadout = new HashMap<Integer, ItemStack>();
private int defaultLifepool = 21;
@ -100,6 +102,8 @@ public class War extends JavaPlugin {
if(warHub != null) {
warHub.getVolume().resetBlocks();
}
// To save the disconnected users state
//WarMapper.saveDisconnected(this);
this.logInfo("Done. War v" + desc.getVersion() + " is off.");
}
@ -1514,4 +1518,12 @@ public class War extends JavaPlugin {
return defaultNoCreatures;
}
public void setDisconnected(HashMap<String, InventoryStash> disconnected) {
this.disconnected = disconnected;
}
public HashMap<String, InventoryStash> getDisconnected() {
return disconnected;
}
}

View File

@ -40,7 +40,7 @@ public class WarPlayerListener extends PlayerListener {
private final War war;
private Random random = null;
private HashMap<String, InventoryStash> disconnected = new HashMap<String, InventoryStash>();
public WarPlayerListener(War war) {
this.war = war;
@ -50,39 +50,39 @@ public class WarPlayerListener extends PlayerListener {
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
// Disconnected
if(disconnected.containsKey(player.getName())) {
// restore the disconnected player's inventory
InventoryStash originalContents = disconnected.remove(player.getName());
PlayerInventory playerInv = player.getInventory();
playerInv.clear(playerInv.getSize() + 0);
playerInv.clear(playerInv.getSize() + 1);
playerInv.clear(playerInv.getSize() + 2);
playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead
if(originalContents != null) {
playerInv.clear();
for(ItemStack item : originalContents.getContents()) {
if(item != null && item.getTypeId() != 0) {
playerInv.addItem(item);
}
}
if(originalContents.getHelmet() != null && originalContents.getHelmet().getType() != Material.AIR) {
playerInv.setHelmet(originalContents.getHelmet());
}
if(originalContents.getChest() != null && originalContents.getHelmet().getType() != Material.AIR) {
playerInv.setChestplate(originalContents.getChest());
}
if(originalContents.getLegs() != null && originalContents.getHelmet().getType() != Material.AIR) {
playerInv.setLeggings(originalContents.getLegs());
}
if(originalContents.getFeet() != null && originalContents.getHelmet().getType() != Material.AIR) {
playerInv.setBoots(originalContents.getFeet());
}
}
war.msg(player, "You were disconnected while playing War. Here's your inventory from last time.");
}
// if(war.getDisconnected().containsKey(player.getName())) {
// // restore the disconnected player's inventory
// InventoryStash originalContents = war.getDisconnected().remove(player.getName());
// PlayerInventory playerInv = player.getInventory();
// playerInv.clear(playerInv.getSize() + 0);
// playerInv.clear(playerInv.getSize() + 1);
// playerInv.clear(playerInv.getSize() + 2);
// playerInv.clear(playerInv.getSize() + 3); // helmet/blockHead
// if(originalContents != null) {
// playerInv.clear();
//
// for(ItemStack item : originalContents.getContents()) {
// if(item != null && item.getTypeId() != 0) {
// playerInv.addItem(item);
// }
// }
//
// if(originalContents.getHelmet() != null && originalContents.getHelmet().getType() != Material.AIR) {
// playerInv.setHelmet(originalContents.getHelmet());
// }
// if(originalContents.getChest() != null && originalContents.getHelmet().getType() != Material.AIR) {
// playerInv.setChestplate(originalContents.getChest());
// }
// if(originalContents.getLegs() != null && originalContents.getHelmet().getType() != Material.AIR) {
// playerInv.setLeggings(originalContents.getLegs());
// }
// if(originalContents.getFeet() != null && originalContents.getHelmet().getType() != Material.AIR) {
// playerInv.setBoots(originalContents.getFeet());
// }
// }
//
// war.msg(player, "You were disconnected while playing War. Here's your inventory from last time.");
// }
}
public void onPlayerQuit(PlayerQuitEvent event) {
@ -91,9 +91,9 @@ public class WarPlayerListener extends PlayerListener {
if(team != null) {
Warzone zone = war.getPlayerTeamWarzone(player.getName());
if(zone != null) {
if(zone.hasPlayerInventory(player.getName())) {
disconnected.put(player.getName(), zone.getPlayerInventory(player.getName()));
}
// if(zone.hasPlayerInventory(player.getName())) {
// war.getDisconnected().put(player.getName(), zone.getPlayerInventory(player.getName()));
// }
zone.handlePlayerLeave(player, zone.getTeleport(), true);
}
if(war.isWandBearer(player)) {
@ -243,7 +243,6 @@ public class WarPlayerListener extends PlayerListener {
} else if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
setter.placeCorner2(event.getClickedBlock());
event.setUseItemInHand(Result.ALLOW);
//player.getInventory().addItem(new ItemStack(Material.WOOD_SWORD));
}
}

View File

@ -1,9 +1,17 @@
package com.tommytony.war.mappers;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import bukkit.tommytony.war.War;
@ -12,6 +20,7 @@ import com.tommytony.war.WarHub;
import com.tommytony.war.Warzone;
import com.tommytony.war.jobs.RestoreWarhubJob;
import com.tommytony.war.jobs.RestoreWarzonesJob;
import com.tommytony.war.utils.InventoryStash;
/**
*
@ -152,6 +161,8 @@ public class WarMapper {
}
warConfig.close();
//loadDisconnected(war);
}
public static void save(War war) {
@ -248,4 +259,108 @@ public class WarMapper {
warConfig.save();
warConfig.close();
}
// private static void loadDisconnected(War war) {
// BufferedReader in = null;
// try {
// in = new BufferedReader(new FileReader(new File(war.getDataFolder().getPath() +
// "/dat/disconnected.dat")));
// String blockLine = in.readLine();
// while(blockLine != null && !blockLine.equals("")) {
// String[] blockSplit = blockLine.split(",");
// if(blockLine != null && !blockLine.equals("") && blockSplit.length > 1) {
// String playerName = blockSplit[0];
// List<ItemStack> items = new ArrayList<ItemStack>();
// if(blockSplit.length > 1) {
// String itemsStr = blockSplit[1];
// String[] itemsStrSplit = itemsStr.split(";;");
// for(String itemStr : itemsStrSplit) {
// String[] itemStrSplit = itemStr.split(";");
// if(itemStrSplit.length == 4) {
// ItemStack stack = new ItemStack(Integer.parseInt(itemStrSplit[0]),
// Integer.parseInt(itemStrSplit[1]));
// stack.setData(new MaterialData(stack.getTypeId(),Byte.parseByte(itemStrSplit[3])));
// short durability = (short)Integer.parseInt(itemStrSplit[2]);
// stack.setDurability(durability);
// items.add(stack);
// } else if(itemStrSplit.length == 3) {
// ItemStack stack = new ItemStack(Integer.parseInt(itemStrSplit[0]),
// Integer.parseInt(itemStrSplit[1]));
// short durability = (short)Integer.parseInt(itemStrSplit[2]);
// stack.setDurability(durability);
// items.add(stack);
// } else {
// items.add(new ItemStack(Integer.parseInt(itemStrSplit[0]),
// Integer.parseInt(itemStrSplit[1])));
// }
// }
// }
// war.getDisconnected().put(playerName, new InventoryStash(items));
// }
//
// }
// } catch (IOException e) {
// war.logWarn("Failed to read volume file " + volume.getName() +
// " for warzone " + zoneName + ". " + e.getClass().getName() + " " + e.getMessage());
// e.printStackTrace();
// } catch (Exception e) {
// war.logWarn("Unexpected error caused failure to read volume file " + zoneName +
// " for warzone " + volume.getName() + ". " + e.getClass().getName() + " " + e.getMessage());
// e.printStackTrace();
// } finally {
// if(in != null)
// try {
// in.close();
// } catch (IOException e) {
// war.logWarn("Failed to close file reader for volume " + volume.getName() +
// " for warzone " + zoneName + ". " + e.getClass().getName() + " " + e.getMessage());
// e.printStackTrace();
// }
// }
// }
//
// public static void saveDisconnected(War war) {
// BufferedWriter out = null;
// try {
// out = new BufferedWriter(new FileWriter(new File(war.getDataFolder().getPath() + "/dat/disconnected.dat")));
// HashMap<String,InventoryStash> disconnected = war.getDisconnected();
// for(String key : disconnected.keySet()){
// String userString = key + ",";
// InventoryStash userInv = disconnected.get(userString);
// for(ItemStack item : userInv.getContents()) {
// String extra = "";
// if(item != null) {
// extra += item.getTypeId() + ";"
// + item.getAmount() + ";"
// + item.getDurability();
// if(item.getData() != null)
// extra += ";" + item.getData().getData() ;
// extra += ";;";
// }
// userString += extra;
// }
// if(userInv.getHelmet() != null) {
//
// }
//
// out.write(userString);
// out.newLine();
// }
// } catch (IOException e) {
// war.logWarn("Failed while writing disconnected user inventories. " + e.getMessage());
// e.printStackTrace();
// } catch (Exception e) {
// war.logWarn("Unexpected error caused failure while writing disconnected user inventories. " + e.getClass().getName() + " " + e.getMessage());
// e.printStackTrace();
// }
// finally {
// if(out != null)
// try {
// out.close();
// } catch (IOException e) {
// war.logWarn("Failed to close file writer for disconnected user inventories. " + e.getMessage());
// e.printStackTrace();
// }
// }
// }
}