-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35ff32b
commit cc7232f
Showing
11 changed files
with
159 additions
and
9 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
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
73 changes: 73 additions & 0 deletions
73
common/src/main/java/dev/lukebemish/tempest/impl/data/WeatherSpawnProvider.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,73 @@ | ||
package dev.lukebemish.tempest.impl.data; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.JsonOps; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import dev.lukebemish.tempest.api.WeatherStatus; | ||
import dev.lukebemish.tempest.impl.Constants; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.server.packs.resources.ResourceManager; | ||
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener; | ||
import net.minecraft.util.profiling.ProfilerFiller; | ||
import net.minecraft.util.random.WeightedRandomList; | ||
import net.minecraft.world.entity.MobCategory; | ||
import net.minecraft.world.level.biome.MobSpawnSettings; | ||
|
||
import java.util.ArrayList; | ||
import java.util.EnumMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public record WeatherSpawnProvider(WeatherStatus.Kind kind, List<MobSpawnSettings.SpawnerData> spawners) { | ||
public static final Codec<WeatherSpawnProvider> CODEC = RecordCodecBuilder.create(i -> i.group( | ||
WeatherStatus.Kind.CODEC.comapFlatMap(kind -> kind == WeatherStatus.Kind.CLEAR ? DataResult.error(() -> "Weather kind 'clear' is not valid here") : DataResult.success(kind), Function.identity()).fieldOf("kind").forGetter(WeatherSpawnProvider::kind), | ||
MobSpawnSettings.SpawnerData.CODEC.listOf().fieldOf("spawners").forGetter(WeatherSpawnProvider::spawners) | ||
).apply(i, WeatherSpawnProvider::new)); | ||
|
||
public static WeightedRandomList<MobSpawnSettings.SpawnerData> extendList(WeightedRandomList<MobSpawnSettings.SpawnerData> original, ServerLevel level, BlockPos pos, MobCategory mobCategory) { | ||
if (level.canSeeSky(pos)) { | ||
var kind = WeatherStatus.atPosition(level, pos).kind(); | ||
if (kind == WeatherStatus.Kind.CLEAR) { | ||
return original; | ||
} | ||
var list = new ArrayList<>(original.unwrap()); | ||
list.addAll(ReloadListener.PROVIDERS.getOrDefault(kind, Map.of()).getOrDefault(mobCategory, List.of())); | ||
return WeightedRandomList.create(list); | ||
} | ||
return original; | ||
} | ||
|
||
public static class ReloadListener extends SimpleJsonResourceReloadListener { | ||
public static final String DIRECTORY = Constants.MOD_ID + "/spawn_providers"; | ||
|
||
public ReloadListener() { | ||
super(Constants.GSON, DIRECTORY); | ||
} | ||
|
||
public static final Map<WeatherStatus.Kind, Map<MobCategory, List<MobSpawnSettings.SpawnerData>>> PROVIDERS = new EnumMap<>(WeatherStatus.Kind.class); | ||
|
||
@Override | ||
protected void apply(Map<ResourceLocation, JsonElement> object, ResourceManager resourceManager, ProfilerFiller profiler) { | ||
PROVIDERS.clear(); | ||
object.forEach((id, element) -> { | ||
var result = CODEC.parse(JsonOps.INSTANCE, element); | ||
if (result.result().isEmpty()) { | ||
Constants.LOGGER.error("Failed to decode spawn provider {}: {}", id, result.error().orElseThrow().message()); | ||
} else { | ||
var provider = result.result().get(); | ||
var map = PROVIDERS.computeIfAbsent(provider.kind(), k -> new EnumMap<>(MobCategory.class)); | ||
provider.spawners().forEach(spawner -> { | ||
var list = map.computeIfAbsent(spawner.type.getCategory(), k -> new ArrayList<>()); | ||
list.add(spawner); | ||
}); | ||
} | ||
}); | ||
Constants.LOGGER.info("Loaded {} weather spawn providers", object.size()); | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
common/src/main/java/dev/lukebemish/tempest/impl/mixin/NaturalSpawnerMixin.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,34 @@ | ||
package dev.lukebemish.tempest.impl.mixin; | ||
|
||
import com.llamalad7.mixinextras.injector.ModifyExpressionValue; | ||
import dev.lukebemish.tempest.impl.data.WeatherSpawnProvider; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.util.random.WeightedRandomList; | ||
import net.minecraft.world.entity.MobCategory; | ||
import net.minecraft.world.level.NaturalSpawner; | ||
import net.minecraft.world.level.StructureManager; | ||
import net.minecraft.world.level.biome.Biome; | ||
import net.minecraft.world.level.biome.MobSpawnSettings; | ||
import net.minecraft.world.level.chunk.ChunkGenerator; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(NaturalSpawner.class) | ||
public class NaturalSpawnerMixin { | ||
@ModifyExpressionValue( | ||
method = "mobsAt(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/level/StructureManager;Lnet/minecraft/world/level/chunk/ChunkGenerator;Lnet/minecraft/world/entity/MobCategory;Lnet/minecraft/core/BlockPos;Lnet/minecraft/core/Holder;)Lnet/minecraft/util/random/WeightedRandomList;", | ||
at = @At( | ||
value = "INVOKE", | ||
target = "Lnet/minecraft/world/level/chunk/ChunkGenerator;getMobsAt(Lnet/minecraft/core/Holder;Lnet/minecraft/world/level/StructureManager;Lnet/minecraft/world/entity/MobCategory;Lnet/minecraft/core/BlockPos;)Lnet/minecraft/util/random/WeightedRandomList;" | ||
) | ||
) | ||
private static WeightedRandomList<MobSpawnSettings.SpawnerData> tempest$mobsAt( | ||
WeightedRandomList<MobSpawnSettings.SpawnerData> original, | ||
ServerLevel level, StructureManager structureManager, ChunkGenerator generator, MobCategory category, BlockPos pos, @Nullable Holder<Biome> biome | ||
) { | ||
return WeatherSpawnProvider.extendList(original, level, pos, category); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
common/src/main/resources/data/tempest/tags/entity_types/damaged_by_hail.json
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,5 @@ | ||
{ | ||
"values": [ | ||
"minecraft:player" | ||
] | ||
} |
7 changes: 7 additions & 0 deletions
7
common/src/main/resources/data/tempest/tags/entity_types/immune_to_hail.json
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,7 @@ | ||
{ | ||
"values": [ | ||
"minecraft:snow_golem", | ||
"minecraft:polar_bear", | ||
"minecraft:fox" | ||
] | ||
} |
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
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