-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathTheEndBiomeSourceMixin.java
More file actions
127 lines (108 loc) · 4.36 KB
/
TheEndBiomeSourceMixin.java
File metadata and controls
127 lines (108 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.biome;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import com.google.common.base.Suppliers;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.RegistryOps;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Climate;
import net.minecraft.world.level.biome.TheEndBiomeSource;
import net.fabricmc.fabric.impl.biome.TheEndBiomeData;
@Mixin(TheEndBiomeSource.class)
public class TheEndBiomeSourceMixin extends BiomeSourceMixin {
@Shadow
@Mutable
@Final
public static MapCodec<TheEndBiomeSource> CODEC;
@Unique
private Supplier<TheEndBiomeData.Overrides> overrides;
@Unique
private boolean biomeSetModified = false;
@Unique
private boolean hasCheckedForModifiedSet = false;
/**
* Modifies the codec, so it calls the static factory method that gives us access to the
* full biome registry instead of just the pre-defined biomes that vanilla uses.
*/
@Inject(method = "<clinit>", at = @At("TAIL"))
private static void modifyCodec(CallbackInfo ci) {
CODEC = RecordCodecBuilder.mapCodec((instance) -> {
return instance.group(RegistryOps.retrieveGetter(Registries.BIOME)).apply(instance, instance.stable(TheEndBiomeSource::create));
});
}
/**
* Captures the biome registry at the beginning of the static factory method to allow access to it in the
* constructor.
*/
@Inject(method = "create", at = @At("HEAD"))
private static void rememberLookup(HolderGetter<Biome> biomes, CallbackInfoReturnable<?> ci) {
TheEndBiomeData.biomeRegistry.set(biomes);
}
/**
* Frees up the captured biome registry.
*/
@Inject(method = "create", at = @At("TAIL"))
private static void clearLookup(HolderGetter<Biome> biomes, CallbackInfoReturnable<?> ci) {
TheEndBiomeData.biomeRegistry.remove();
}
/**
* Uses the captured biome registry to set up the modded end biomes.
*/
@Inject(method = "<init>", at = @At("RETURN"))
private void init(Holder<Biome> end, Holder<Biome> highlands, Holder<Biome> midlands, Holder<Biome> islands, Holder<Biome> barrens, CallbackInfo ci) {
HolderGetter<Biome> biomes = TheEndBiomeData.biomeRegistry.get();
if (biomes == null) {
throw new IllegalStateException("Biome registry not set by Mixin");
}
overrides = Suppliers.memoize(() -> {
return TheEndBiomeData.createOverrides(biomes);
});
}
@Inject(method = "getNoiseBiome", at = @At("RETURN"), cancellable = true)
private void getWeightedEndBiome(int quartX, int quartY, int quartZ, Climate.Sampler sampler, CallbackInfoReturnable<Holder<Biome>> cir) {
cir.setReturnValue(overrides.get().pick(quartX, quartY, quartZ, sampler, cir.getReturnValue()));
}
@Override
protected Set<Holder<Biome>> modifyBiomeSet(Set<Holder<Biome>> biomes) {
if (!hasCheckedForModifiedSet) {
hasCheckedForModifiedSet = true;
biomeSetModified = !overrides.get().customBiomes.isEmpty();
}
if (biomeSetModified) {
var modifiedBiomes = new LinkedHashSet<>(biomes);
modifiedBiomes.addAll(overrides.get().customBiomes);
return Collections.unmodifiableSet(modifiedBiomes);
}
return biomes;
}
}