Backport: Re-add PotentialSpawns event (#8944)

This commit is contained in:
TheMuso authored and GitHub committed 2022-08-08 15:01:33 +02:00
1 parent 73a937e088
commit 49c927a505
5 files changed
+157 -1

No files matched your search

@@ -4,7 +4,7 @@
import net.minecraft.world.phys.Vec3;
import org.slf4j.Logger;
+// TODO: ForgeEventFactory.getPotentialSpawns, ForgeHooks.canEntitySpawn
+// TODO: ForgeHooks.canEntitySpawn
public final class NaturalSpawner {
private static final Logger f_46977_ = LogUtils.getLogger();
private static final int f_151589_ = 24;
@@ -35,6 +35,15 @@
return;
}
@@ -265,7 +_,7 @@
}
private static WeightedRandomList<MobSpawnSettings.SpawnerData> m_204168_(ServerLevel p_204169_, StructureFeatureManager p_204170_, ChunkGenerator p_204171_, MobCategory p_204172_, BlockPos p_204173_, @Nullable Holder<Biome> p_204174_) {
- return m_186529_(p_204173_, p_204169_, p_204172_, p_204170_) ? NetherFortressFeature.f_66381_ : p_204171_.m_203315_(p_204174_ != null ? p_204174_ : p_204169_.m_204166_(p_204173_), p_204170_, p_204172_, p_204173_);
+ return net.minecraftforge.event.ForgeEventFactory.getPotentialSpawns(p_204169_, p_204172_, p_204173_, m_186529_(p_204173_, p_204169_, p_204172_, p_204170_) ? NetherFortressFeature.f_66381_ : p_204171_.m_203315_(p_204174_ != null ? p_204174_ : p_204169_.m_204166_(p_204173_), p_204170_, p_204172_, p_204173_));
}
public static boolean m_186529_(BlockPos p_186530_, ServerLevel p_186531_, MobCategory p_186532_, StructureFeatureManager p_186533_) {
@@ -304,6 +_,13 @@
if (p_47052_ == SpawnPlacements.Type.NO_RESTRICTIONS) {
return true;
@@ -19,10 +19,13 @@ import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.ReloadableServerResources;
import net.minecraft.server.players.PlayerList;
import net.minecraft.util.random.WeightedRandomList;
import net.minecraft.world.Container;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.projectile.FireworkRocketEntity;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.entity.projectile.ThrownEnderpearl;
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraft.world.level.portal.PortalShape;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.item.TooltipFlag;
@@ -938,4 +941,13 @@ public class ForgeEventFactory
{
MinecraftForge.EVENT_BUS.post(new TickEvent.ServerTickEvent(TickEvent.Phase.END, haveTime));
}
public static WeightedRandomList<MobSpawnSettings.SpawnerData> getPotentialSpawns(LevelAccessor level, MobCategory category, BlockPos pos, WeightedRandomList<MobSpawnSettings.SpawnerData> oldList)
{
WorldEvent.PotentialSpawns event = new WorldEvent.PotentialSpawns(level, category, pos, oldList);
if (MinecraftForge.EVENT_BUS.post(event))
return WeightedRandomList.create();
return WeightedRandomList.create(event.getSpawnerDataList());
}
}
@@ -7,16 +7,24 @@ package net.minecraftforge.event.world;
import java.util.function.Supplier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.ProgressListener;
import net.minecraft.util.random.WeightedRandomList;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.storage.ServerLevelData;
import net.minecraftforge.common.ForgeInternalHandler;
@@ -118,4 +126,83 @@ public class WorldEvent extends Event
return settings;
}
}
/**
* Fired when building a list of all possible entities that can spawn at the specified location.
*
* <p>If an entry is added to the list, it needs to be a globally unique instance.</p>
*
* The event is called in {@link net.minecraft.world.level.NaturalSpawner#mobsAt(ServerLevel,
* StructureManager, ChunkGenerator, MobCategory, RandomSource, BlockPos)}.</p>
*
* <p>This event is {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.
* Canceling the event will result in an empty list, meaning no entity will be spawned.</p>
*/
@Cancelable
public static class PotentialSpawns extends WorldEvent
{
private final MobCategory mobcategory;
private final BlockPos pos;
private final List<MobSpawnSettings.SpawnerData> list;
private final List<MobSpawnSettings.SpawnerData> view;
public PotentialSpawns(LevelAccessor level, MobCategory category, BlockPos pos, WeightedRandomList<MobSpawnSettings.SpawnerData> oldList)
{
super(level);
this.pos = pos;
this.mobcategory = category;
if (!oldList.isEmpty())
this.list = new ArrayList<>(oldList.unwrap());
else
this.list = new ArrayList<>();
this.view = Collections.unmodifiableList(list);
}
/**
* {@return the category of the mobs in the spawn list.}
*/
public MobCategory getMobCategory()
{
return mobcategory;
}
/**
* {@return the block position where the chosen mob will be spawned.}
*/
public BlockPos getPos()
{
return pos;
}
/**
* {@return the list of mobs that can potentially be spawned.}
*/
public List<MobSpawnSettings.SpawnerData> getSpawnerDataList()
{
return view;
}
/**
* Appends a SpawnerData entry to the spawn list.
*
* @param data SpawnerData entry to be appended to the spawn list.
*/
public void addSpawnerData(MobSpawnSettings.SpawnerData data)
{
list.add(data);
}
/**
* Removes a SpawnerData entry from the spawn list.
*
* @param data SpawnerData entry to be removed from the spawn list.
*
* {@return {@code true} if the spawn list contained the specified element.}
*/
public boolean removeSpawnerData(MobSpawnSettings.SpawnerData data)
{
return list.remove(data);
}
}
}
@@ -0,0 +1,46 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug.world;
import net.minecraft.core.BlockPos;
import net.minecraft.world.Difficulty;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.level.LevelAccessor;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.Mod;
/**
* <p>This tests net.minecraftforge.event.world.World.WorldEvent.PotentialSpawns. If ENABLED is set to true,
* this test mod uses the PotentialSpawns event to prevent mobs in the MONSTER mob category from spawning if the
* game difficulty is set to anything other than hard.</p>
*/
@Mod("potentialspawnsevent_test")
public class PotentialSpawnsEventTest
{
public static final boolean ENABLED = false;
public PotentialSpawnsEventTest()
{
if (ENABLED)
{
MinecraftForge.EVENT_BUS.addListener(PotentialSpawnsEventTest::onlySpawnHostileMobs);
}
}
public static void onlySpawnHostileMobs(WorldEvent.PotentialSpawns event)
{
LevelAccessor level = event.getWorld();
BlockPos pos = event.getPos();
Difficulty difficulty = level.getCurrentDifficultyAt(pos).getDifficulty();
MobCategory category = event.getMobCategory();
if (category == MobCategory.MONSTER && difficulty != Difficulty.HARD)
{
event.setCanceled(true);
}
}
}
+2
View File
@@ -234,5 +234,7 @@ license="LGPL v2.1"
modId="blockstate_change_test"
[[mods]]
modId="dyn_map_color_test"
[[mods]]
modId="potentialspawnsevent_test"
# ADD ABOVE THIS LINE