2013年5月15日 星期三

【Java教學】製作更名卡

首先 L1PcInstance 在裡面加入以下


private boolean _isChangeName = false;


public boolean isChangeName()
   {
     return this._isChangeName;
   }
   public void setChangeName(boolean flag) {
    this._isChangeName = flag;
  }


在來 C_Chat 裡面
最上面先加入


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

在來找這段 if (chatType == 0) {在它上面加入


     if (pc.isChangeName()) {
       L1PcInstance result = L1PcInstance.load(chatText);
       if (result == null) {
         ChaneName(pc, chatText);
         pc.setChangeName(false);
         pc.sendPackets(new S_Paralysis(5, false));
         pc.sendPackets(new S_SystemMessage("人物姓名修改成功,強制中斷遊戲連線。"));
        pc.sendPackets(new S_Disconnect());
       }
       else
       {
         pc.sendPackets(new S_SystemMessage("這個ID【" + chatText + "】已經有人使用了,請重新輸入別的ID。"));
     }
      return;
   }

在來找這段 public String getType() {在它上面加入


   private static void ChaneName(L1PcInstance pc, String newName) {
     Connection con = null;
     PreparedStatement pstm = null;
     ResultSet rs = null;
     try {
       con = L1DatabaseFactory.getInstance().getConnection();
       String sqlstr = "UPDATE characters SET char_name=? WHERE objid=?";
       pstm = con.prepareStatement(sqlstr);
       pstm.setString(1, newName);
       pstm.setInt(2, pc.getId());

       pstm.execute();
     } catch (SQLException localSQLException) {
     }
     finally {
       SQLUtil.close(rs);
       SQLUtil.close(pstm);
       SQLUtil.close(con);
     }
   }

在來換 C_ItemUSe 裡面找 else if (itemId == 43000) {在它上面加入


           else if (itemId == 60103) {
             if (pc.getClanid() != 0) {//判定有沒有入盟
               pc.sendPackets(new S_SystemMessage("\\fY退盟以後才可以使用更名卡喔..."));
               return;
             }
             if (!pc.getPetList().isEmpty()) {
               //寵物清單MAP 內的寵物元素是否完全為空 true = isEmpty 
               //也就是你這個寵物清單內完全沒有任何元素 ! isEmpty 也就是你至少帶了一個寵物以上
               pc.sendPackets(new S_ServerMessage(1181));
               return;
             }
             if (!pc.getDollList().isEmpty()) {//娃娃
               pc.sendPackets(new S_ServerMessage(1181));
               return;
             }
             BuddyTable.getInstance().removeBuddy(pc.getId(), pc.getName());
             pc.sendPackets(new S_Paralysis(5, true));
             pc.setChangeName(true);
             pc.sendPackets(new S_SystemMessage("\\fY要變更的姓名請以繁體字輸入,輸入要修改的姓名後,按(Enter)。"));
             pc.getInventory().removeItem(l1iteminstance, 1);
           }

在來換 CharacterTable 找一個地方新增


                public L1PcInstance loadCharacter(String charName) throws Exception {
        L1PcInstance pc = null;// pc數據 初始為空 null
        try {
                pc = restoreCharacter(charName);// 依據名稱字串讀取PC數據
               // XXX 當讀取的數據為空 也就代表這個ID沒人用的同時 則 pc == null
               // 由於你的改名程序是由這個函式來寫 你只不過是要判斷名子有沒有人用
               // 所以這邊 當 PC為null或是不為null 都會執行以下的程序
               // 那假設pc傳回null時 又執行下面的程序 空指針的錯誤就會出現了                                             
               if (pc == null) {// pc為null 就到這為止吧
                  return null;// 傳回null
            }





沒有留言:

張貼留言