Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit d5eb46a

Browse files
authored
Merge pull request #60 from valoeghese/master
Start patchwork-level-generators
2 parents 0367543 + 717e9f5 commit d5eb46a

File tree

17 files changed

+661
-0
lines changed

17 files changed

+661
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# patchwork-level-generators
2+
3+
## Reasoning
4+
5+
* Minecraft forge patches the LevelGeneratorType to be extensible through providing IForgeWorldType and making a public constrcutor
6+
* Numerous mods for forge use this LevelGeneratorType API
7+
8+
## TODO
9+
* Use IForgeDimension for the getHorizonHeight client mixin to World
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
archivesBaseName = "patchwork-level-generators"
2+
version = getSubprojectVersion(project, "0.1.0")
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.common.extensions;
21+
22+
import java.util.Random;
23+
import java.util.function.LongFunction;
24+
25+
import net.minecraft.client.MinecraftClient;
26+
import net.minecraft.client.gui.screen.CustomizeBuffetLevelScreen;
27+
import net.minecraft.client.gui.screen.CustomizeFlatLevelScreen;
28+
import net.minecraft.client.gui.screen.world.CreateWorldScreen;
29+
import net.minecraft.world.IWorld;
30+
import net.minecraft.world.World;
31+
import net.minecraft.world.biome.layer.AddBambooJungleLayer;
32+
import net.minecraft.world.biome.layer.EaseBiomeEdgeLayer;
33+
import net.minecraft.world.biome.layer.ScaleLayer;
34+
import net.minecraft.world.biome.layer.SetBaseBiomesLayer;
35+
import net.minecraft.world.biome.layer.util.LayerFactory;
36+
import net.minecraft.world.biome.layer.util.LayerSampleContext;
37+
import net.minecraft.world.biome.layer.util.LayerSampler;
38+
import net.minecraft.world.gen.chunk.ChunkGenerator;
39+
import net.minecraft.world.gen.chunk.OverworldChunkGeneratorConfig;
40+
import net.minecraft.world.level.LevelGeneratorType;
41+
42+
import net.fabricmc.api.EnvType;
43+
import net.fabricmc.api.Environment;
44+
45+
import net.patchworkmc.mixin.levelgenerators.AccessorBiomeLayers;
46+
47+
public interface IForgeWorldType {
48+
default LevelGeneratorType getWorldType() {
49+
return (LevelGeneratorType) this;
50+
}
51+
52+
/**
53+
* Called when 'Create New World' button is pressed before starting game.
54+
*/
55+
default void onGUICreateWorldPress() {
56+
}
57+
58+
/**
59+
* Called when the 'Customize' button is pressed on world creation GUI.
60+
*/
61+
@Environment(EnvType.CLIENT)
62+
default void onCustomizeButton(MinecraftClient client, CreateWorldScreen screen) {
63+
if (this == LevelGeneratorType.FLAT) {
64+
client.openScreen(new CustomizeFlatLevelScreen(screen, screen.generatorOptionsTag));
65+
} else if (this == LevelGeneratorType.BUFFET) {
66+
client.openScreen(new CustomizeBuffetLevelScreen(screen, screen.generatorOptionsTag));
67+
}
68+
}
69+
70+
default boolean handleSlimeSpawnReduction(Random random, IWorld world) {
71+
return this == LevelGeneratorType.FLAT && random.nextInt(4) != 1;
72+
}
73+
74+
default double getHorizon(World world) {
75+
return this == LevelGeneratorType.FLAT ? 0.0D : 63.0D;
76+
}
77+
78+
default double voidFadeMagnitude() {
79+
return this == LevelGeneratorType.FLAT ? 1.0D : 0.03125D;
80+
}
81+
82+
/**
83+
* Get the height to render the clouds for this world type.
84+
*/
85+
default float getCloudHeight() {
86+
return 128.0F;
87+
}
88+
89+
default ChunkGenerator<?> createChunkGenerator(World world) {
90+
return world.dimension.createChunkGenerator();
91+
}
92+
93+
/**
94+
* Allows modifying the {@link LayerFactory} used for this type's biome
95+
* generation.
96+
*
97+
* @param <T> The type of {@link LayerSampler}.
98+
* @param <C> The type of {@link LayerSampleContext}.
99+
* @param parentLayer The parent layer to feed into any layer you return
100+
* @param settings The {@link OverworldChunkGeneratorConfig} used to create the
101+
* {@link SetBaseBiomesLayer}.
102+
* @param contextFactory A {@link LongFunction} factory to create contexts of
103+
* the supplied size.
104+
* @return A {@link LayerFactory} that representing the Biomes to be generated.
105+
* @see SetBaseBiomesLayer
106+
*/
107+
default <T extends LayerSampler, C extends LayerSampleContext<T>> LayerFactory<T> getBiomeLayer(LayerFactory<T> parentLayer, OverworldChunkGeneratorConfig settings, LongFunction<C> contextFactory) {
108+
parentLayer = (new SetBaseBiomesLayer(getWorldType(), settings)).create(contextFactory.apply(200L), parentLayer);
109+
parentLayer = AddBambooJungleLayer.INSTANCE.create(contextFactory.apply(1001L), parentLayer);
110+
parentLayer = AccessorBiomeLayers.patchwork$stack(1000L, ScaleLayer.NORMAL, parentLayer, 2, contextFactory);
111+
parentLayer = EaseBiomeEdgeLayer.INSTANCE.create(contextFactory.apply(1000L), parentLayer);
112+
return parentLayer;
113+
}
114+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.api.levelgenerators;
21+
22+
import net.minecraftforge.common.extensions.IForgeWorldType;
23+
24+
/**
25+
* Used by Patchwork to mark forge level generator types. Added in the patching phase by patcher.
26+
*/
27+
public interface PatchworkLevelGeneratorType extends IForgeWorldType {
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.impl.levelgenerators;
21+
22+
import java.util.Arrays;
23+
24+
import net.minecraft.world.level.LevelGeneratorType;
25+
26+
import net.patchworkmc.mixin.levelgenerators.AccessorLevelGeneratorType;
27+
28+
public class PatchworkLevelGeneratorTypes {
29+
public static int getNextID() {
30+
LevelGeneratorType[] types = LevelGeneratorType.TYPES;
31+
32+
for (int x = 0; x < types.length; x++) {
33+
if (types[x] == null) {
34+
return x;
35+
}
36+
}
37+
38+
int old = types.length;
39+
AccessorLevelGeneratorType.patchwork$setTypes(Arrays.copyOf(types, old + 16));
40+
return old;
41+
}
42+
}
43+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.levelgenerators;
21+
22+
import java.util.function.LongFunction;
23+
24+
import org.spongepowered.asm.mixin.Mixin;
25+
import org.spongepowered.asm.mixin.gen.Invoker;
26+
27+
import net.minecraft.world.biome.layer.BiomeLayers;
28+
import net.minecraft.world.biome.layer.type.ParentedLayer;
29+
import net.minecraft.world.biome.layer.util.LayerFactory;
30+
import net.minecraft.world.biome.layer.util.LayerSampleContext;
31+
import net.minecraft.world.biome.layer.util.LayerSampler;
32+
33+
@Mixin(BiomeLayers.class)
34+
public interface AccessorBiomeLayers {
35+
@Invoker("stack")
36+
static <T extends LayerSampler, C extends LayerSampleContext<T>> LayerFactory<T> patchwork$stack(long seed, ParentedLayer layer, LayerFactory<T> parent, int count, LongFunction<C> contextProvider) {
37+
throw new RuntimeException("Failed to create invoker: BiomeLayers#stack!");
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.levelgenerators;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.gen.Accessor;
24+
25+
import net.minecraft.world.level.LevelGeneratorType;
26+
27+
@Mixin(LevelGeneratorType.class)
28+
public interface AccessorLevelGeneratorType {
29+
@Accessor("TYPES")
30+
static void patchwork$setTypes(LevelGeneratorType[] types) {
31+
throw new RuntimeException("Failed to create accessor: LevelGeneratorType#TYPES!");
32+
}
33+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.levelgenerators;
21+
22+
import java.util.function.LongFunction;
23+
24+
import net.minecraftforge.common.extensions.IForgeWorldType;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.Shadow;
27+
import org.spongepowered.asm.mixin.injection.At;
28+
import org.spongepowered.asm.mixin.injection.Redirect;
29+
30+
import net.minecraft.world.biome.layer.AddBambooJungleLayer;
31+
import net.minecraft.world.biome.layer.BiomeLayers;
32+
import net.minecraft.world.biome.layer.EaseBiomeEdgeLayer;
33+
import net.minecraft.world.biome.layer.SetBaseBiomesLayer;
34+
import net.minecraft.world.biome.layer.type.ParentedLayer;
35+
import net.minecraft.world.biome.layer.util.LayerFactory;
36+
import net.minecraft.world.biome.layer.util.LayerSampleContext;
37+
import net.minecraft.world.biome.layer.util.LayerSampler;
38+
import net.minecraft.world.gen.chunk.OverworldChunkGeneratorConfig;
39+
import net.minecraft.world.level.LevelGeneratorType;
40+
41+
import net.patchworkmc.api.levelgenerators.PatchworkLevelGeneratorType;
42+
43+
@Mixin(BiomeLayers.class)
44+
public class MixinBiomeLayers {
45+
@Shadow
46+
private static <T extends LayerSampler, C extends LayerSampleContext<T>> LayerFactory<T> stack(long seed, ParentedLayer layer, LayerFactory<T> parent, int count, LongFunction<C> contextProvider) {
47+
throw new RuntimeException("Failed to shadow BiomeLayers#stack!");
48+
}
49+
50+
@Redirect(method = "build(Lnet/minecraft/world/level/LevelGeneratorType;Lnet/minecraft/world/gen/chunk/OverworldChunkGeneratorConfig;Ljava/util/function/LongFunction;)Lcom/google/common/collect/ImmutableList;",
51+
at = @At(value = "INVOKE", target = "net/minecraft/world/biome/layer/SetBaseBiomesLayer.create(Lnet/minecraft/world/biome/layer/util/LayerSampleContext;Lnet/minecraft/world/biome/layer/util/LayerFactory;)Lnet/minecraft/world/biome/layer/util/LayerFactory;", ordinal = 0))
52+
private static <T extends LayerSampler, C extends LayerSampleContext<T>> LayerFactory<T> addForgeBiomeLayers(SetBaseBiomesLayer instance, C contextParam, LayerFactory<T> parentLayer, LevelGeneratorType generatorType, OverworldChunkGeneratorConfig settings, LongFunction<C> contextProvider) {
53+
if (generatorType instanceof PatchworkLevelGeneratorType) {
54+
return ((IForgeWorldType) generatorType).getBiomeLayer(parentLayer, settings, contextProvider);
55+
} else {
56+
// vanilla behaviour
57+
return instance.create(contextParam, parentLayer);
58+
}
59+
}
60+
61+
@Redirect(method = "build(Lnet/minecraft/world/level/LevelGeneratorType;Lnet/minecraft/world/gen/chunk/OverworldChunkGeneratorConfig;Ljava/util/function/LongFunction;)Lcom/google/common/collect/ImmutableList;",
62+
at = @At(value = "INVOKE", target = "net/minecraft/world/biome/layer/AddBambooJungleLayer.create(Lnet/minecraft/world/biome/layer/util/LayerSampleContext;Lnet/minecraft/world/biome/layer/util/LayerFactory;)Lnet/minecraft/world/biome/layer/util/LayerFactory;", ordinal = 0))
63+
private static <T extends LayerSampler, C extends LayerSampleContext<T>> LayerFactory<T> redirectBambooJungle(AddBambooJungleLayer instance, C contextParam, LayerFactory<T> parentLayer, LevelGeneratorType generatorType, OverworldChunkGeneratorConfig settings, LongFunction<C> contextProvider) {
64+
if (generatorType instanceof PatchworkLevelGeneratorType) {
65+
return parentLayer;
66+
} else {
67+
// vanilla behaviour
68+
return instance.create(contextParam, parentLayer);
69+
}
70+
}
71+
72+
@Redirect(method = "build(Lnet/minecraft/world/level/LevelGeneratorType;Lnet/minecraft/world/gen/chunk/OverworldChunkGeneratorConfig;Ljava/util/function/LongFunction;)Lcom/google/common/collect/ImmutableList;",
73+
at = @At(value = "INVOKE", target = "Lnet/minecraft/world/biome/layer/BiomeLayers;stack(JLnet/minecraft/world/biome/layer/type/ParentedLayer;Lnet/minecraft/world/biome/layer/util/LayerFactory;ILjava/util/function/LongFunction;)Lnet/minecraft/world/biome/layer/util/LayerFactory;", ordinal = 3))
74+
private static <T extends LayerSampler, C extends LayerSampleContext<T>> LayerFactory<T> redirectStack(long seed, ParentedLayer layer, LayerFactory<T> parentLayer, int count, LongFunction<C> contextProvider, LevelGeneratorType generatorType, OverworldChunkGeneratorConfig settings, LongFunction<C> contextProviderParam) {
75+
if (generatorType instanceof PatchworkLevelGeneratorType) {
76+
return parentLayer;
77+
} else {
78+
// vanilla behaviour
79+
return stack(seed, layer, parentLayer, count, contextProvider);
80+
}
81+
}
82+
83+
@Redirect(method = "build(Lnet/minecraft/world/level/LevelGeneratorType;Lnet/minecraft/world/gen/chunk/OverworldChunkGeneratorConfig;Ljava/util/function/LongFunction;)Lcom/google/common/collect/ImmutableList;",
84+
at = @At(value = "INVOKE", target = "net/minecraft/world/biome/layer/EaseBiomeEdgeLayer.create(Lnet/minecraft/world/biome/layer/util/LayerSampleContext;Lnet/minecraft/world/biome/layer/util/LayerFactory;)Lnet/minecraft/world/biome/layer/util/LayerFactory;", ordinal = 0))
85+
private static <T extends LayerSampler, C extends LayerSampleContext<T>> LayerFactory<T> redirectEaseBiomeEdge(EaseBiomeEdgeLayer instance, C contextParam, LayerFactory<T> parentLayer, LevelGeneratorType generatorType, OverworldChunkGeneratorConfig settings, LongFunction<C> contextProvider) {
86+
if (generatorType instanceof PatchworkLevelGeneratorType) {
87+
return parentLayer;
88+
} else {
89+
// vanilla behaviour
90+
return instance.create(contextParam, parentLayer);
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)