📄 entitylistfile.java
字号:
mount, false, false)); haveSlot = true; } } // Check the next ammo. // TODO: handle slotless equipment. // TODO: handle tank crits. } // End is-tank-or-proto // Did we record information for this location? if (thisLoc.length() > 0) { // Add this location to the output string. output.append(" <location index=\""); output.append(String.valueOf(loc)); if (isDestroyed) { output.append("\" isDestroyed=\"true"); } output.append("\"> "); output.append(entity.getLocationName(loc)); if (blownOff) { output.append(" has been blown off."); } output.append(CommonConstants.NL); output.append(thisLoc.toString()); output.append(" </location>"); output.append(CommonConstants.NL); // Reset the location buffer. thisLoc = new StringBuffer(); blownOff = false; } // End output-location // If the location is completely destroyed, log it anyway. else if (isDestroyed) { // Add this location to the output string. output.append(" <location index=\""); output.append(String.valueOf(loc)); output.append("\" isDestroyed=\"true\" /> "); output.append(entity.getLocationName(loc)); output.append(CommonConstants.NL); } // End location-completely-destroyed // Reset the "location is destroyed" flag. isDestroyed = false; } // Handle the next location // If there is no location string, return a null. if (output.length() == 0) { return null; } // If we recorded a slot, remind the player that slots start at 1. if (haveSlot) { output.insert(0, CommonConstants.NL); output.insert (0, " The first slot in a location is at index=\"1\"."); // Tanks do wierd things with ammo. if (entity instanceof Tank) { output.insert(0, CommonConstants.NL); output.insert(0, " Tanks have special needs, so don't delete any ammo slots."); } } // Convert the output into a String and return it. return output.toString(); } // End private static String getLocString( Entity ) /** * Save the <code>Entity</code>s in the list to the given file. * <p/> * The <code>Entity</code>s\" pilots, damage, ammo loads, ammo usage, and * other campaign-related information are retained but data specific to * a particular game is ignored. * * @param file - The current contents of the file will be discarded and all * <code>Entity</code>s in the list will be written to the file. * @param list - a <code>Vector</code> containing <code>Entity</code>s * to be stored in a file. * @throws IOException is thrown on any error. */ public static void saveTo(File file, Vector list) throws IOException { /* ** The MS JVM can't handle UTF-8 files. MS says "don't use them". ** TODO: restore UTF-8 after upgrade to more recent jre. ** // Open up the file. Produce UTF-8 output. Writer output = new BufferedWriter( new OutputStreamWriter ( new FileOutputStream( new File( filePath, fileName ) ), "UTF-8" ) ); // Output the doctype and header stuff. output.write( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ); */ // Open up the file. Writer output = new BufferedWriter(new OutputStreamWriter (new FileOutputStream(file))); // Output the doctype and header stuff. output.write("<?xml version=\"1.0\"?>"); output.write(CommonConstants.NL); output.write(CommonConstants.NL); output.write("<unit>"); output.write(CommonConstants.NL); output.write(CommonConstants.NL); // Walk through the list of entities. Enumeration items = list.elements(); while (items.hasMoreElements()) { final Entity entity = (Entity) items.nextElement(); // Start writing this entity to the file. output.write(" <entity chassis=\""); output.write(entity.getChassis().replaceAll("\"", """)); output.write("\" model=\""); output.write(entity.getModel().replaceAll("\"", """)); output.write("\" type=\""); output.write(entity.getMovementModeAsString()); output.write("\">"); output.write(CommonConstants.NL); // Add the crew this entity. final Pilot crew = entity.getCrew(); output.write(" <pilot name=\""); output.write(crew.getName().replaceAll("\"", """)); output.write("\" gunnery=\""); output.write(String.valueOf(crew.getGunnery())); output.write("\" piloting=\""); output.write(String.valueOf(crew.getPiloting())); if (crew.isDead() || crew.getHits() > 5) { output.write("\" hits=\"Dead"); } else if (crew.getHits() > 0) { output.write("\" hits=\""); output.write(String.valueOf(crew.getHits())); } if (crew.countAdvantages() > 0) { output.write("\" advantages=\""); output.write(String.valueOf(crew.getAdvantageList("::"))); } if (entity instanceof Mech) { if (((Mech) entity).isAutoEject()) { output.write("\" autoeject=\"true"); } else { output.write("\" autoeject=\"false"); } } output.write("\"/>"); output.write(CommonConstants.NL); // If it's a tank, add a movement tag. if (entity instanceof Tank) { Tank tentity = (Tank) entity; output.write(getMovementString(tentity)); if (tentity.isTurretLocked()) output.write(getTurretLockedString(tentity)); } // Add the locations of this entity (if any are needed). String loc = getLocString(entity); if (null != loc) { output.write(loc); } // Finish writing this entity to the file. output.write(" </entity>"); output.write(CommonConstants.NL); output.write(CommonConstants.NL); } // Handle the next entity // Finish writing. output.write("</unit>"); output.write(CommonConstants.NL); output.flush(); output.close(); } private static String getTurretLockedString(Tank e) { String retval = " <turretlock direction=\""; retval = retval.concat(Integer.toString(e.getSecondaryFacing())); retval = retval.concat("\"/>\n"); return retval; } private static String getMovementString(Tank e) { String retVal = " <movement speed=\""; if (e.isImmobile()) retVal = retVal.concat("immobile"); else retVal = retVal.concat(Integer.toString(e.getOriginalWalkMP())); retVal = retVal.concat("\"/>\n"); return retVal; } /** * Load a list of <code>Entity</code>s from the given file. * <p/> * The <code>Entity</code>s\" pilots, damage, ammo loads, ammo usage, and * other campaign-related information are retained but data specific to * a particular game is ignored. * * @param file - the <code>File</code> to load from. * @return A <code>Vector</code> containing <code>Entity</code>s * loaded from the file. This vector may be empty, but * it will not be <code>null</code>. * @throws IOException is thrown on any error. */ public static Vector loadFrom(File file) throws IOException { // Create an empty parser. XMLStreamParser parser = new XMLStreamParser(); // Open up the file. InputStream listStream = new FileInputStream (file); // Read a Vector from the file. try { parser.parse(listStream); listStream.close(); } catch (ParseException excep) { excep.printStackTrace(System.err); throw new IOException("Unable to read from: " + file); } // Was there any error in parsing? if (parser.hasWarningMessage()) { System.out.println(parser.getWarningMessage()); } // Return the entities. return parser.getEntities(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -