Changeset 13b2f458e8bc0b2cac80f3c0b6fd3c9084eb0a38

Show
Ignore:
Timestamp:
05/23/08 10:59:55 (5 months ago)
Author:
Christopher Jung <bktheg@web.de>
git-committer:
Christopher Jung <bktheg@web.de> 1211533195 +0200
git-parent:

[c9db94eb87cb1cfcbc1fe72bc64c1dd7b7b2cea1]

git-author:
Christopher Jung <bktheg@web.de> 1211533195 +0200
Message:

[bug] NPE bei Waffenfabriken ohne Eintrag in WeaponFactory? gefixt

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/net/driftingsouls/ds2/server/bases/Waffenfabrik.java

    r373b1b8 r13b2f45  
    7676                boolean init = false; 
    7777                 
     78                /** 
     79                 * Konstruktor 
     80                 */ 
    7881                public ContextVars() { 
    7982                        // EMPTY 
     
    9093        private String loaddata( Base base ) { 
    9194                Context context = ContextMap.getContext(); 
    92                 org.hibernate.Session db = context.getDB(); 
    9395                 
    9496                User user = base.getOwner(); 
     
    103105                        context.putVariable(getClass(), "last_user", user.getId()); 
    104106                 
    105                         // Iterator, da sich die Ammo-Objekte sich mit hoher Wahrscheinlichkeit 
    106                         // bereits im Cache befinden 
    107                         Iterator ammoIter = db.createQuery("from Ammo").list().iterator(); 
    108                         for( ; ammoIter.hasNext(); ) { 
    109                                 Ammo ammo = (Ammo)ammoIter.next(); 
    110                                 if( !user.hasResearched(ammo.getRes1()) || !user.hasResearched(ammo.getRes2()) || !user.hasResearched(ammo.getRes3()) ) { 
    111                                         continue; 
    112                                 } 
    113                                 vars.ownerammobase.add(ammo); 
    114                                  
    115                                 if( (ammo.getReplaces() != null) && !removelist.contains(ammo.getReplaces()) ) { 
    116                                         removelist.add(ammo.getReplaces()); 
    117                                 } 
    118                         } 
    119                          
    120                         if( user.getAlly() != null ) { 
    121                                 Cargo itemlist = new Cargo( Cargo.Type.ITEMSTRING, user.getAlly().getItems() );  
    122                                  
    123                                 List<ItemCargoEntry> list = itemlist.getItemsWithEffect( ItemEffect.Type.DRAFT_AMMO ) ; 
    124                                 for( ItemCargoEntry item : list ) { 
    125                                         IEDraftAmmo itemeffect = (IEDraftAmmo)item.getItemEffect(); 
    126                                         Ammo ammo = itemeffect.getAmmo(); 
    127                                          
    128                                         vars.ownerammobase.add(ammo); 
    129                                          
    130                                         if( (ammo.getReplaces() != null) && removelist.contains(ammo.getReplaces()) ) { 
    131                                                 removelist.add(ammo.getReplaces()); 
     107                        removelist = loadOwnerAmmoBase(user, vars); 
     108                } 
     109                 
     110                if( !vars.usedcapacity.containsKey(base.getId()) ) { 
     111                        return loadAmmoTasks(base, vars, removelist); 
     112                } 
     113                 
     114                return ""; 
     115        } 
     116 
     117        private String loadAmmoTasks(Base base, ContextVars vars, List<Ammo> removelist) { 
     118                Context context = ContextMap.getContext(); 
     119                org.hibernate.Session db = context.getDB(); 
     120                 
     121                StringBuilder wfreason = new StringBuilder(100); 
     122                 
     123                if( !vars.stats.containsKey(base.getId()) ) { 
     124                        vars.stats.put(base.getId(), new Cargo()); 
     125                } 
     126                 
     127                boolean ok = true; 
     128                Set<Ammo> thisammolist = vars.ownerammobase;     
     129                 
     130                Cargo cargo = base.getCargo(); 
     131                 
     132                List<ItemCargoEntry> list = cargo.getItemsWithEffect( ItemEffect.Type.DRAFT_AMMO ) ; 
     133                for( ItemCargoEntry item : list ) { 
     134                        IEDraftAmmo itemeffect = (IEDraftAmmo)item.getItemEffect(); 
     135                        Ammo ammo = itemeffect.getAmmo(); 
     136                         
     137                        thisammolist.add(ammo); 
     138                } 
     139                 
     140                WeaponFactory wf = (WeaponFactory)db.get(WeaponFactory.class, base.getId()); 
     141                if( wf == null ) { 
     142                        vars.usedcapacity.put(base.getId(), BigDecimal.valueOf(-1)); 
     143                         
     144                        LOG.warn("Basis "+base.getId()+" verfuegt ueber keinen Waffenfabrik-Eintrag, obwohl es eine Waffenfabrik hat"); 
     145                        return "Basis "+base.getId()+" verfuegt ueber keinen Waffenfabrik-Eintrag, obwohl es eine Waffenfabrik hat"; 
     146                } 
     147                WeaponFactory.Task[] plist = wf.getProduces(); 
     148                for( int i=0; i < plist.length; i++ ) { 
     149                        Ammo ammo = plist[i].getAmmo(); 
     150                        int count = plist[i].getCount(); 
     151                         
     152                        if( ammo == null ) { 
     153                                plist = (WeaponFactory.Task[])ArrayUtils.remove(plist, i); 
     154                                i--; 
     155                                continue; 
     156                        } 
     157                         
     158                        // Ammo ohne Plaene melden - veraltete Ammo aber ignorieren! 
     159                        if( (count > 0) && !thisammolist.contains(ammo) && !removelist.contains(ammo) ) { 
     160                                ok = false; 
     161                                wfreason.append("Es existieren nicht die n&ouml;tigen Baupl&auml;ne f&uuml;r "+ammo.getName()+"\n"); 
     162                                break; 
     163                        } 
     164                        else if( (count > 0) && !thisammolist.contains(ammo) ) { 
     165                                plist = (WeaponFactory.Task[])ArrayUtils.remove(plist, i); 
     166                                i--; 
     167                        } 
     168                } 
     169                         
     170                if( ok ) { 
     171                        for( int i=0; i < plist.length; i++  ) { 
     172                                Ammo ammo = plist[i].getAmmo(); 
     173                                int count = plist[i].getCount(); 
     174                                 
     175                                if( !vars.usedcapacity.containsKey(base.getId()) ) { 
     176                                        vars.usedcapacity.put(base.getId(), new BigDecimal(0, MathContext.DECIMAL32)); 
     177                                } 
     178                                vars.usedcapacity.put(base.getId(), vars.usedcapacity.get(base.getId()).add(ammo.getDauer().multiply((new BigDecimal(count)))) ); 
     179                                if( count > 0 ) { 
     180                                        Cargo tmpcargo = new Cargo(ammo.getBuildCosts()); 
     181                                        if( count > 1 ) { 
     182                                                tmpcargo.multiply( count, Cargo.Round.NONE ); 
    132183                                        } 
    133                                 } 
    134                         } 
    135                          
    136                         // Alle Raks entfernen, die durch andere Raks ersetzt wurden (DB-Feld: replaces) 
    137                         if( !removelist.isEmpty() ) { 
    138                                 for( Ammo removeentry : removelist ) { 
    139                                         vars.ownerammobase.remove(removeentry); 
    140                                 } 
    141                         } 
    142                 } 
    143                  
    144                 StringBuilder wfreason = new StringBuilder(100); 
    145                  
    146                 if( !vars.usedcapacity.containsKey(base.getId()) ) { 
    147                         if( !vars.stats.containsKey(base.getId()) ) { 
    148                                 vars.stats.put(base.getId(), new Cargo()); 
    149                         } 
    150                          
    151                         boolean ok = true; 
    152                         Set<Ammo> thisammolist = vars.ownerammobase;     
    153                          
    154                         Cargo cargo = base.getCargo(); 
    155                          
    156                         List<ItemCargoEntry> list = cargo.getItemsWithEffect( ItemEffect.Type.DRAFT_AMMO ) ; 
     184                                        vars.stats.get(base.getId()).substractCargo( tmpcargo ); 
     185                                        vars.stats.get(base.getId()).addResource( new ItemID(ammo.getItemId()), count ); 
     186                                } 
     187                        } 
     188                } 
     189                else { 
     190                        String basename = base.getName(); 
     191                        wfreason.insert(0, "[b]"+basename+"[/b] - Die Arbeiten in der Waffenfabrik zeitweise eingestellt.\nGrund:\n"); 
     192                } 
     193                 
     194                if( !vars.usedcapacity.containsKey(base.getId()) || (vars.usedcapacity.get(base.getId()).doubleValue() <= 0) ) { 
     195                        vars.usedcapacity.put(base.getId(), new BigDecimal(-1)); 
     196                } 
     197                 
     198                return wfreason.toString(); 
     199        } 
     200 
     201        private List<Ammo> loadOwnerAmmoBase(User user, ContextVars vars) { 
     202                Context context = ContextMap.getContext(); 
     203                org.hibernate.Session db = context.getDB(); 
     204                 
     205                List<Ammo> removelist = new ArrayList<Ammo>(); 
     206                 
     207                // Iterator, da sich die Ammo-Objekte sich mit hoher Wahrscheinlichkeit 
     208                // bereits im Cache befinden 
     209                Iterator ammoIter = db.createQuery("from Ammo").list().iterator(); 
     210                for( ; ammoIter.hasNext(); ) { 
     211                        Ammo ammo = (Ammo)ammoIter.next(); 
     212                        if( !user.hasResearched(ammo.getRes1()) || !user.hasResearched(ammo.getRes2()) || !user.hasResearched(ammo.getRes3()) ) { 
     213                                continue; 
     214                        } 
     215                        vars.ownerammobase.add(ammo); 
     216                         
     217                        if( (ammo.getReplaces() != null) && !removelist.contains(ammo.getReplaces()) ) { 
     218                                removelist.add(ammo.getReplaces()); 
     219                        } 
     220                } 
     221                 
     222                if( user.getAlly() != null ) { 
     223                        Cargo itemlist = new Cargo( Cargo.Type.ITEMSTRING, user.getAlly().getItems() );  
     224                         
     225                        List<ItemCargoEntry> list = itemlist.getItemsWithEffect( ItemEffect.Type.DRAFT_AMMO ) ; 
    157226                        for( ItemCargoEntry item : list ) { 
    158227                                IEDraftAmmo itemeffect = (IEDraftAmmo)item.getItemEffect(); 
    159228                                Ammo ammo = itemeffect.getAmmo(); 
    160229                                 
    161                                 thisammolist.add(ammo); 
    162                         } 
    163                          
    164                         WeaponFactory wf = (WeaponFactory)db.get(WeaponFactory.class, base.getId()); 
    165                         if( wf == null ) { 
    166                                 LOG.warn("Basis "+base.getId()+" verfuegt ueber keinen Waffenfabrik-Eintrag, obwohl es eine Waffenfabrik hat"); 
    167                                 return "Basis "+base.getId()+" verfuegt ueber keinen Waffenfabrik-Eintrag, obwohl es eine Waffenfabrik hat"; 
    168                         } 
    169                         WeaponFactory.Task[] plist = wf.getProduces(); 
    170                         for( int i=0; i < plist.length; i++ ) { 
    171                                 Ammo ammo = plist[i].getAmmo(); 
    172                                 int count = plist[i].getCount(); 
    173                                  
    174                                 if( ammo == null ) { 
    175                                         plist = (WeaponFactory.Task[])ArrayUtils.remove(plist, i); 
    176                                         i--; 
    177                                         continue; 
    178                                 } 
    179                                  
    180                                 // Ammo ohne Plaene melden - veraltete Ammo aber ignorieren! 
    181                                 if( (count > 0) && !thisammolist.contains(ammo) && !removelist.contains(ammo) ) { 
    182                                         ok = false; 
    183                                         wfreason.append("Es existieren nicht die n&ouml;tigen Baupl&auml;ne f&uuml;r "+ammo.getName()+"\n"); 
    184                                         break; 
    185                                 } 
    186                                 else if( (count > 0) && !thisammolist.contains(ammo) ) { 
    187                                         plist = (WeaponFactory.Task[])ArrayUtils.remove(plist, i); 
    188                                         i--; 
    189                                 } 
    190                         } 
    191                          
    192                         if( ok ) { 
    193                                 for( int i=0; i < plist.length; i++  ) { 
    194                                         Ammo ammo = plist[i].getAmmo(); 
    195                                         int count = plist[i].getCount(); 
    196                                          
    197                                         if( !vars.usedcapacity.containsKey(base.getId()) ) { 
    198                                                 vars.usedcapacity.put(base.getId(), new BigDecimal(0, MathContext.DECIMAL32)); 
    199                                         } 
    200                                         vars.usedcapacity.put(base.getId(), vars.usedcapacity.get(base.getId()).add(ammo.getDauer().multiply((new BigDecimal(count)))) ); 
    201                                         if( count > 0 ) { 
    202                                                 Cargo tmpcargo = new Cargo(ammo.getBuildCosts()); 
    203                                                 if( count > 1 ) { 
    204                                                         tmpcargo.multiply( count, Cargo.Round.NONE ); 
    205                                                 } 
    206                                                 vars.stats.get(base.getId()).substractCargo( tmpcargo ); 
    207                                                 vars.stats.get(base.getId()).addResource( new ItemID(ammo.getItemId()), count ); 
    208                                         } 
    209                                 } 
    210                         } 
    211                         else { 
    212                                 String basename = base.getName(); 
    213                                 wfreason.insert(0, "[b]"+basename+"[/b] - Die Arbeiten in der Waffenfabrik zeitweise eingestellt.\nGrund:\n"); 
    214                         } 
    215                          
    216                         if( !vars.usedcapacity.containsKey(base.getId()) || (vars.usedcapacity.get(base.getId()).doubleValue() <= 0) ) { 
    217                                 vars.usedcapacity.put(base.getId(), new BigDecimal(-1)); 
    218                         } 
    219                 } 
    220                  
    221                 return wfreason.toString(); 
     230                                vars.ownerammobase.add(ammo); 
     231                                 
     232                                if( (ammo.getReplaces() != null) && removelist.contains(ammo.getReplaces()) ) { 
     233                                        removelist.add(ammo.getReplaces()); 
     234                                } 
     235                        } 
     236                } 
     237                 
     238                // Alle Raks entfernen, die durch andere Raks ersetzt wurden (DB-Feld: replaces) 
     239                if( !removelist.isEmpty() ) { 
     240                        for( Ammo removeentry : removelist ) { 
     241                                vars.ownerammobase.remove(removeentry); 
     242                        } 
     243                } 
     244                 
     245                return removelist; 
    222246        } 
    223247