Re-add PotentialSpawns event (#8712)

This commit is contained in:
TheMuso authored and GitHub committed 2022-08-05 09:29:02 -07:00
1 parent c713bad8c3
commit 06d9d89f98
5 files changed
+150 -3

No files matched your search

@@ -18,9 +18,12 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.server.ReloadableServerResources;
import net.minecraft.server.players.PlayerList;
import net.minecraft.util.RandomSource;
import net.minecraft.util.random.WeightedRandomList;
import net.minecraft.world.Container;
import net.minecraft.world.entity.MobCategory;
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.chunk.LevelChunk;
import net.minecraft.world.level.portal.PortalShape;
import net.minecraft.world.level.block.state.BlockState;
@@ -801,4 +804,12 @@ public class ForgeEventFactory
{
MinecraftForge.EVENT_BUS.post(new TickEvent.ServerTickEvent(TickEvent.Phase.END, haveTime, server));
}
public static WeightedRandomList<MobSpawnSettings.SpawnerData> getPotentialSpawns(LevelAccessor level, MobCategory category, BlockPos pos, WeightedRandomList<MobSpawnSettings.SpawnerData> oldList)
{
LevelEvent.PotentialSpawns event = new LevelEvent.PotentialSpawns(level, category, pos, oldList);
if (MinecraftForge.EVENT_BUS.post(event))
return WeightedRandomList.create();
return WeightedRandomList.create(event.getSpawnerDataList());
}
}
@@ -5,13 +5,21 @@
package net.minecraftforge.event.level;
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.core.BlockPos;
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.storage.ServerLevelData;
import net.minecraftforge.common.ForgeInternalHandler;
import net.minecraftforge.common.MinecraftForge;
@@ -117,4 +125,83 @@ public class LevelEvent 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#getRandomSpawnMobAt(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 LevelEvent
{
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);
}
}
}