diff --git a/SPD-classes/src/main/java/com/watabou/glscripts/Script.java b/SPD-classes/src/main/java/com/watabou/glscripts/Script.java index 8712cd575..017d6459d 100644 --- a/SPD-classes/src/main/java/com/watabou/glscripts/Script.java +++ b/SPD-classes/src/main/java/com/watabou/glscripts/Script.java @@ -24,6 +24,7 @@ package com.watabou.glscripts; import com.watabou.glwrap.Program; import com.watabou.glwrap.Shader; import com.watabou.noosa.Game; +import com.watabou.utils.Reflection; import java.util.HashMap; @@ -42,11 +43,7 @@ public class Script extends Program { Script script = all.get( c ); if (script == null) { - try { - script = c.newInstance(); - } catch (Exception e) { - Game.reportException(e); - } + script = Reflection.newInstance( c ); all.put( c, script ); } diff --git a/SPD-classes/src/main/java/com/watabou/noosa/Game.java b/SPD-classes/src/main/java/com/watabou/noosa/Game.java index 0868f1412..6729149d5 100644 --- a/SPD-classes/src/main/java/com/watabou/noosa/Game.java +++ b/SPD-classes/src/main/java/com/watabou/noosa/Game.java @@ -33,6 +33,7 @@ import com.watabou.input.KeyEvent; import com.watabou.noosa.audio.Music; import com.watabou.noosa.audio.Sample; import com.watabou.utils.PlatformSupport; +import com.watabou.utils.Reflection; import java.io.PrintWriter; import java.io.StringWriter; @@ -187,14 +188,10 @@ public class Game implements ApplicationListener { if (requestedReset) { requestedReset = false; - - try { - requestedScene = sceneClass.newInstance(); + + requestedScene = Reflection.newInstance(sceneClass); + if (requestedScene != null){ switchScene(); - } catch (InstantiationException e){ - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); } } diff --git a/SPD-classes/src/main/java/com/watabou/noosa/Group.java b/SPD-classes/src/main/java/com/watabou/noosa/Group.java index d7c08adb9..bf8167e8a 100644 --- a/SPD-classes/src/main/java/com/watabou/noosa/Group.java +++ b/SPD-classes/src/main/java/com/watabou/noosa/Group.java @@ -23,6 +23,7 @@ package com.watabou.noosa; import com.watabou.noosa.particles.Emitter; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -190,11 +191,11 @@ public class Group extends Gizmo { } else { - try { - return add( c.newInstance() ); - } catch (Exception e) { - Game.reportException(e); + g = Reflection.newInstance(c); + if (g != null) { + return add(g); } + } return null; diff --git a/SPD-classes/src/main/java/com/watabou/utils/Bundle.java b/SPD-classes/src/main/java/com/watabou/utils/Bundle.java index 7a6d00b0b..3b27a4007 100644 --- a/SPD-classes/src/main/java/com/watabou/utils/Bundle.java +++ b/SPD-classes/src/main/java/com/watabou/utils/Bundle.java @@ -97,13 +97,8 @@ public class Bundle { if (aliases.containsKey( clName )) { clName = aliases.get( clName ); } - try { - Class cl = Class.forName( clName ); - return cl; - } catch (ClassNotFoundException e) { - Game.reportException(e); - return null; - } + + return Reflection.forName( clName ); } return null; } @@ -114,30 +109,24 @@ public class Bundle { private Bundlable get() { if (data == null) return null; - try { - String clName = getString( CLASS_NAME ); - if (aliases.containsKey( clName )) { - clName = aliases.get( clName ); - } - - Class cl = Class.forName( clName ); - if (cl != null && (!cl.isMemberClass() || Modifier.isStatic(cl.getModifiers()))) { - Bundlable object = (Bundlable)cl.newInstance(); - object.restoreFromBundle( this ); - return object; - } else { - return null; - } - } catch (ClassNotFoundException e ) { - Game.reportException(e); - return null; - } catch (InstantiationException e ) { - Game.reportException(e); - return null; - } catch (IllegalAccessException e ) { - Game.reportException(e); - return null; + + String clName = getString( CLASS_NAME ); + if (aliases.containsKey( clName )) { + clName = aliases.get( clName ); } + + Class cl = Reflection.forName( clName ); + //Skip none-static inner classes as they can't be instantiated through bundle restoring + //Classes which make use of none-static inner classes must manage instantiation manually + if (cl != null && (!Reflection.isMemberClass(cl) || Reflection.isStatic(cl))) { + Bundlable object = (Bundlable) Reflection.newInstance(cl); + if (object != null) { + object.restoreFromBundle(this); + return object; + } + } + + return null; } public Bundlable get( String key ) { @@ -226,13 +215,8 @@ public class Bundle { if (aliases.containsKey( clName )) { clName = aliases.get( clName ); } - try { - Class cl = Class.forName( clName ); - result[i] = cl; - } catch (ClassNotFoundException e) { - Game.reportException(e); - result[i] = null; - } + Class cl = Reflection.forName( clName ); + result[i] = cl; } return result; } catch (JSONException e) { @@ -404,7 +388,7 @@ public class Bundle { //Classes which make use of none-static inner classes must manage instantiation manually if (object != null) { Class cl = object.getClass(); - if (!cl.isMemberClass() || Modifier.isStatic(cl.getModifiers())) { + if ((!Reflection.isMemberClass(cl) || Reflection.isStatic(cl))) { Bundle bundle = new Bundle(); bundle.put(CLASS_NAME, cl.getName()); object.storeInBundle(bundle); diff --git a/SPD-classes/src/main/java/com/watabou/utils/Reflection.java b/SPD-classes/src/main/java/com/watabou/utils/Reflection.java new file mode 100644 index 000000000..09ace4049 --- /dev/null +++ b/SPD-classes/src/main/java/com/watabou/utils/Reflection.java @@ -0,0 +1,65 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2019 Evan Debenham + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.watabou.utils; + +import com.badlogic.gdx.utils.reflect.ClassReflection; +import com.badlogic.gdx.utils.reflect.ReflectionException; +import com.watabou.noosa.Game; + +//wrapper for LibGDX reflection +public class Reflection { + + public static boolean isMemberClass( Class cls ){ + return ClassReflection.isMemberClass(cls); + } + + public static boolean isStatic( Class cls ){ + return ClassReflection.isStaticClass(cls); + } + + public static T newInstance( Class cls ){ + try { + return ClassReflection.newInstance(cls); + } catch (ReflectionException e) { + Game.reportException(e); + return null; + } + } + + public static T newInstanceUnhandled( Class cls ) throws Exception { + return ClassReflection.newInstance(cls); + } + + public static Class forName( String name ){ + try { + return ClassReflection.forName( name ); + } catch (ReflectionException e) { + Game.reportException(e); + return null; + } + } + + public static Class forNameUnhandled( String name ) throws Exception { + return ClassReflection.forName( name ); + } + +} diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Bones.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Bones.java index e66b89763..cca63079a 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Bones.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/Bones.java @@ -30,6 +30,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.weapon.missiles.MissileWea import com.watabou.utils.Bundle; import com.watabou.utils.FileUtils; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.io.IOException; import java.util.ArrayList; @@ -145,18 +146,19 @@ public class Bones { //Enforces artifact uniqueness if (item instanceof Artifact){ if (Generator.removeArtifact(((Artifact)item).getClass())) { - try { - //generates a new artifact of the same type, always +0 - Artifact artifact = (Artifact)item.getClass().newInstance(); - - artifact.cursed = true; - artifact.cursedKnown = true; - - return artifact; - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); + + //generates a new artifact of the same type, always +0 + Artifact artifact = Reflection.newInstance(((Artifact)item).getClass()); + + if (artifact == null){ return new Gold(item.price()); } + + artifact.cursed = true; + artifact.cursedKnown = true; + + return artifact; + } else { return new Gold(item.price()); } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/blobs/Blob.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/blobs/Blob.java index 62cd27652..03f1b50bb 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/blobs/Blob.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/blobs/Blob.java @@ -28,6 +28,7 @@ import com.shatteredpixel.shatteredpixeldungeon.effects.BlobEmitter; import com.shatteredpixel.shatteredpixeldungeon.levels.Level; import com.watabou.utils.Bundle; import com.watabou.utils.Rect; +import com.watabou.utils.Reflection; public class Blob extends Actor { @@ -223,22 +224,19 @@ public class Blob extends Actor { @SuppressWarnings("unchecked") public static T seed( int cell, int amount, Class type, Level level ) { - try { - - T gas = (T)level.blobs.get( type ); - if (gas == null) { - gas = type.newInstance(); - level.blobs.put( type, gas ); - } - - gas.seed( level, cell, amount ); - - return gas; - - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + + T gas = (T)level.blobs.get( type ); + + if (gas == null) { + gas = Reflection.newInstance(type); } + + if (gas != null){ + level.blobs.put( type, gas ); + gas.seed( level, cell, amount ); + } + + return gas; } public static int volumeAt( int cell, Class type){ diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/blobs/WaterOfTransmutation.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/blobs/WaterOfTransmutation.java index b7ea6b910..351906b72 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/blobs/WaterOfTransmutation.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/blobs/WaterOfTransmutation.java @@ -44,6 +44,7 @@ import com.shatteredpixel.shatteredpixeldungeon.journal.Notes.Landmark; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.plants.Plant; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; public class WaterOfTransmutation extends WellWater { @@ -119,12 +120,7 @@ public class WaterOfTransmutation extends WellWater { Category c = Generator.wepTiers[w.tier-1]; do { - try { - n = (MeleeWeapon)c.classes[Random.chances(c.probs)].newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + n = (MeleeWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]); } while (Challenges.isItemBlocked(n) || n.getClass() == w.getClass()); int level = w.level(); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java index 0ce9e81bc..1436025ea 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java @@ -26,6 +26,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Actor; import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator; import com.watabou.noosa.Image; +import com.watabou.utils.Reflection; import java.text.DecimalFormat; import java.util.HashSet; @@ -113,14 +114,9 @@ public class Buff extends Actor { //creates a fresh instance of the buff and attaches that, this allows duplication. public static T append( Char target, Class buffClass ) { - try { - T buff = buffClass.newInstance(); - buff.attachTo( target ); - return buff; - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + T buff = Reflection.newInstance(buffClass); + buff.attachTo( target ); + return buff; } public static T append( Char target, Class buffClass, float duration ) { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java index 7172da5e0..48abf4afd 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Mob.java @@ -63,6 +63,7 @@ import com.watabou.utils.Bundle; import com.watabou.utils.GameMath; import com.watabou.utils.PathFinder; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.Collections; @@ -153,13 +154,7 @@ public abstract class Mob extends Char { } public CharSprite sprite() { - CharSprite sprite = null; - try { - sprite = spriteClass.newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } - return sprite; + return Reflection.newInstance(spriteClass); } @Override diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Slime.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Slime.java index cf0527810..dc601228d 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Slime.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/Slime.java @@ -28,6 +28,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.items.weapon.melee.MeleeWeapon; import com.shatteredpixel.shatteredpixeldungeon.sprites.SlimeSprite; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; public class Slime extends Mob { @@ -78,15 +79,10 @@ public class Slime extends Mob { @Override protected Item createLoot() { - try { - Generator.Category c = Generator.Category.WEP_T2; - MeleeWeapon w = (MeleeWeapon)c.classes[Random.chances(c.probs)].newInstance(); - w.random(); - w.level(0); - return w; - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + Generator.Category c = Generator.Category.WEP_T2; + MeleeWeapon w = (MeleeWeapon) Reflection.newInstance(c.classes[Random.chances(c.probs)]); + w.random(); + w.level(0); + return w; } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Ghost.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Ghost.java index f19165fcf..b59535b93 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Ghost.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/mobs/npcs/Ghost.java @@ -54,6 +54,7 @@ import com.shatteredpixel.shatteredpixeldungeon.windows.WndSadGhost; import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; public class Ghost extends NPC { @@ -287,15 +288,9 @@ public class Ghost extends NPC { wepTier = 5; armor = new PlateArmor(); } - - try { - do { - weapon = (Weapon) Generator.wepTiers[wepTier - 1].classes[Random.chances(Generator.wepTiers[wepTier - 1].probs)].newInstance(); - } while (!(weapon instanceof MeleeWeapon)); - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - weapon = new Shortsword(); - } + + Generator.Category c = Generator.wepTiers[wepTier - 1]; + weapon = (MeleeWeapon) Reflection.newInstance(c.classes[Random.chances(c.probs)]); //50%:+0, 30%:+1, 15%:+2, 5%:+3 float itemLevelRoll = Random.Float(); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Generator.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Generator.java index fa01c6028..b544ea6dc 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Generator.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Generator.java @@ -171,6 +171,7 @@ import com.shatteredpixel.shatteredpixeldungeon.plants.Swiftthistle; import com.watabou.utils.Bundle; import com.watabou.utils.GameMath; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -479,42 +480,24 @@ public class Generator { } public static Item random( Category cat ) { - try { - - switch (cat) { - case ARMOR: - return randomArmor(); - case WEAPON: - return randomWeapon(); - case MISSILE: - return randomMissile(); - case ARTIFACT: - Item item = randomArtifact(); - //if we're out of artifacts, return a ring instead. - return item != null ? item : random(Category.RING); - default: - return ((Item)cat.classes[Random.chances( cat.probs )].newInstance()).random(); - } - - } catch (Exception e) { - - ShatteredPixelDungeon.reportException(e); - return null; - + switch (cat) { + case ARMOR: + return randomArmor(); + case WEAPON: + return randomWeapon(); + case MISSILE: + return randomMissile(); + case ARTIFACT: + Item item = randomArtifact(); + //if we're out of artifacts, return a ring instead. + return item != null ? item : random(Category.RING); + default: + return ((Item) Reflection.newInstance(cat.classes[Random.chances( cat.probs )])).random(); } } public static Item random( Class cl ) { - try { - - return ((Item)cl.newInstance()).random(); - - } catch (Exception e) { - - ShatteredPixelDungeon.reportException(e); - return null; - - } + return Reflection.newInstance(cl).random(); } public static Armor randomArmor(){ @@ -524,15 +507,10 @@ public class Generator { public static Armor randomArmor(int floorSet) { floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1); - - try { - Armor a = (Armor)Category.ARMOR.classes[Random.chances(floorSetTierProbs[floorSet])].newInstance(); - a.random(); - return a; - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + + Armor a = (Armor)Reflection.newInstance(Category.ARMOR.classes[Random.chances(floorSetTierProbs[floorSet])]); + a.random(); + return a; } public static final Category[] wepTiers = new Category[]{ @@ -550,16 +528,11 @@ public class Generator { public static MeleeWeapon randomWeapon(int floorSet) { floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1); - - try { - Category c = wepTiers[Random.chances(floorSetTierProbs[floorSet])]; - MeleeWeapon w = (MeleeWeapon)c.classes[Random.chances(c.probs)].newInstance(); - w.random(); - return w; - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + + Category c = wepTiers[Random.chances(floorSetTierProbs[floorSet])]; + MeleeWeapon w = (MeleeWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]); + w.random(); + return w; } public static final Category[] misTiers = new Category[]{ @@ -578,43 +551,30 @@ public class Generator { floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1); - try { - Category c = misTiers[Random.chances(floorSetTierProbs[floorSet])]; - MissileWeapon w = (MissileWeapon)c.classes[Random.chances(c.probs)].newInstance(); - w.random(); - return w; - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + Category c = misTiers[Random.chances(floorSetTierProbs[floorSet])]; + MissileWeapon w = (MissileWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]); + w.random(); + return w; } //enforces uniqueness of artifacts throughout a run. public static Artifact randomArtifact() { - try { - Category cat = Category.ARTIFACT; - int i = Random.chances( cat.probs ); + Category cat = Category.ARTIFACT; + int i = Random.chances( cat.probs ); - //if no artifacts are left, return null - if (i == -1){ - return null; - } - - Class art = (Class) cat.classes[i]; + //if no artifacts are left, return null + if (i == -1){ + return null; + } + + Class art = (Class) cat.classes[i]; - if (removeArtifact(art)) { - Artifact artifact = art.newInstance(); - - artifact.random(); - - return artifact; - } else { - return null; - } - - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); + if (removeArtifact(art)) { + Artifact artifact = Reflection.newInstance(art); + artifact.random(); + return artifact; + } else { return null; } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java index ed66e069e..60c811397 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java @@ -44,7 +44,9 @@ import com.watabou.noosa.particles.Emitter; import com.watabou.utils.Bundlable; import com.watabou.utils.Bundle; import com.watabou.utils.Callback; +import com.watabou.utils.Reflection; +import java.sql.Ref; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -219,21 +221,20 @@ public class Item implements Bundlable { if (amount <= 0 || amount >= quantity()) { return null; } else { - try { - - //pssh, who needs copy constructors? - Item split = getClass().newInstance(); - Bundle copy = new Bundle(); - this.storeInBundle(copy); - split.restoreFromBundle(copy); - split.quantity(amount); - quantity -= amount; - - return split; - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); + //pssh, who needs copy constructors? + Item split = Reflection.newInstance(getClass()); + + if (split == null){ return null; } + + Bundle copy = new Bundle(); + this.storeInBundle(copy); + split.restoreFromBundle(copy); + split.quantity(amount); + quantity -= amount; + + return split; } } @@ -426,17 +427,12 @@ public class Item implements Bundlable { } public Item virtual(){ - try { - - Item item = getClass().newInstance(); - item.quantity = 0; - item.level = level; - return item; - - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + Item item = Reflection.newInstance(getClass()); + if (item == null) return null; + + item.quantity = 0; + item.level = level; + return item; } public Item random() { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Recipe.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Recipe.java index 8033f9aa0..c1ed9b1a7 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Recipe.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Recipe.java @@ -57,6 +57,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.spells.Recycle; import com.shatteredpixel.shatteredpixeldungeon.items.spells.WildEnergy; import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand; import com.shatteredpixel.shatteredpixeldungeon.items.weapon.missiles.darts.Dart; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -86,15 +87,10 @@ public abstract class Recipe { //gets a simple list of items based on inputs public ArrayList getIngredients() { ArrayList result = new ArrayList<>(); - try { - for (int i = 0; i < inputs.length; i++) { - Item ingredient = inputs[i].newInstance(); - ingredient.quantity(inQuantity[i]); - result.add(ingredient); - } - } catch (Exception e){ - ShatteredPixelDungeon.reportException( e ); - return null; + for (int i = 0; i < inputs.length; i++) { + Item ingredient = Reflection.newInstance(inputs[i]); + ingredient.quantity(inQuantity[i]); + result.add(ingredient); } return result; } @@ -154,7 +150,7 @@ public abstract class Recipe { //ingredients are ignored, as output doesn't vary public final Item sampleOutput(ArrayList ingredients){ try { - Item result = output.newInstance(); + Item result = Reflection.newInstance(output); result.quantity(outQuantity); return result; } catch (Exception e) { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/Armor.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/Armor.java index 4b42cbd10..cb6e61ad9 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/Armor.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/Armor.java @@ -65,6 +65,7 @@ import com.watabou.noosa.particles.Emitter; import com.watabou.utils.Bundlable; import com.watabou.utils.Bundle; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.Arrays; @@ -621,65 +622,45 @@ public class Armor extends EquipableItem { @SuppressWarnings("unchecked") public static Glyph randomCommon( Class ... toIgnore ){ - try { - ArrayList> glyphs = new ArrayList<>(Arrays.asList(common)); - glyphs.removeAll(Arrays.asList(toIgnore)); - if (glyphs.isEmpty()) { - return random(); - } else { - return (Glyph) Random.element(glyphs).newInstance(); - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + ArrayList> glyphs = new ArrayList<>(Arrays.asList(common)); + glyphs.removeAll(Arrays.asList(toIgnore)); + if (glyphs.isEmpty()) { + return random(); + } else { + return (Glyph) Reflection.newInstance(Random.element(glyphs)); } } @SuppressWarnings("unchecked") public static Glyph randomUncommon( Class ... toIgnore ){ - try { - ArrayList> glyphs = new ArrayList<>(Arrays.asList(uncommon)); - glyphs.removeAll(Arrays.asList(toIgnore)); - if (glyphs.isEmpty()) { - return random(); - } else { - return (Glyph) Random.element(glyphs).newInstance(); - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + ArrayList> glyphs = new ArrayList<>(Arrays.asList(uncommon)); + glyphs.removeAll(Arrays.asList(toIgnore)); + if (glyphs.isEmpty()) { + return random(); + } else { + return (Glyph) Reflection.newInstance(Random.element(glyphs)); } } @SuppressWarnings("unchecked") public static Glyph randomRare( Class ... toIgnore ){ - try { - ArrayList> glyphs = new ArrayList<>(Arrays.asList(rare)); - glyphs.removeAll(Arrays.asList(toIgnore)); - if (glyphs.isEmpty()) { - return random(); - } else { - return (Glyph) Random.element(glyphs).newInstance(); - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + ArrayList> glyphs = new ArrayList<>(Arrays.asList(rare)); + glyphs.removeAll(Arrays.asList(toIgnore)); + if (glyphs.isEmpty()) { + return random(); + } else { + return (Glyph) Reflection.newInstance(Random.element(glyphs)); } } @SuppressWarnings("unchecked") public static Glyph randomCurse( Class ... toIgnore ){ - try { - ArrayList> glyphs = new ArrayList<>(Arrays.asList(curses)); - glyphs.removeAll(Arrays.asList(toIgnore)); - if (glyphs.isEmpty()) { - return random(); - } else { - return (Glyph) Random.element(glyphs).newInstance(); - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + ArrayList> glyphs = new ArrayList<>(Arrays.asList(curses)); + glyphs.removeAll(Arrays.asList(toIgnore)); + if (glyphs.isEmpty()) { + return random(); + } else { + return (Glyph) Reflection.newInstance(Random.element(glyphs)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/curses/Multiplicity.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/curses/Multiplicity.java index ba9bc63f7..97f64043c 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/curses/Multiplicity.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/curses/Multiplicity.java @@ -39,6 +39,7 @@ import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSprite; import com.watabou.utils.Bundle; import com.watabou.utils.PathFinder; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -72,30 +73,27 @@ public class Multiplicity extends Armor.Glyph { || attacker instanceof Mimic || attacker instanceof Statue){ m = Dungeon.level.createMob(); } else { - try { - Actor.fixTime(); + Actor.fixTime(); + + m = (Mob)Reflection.newInstance(attacker.getClass()); + + if (m != null) { - m = (Mob)attacker.getClass().newInstance(); Bundle store = new Bundle(); attacker.storeInBundle(store); m.restoreFromBundle(store); m.pos = 0; m.HP = m.HT; - if (m.buff(PinCushion.class) != null){ + if (m.buff(PinCushion.class) != null) { m.remove(m.buff(PinCushion.class)); } - + //If a thief has stolen an item, that item is not duplicated. - if (m instanceof Thief){ + if (m instanceof Thief) { ((Thief) m).item = null; } - - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - m = null; } } - } if (m != null) { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/artifacts/UnstableSpellbook.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/artifacts/UnstableSpellbook.java index 16a45d4e1..d10d49aa9 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/artifacts/UnstableSpellbook.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/artifacts/UnstableSpellbook.java @@ -46,6 +46,7 @@ import com.shatteredpixel.shatteredpixeldungeon.windows.WndOptions; import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.Collections; @@ -138,13 +139,9 @@ public class UnstableSpellbook extends Artifact { @Override protected void onSelect(int index) { if (index == 1){ - try { - Scroll scroll = ExoticScroll.regToExo.get(fScroll.getClass()).newInstance(); - charge --; - scroll.doRead(); - } catch ( Exception e) { - ShatteredPixelDungeon.reportException(e); - } + Scroll scroll = Reflection.newInstance(ExoticScroll.regToExo.get(fScroll.getClass())); + charge--; + scroll.doRead(); } else { fScroll.doRead(); } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bombs/Bomb.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bombs/Bomb.java index e73cff3e0..1784f604e 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bombs/Bomb.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bombs/Bomb.java @@ -55,6 +55,7 @@ import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; import com.watabou.utils.PathFinder; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -399,11 +400,7 @@ public class Bomb extends Item { for (Item i : ingredients){ i.quantity(i.quantity()-1); if (validIngredients.containsKey(i.getClass())){ - try { - result = validIngredients.get(i.getClass()).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + result = Reflection.newInstance(validIngredients.get(i.getClass())); } } @@ -414,11 +411,7 @@ public class Bomb extends Item { public Item sampleOutput(ArrayList ingredients) { for (Item i : ingredients){ if (validIngredients.containsKey(i.getClass())){ - try { - return validIngredients.get(i.getClass()).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + return Reflection.newInstance(validIngredients.get(i.getClass())); } } return null; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/food/Blandfruit.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/food/Blandfruit.java index 810869073..fb82e2a7a 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/food/Blandfruit.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/food/Blandfruit.java @@ -49,6 +49,7 @@ import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSprite; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.watabou.utils.Bundle; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -124,14 +125,7 @@ public class Blandfruit extends Food { } public Item cook(Seed seed){ - - try { - return imbuePotion(Potion.SeedToPotion.types.get(seed.getClass()).newInstance()); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } - + return imbuePotion(Reflection.newInstance(Potion.SeedToPotion.types.get(seed.getClass()))); } public Item imbuePotion(Potion potion){ diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/AlchemicalCatalyst.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/AlchemicalCatalyst.java index 7bbeb393e..434a04b0e 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/AlchemicalCatalyst.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/AlchemicalCatalyst.java @@ -29,6 +29,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.stones.Runestone; import com.shatteredpixel.shatteredpixeldungeon.plants.Plant; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -57,25 +58,17 @@ public class AlchemicalCatalyst extends Potion { @Override public void apply(Hero hero) { - try { - Potion p = Random.chances(potionChances).newInstance(); - p.anonymize(); - p.apply(hero); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + Potion p = Reflection.newInstance(Random.chances(potionChances)); + p.anonymize(); + p.apply(hero); } @Override public void shatter(int cell) { - try { - Potion p = Random.chances(potionChances).newInstance(); - p.anonymize(); - curItem = p; - p.shatter(cell); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + Potion p = Reflection.newInstance(Random.chances(potionChances)); + p.anonymize(); + curItem = p; + p.shatter(cell); } @Override diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/Potion.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/Potion.java index 87209fba8..562aaab78 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/Potion.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/Potion.java @@ -73,6 +73,7 @@ import com.shatteredpixel.shatteredpixeldungeon.windows.WndOptions; import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -516,14 +517,7 @@ public class Potion extends Item { result = Generator.random( Generator.Category.POTION ); } else { - - Class itemClass = types.get(Random.element(ingredients).getClass()); - try { - result = itemClass.newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - result = Generator.random( Generator.Category.POTION ); - } + result = Reflection.newInstance(types.get(Random.element(ingredients).getClass())); } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/exotic/ExoticPotion.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/exotic/ExoticPotion.java index 3fbede664..225e57c42 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/exotic/ExoticPotion.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/potions/exotic/ExoticPotion.java @@ -39,6 +39,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfPurity; import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfStrength; import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfToxicGas; import com.shatteredpixel.shatteredpixeldungeon.plants.Plant; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -118,12 +119,7 @@ public class ExoticPotion extends Potion { @Override //20 gold more than its none-exotic equivalent public int price() { - try { - return (exoToReg.get(getClass()).newInstance().price() + 20) * quantity; - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - return 0; - } + return (Reflection.newInstance(exoToReg.get(getClass())).price() + 20) * quantity; } public static class PotionToExotic extends Recipe{ @@ -156,11 +152,7 @@ public class ExoticPotion extends Potion { for (Item i : ingredients){ i.quantity(i.quantity()-1); if (regToExo.containsKey(i.getClass())) { - try { - result = regToExo.get(i.getClass()).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + result = Reflection.newInstance(regToExo.get(i.getClass())); } } return result; @@ -170,11 +162,7 @@ public class ExoticPotion extends Potion { public Item sampleOutput(ArrayList ingredients) { for (Item i : ingredients){ if (regToExo.containsKey(i.getClass())) { - try { - return regToExo.get(i.getClass()).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + return Reflection.newInstance(regToExo.get(i.getClass())); } } return null; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/Scroll.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/Scroll.java index 0b2dabb42..02e16e002 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/Scroll.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/Scroll.java @@ -51,6 +51,7 @@ import com.shatteredpixel.shatteredpixeldungeon.sprites.HeroSprite; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.watabou.utils.Bundle; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -355,25 +356,15 @@ public abstract class Scroll extends Item { s.quantity(s.quantity() - 1); - try{ - return stones.get(s.getClass()).newInstance().quantity(amnts.get(s.getClass())); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + return Reflection.newInstance(stones.get(s.getClass())).quantity(amnts.get(s.getClass())); } @Override public Item sampleOutput(ArrayList ingredients) { if (!testIngredients(ingredients)) return null; - try{ - Scroll s = (Scroll) ingredients.get(0); - return stones.get(s.getClass()).newInstance().quantity(amnts.get(s.getClass())); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + Scroll s = (Scroll) ingredients.get(0); + return Reflection.newInstance(stones.get(s.getClass())).quantity(amnts.get(s.getClass())); } } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/ScrollOfTransmutation.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/ScrollOfTransmutation.java index 831fef098..a9925174c 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/ScrollOfTransmutation.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/ScrollOfTransmutation.java @@ -48,6 +48,7 @@ import com.shatteredpixel.shatteredpixeldungeon.plants.Plant; import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; public class ScrollOfTransmutation extends InventoryScroll { @@ -150,12 +151,7 @@ public class ScrollOfTransmutation extends InventoryScroll { } do { - try { - n = (Weapon)c.classes[Random.chances(c.probs)].newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + n = (Weapon) Reflection.newInstance(c.classes[Random.chances(c.probs)]); } while (Challenges.isItemBlocked(n) || n.getClass() == w.getClass()); int level = w.level(); @@ -256,28 +252,18 @@ public class ScrollOfTransmutation extends InventoryScroll { } private Scroll changeScroll( Scroll s ) { - try { - if (s instanceof ExoticScroll) { - return ExoticScroll.exoToReg.get(s.getClass()).newInstance(); - } else { - return ExoticScroll.regToExo.get(s.getClass()).newInstance(); - } - } catch ( Exception e ){ - ShatteredPixelDungeon.reportException(e); - return null; + if (s instanceof ExoticScroll) { + return Reflection.newInstance(ExoticScroll.exoToReg.get(s.getClass())); + } else { + return Reflection.newInstance(ExoticScroll.regToExo.get(s.getClass())); } } private Potion changePotion( Potion p ) { - try { - if (p instanceof ExoticPotion) { - return ExoticPotion.exoToReg.get(p.getClass()).newInstance(); - } else { - return ExoticPotion.regToExo.get(p.getClass()).newInstance(); - } - } catch ( Exception e ){ - ShatteredPixelDungeon.reportException(e); - return null; + if (p instanceof ExoticPotion) { + return Reflection.newInstance(ExoticPotion.exoToReg.get(p.getClass())); + } else { + return Reflection.newInstance(ExoticPotion.regToExo.get(p.getClass())); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ExoticScroll.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ExoticScroll.java index 1488f42ec..ca89e15b6 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ExoticScroll.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ExoticScroll.java @@ -38,6 +38,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.ScrollOfTerror; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.ScrollOfTransmutation; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.ScrollOfUpgrade; import com.shatteredpixel.shatteredpixeldungeon.items.stones.Runestone; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -118,12 +119,7 @@ public abstract class ExoticScroll extends Scroll { @Override //20 gold more than its none-exotic equivalent public int price() { - try { - return (exoToReg.get(getClass()).newInstance().price() + 20) * quantity; - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - return 0; - } + return (Reflection.newInstance(exoToReg.get(getClass())).price() + 20) * quantity; } public static class ScrollToExotic extends Recipe { @@ -156,11 +152,7 @@ public abstract class ExoticScroll extends Scroll { for (Item i : ingredients){ i.quantity(i.quantity()-1); if (regToExo.containsKey(i.getClass())) { - try { - result = regToExo.get(i.getClass()).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + result = Reflection.newInstance(regToExo.get(i.getClass())); } } return result; @@ -170,11 +162,7 @@ public abstract class ExoticScroll extends Scroll { public Item sampleOutput(ArrayList ingredients) { for (Item i : ingredients){ if (regToExo.containsKey(i.getClass())) { - try { - return regToExo.get(i.getClass()).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + return Reflection.newInstance(regToExo.get(i.getClass())); } } return null; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ScrollOfDivination.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ScrollOfDivination.java index 6b500db15..1adf582fc 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ScrollOfDivination.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/scrolls/exotic/ScrollOfDivination.java @@ -39,6 +39,7 @@ import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.shatteredpixel.shatteredpixeldungeon.windows.IconTitle; import com.watabou.noosa.audio.Sample; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashSet; @@ -78,47 +79,43 @@ public class ScrollOfDivination extends ExoticScroll { float[] probs = baseProbs.clone(); while (left > 0 && total > 0) { - try { - switch (Random.chances(probs)) { - default: - probs = baseProbs.clone(); + switch (Random.chances(probs)) { + default: + probs = baseProbs.clone(); + continue; + case 0: + if (potions.isEmpty()) { + probs[0] = 0; continue; - case 0: - if (potions.isEmpty()) { - probs[0] = 0; - continue; - } - probs[0]--; - Potion p = Random.element(potions).newInstance(); - p.setKnown(); - IDed.add(p); - potions.remove(p.getClass()); - break; - case 1: - if (scrolls.isEmpty()) { - probs[1] = 0; - continue; - } - probs[1]--; - Scroll s = Random.element(scrolls).newInstance(); - s.setKnown(); - IDed.add(s); - scrolls.remove(s.getClass()); - break; - case 2: - if (rings.isEmpty()) { - probs[2] = 0; - continue; - } - probs[2]--; - Ring r = Random.element(rings).newInstance(); - r.setKnown(); - IDed.add(r); - rings.remove(r.getClass()); - break; - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); + } + probs[0]--; + Potion p = Reflection.newInstance(Random.element(potions)); + p.setKnown(); + IDed.add(p); + potions.remove(p.getClass()); + break; + case 1: + if (scrolls.isEmpty()) { + probs[1] = 0; + continue; + } + probs[1]--; + Scroll s = Reflection.newInstance(Random.element(scrolls)); + s.setKnown(); + IDed.add(s); + scrolls.remove(s.getClass()); + break; + case 2: + if (rings.isEmpty()) { + probs[2] = 0; + continue; + } + probs[2]--; + Ring r = Reflection.newInstance(Random.element(rings)); + r.setKnown(); + IDed.add(r); + rings.remove(r.getClass()); + break; } left --; total --; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/ArcaneCatalyst.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/ArcaneCatalyst.java index 61f043953..66ceba320 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/ArcaneCatalyst.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/ArcaneCatalyst.java @@ -41,6 +41,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.stones.Runestone; import com.shatteredpixel.shatteredpixeldungeon.plants.Plant; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -72,14 +73,10 @@ public class ArcaneCatalyst extends Spell { detach( curUser.belongings.backpack ); updateQuickslot(); - try { - Scroll s = Random.chances(scrollChances).newInstance(); - s.anonymize(); - curItem = s; - s.doRead(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + Scroll s = Reflection.newInstance(Random.chances(scrollChances)); + s.anonymize(); + curItem = s; + s.doRead(); } @Override diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/ReclaimTrap.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/ReclaimTrap.java index 5f3fe3436..e668ebeac 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/ReclaimTrap.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/ReclaimTrap.java @@ -36,6 +36,7 @@ import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; +import com.watabou.utils.Reflection; public class ReclaimTrap extends TargetedSpell { @@ -62,16 +63,12 @@ public class ReclaimTrap extends TargetedSpell { } } else { - try { - Trap t = storedTrap.newInstance(); - storedTrap = null; - - t.pos = bolt.collisionPos; - t.activate(); - - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + Trap t = Reflection.newInstance(storedTrap); + storedTrap = null; + + t.pos = bolt.collisionPos; + t.activate(); + } } @@ -111,11 +108,7 @@ public class ReclaimTrap extends TargetedSpell { @Override public ItemSprite.Glowing glowing() { if (storedTrap != null){ - try { - return COLORS[storedTrap.newInstance().color]; - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + return COLORS[Reflection.newInstance(storedTrap).color]; } return null; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/Recycle.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/Recycle.java index d55113c94..edf12455f 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/Recycle.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/spells/Recycle.java @@ -39,6 +39,7 @@ import com.shatteredpixel.shatteredpixeldungeon.plants.Plant; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag; +import com.watabou.utils.Reflection; public class Recycle extends InventorySpell { @@ -54,22 +55,12 @@ public class Recycle extends InventorySpell { if (item instanceof Potion) { result = Generator.random(Generator.Category.POTION); if (item instanceof ExoticPotion){ - try { - result = ExoticPotion.regToExo.get(result.getClass()).newInstance(); - } catch ( Exception e ){ - ShatteredPixelDungeon.reportException(e); - result = item; - } + result = Reflection.newInstance(ExoticPotion.regToExo.get(result.getClass())); } } else if (item instanceof Scroll) { result = Generator.random(Generator.Category.SCROLL); if (item instanceof ExoticScroll){ - try { - result = ExoticScroll.regToExo.get(result.getClass()).newInstance(); - } catch ( Exception e ){ - ShatteredPixelDungeon.reportException(e); - result = item; - } + result = Reflection.newInstance(ExoticScroll.regToExo.get(result.getClass())); } } else if (item instanceof Plant.Seed) { result = Generator.random(Generator.Category.SEED); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/Weapon.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/Weapon.java index 1aecf35fb..dfda976ea 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/Weapon.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/Weapon.java @@ -57,6 +57,7 @@ import com.shatteredpixel.shatteredpixeldungeon.utils.GLog; import com.watabou.utils.Bundlable; import com.watabou.utils.Bundle; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.Arrays; @@ -371,65 +372,45 @@ abstract public class Weapon extends KindOfWeapon { @SuppressWarnings("unchecked") public static Enchantment randomCommon( Class ... toIgnore ) { - try { - ArrayList> enchants = new ArrayList<>(Arrays.asList(common)); - enchants.removeAll(Arrays.asList(toIgnore)); - if (enchants.isEmpty()) { - return random(); - } else { - return (Enchantment) Random.element(enchants).newInstance(); - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + ArrayList> enchants = new ArrayList<>(Arrays.asList(common)); + enchants.removeAll(Arrays.asList(toIgnore)); + if (enchants.isEmpty()) { + return random(); + } else { + return (Enchantment) Reflection.newInstance(Random.element(enchants)); } } @SuppressWarnings("unchecked") public static Enchantment randomUncommon( Class ... toIgnore ) { - try { - ArrayList> enchants = new ArrayList<>(Arrays.asList(uncommon)); - enchants.removeAll(Arrays.asList(toIgnore)); - if (enchants.isEmpty()) { - return random(); - } else { - return (Enchantment) Random.element(enchants).newInstance(); - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + ArrayList> enchants = new ArrayList<>(Arrays.asList(uncommon)); + enchants.removeAll(Arrays.asList(toIgnore)); + if (enchants.isEmpty()) { + return random(); + } else { + return (Enchantment) Reflection.newInstance(Random.element(enchants)); } } @SuppressWarnings("unchecked") public static Enchantment randomRare( Class ... toIgnore ) { - try { - ArrayList> enchants = new ArrayList<>(Arrays.asList(rare)); - enchants.removeAll(Arrays.asList(toIgnore)); - if (enchants.isEmpty()) { - return random(); - } else { - return (Enchantment) Random.element(enchants).newInstance(); - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + ArrayList> enchants = new ArrayList<>(Arrays.asList(rare)); + enchants.removeAll(Arrays.asList(toIgnore)); + if (enchants.isEmpty()) { + return random(); + } else { + return (Enchantment) Reflection.newInstance(Random.element(enchants)); } } @SuppressWarnings("unchecked") public static Enchantment randomCurse( Class ... toIgnore ){ - try { - ArrayList> enchants = new ArrayList<>(Arrays.asList(curses)); - enchants.removeAll(Arrays.asList(toIgnore)); - if (enchants.isEmpty()) { - return random(); - } else { - return (Enchantment) Random.element(enchants).newInstance(); - } - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + ArrayList> enchants = new ArrayList<>(Arrays.asList(curses)); + enchants.removeAll(Arrays.asList(toIgnore)); + if (enchants.isEmpty()) { + return random(); + } else { + return (Enchantment) Reflection.newInstance(Random.element(enchants)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/enchantments/Unstable.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/enchantments/Unstable.java index f14145bc2..163d22fc1 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/enchantments/Unstable.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/enchantments/Unstable.java @@ -26,6 +26,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.items.weapon.Weapon; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSprite; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; public class Unstable extends Weapon.Enchantment { @@ -55,11 +56,7 @@ public class Unstable extends Weapon.Enchantment { attacker.buff(Kinetic.ConservedDamage.class).detach(); } - try { - damage = Random.oneOf(randomEnchants).newInstance().proc( weapon, attacker, defender, damage ); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + damage = Reflection.newInstance(Random.oneOf(randomEnchants)).proc( weapon, attacker, defender, damage ); return damage + conservedDamage; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/missiles/darts/TippedDart.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/missiles/darts/TippedDart.java index 49e212fc3..d2a0c89b4 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/missiles/darts/TippedDart.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/weapon/missiles/darts/TippedDart.java @@ -45,6 +45,7 @@ import com.shatteredpixel.shatteredpixeldungeon.plants.Sungrass; import com.shatteredpixel.shatteredpixeldungeon.plants.Swiftthistle; import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; import com.shatteredpixel.shatteredpixeldungeon.windows.WndOptions; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.HashMap; @@ -156,12 +157,7 @@ public abstract class TippedDart extends Dart { } public static TippedDart getTipped( Plant.Seed s, int quantity ){ - try { - return (TippedDart) types.get(s.getClass()).newInstance().quantity(quantity); - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - return null; - } + return (TippedDart) Reflection.newInstance(types.get(s.getClass())).quantity(quantity); } public static TippedDart randomTipped( int quantity ){ diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java index 213261df5..e4b16a347 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java @@ -81,6 +81,7 @@ import com.watabou.utils.Bundle; import com.watabou.utils.PathFinder; import com.watabou.utils.Point; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import com.watabou.utils.SparseArray; import java.util.ArrayList; @@ -439,15 +440,11 @@ public abstract class Level implements Bundlable { private ArrayList> mobsToSpawn = new ArrayList<>(); public Mob createMob() { - if (mobsToSpawn == null || mobsToSpawn.isEmpty()) + if (mobsToSpawn == null || mobsToSpawn.isEmpty()) { mobsToSpawn = Bestiary.getMobRotation(Dungeon.depth); - - try { - return mobsToSpawn.remove(0).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; } + + return Reflection.newInstance(mobsToSpawn.remove(0)); } abstract protected void createMobs(); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/RegularPainter.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/RegularPainter.java index ad11d1325..e5b6f9056 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/RegularPainter.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/RegularPainter.java @@ -34,6 +34,7 @@ import com.watabou.utils.PathFinder; import com.watabou.utils.Point; import com.watabou.utils.Random; import com.watabou.utils.Rect; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -356,14 +357,10 @@ public abstract class RegularPainter extends Painter { Integer trapPos = Random.element(validCells); validCells.remove(trapPos); //removes the integer object, not at the index - try { - Trap trap = trapClasses[Random.chances( trapChances )].newInstance().hide(); - l.setTrap( trap, trapPos ); - //some traps will not be hidden - l.map[trapPos] = trap.visible ? Terrain.TRAP : Terrain.SECRET_TRAP; - } catch (Exception e) { - throw new RuntimeException(e); - } + Trap trap = Reflection.newInstance(trapClasses[Random.chances( trapChances )]).hide(); + l.setTrap( trap, trapPos ); + //some traps will not be hidden + l.map[trapPos] = trap.visible ? Terrain.TRAP : Terrain.SECRET_TRAP; } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/ConnectionRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/ConnectionRoom.java index fcb0b5068..97cba3651 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/ConnectionRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/ConnectionRoom.java @@ -26,6 +26,7 @@ import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.Room; import com.watabou.utils.Point; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -86,11 +87,6 @@ public abstract class ConnectionRoom extends Room { } public static ConnectionRoom createRoom(){ - try { - return rooms.get(Random.chances(chances[Dungeon.depth])).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + return Reflection.newInstance(rooms.get(Random.chances(chances[Dungeon.depth]))); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretHoardRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretHoardRoom.java index af1c59812..a3d47b523 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretHoardRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretHoardRoom.java @@ -34,6 +34,7 @@ import com.shatteredpixel.shatteredpixeldungeon.levels.traps.RockfallTrap; import com.shatteredpixel.shatteredpixeldungeon.levels.traps.Trap; import com.watabou.utils.Point; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; public class SecretHoardRoom extends SecretRoom { @@ -68,12 +69,8 @@ public class SecretHoardRoom extends SecretRoom { for (Point p : getPoints()){ if (Random.Int(2) == 0 && level.map[level.pointToCell(p)] == Terrain.EMPTY){ - try { - level.setTrap(trapClass.newInstance().reveal(), level.pointToCell(p)); - Painter.set(level, p, Terrain.TRAP); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + level.setTrap(Reflection.newInstance(trapClass).reveal(), level.pointToCell(p)); + Painter.set(level, p, Terrain.TRAP); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretLaboratoryRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretLaboratoryRoom.java index 2efe6dbe4..72f3d55b0 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretLaboratoryRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretLaboratoryRoom.java @@ -41,6 +41,7 @@ import com.shatteredpixel.shatteredpixeldungeon.levels.Terrain; import com.shatteredpixel.shatteredpixeldungeon.levels.painters.Painter; import com.watabou.utils.Point; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.HashMap; @@ -81,14 +82,9 @@ public class SecretLaboratoryRoom extends SecretRoom { pos = level.pointToCell(random()); } while (level.map[pos] != Terrain.EMPTY_SP || level.heaps.get( pos ) != null); - try{ - Class potionCls = Random.chances(chances); - chances.put(potionCls, 0f); - level.drop( potionCls.newInstance(), pos ); - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - } - + Class potionCls = Random.chances(chances); + chances.put(potionCls, 0f); + level.drop( Reflection.newInstance(potionCls), pos ); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretLibraryRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretLibraryRoom.java index e75b4addd..5f5aa0375 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretLibraryRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretLibraryRoom.java @@ -38,6 +38,7 @@ import com.shatteredpixel.shatteredpixeldungeon.levels.Level; import com.shatteredpixel.shatteredpixeldungeon.levels.Terrain; import com.shatteredpixel.shatteredpixeldungeon.levels.painters.Painter; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.HashMap; @@ -91,14 +92,9 @@ public class SecretLibraryRoom extends SecretRoom { pos = level.pointToCell(random()); } while (level.map[pos] != Terrain.EMPTY_SP || level.heaps.get( pos ) != null); - try{ - Class scrollCls = Random.chances(chances); - chances.put(scrollCls, 0f); - level.drop( scrollCls.newInstance(), pos ); - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - } - + Class scrollCls = Random.chances(chances); + chances.put(scrollCls, 0f); + level.drop( Reflection.newInstance(scrollCls), pos ); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretRoom.java index 6fe710634..14e0d993c 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretRoom.java @@ -27,6 +27,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.hero.HeroClass; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.special.SpecialRoom; import com.watabou.utils.Bundle; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.Arrays; @@ -102,11 +103,8 @@ public abstract class SecretRoom extends SpecialRoom { int newidx = Random.Int( runSecrets.size() ); if (newidx < index) index = newidx; } - try { - r = runSecrets.get( index ).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + + r = Reflection.newInstance(runSecrets.get( index )); runSecrets.add(runSecrets.remove(index)); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/SpecialRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/SpecialRoom.java index 3b4c736cd..a0fdb857b 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/SpecialRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/SpecialRoom.java @@ -26,6 +26,7 @@ import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.Room; import com.watabou.utils.Bundle; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.Arrays; @@ -147,11 +148,8 @@ public abstract class SpecialRoom extends Room { int newidx = Random.Int( floorSpecials.size() ); if (newidx < index) index = newidx; } - try { - r = floorSpecials.get( index ).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + + r = Reflection.newInstance(floorSpecials.get( index )); if (r instanceof WeakFloorRoom){ pitNeededDepth = Dungeon.depth + 1; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/TrapsRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/TrapsRoom.java index d3521b804..1fe1599ff 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/TrapsRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/TrapsRoom.java @@ -43,6 +43,7 @@ import com.shatteredpixel.shatteredpixeldungeon.levels.traps.Trap; import com.shatteredpixel.shatteredpixeldungeon.levels.traps.WarpingTrap; import com.watabou.utils.Point; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; public class TrapsRoom extends SpecialRoom { @@ -94,11 +95,7 @@ public class TrapsRoom extends SpecialRoom { for(Point p : getPoints()) { int cell = level.pointToCell(p); if (level.map[cell] == Terrain.TRAP){ - try { - level.setTrap(((Trap) trapClass.newInstance()).reveal(), cell); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + level.setTrap(Reflection.newInstance(trapClass).reveal(), cell); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/standard/StandardRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/standard/StandardRoom.java index 8ac22d700..16bca8a25 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/standard/StandardRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/standard/StandardRoom.java @@ -25,6 +25,7 @@ import com.shatteredpixel.shatteredpixeldungeon.Dungeon; import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.Room; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -156,12 +157,7 @@ public abstract class StandardRoom extends Room { public static StandardRoom createRoom(){ - try{ - return rooms.get(Random.chances(chances[Dungeon.depth])).newInstance(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; - } + return Reflection.newInstance(rooms.get(Random.chances(chances[Dungeon.depth]))); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/plants/Plant.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/plants/Plant.java index 7f6588547..6ed2b1e94 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/plants/Plant.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/plants/Plant.java @@ -41,6 +41,7 @@ import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundlable; import com.watabou.utils.Bundle; import com.watabou.utils.PathFinder; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -150,17 +151,12 @@ public abstract class Plant implements Bundlable { } public Plant couch( int pos, Level level ) { - try { - if (level != null && level.heroFOV != null && level.heroFOV[pos]) { - Sample.INSTANCE.play(Assets.SND_PLANT); - } - Plant plant = plantClass.newInstance(); - plant.pos = pos; - return plant; - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - return null; + if (level != null && level.heroFOV != null && level.heroFOV[pos]) { + Sample.INSTANCE.play(Assets.SND_PLANT); } + Plant plant = Reflection.newInstance(plantClass); + plant.pos = pos; + return plant; } @Override diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java index 620eaf4fc..75590678b 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java @@ -41,6 +41,7 @@ import com.watabou.noosa.Scene; import com.watabou.noosa.Visual; import com.watabou.noosa.ui.Component; import com.watabou.utils.BitmapCache; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -152,7 +153,7 @@ public class PixelScene extends Scene { if (getClass().equals(savedClass)){ for (Class w : savedWindows){ try{ - add(w.newInstance()); + add(Reflection.newInstanceUnhandled(w)); } catch (Exception e){ //window has no public zero-arg constructor, just eat the exception } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/AttackIndicator.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/AttackIndicator.java index 555dec0b6..3c397b140 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/AttackIndicator.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/AttackIndicator.java @@ -29,6 +29,7 @@ import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene; import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite; import com.watabou.noosa.Game; import com.watabou.utils.Random; +import com.watabou.utils.Reflection; import java.util.ArrayList; @@ -134,21 +135,16 @@ public class AttackIndicator extends Tag { sprite = null; } - try { - sprite = lastTarget.spriteClass.newInstance(); - active = true; - sprite.linkVisuals(lastTarget); - sprite.idle(); - sprite.paused = true; - add( sprite ); + sprite = Reflection.newInstance(lastTarget.spriteClass); + active = true; + sprite.linkVisuals(lastTarget); + sprite.idle(); + sprite.paused = true; + add( sprite ); - sprite.x = x + (width - sprite.width()) / 2 + 1; - sprite.y = y + (height - sprite.height()) / 2; - PixelScene.align(sprite); - - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + sprite.x = x + (width - sprite.width()) / 2 + 1; + sprite.y = y + (height - sprite.height()) / 2; + PixelScene.align(sprite); } private boolean enabled = true; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/QuickRecipe.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/QuickRecipe.java index a7e1b35bf..2be7feb33 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/QuickRecipe.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ui/QuickRecipe.java @@ -73,6 +73,7 @@ import com.watabou.noosa.BitmapText; import com.watabou.noosa.Group; import com.watabou.noosa.Image; import com.watabou.noosa.ui.Component; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.Arrays; @@ -254,14 +255,10 @@ public class QuickRecipe extends Component { case 1: Recipe r = new Scroll.ScrollToStone(); for (Class cls : Generator.Category.SCROLL.classes){ - try{ - Scroll scroll = (Scroll) cls.newInstance(); - if (!scroll.isKnown()) scroll.anonymize(); - ArrayList in = new ArrayList(Arrays.asList(scroll)); - result.add(new QuickRecipe( r, in, r.sampleOutput(in))); - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - } + Scroll scroll = (Scroll) Reflection.newInstance(cls); + if (!scroll.isKnown()) scroll.anonymize(); + ArrayList in = new ArrayList(Arrays.asList(scroll)); + result.add(new QuickRecipe( r, in, r.sampleOutput(in))); } return result; case 2: @@ -292,42 +289,30 @@ public class QuickRecipe extends Component { r = new Bomb.EnhanceBomb(); int i = 0; for (Class cls : Bomb.EnhanceBomb.validIngredients.keySet()){ - try{ - if (i == 2){ - result.add(null); - i = 0; - } - Item item = (Item) cls.newInstance(); - ArrayList in = new ArrayList(Arrays.asList(new Bomb(), item)); - result.add(new QuickRecipe( r, in, r.sampleOutput(in))); - i++; - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); + if (i == 2){ + result.add(null); + i = 0; } + Item item = (Item) Reflection.newInstance(cls); + ArrayList in = new ArrayList(Arrays.asList(new Bomb(), item)); + result.add(new QuickRecipe( r, in, r.sampleOutput(in))); + i++; } return result; case 4: r = new ExoticPotion.PotionToExotic(); for (Class cls : Generator.Category.POTION.classes){ - try{ - Potion pot = (Potion) cls.newInstance(); - ArrayList in = new ArrayList<>(Arrays.asList(pot, new Plant.Seed.PlaceHolder().quantity(2))); - result.add(new QuickRecipe( r, in, r.sampleOutput(in))); - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - } + Potion pot = (Potion) Reflection.newInstance(cls); + ArrayList in = new ArrayList<>(Arrays.asList(pot, new Plant.Seed.PlaceHolder().quantity(2))); + result.add(new QuickRecipe( r, in, r.sampleOutput(in))); } return result; case 5: r = new ExoticScroll.ScrollToExotic(); for (Class cls : Generator.Category.SCROLL.classes){ - try{ - Scroll scroll = (Scroll) cls.newInstance(); - ArrayList in = new ArrayList<>(Arrays.asList(scroll, new Runestone.PlaceHolder().quantity(2))); - result.add(new QuickRecipe( r, in, r.sampleOutput(in))); - } catch (Exception e){ - ShatteredPixelDungeon.reportException(e); - } + Scroll scroll = (Scroll) Reflection.newInstance(cls); + ArrayList in = new ArrayList<>(Arrays.asList(scroll, new Runestone.PlaceHolder().quantity(2))); + result.add(new QuickRecipe( r, in, r.sampleOutput(in))); } return result; case 6: diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java index 90886ca67..cd8506400 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java @@ -46,6 +46,7 @@ import com.watabou.noosa.BitmapText; import com.watabou.noosa.ColorBlock; import com.watabou.noosa.Image; import com.watabou.noosa.ui.Component; +import com.watabou.utils.Reflection; import java.util.ArrayList; import java.util.Collections; @@ -669,16 +670,12 @@ public class WndJournal extends WndTabbed { float pos = 0; for (Class itemClass : itemClasses) { - try{ - CatalogItem item = new CatalogItem(itemClass.newInstance(), known.get(itemClass), Catalog.isSeen(itemClass)); - item.setRect( 0, pos, width, ITEM_HEIGHT ); - content.add( item ); - items.add( item ); - - pos += item.height(); - } catch (Exception e) { - ShatteredPixelDungeon.reportException(e); - } + CatalogItem item = new CatalogItem(Reflection.newInstance(itemClass), known.get(itemClass), Catalog.isSeen(itemClass)); + item.setRect( 0, pos, width, ITEM_HEIGHT ); + content.add( item ); + items.add( item ); + + pos += item.height(); } content.setSize( width, pos );