Skip to content

Commit

Permalink
Forge can now generate structures in villages
Browse files Browse the repository at this point in the history
  • Loading branch information
JT122406 committed Nov 29, 2023
1 parent f225a2a commit 296eabb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package generations.gg.generations.structures.generationsstructures.village;

import com.mojang.datafixers.util.Pair;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.ProcessorLists;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.levelgen.structure.pools.SinglePoolElement;
import net.minecraft.world.level.levelgen.structure.pools.StructurePoolElement;
import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorList;

import java.util.ArrayList;
import java.util.List;

public class PlaceInVillage {

private static void addBuildingToPool(Registry<StructureTemplatePool> templatePoolRegistry,
Expand All @@ -15,6 +23,31 @@ private static void addBuildingToPool(Registry<StructureTemplatePool> templatePo
String nbtPieceRL,
int weight) {

Holder<StructureProcessorList> emptyProcessorList = processorListRegistry.getHolderOrThrow(ProcessorLists.EMPTY);

// Grab the pool we want to add to
StructureTemplatePool pool = templatePoolRegistry.get(poolRL);
if (pool == null) return;

// Grabs the nbt piece and creates a SinglePoolElement of it that we can add to a structure's pool.
// Use .legacy( for villages/outposts and .single( for everything else
SinglePoolElement piece = SinglePoolElement.legacy(nbtPieceRL,
emptyProcessorList).apply(StructureTemplatePool.Projection.RIGID);

// Use AccessTransformer or Accessor Mixin to make StructureTemplatePool's templates field public for us to see.
// Weight is handled by how many times the entry appears in this list.
// We do not need to worry about immutability as this field is created using Lists.newArrayList(); which makes a mutable list.
for (int i = 0; i < weight; i++)
pool.templates.add(piece);


// Use AccessTransformer or Accessor Mixin to make StructureTemplatePool's rawTemplates field public for us to see.
// This list of pairs of pieces and weights is not used by vanilla by default but another mod may need it for efficiency.
// So lets add to this list for completeness. We need to make a copy of the array as it can be an immutable list.
List<Pair<StructurePoolElement, Integer>> listOfPieceEntries = new ArrayList<>(pool.rawTemplates);
listOfPieceEntries.add(new Pair<>(piece, weight));
pool.rawTemplates = listOfPieceEntries;

}

public static void addStructuresToVillages(MinecraftServer server) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
accessWidener v2 named
accessWidener v2 named

accessible field net/minecraft/data/worldgen/ProcessorLists EMPTY Lnet/minecraft/resources/ResourceKey;
accessible field net/minecraft/data/worldgen/ProcessorLists EMPTY Lnet/minecraft/resources/ResourceKey;
accessible field net/minecraft/world/level/levelgen/structure/pools/StructureTemplatePool templates Lit/unimi/dsi/fastutil/objects/ObjectArrayList;
mutable field net/minecraft/world/level/levelgen/structure/pools/StructureTemplatePool rawTemplates Ljava/util/List;
accessible field net/minecraft/world/level/levelgen/structure/pools/StructureTemplatePool rawTemplates Ljava/util/List;
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package generations.gg.generations.structures.generationsstructures.fabric;

import generations.gg.generations.structures.generationsstructures.GenerationsStructures;
import generations.gg.generations.structures.generationsstructures.byg.BYG;
import generations.gg.generations.structures.generationsstructures.integration.Default;
import generations.gg.generations.structures.generationsstructures.processors.StructureProcessors;
import generations.gg.generations.structures.generationsstructures.village.PlaceInVillage;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;

/**
* Fabric initializer for Generations Structures
Expand All @@ -19,8 +14,7 @@
public class GenerationsStructuresFabric implements ModInitializer {
@Override
public void onInitialize() {
GenerationsStructures.init(FabricLoader.getInstance().isModLoaded(potionstudios.byg.BYG.MOD_ID) ? new BYG() : new Default());
GenerationsStructures.init(new Default()); //TODO: FabricLoader.getInstance().isModLoaded(potionstudios.byg.BYG.MOD_ID) ? new BYG() : new Default()
StructureProcessors.init();
//PlaceInVillage.addStructuresToVillages();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import generations.gg.generations.structures.generationsstructures.integration.Default;
import generations.gg.generations.structures.generationsstructures.integration.Integration;
import generations.gg.generations.structures.generationsstructures.processors.StructureProcessors;
import generations.gg.generations.structures.generationsstructures.village.PlaceInVillage;
import net.minecraftforge.event.server.ServerAboutToStartEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
Expand All @@ -32,7 +30,6 @@ public GenerationsStructuresForge() {
GenerationsStructures.init(integration);
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(this::commonSetup);
//bus.addListener(this::aboutToStartEvent);
}

/**
Expand All @@ -42,7 +39,4 @@ private void commonSetup(final FMLCommonSetupEvent event) {
event.enqueueWork(StructureProcessors::init);
}

private void aboutToStartEvent(final ServerAboutToStartEvent event) {
PlaceInVillage.addStructuresToVillages(event.getServer());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package generations.gg.generations.structures.generationsstructures.forge.events;

import generations.gg.generations.structures.generationsstructures.GenerationsStructures;
import generations.gg.generations.structures.generationsstructures.village.PlaceInVillage;
import net.minecraftforge.event.server.ServerAboutToStartEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = GenerationsStructures.MOD_ID)
public class LifeCycleEvents {
/**
* Adds all custom structures to villages.
*/
@SubscribeEvent
public static void aboutToStartEvent(final ServerAboutToStartEvent event) {
PlaceInVillage.addStructuresToVillages(event.getServer());
}
}

0 comments on commit 296eabb

Please sign in to comment.