-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide more-sane eldritch altar mob-spawning behaviors (#101)
* Pick coordinates for knights and guardians in an even distribution * Unrelated fix: set remap = false for levitator * Create an enum setting type * Provide an origin-centered eldritch altar spawning behavior * Add documentation for eldritchAltarSpawningMethod * Inline local variable updates, suppress local param warnings * Condense two mixins into one
- Loading branch information
Showing
7 changed files
with
194 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/dev/rndmorris/salisarcana/config/settings/EldritchAltarMobSpawnSetting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package dev.rndmorris.salisarcana.config.settings; | ||
|
||
import java.util.Random; | ||
|
||
import net.minecraft.util.MathHelper; | ||
|
||
import dev.rndmorris.salisarcana.config.ConfigPhase; | ||
import dev.rndmorris.salisarcana.config.IEnabler; | ||
|
||
public class EldritchAltarMobSpawnSetting extends EnumSetting<EldritchAltarMobSpawnSetting.Options> { | ||
|
||
public EldritchAltarMobSpawnSetting(IEnabler dependency, ConfigPhase phase, String name, String comment) { | ||
super(dependency, phase, name, comment, Options.DEFAULT); | ||
} | ||
|
||
public int randomHorizontal(Random random) { | ||
return switch (value) { | ||
case EVEN_SPREAD -> horizontalEven(random); | ||
case CENTER_WEIGHTED -> horizontalWeighted(random); | ||
default -> throw new RuntimeException( | ||
String.format( | ||
"%s called while its mixin should be disabled.", | ||
EldritchAltarMobSpawnSetting.class.getName())); | ||
}; | ||
} | ||
|
||
public int randomVertical(Random random) { | ||
return switch (value) { | ||
case EVEN_SPREAD -> verticalEven(random); | ||
case CENTER_WEIGHTED -> verticalWeighted(random); | ||
default -> throw new RuntimeException( | ||
String.format( | ||
"%s called while its mixin should be disabled.", | ||
EldritchAltarMobSpawnSetting.class.getName())); | ||
}; | ||
} | ||
|
||
private int horizontalEven(Random random) { | ||
return (MathHelper.getRandomIntegerInRange(random, 0, 6) + 4) * (random.nextBoolean() ? 1 : -1); | ||
} | ||
|
||
private int verticalEven(Random random) { | ||
return MathHelper.getRandomIntegerInRange(random, -3, 3); | ||
} | ||
|
||
private int horizontalWeighted(Random random) { | ||
final var val = MathHelper.getRandomIntegerInRange(random, 0, 6) | ||
- MathHelper.getRandomIntegerInRange(random, 0, 6); | ||
if (val == 0) { | ||
return val + (4 * (random.nextBoolean() ? 1 : -1)); | ||
} | ||
if (val < 0) { | ||
return val - 4; | ||
} | ||
return val + 4; | ||
} | ||
|
||
private int verticalWeighted(Random random) { | ||
return MathHelper.getRandomIntegerInRange(random, 0, 3) - MathHelper.getRandomIntegerInRange(random, 0, 3); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return super.isEnabled() && value != Options.DEFAULT; | ||
} | ||
|
||
public enum Options { | ||
DEFAULT, | ||
EVEN_SPREAD, | ||
CENTER_WEIGHTED | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/dev/rndmorris/salisarcana/config/settings/EnumSetting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dev.rndmorris.salisarcana.config.settings; | ||
|
||
import java.util.Arrays; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import net.minecraftforge.common.config.Configuration; | ||
|
||
import dev.rndmorris.salisarcana.config.ConfigPhase; | ||
import dev.rndmorris.salisarcana.config.IEnabler; | ||
|
||
public class EnumSetting<E extends Enum<E>> extends Setting { | ||
|
||
protected final Class<E> enumClass; | ||
protected final String name; | ||
protected final String comment; | ||
protected E value; | ||
|
||
public EnumSetting(IEnabler dependency, ConfigPhase phase, String name, String comment, @Nonnull E defaultValue) { | ||
super(dependency, phase); | ||
this.name = name; | ||
enumClass = defaultValue.getDeclaringClass(); | ||
value = defaultValue; | ||
|
||
final var sb = new StringBuilder(); | ||
final var $vals = Arrays.stream(enumClass.getEnumConstants()) | ||
.iterator(); | ||
while ($vals.hasNext()) { | ||
sb.append( | ||
$vals.next() | ||
.toString()); | ||
if ($vals.hasNext()) { | ||
sb.append(", "); | ||
} | ||
} | ||
|
||
this.comment = comment + " Valid values: [" + sb + "]"; | ||
} | ||
|
||
@Override | ||
public void loadFromConfiguration(Configuration configuration) { | ||
final var validValues = Arrays.stream(enumClass.getEnumConstants()) | ||
.map(Enum::toString) | ||
.toArray(String[]::new); | ||
final var valueString = configuration.getString(name, getCategory(), value.toString(), comment, validValues); | ||
value = Enum.valueOf(enumClass, valueString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...in/java/dev/rndmorris/salisarcana/mixins/late/tiles/MixinTileEldritchAltar_SpawnMobs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dev.rndmorris.salisarcana.mixins.late.tiles; | ||
|
||
import java.util.Random; | ||
|
||
import net.minecraft.world.IBlockAccess; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef; | ||
|
||
import dev.rndmorris.salisarcana.config.ConfigModuleRoot; | ||
import thaumcraft.api.TileThaumcraft; | ||
import thaumcraft.common.tiles.TileEldritchAltar; | ||
|
||
@Mixin(value = TileEldritchAltar.class, remap = false) | ||
public abstract class MixinTileEldritchAltar_SpawnMobs extends TileThaumcraft { | ||
|
||
@WrapOperation( | ||
method = { "spawnGuards", "spawnGuardian" }, | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/util/MathHelper;getRandomIntegerInRange(Ljava/util/Random;II)I")) | ||
private int preventRandCalls(Random random, int min, int max, Operation<Integer> original) { | ||
return 0; | ||
} | ||
|
||
@WrapOperation( | ||
method = { "spawnGuards", "spawnGuardian" }, | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/world/World;doesBlockHaveSolidTopSurface(Lnet/minecraft/world/IBlockAccess;III)Z")) | ||
@SuppressWarnings("ParameterCanBeLocal") | ||
private boolean pickAndCheckCoords(IBlockAccess worldIn, int x, int y, int z, Operation<Boolean> original, | ||
@Local(name = "i1") LocalIntRef xRef, @Local(name = "j1") LocalIntRef yRef, | ||
@Local(name = "k1") LocalIntRef zRef) { | ||
|
||
final var spawnSettings = ConfigModuleRoot.enhancements.eldritchAltarSpawningMethod; | ||
|
||
xRef.set(x = xCoord + spawnSettings.randomHorizontal(worldObj.rand)); | ||
yRef.set(y = yCoord + spawnSettings.randomVertical(worldObj.rand)); | ||
zRef.set(z = zCoord + spawnSettings.randomHorizontal(worldObj.rand)); | ||
|
||
return original.call(worldIn, x, y - 1, z); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters