Skip to content

Commit

Permalink
load nosundamage biomes and dimensions from data packs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterpaul committed Sep 19, 2023
1 parent 051cae4 commit ff2c0df
Show file tree
Hide file tree
Showing 17 changed files with 567 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package de.teamlapen.vampirism.api.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.dimension.DimensionType;
import org.jetbrains.annotations.NotNull;

/**
* Manages sundamage for biomes and dimensions
Expand All @@ -13,20 +17,64 @@ public interface ISundamageRegistry {


/**
* Register a biome by instance in which no sundamage applies to vampires.*
* Register a biome by instance in which no sundamage applies to vampires.
*
* @deprecated use datapacks or {@link #addNoSundamageBiomes(ResourceKey[])} instead
*/
@Deprecated
void addNoSundamageBiomes(ResourceLocation... biomes);

/**
* Register a biome by instance in which no sundamage applies to vampires.
* <p>
* Use this or a datapack.
*/
void addNoSundamageBiomes(ResourceKey<Biome>... biomes);

/**
* Specifies if vampires should get sundamage in this dimension.
* <p>
* use this or a datapack.
*/
void specifySundamageForDim(ResourceKey<Level> dimension, boolean sundamage);

/**
* Register a dimension as one in which no sundamage applies to vampires.
*
* @param dimensionType The resource key of the dimension type
*/
void addNoSundamageDimensionType(ResourceKey<DimensionType> dimensionType);

/**
* Check if vampires can get sundamage in that biome
*
* @return Whether vampires can get sundamage in that biome
* @deprecated use {@link #hasBiomeSundamage(net.minecraft.resources.ResourceKey)} )} instead
*/
@Deprecated
boolean getSundamageInBiome(ResourceLocation registryName);

/**
* Check if vampires can get sundamage in that biome
*
* @param biome the resource key of the biome
* @return Whether vampires can get sundamage in that biome
*/
boolean hasBiomeSundamage(ResourceKey<Biome> biome);

/**
* Check if vampires can get sundamage in that dimension type
*
* @param dimensionType the resource key of the dimension type
* @return Whether vampires can get sundamage in that dimension
*/
boolean hasDimensionTypeSundamage(ResourceKey<DimensionType> dimensionType);

/**
* Checks if vampires can get sundamage in that dimension
* Check if vampires can get sundamage in that dimension
*
* @param dim the resource key of the dimension
* @return Whether vampires can get sundamage in that dimension
*/
boolean getSundamageInDim(ResourceKey<Level> dim);

