New BiomeLoadingEvent that allows modders to edit biomes as they are being deserialized.

This commit is contained in:
LexManos committed 2020-09-14 17:43:01 -07:00
1 parent ce6c664acd
commit 11e3281b3f
10 files changed
+386 -1

No files matched your search

@@ -39,6 +39,8 @@ import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.advancements.Advancement;
@@ -104,9 +106,15 @@ import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldReader;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.BiomeAmbience;
import net.minecraft.world.biome.BiomeGenerationSettings;
import net.minecraft.world.biome.MobSpawnInfo;
import net.minecraftforge.common.loot.IGlobalLootModifier;
import net.minecraftforge.common.loot.LootModifierManager;
import net.minecraftforge.common.util.BlockSnapshot;
import net.minecraftforge.common.world.BiomeGenerationSettingsBuilder;
import net.minecraftforge.common.world.MobSpawnInfoBuilder;
import net.minecraftforge.event.AnvilUpdateEvent;
import net.minecraftforge.event.DifficultyChangeEvent;
import net.minecraftforge.event.ForgeEventFactory;
@@ -130,6 +138,7 @@ import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.CriticalHitEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BiomeLoadingEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.NoteBlockEvent;
import net.minecraftforge.eventbus.api.Event.Result;
@@ -839,6 +848,21 @@ public class ForgeHooks
throw new RuntimeException("Mod fluids must override createAttributes.");
}
@FunctionalInterface
public interface BiomeCallbackFunction
{
Biome apply(final Biome.Climate climate, final Biome.Category category, final Float depth, final Float scale, final BiomeAmbience effects, final BiomeGenerationSettings gen, final MobSpawnInfo spawns);
}
public static Biome enhanceBiome(final ResourceLocation name, final Biome.Climate climate, final Biome.Category category, final Float depth, final Float scale, final BiomeAmbience effects, final BiomeGenerationSettings gen, final MobSpawnInfo spawns, final RecordCodecBuilder.Instance<Biome> codec, final BiomeCallbackFunction callback)
{
BiomeGenerationSettingsBuilder genBuilder = new BiomeGenerationSettingsBuilder(gen);
MobSpawnInfoBuilder spawnBuilder = new MobSpawnInfoBuilder(spawns);
BiomeLoadingEvent event = new BiomeLoadingEvent(name, climate, category, depth, scale, effects, genBuilder, spawnBuilder);
MinecraftForge.EVENT_BUS.post(event);
return callback.apply(event.getClimate(), event.getCategory(), event.getDepth(), event.getScale(), event.getEffects(), event.getGeneration().func_242508_a(), event.getSpawns().func_242577_b());
}
private static class LootTableContext
{
public final ResourceLocation name;
@@ -0,0 +1,36 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2020.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.world;
import net.minecraft.world.biome.BiomeGenerationSettings;
import java.util.ArrayList;
import java.util.Optional;
public class BiomeGenerationSettingsBuilder extends BiomeGenerationSettings.Builder
{
public BiomeGenerationSettingsBuilder(BiomeGenerationSettings orig)
{
field_242504_a = Optional.of(orig.func_242500_d());
orig.getCarvingStages().forEach(k -> field_242505_b.put(k, orig.func_242489_a(k)));
orig.func_242498_c().forEach(l -> field_242506_c.add(new ArrayList<>(l)));
field_242507_d.addAll(orig.func_242487_a());
}
}
@@ -0,0 +1,79 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2020.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.common.world;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.EntityType;
import net.minecraft.world.biome.MobSpawnInfo;
public class MobSpawnInfoBuilder extends MobSpawnInfo.Builder
{
private final Set<EntityClassification> typesView = Collections.unmodifiableSet(this.field_242567_a.keySet());
private final Set<EntityType<?>> costView = Collections.unmodifiableSet(this.field_242568_b.keySet());
public MobSpawnInfoBuilder(MobSpawnInfo orig)
{
orig.getSpawnerTypes().forEach(k -> {
field_242567_a.get(k).clear();
field_242567_a.get(k).addAll(new java.util.ArrayList<>(orig.func_242559_a(k)));
});
orig.getEntityTypes().forEach(k -> field_242568_b.put(k, orig.func_242558_a(k)));
field_242569_c = orig.func_242557_a();
field_242570_d = orig.func_242562_b();
}
public Set<EntityClassification> getSpawnerTypes()
{
return this.typesView;
}
public List<MobSpawnInfo.Spawners> getSpawner(EntityClassification type)
{
return this.field_242567_a.get(type);
}
public Set<EntityType<?>> getEntityTypes()
{
return this.costView;
}
@Nullable
public MobSpawnInfo.SpawnCosts getCost(EntityType<?> type)
{
return this.field_242568_b.get(type);
}
public float getProbability()
{
return this.field_242569_c;
}
public MobSpawnInfoBuilder disablePlayerSpawn()
{
this.field_242570_d = false;
return this;
}
}