Add hooks to allow modification of structures spawn lists (#7344)

This commit is contained in:
Richard Freimer authored and GitHub committed 2020-10-07 13:04:03 -07:00
1 parent 10448c1ac9
commit f5b53d5d10
12 files changed
+538 -19

No files matched your search

@@ -0,0 +1,73 @@
/*
* 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.extensions;
import java.util.Collections;
import java.util.List;
import net.minecraft.entity.EntityClassification;
import net.minecraft.world.biome.MobSpawnInfo;
import net.minecraft.world.gen.feature.structure.Structure;
public interface IForgeStructure
{
default Structure<?> getStructure()
{
return (Structure<?>) this;
}
/**
* Gets the default list of {@link EntityClassification#MONSTER} spawns for this structure.
*
* @apiNote Implement this over {@link Structure#getSpawnList()}
*/
default List<MobSpawnInfo.Spawners> getDefaultSpawnList()
{
return Collections.emptyList();
}
/**
* Gets the default list of {@link EntityClassification#CREATURE} spawns for this structure.
*
* @apiNote Implement this over {@link Structure#getCreatureSpawnList()}
*/
default List<MobSpawnInfo.Spawners> getDefaultCreatureSpawnList()
{
return Collections.emptyList();
}
/**
* Gets the default for if entity spawns are restricted to being inside of the pieces making up the structure or if being in the bounds of the overall structure
* is good enough.
* @return {@code true} if the location to check spawns for has to be inside of the pieces making up the structure, {@code false} otherwise.
*/
default boolean getDefaultRestrictsSpawnsToInside()
{
//The Pillager Outpost and Ocean Monument check the full structure by default instead of limiting themselves to being within the structure's bounds
return getStructure() != Structure.field_236366_b_ && getStructure() != Structure.field_236376_l_;
}
/**
* Helper method to get the list of entity spawns for this structure for the given classification.
* @param classification The classification of entities.
* @apiNote This method is marked as final in {@link Structure} so as to not be overridden by modders and breaking support for
* {@link net.minecraftforge.event.world.StructureSpawnListGatherEvent}.
*/
List<MobSpawnInfo.Spawners> getSpawnList(EntityClassification classification);
}
@@ -0,0 +1,134 @@
/*
* 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 com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import net.minecraft.entity.EntityClassification;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.biome.MobSpawnInfo;
import net.minecraft.world.gen.feature.structure.Structure;
import net.minecraft.world.gen.feature.structure.StructureManager;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.StructureSpawnListGatherEvent;
import net.minecraftforge.registries.ForgeRegistries;
/**
* Class to help manage entity spawns inside of structures
*/
public class StructureSpawnManager
{
private static Map<Structure<?>, StructureSpawnInfo> structuresWithSpawns = Collections.emptyMap();
/**
* Gathers potential entity spawns for all the different registered structures.
* @apiNote Internal
*/
public static void gatherEntitySpawns()
{
//We use a linked hash map to ensure that we check the structures in an order that if there are multiple structures a position satisfies
// then we have the same behavior as vanilla as vanilla checks, swamp huts, pillager outposts, ocean monuments, and nether fortresses in
// that order.
Map<Structure<?>, StructureSpawnInfo> structuresWithSpawns = new LinkedHashMap<>();
gatherEntitySpawns(structuresWithSpawns, Structure.field_236374_j_);
gatherEntitySpawns(structuresWithSpawns, Structure.field_236366_b_);
gatherEntitySpawns(structuresWithSpawns, Structure.field_236376_l_);
gatherEntitySpawns(structuresWithSpawns, Structure.field_236378_n_);
for (Structure<?> structure : ForgeRegistries.STRUCTURE_FEATURES.getValues())
{
if (structure != Structure.field_236374_j_ && structure != Structure.field_236366_b_ && structure != Structure.field_236376_l_ &&
structure != Structure.field_236378_n_)
{
//If we didn't already gather the spawns already to ensure we do vanilla ones already
// gather the spawns for this structure
gatherEntitySpawns(structuresWithSpawns, structure);
}
}
StructureSpawnManager.structuresWithSpawns = structuresWithSpawns;
}
private static void gatherEntitySpawns(Map<Structure<?>, StructureSpawnInfo> structuresWithSpawns, Structure<?> structure)
{
StructureSpawnListGatherEvent event = new StructureSpawnListGatherEvent(structure);
MinecraftForge.EVENT_BUS.post(event);
ImmutableMap.Builder<net.minecraft.entity.EntityClassification, List<MobSpawnInfo.Spawners>> builder = ImmutableMap.builder();
event.getEntitySpawns().forEach((classification, spawns) -> {
if (!spawns.isEmpty())
builder.put(classification, ImmutableList.copyOf(spawns));
});
Map<EntityClassification, List<MobSpawnInfo.Spawners>> entitySpawns = builder.build();
if (!entitySpawns.isEmpty())
structuresWithSpawns.put(structure, new StructureSpawnInfo(entitySpawns, event.isInsideOnly()));
}
/**
* Looks up if a given position is within a structure and returns any entity spawns that structure has for the given classification, or null if
* none are found.
* @param structureManager Structure Manager, used to check if a position is within a structure.
* @param classification Entity classification
* @param pos Position to get entity spawns of
*/
@Nullable
public static List<MobSpawnInfo.Spawners> getStructureSpawns(StructureManager structureManager, EntityClassification classification, BlockPos pos)
{
for (Entry<Structure<?>, StructureSpawnInfo> entry : structuresWithSpawns.entrySet())
{
Structure<?> structure = entry.getKey();
StructureSpawnInfo spawnInfo = entry.getValue();
//Note: We check if the structure has spawns for a type first before looking at the world as it should be a cheaper check
if (spawnInfo.spawns.containsKey(classification) && structureManager.func_235010_a_(pos, spawnInfo.insideOnly, structure).isValid())
return spawnInfo.spawns.get(classification);
}
return null;
}
/**
* Gets the entity spawn lists for entities of a given classification for a given structure.
* @param structure The Structure
* @param classification The classification to lookup
*/
public static List<MobSpawnInfo.Spawners> getSpawnList(Structure<?> structure, EntityClassification classification)
{
if (structuresWithSpawns.containsKey(structure))
return structuresWithSpawns.get(structure).spawns.getOrDefault(classification, Collections.emptyList());
return Collections.emptyList();
}
/**
* Helper class to keep track of spawns and if the spawns should be restricted to inside the structure pieces.
*/
private static class StructureSpawnInfo
{
private final Map<EntityClassification, List<MobSpawnInfo.Spawners>> spawns;
private final boolean insideOnly;
private StructureSpawnInfo(Map<EntityClassification, List<MobSpawnInfo.Spawners>> spawns, boolean insideOnly)
{
this.spawns = spawns;
this.insideOnly = insideOnly;
}
}
}
@@ -0,0 +1,150 @@
/*
* 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.event.world;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.entity.EntityClassification;
import net.minecraft.world.biome.MobSpawnInfo;
import net.minecraft.world.gen.feature.structure.Structure;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.EventPriority;
/**
* This event fires when a Structure is gathering what mobs/creatures can spawn in it.
*
* In order to maintain the most compatibility possible with other mods' modifications to a structure,
* the event should be assigned a {@link net.minecraftforge.eventbus.api.EventPriority} as follows:
*
* - Additions : {@link EventPriority#HIGH}
* - Removals : {@link EventPriority#NORMAL}
* - Any other modification : {@link EventPriority#LOW}
*
* Be aware that another mod could have done an operation beforehand, so an expected value out of a vanilla structure might not
* always be the same, depending on other mods.
*/
public class StructureSpawnListGatherEvent extends Event
{
private final Structure<?> structure;
private final Map<EntityClassification, List<MobSpawnInfo.Spawners>> entitySpawns = new HashMap<>();
private final Map<EntityClassification, List<MobSpawnInfo.Spawners>> entitySpawnsUnmodifiableLists = new HashMap<>();
private final Map<EntityClassification, List<MobSpawnInfo.Spawners>> entitySpawnsUnmodifiable = Collections.unmodifiableMap(entitySpawnsUnmodifiableLists);
private boolean insideOnly;
public StructureSpawnListGatherEvent(Structure<?> structure)
{
this.structure = structure;
this.insideOnly = this.structure.getDefaultRestrictsSpawnsToInside();
addEntitySpawns(EntityClassification.MONSTER, this.structure.getDefaultSpawnList());
addEntitySpawns(EntityClassification.CREATURE, this.structure.getDefaultCreatureSpawnList());
}
/**
* @return Structure to add or remove spawns to/from.
*/
public Structure<?> getStructure()
{
return structure;
}
/**
* Change if entity spawn location checks are done against the entire bounds of the structure or only the inside the pieces of the structure.
* @param insideOnly {@code true} to restrict the spawn checks to inside the pieces of the structure, {@code false} to allow spawns outside
*/
public void setInsideOnly(boolean insideOnly)
{
this.insideOnly = insideOnly;
}
/**
* Checks if spawns for the structure are restricted to being inside the individual pieces of the structure.
*/
public boolean isInsideOnly()
{
return insideOnly;
}
/**
* Gets an unmodifiable view of the the list representing the entity spawns for the given classification.
* @param classification Entity Classification
* @return The list of spawns for the given classification.
*/
public List<MobSpawnInfo.Spawners> getEntitySpawns(EntityClassification classification)
{
return this.entitySpawnsUnmodifiableLists.getOrDefault(classification, Collections.emptyList());
}
/**
* Gets the internal spawn list for a given entity classification, or adds one if needed. (This includes adding it to the unmodifiable view)
*/
private List<MobSpawnInfo.Spawners> getOrCreateEntitySpawns(EntityClassification classification)
{
return this.entitySpawns.computeIfAbsent(classification, c -> {
List<MobSpawnInfo.Spawners> spawners = new ArrayList<>();
//If the classification isn't in entitySpawns yet, also add an unmodifiable view of the list to
// the unmodifiable list spawn map
this.entitySpawnsUnmodifiableLists.put(c, Collections.unmodifiableList(spawners));
return spawners;
});
}
/**
* Adds a spawn to the list of spawns for the given classification.
* @param classification Entity Classification
* @param spawner Spawner
*/
public void addEntitySpawn(EntityClassification classification, MobSpawnInfo.Spawners spawner)
{
getOrCreateEntitySpawns(classification).add(spawner);
}
/**
* Adds spawns to the list of spawns for the given classification.
* @param classification Entity Classification
* @param spawners Spawners to add
*/
public void addEntitySpawns(EntityClassification classification, List<MobSpawnInfo.Spawners> spawners)
{
getOrCreateEntitySpawns(classification).addAll(spawners);
}
/**
* Removes a spawn from the list of spawns for the given classification.
* @param classification Entity Classification
* @param spawner Spawner
*/
public void removeEntitySpawn(EntityClassification classification, MobSpawnInfo.Spawners spawner)
{
if (this.entitySpawns.containsKey(classification))
this.entitySpawns.get(classification).remove(spawner);
}
/**
* Gets an unmodifiable view of the map of spawns based on entity classification that is used to fill in the various spawn lists for the structure.
*/
public Map<EntityClassification, List<MobSpawnInfo.Spawners>> getEntitySpawns()
{
return entitySpawnsUnmodifiable;
}
}