From 4f4174c7b9fe5c6fb745747062afd95223a12529 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Thu, 31 Dec 2020 22:20:27 -0500 Subject: [PATCH] v0.9.1b: added various safety checks to prevent crashes --- .../src/main/java/com/watabou/utils/Random.java | 16 ++++++++-------- .../shatteredpixeldungeon/items/Item.java | 3 ++- .../sprites/GhoulSprite.java | 15 +++++++++++++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/SPD-classes/src/main/java/com/watabou/utils/Random.java b/SPD-classes/src/main/java/com/watabou/utils/Random.java index bb9e0d76c..b5ca8f492 100644 --- a/SPD-classes/src/main/java/com/watabou/utils/Random.java +++ b/SPD-classes/src/main/java/com/watabou/utils/Random.java @@ -39,20 +39,20 @@ public class Random { resetGenerators(); } - public static void resetGenerators(){ + public static synchronized void resetGenerators(){ generators = new ArrayDeque<>(); generators.push(new java.util.Random()); } - public static void pushGenerator(){ + public static synchronized void pushGenerator(){ generators.push( new java.util.Random() ); } - public static void pushGenerator( long seed ){ + public static synchronized void pushGenerator( long seed ){ generators.push( new java.util.Random( seed ) ); } - public static void popGenerator(){ + public static synchronized void popGenerator(){ if (generators.size() == 1){ Game.reportException( new RuntimeException("tried to pop the last random number generator!")); } else { @@ -61,7 +61,7 @@ public class Random { } //returns a uniformly distributed float in the range [0, 1) - public static float Float() { + public static synchronized float Float() { return generators.peek().nextFloat(); } @@ -81,7 +81,7 @@ public class Random { } //returns a uniformly distributed int in the range [0, max) - public static int Int( int max ) { + public static synchronized int Int( int max ) { return max > 0 ? generators.peek().nextInt(max) : 0; } @@ -101,7 +101,7 @@ public class Random { } //returns a uniformly distributed long in the range [-2^63, 2^63) - public static long Long() { + public static synchronized long Long() { return generators.peek().nextLong(); } @@ -190,7 +190,7 @@ public class Random { null; } - public static void shuffle( List list){ + public synchronized static void shuffle( List list){ Collections.shuffle(list, generators.peek()); } 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 0457acf7a..95a739ba9 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java @@ -570,7 +570,8 @@ public class Item implements Bundlable { @Override public void call() { curUser = user; - Item.this.detach(user.belongings.backpack).onThrow(cell); + Item i = Item.this.detach(user.belongings.backpack); + if (i != null) i.onThrow(cell); if (curUser.hasTalent(Talent.IMPROVISED_PROJECTILES) && !(Item.this instanceof MissileWeapon) && curUser.buff(Talent.ImprovisedProjectileCooldown.class) == null){ diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/GhoulSprite.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/GhoulSprite.java index 213be9ca8..969625706 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/GhoulSprite.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/sprites/GhoulSprite.java @@ -22,6 +22,7 @@ package com.shatteredpixel.shatteredpixeldungeon.sprites; import com.shatteredpixel.shatteredpixeldungeon.Assets; +import com.watabou.noosa.Game; import com.watabou.noosa.TextureFilm; public class GhoulSprite extends MobSprite { @@ -44,10 +45,10 @@ public class GhoulSprite extends MobSprite { attack = new Animation( 12, false ); attack.frames( frames, 0, 8, 9 ); - crumple = new Animation( 15, false); + crumple = new Animation( 1, false); crumple.frames( frames, 0, 10, 11, 12 ); - die = new Animation( 15, false ); + die = new Animation( 1, false ); die.frames( frames, 0, 10, 11, 12, 13 ); play( idle ); @@ -58,6 +59,16 @@ public class GhoulSprite extends MobSprite { play(crumple); } + @Override + public void move(int from, int to) { + if (parent == null){ + //fixme this happens rarely, likely due to ghoul like link? + Game.reportException(new RuntimeException("ghoul sprite tried to move with null parent! ghoul HP: " + ch.HP)); + return; + } + super.move(from, to); + } + @Override public void die() { if (curAnim == crumple){