Expand All @@ -37,7 +85,10 @@ public interface ISundamageRegistry {
boolean isGettingSundamage(LivingEntity entity, LevelAccessor world);

/**
* Specifies if vampires should get sundamage in this dimension
* Checks if the level at the given position does apply sundamage
*
* @param pos the position is used to find the biome in the level
*/
void specifySundamageForDim(ResourceKey<Level> dimension, boolean sundamage);
boolean hasSunDamage(@NotNull LevelAccessor levelAccessor, @NotNull BlockPos pos);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"biomes": [
"#vampirism:has_faction/vampire"
],
"dimensions": [
"minecraft:the_nether",
"minecraft:the_end"
]
}
17 changes: 17 additions & 0 deletions src/main/java/de/teamlapen/vampirism/VampirismMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import de.teamlapen.vampirism.core.*;
import de.teamlapen.vampirism.data.reloadlistener.BloodValuesReloadListener;
import de.teamlapen.vampirism.data.reloadlistener.SingleJigsawReloadListener;
import de.teamlapen.vampirism.data.reloadlistener.SundamageReloadListener;
import de.teamlapen.vampirism.entity.ExtendedCreature;
import de.teamlapen.vampirism.entity.ModEntityEventHandler;
import de.teamlapen.vampirism.entity.SundamageRegistry;
Expand All @@ -44,6 +45,8 @@
import de.teamlapen.vampirism.items.crossbow.CrossbowArrowHandler;
import de.teamlapen.vampirism.misc.SettingsProvider;
import de.teamlapen.vampirism.misc.VampirismLogger;
import de.teamlapen.vampirism.mixin.ReloadableServerResourcesAccessor;
import de.teamlapen.vampirism.mixin.TagManagerAccessor;
import de.teamlapen.vampirism.modcompat.IMCHandler;
import de.teamlapen.vampirism.modcompat.terrablender.TerraBlenderCompat;
import de.teamlapen.vampirism.network.ModPacketDispatcher;
Expand All @@ -63,8 +66,10 @@
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.RegisterCapabilitiesEvent;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.event.OnDatapackSyncEvent;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.server.ServerAboutToStartEvent;
import net.minecraftforge.event.server.ServerStoppedEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -121,6 +126,8 @@ public VampirismMod() {
MinecraftForge.EVENT_BUS.addListener(this::onAddReloadListenerEvent);
MinecraftForge.EVENT_BUS.addListener(this::onServerStarting);
MinecraftForge.EVENT_BUS.addListener(VersionUpdater::checkVersionUpdated);
MinecraftForge.EVENT_BUS.addListener(this::onServerStopped);
MinecraftForge.EVENT_BUS.addListener(this::onDataPackSyncEvent);

VampirismConfig.init();

Expand All @@ -138,6 +145,7 @@ public void onAddReloadListenerEvent(@NotNull AddReloadListenerEvent event) {
event.addListener(SkillTreeManager.getInstance());
event.addListener(new BloodValuesReloadListener());
event.addListener(new SingleJigsawReloadListener());
event.addListener(new SundamageReloadListener(((TagManagerAccessor) ((ReloadableServerResourcesAccessor) event.getServerResources()).getTagManager()).getRegistryAccess()));
}

public void onCommandsRegister(@NotNull RegisterCommandsEvent event) {
Expand Down Expand Up @@ -175,6 +183,15 @@ private void registerCapabilities(@NotNull RegisterCapabilitiesEvent event) {

private void onServerStarting(@NotNull ServerAboutToStartEvent event) {
VanillaStructureModifications.addVillageStructures(event.getServer().registryAccess());
((SundamageRegistry) VampirismAPI.sundamageRegistry()).initServer(event.getServer().registryAccess());
}

private void onServerStopped(ServerStoppedEvent event) {
((SundamageRegistry) VampirismAPI.sundamageRegistry()).removeServer();
}

private void onDataPackSyncEvent(OnDatapackSyncEvent event) {
((SundamageRegistry) VampirismAPI.sundamageRegistry()).updateClient(event.getPlayer());
}

private void finalizeConfiguration(RegisterEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public class VampirismConfig {
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/de/teamlapen/vampirism/core/ModBiomes.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package de.teamlapen.vampirism.core;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.teamlapen.vampirism.REFERENCE;
import de.teamlapen.vampirism.api.VampirismAPI;
import de.teamlapen.vampirism.world.biome.OverworldModifications;
import de.teamlapen.vampirism.world.biome.VampirismBiomes;
import de.teamlapen.vampirism.world.gen.modifier.ExtendedAddSpawnsBiomeModifier;
Expand All @@ -13,7 +10,6 @@
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.carver.ConfiguredWorldCarver;
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
Expand All @@ -23,9 +19,6 @@
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

import java.util.List;
import java.util.function.Function;

/**
* For new dynamic registry related things see {@link VampirismBiomes} and {@link OverworldModifications} {@link de.teamlapen.vampirism.world.gen.VampirismFeatures}
*/
Expand All @@ -40,7 +33,6 @@ public class ModBiomes {

static void register(IEventBus bus) {
BIOME_MODIFIER_SERIALIZERS.register(bus);
VampirismAPI.sundamageRegistry().addNoSundamageBiomes(VAMPIRE_FOREST.location());
}

public static void createBiomes(BootstapContext<Biome> context) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/de/teamlapen/vampirism/data/DataGeneration.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.teamlapen.vampirism.core.ModRegistries;
import de.teamlapen.vampirism.data.provider.ConvertiblesGenerator;
import de.teamlapen.vampirism.data.provider.SingleJigsawPiecesGenerator;
import de.teamlapen.vampirism.data.provider.SundamageProvider;
import de.teamlapen.vampirism.mixin.RegistriesDatapackGeneratorAccessor;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.DataGenerator;
Expand Down Expand Up @@ -42,5 +43,6 @@ public static void onGatherData(GatherDataEvent event) {
generator.addProvider(event.includeClient(), new ItemModelGenerator(packOutput, existingFileHelper));
generator.addProvider(event.includeServer(), new SingleJigsawPiecesGenerator(packOutput, REFERENCE.MODID));
generator.addProvider(event.includeServer(), new ConvertiblesGenerator(packOutput, REFERENCE.MODID));
generator.addProvider(event.includeServer(), new SundamageProvider(packOutput, REFERENCE.MODID));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package de.teamlapen.vampirism.data.provider;

import com.google.common.collect.Sets;
import com.mojang.serialization.JsonOps;
import de.teamlapen.vampirism.core.ModTags;
import de.teamlapen.vampirism.data.reloadlistener.SundamageReloadListener;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.dimension.BuiltinDimensionTypes;
import net.minecraft.world.level.dimension.DimensionType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class SundamageProvider implements DataProvider {

private static final Logger LOGGER = LogManager.getLogger();

protected final PackOutput.PathProvider pathProvider;
private final String modId;

public SundamageProvider(PackOutput packOutput, String modId) {
this.pathProvider = packOutput.createPathProvider(PackOutput.Target.DATA_PACK, "vampirism");
this.modId = modId;
}

@Override
public @NotNull CompletableFuture<?> run(@NotNull CachedOutput pOutput) {
Consumer consumer = new Consumer();
this.registerNoSundamageBiomes(consumer);

return DataProvider.saveStable(pOutput, SundamageReloadListener.RawFile.CODEC.encodeStart(JsonOps.INSTANCE, consumer.build()).getOrThrow(false, LOGGER::error), pathProvider.json(new ResourceLocation(modId, "no_sun_damage")));
}

@Override
public @NotNull String getName() {
return "No Sundamage";
}

protected void registerNoSundamageBiomes(Consumer consumer) {
consumer.addBiome(ModTags.Biomes.IS_VAMPIRE_BIOME);
consumer.addDimension(BuiltinDimensionTypes.NETHER);
consumer.addDimension(BuiltinDimensionTypes.END);
}

public static class Consumer {

private final Set<ExtraCodecs.TagOrElementLocation> biomes = Sets.newHashSet();
private final Set<ExtraCodecs.TagOrElementLocation> dimensions = Sets.newHashSet();
private final Set<ResourceKey<Level>> levels = Sets.newHashSet();
private final Set<ResourceKey<Level>> sunDamageLevels = Sets.newHashSet();

public void addBiome(ResourceLocation biome) {
this.biomes.add(new ExtraCodecs.TagOrElementLocation(biome, false));
}

public void addBiome(ResourceKey<Biome> biome) {
this.biomes.add(new ExtraCodecs.TagOrElementLocation(biome.location(), false));
}

public void addBiome(TagKey<Biome> biome) {
this.biomes.add(new ExtraCodecs.TagOrElementLocation(biome.location(), true));
}

public void addDimension(ResourceLocation dimension) {
this.dimensions.add(new ExtraCodecs.TagOrElementLocation(dimension, false));
}

public void addDimension(ResourceKey<DimensionType> dimension) {
this.dimensions.add(new ExtraCodecs.TagOrElementLocation(dimension.location(), false));
}

public void addDimension(TagKey<DimensionType> dimension) {
this.dimensions.add(new ExtraCodecs.TagOrElementLocation(dimension.location(), true));
}

public void noSunDamageLevel(ResourceKey<Level> level) {
this.levels.add(level);
}

public void sunDamageLevel(ResourceKey<Level> level) {
this.sunDamageLevels.add(level);
}

private SundamageReloadListener.RawFile build() {
return new SundamageReloadListener.RawFile(false, new ArrayList<>(this.biomes), new ArrayList<>(this.dimensions), new ArrayList<>(this.levels), new ArrayList<>(this.sunDamageLevels));
}
}
}
Loading

0 comments on commit ff2c0df

Please sign in to comment.