|
| 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 com.patchworkmc.impl.biomes; |
| 21 | + |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import com.google.common.collect.Sets; |
| 26 | + |
| 27 | +import net.minecraft.util.registry.Registry; |
| 28 | +import net.minecraft.world.biome.Biome; |
| 29 | +import net.minecraft.world.biome.Biomes; |
| 30 | + |
| 31 | +import net.fabricmc.api.ModInitializer; |
| 32 | +import net.fabricmc.fabric.api.biomes.v1.OverworldBiomes; |
| 33 | +import net.fabricmc.fabric.api.event.registry.RegistryEntryAddedCallback; |
| 34 | +import net.fabricmc.fabric.api.event.server.ServerStartCallback; |
| 35 | + |
| 36 | +public final class PatchworkBiomes implements ModInitializer { |
| 37 | + private static Set<Biome> failedBiomes = Sets.newHashSet(); |
| 38 | + |
| 39 | + @Override |
| 40 | + public void onInitialize() { |
| 41 | + Registry.BIOME.forEach(PatchworkBiomes::addRivers); |
| 42 | + RegistryEntryAddedCallback.event(Registry.BIOME).register((rawid, id, biome) -> { |
| 43 | + addRivers(biome); |
| 44 | + updateFailedBiomes(); |
| 45 | + }); |
| 46 | + ServerStartCallback.EVENT.register(server -> updateFailedBiomes()); |
| 47 | + } |
| 48 | + |
| 49 | + private static void addRivers(Biome biome) { |
| 50 | + Biome river = ((RiverSupplier) biome).getRiver(); |
| 51 | + |
| 52 | + if (river == null) { |
| 53 | + failedBiomes.add(biome); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + setRiverIfNotDefault(biome, river); |
| 58 | + } |
| 59 | + |
| 60 | + private static void updateFailedBiomes() { |
| 61 | + if (!failedBiomes.isEmpty()) { |
| 62 | + Iterator<Biome> iterator = failedBiomes.iterator(); |
| 63 | + |
| 64 | + while (iterator.hasNext()) { |
| 65 | + Biome biome = iterator.next(); |
| 66 | + Biome river = ((RiverSupplier) biome).getRiver(); |
| 67 | + |
| 68 | + if (river == null) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + iterator.remove(); |
| 73 | + |
| 74 | + setRiverIfNotDefault(biome, river); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static void setRiverIfNotDefault(Biome biome, Biome river) { |
| 80 | + if (river != getDefaultRiver(biome)) { |
| 81 | + OverworldBiomes.setRiverBiome(biome, river); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public static Biome getDefaultRiver(Biome biome) { |
| 86 | + if (biome == Biomes.SNOWY_TUNDRA) { |
| 87 | + return Biomes.FROZEN_RIVER; |
| 88 | + } else if (biome == Biomes.MUSHROOM_FIELDS || biome == Biomes.MUSHROOM_FIELD_SHORE) { |
| 89 | + return Biomes.MUSHROOM_FIELD_SHORE; |
| 90 | + } |
| 91 | + |
| 92 | + return Biomes.RIVER; |
| 93 | + } |
| 94 | +} |
0 commit comments