Skip to content

Commit

Permalink
Add support for ancient debris
Browse files Browse the repository at this point in the history
Took 28 minutes
  • Loading branch information
DerFrZocker committed Jan 30, 2022
1 parent 9d24dc4 commit f7dd2ab
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* MIT License
*
* Copyright (c) 2019 - 2021 Marvin (DerFrZocker)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package de.derfrzocker.feature.impl.v1_18_R1.feature.generator;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.derfrzocker.feature.api.Registries;
import de.derfrzocker.feature.common.value.number.FloatType;
import de.derfrzocker.feature.common.value.number.FloatValue;
import de.derfrzocker.feature.common.value.number.IntegerType;
import de.derfrzocker.feature.common.value.number.IntegerValue;
import de.derfrzocker.feature.impl.v1_18_R1.feature.generator.configuration.OreFeatureConfiguration;
import de.derfrzocker.feature.impl.v1_18_R1.value.target.TargetType;
import de.derfrzocker.feature.impl.v1_18_R1.value.target.TargetValue;
import net.minecraft.world.level.levelgen.feature.OreFeature;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import org.bukkit.generator.LimitedRegion;
import org.bukkit.generator.WorldInfo;
import org.bukkit.util.BlockVector;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;

public class ScatteredOreGenerator extends MinecraftFeatureGenerator<OreConfiguration, OreFeatureConfiguration> {

public ScatteredOreGenerator(Registries registries) {
super(registries, OreFeature.SCATTERED_ORE, "scattered_ore");
}

public Codec<OreFeatureConfiguration> createCodec(Registries registries) {
return RecordCodecBuilder.create((builder) -> builder.group(
Codec.list(registries.getValueTypeRegistry(TargetType.class).
dispatch("target_type", TargetValue::getValueType, TargetType::getCodec)).
optionalFieldOf("targets").
forGetter(config -> Optional.ofNullable(config.getTargets())),
registries.getValueTypeRegistry(IntegerType.class).dispatch("size_type", IntegerValue::getValueType, IntegerType::getCodec).
optionalFieldOf("size").forGetter(config -> Optional.ofNullable(config.getSize())),
registries.getValueTypeRegistry(FloatType.class).dispatch("discard_chance_on_air_exposure_type", FloatValue::getValueType, FloatType::getCodec).
optionalFieldOf("discard_chance_on_air_exposure").forGetter(config -> Optional.ofNullable(config.getDiscardChanceOnAirExposure()))
).apply(builder, (targets, size, discardChanceOnAirExposure) -> new OreFeatureConfiguration(this, targets.orElse(null), size.orElse(null), discardChanceOnAirExposure.orElse(null))));
}

@Override
public OreFeatureConfiguration mergeConfig(OreFeatureConfiguration first, OreFeatureConfiguration second) {
return new OreFeatureConfiguration(this,
first.getTargets() != null ? first.getTargets() : second.getTargets(),
first.getSize() != null ? first.getSize() : second.getSize(),
first.getDiscardChanceOnAirExposure() != null ? first.getDiscardChanceOnAirExposure() : second.getDiscardChanceOnAirExposure());
}

@Override
public OreConfiguration createConfiguration(@NotNull WorldInfo worldInfo, @NotNull Random random, @NotNull BlockVector position, @NotNull LimitedRegion limitedRegion, @NotNull OreFeatureConfiguration configuration) {
List<OreConfiguration.TargetBlockState> blockStates = new ArrayList<>();
if (configuration.getTargets() != null) {
for (TargetValue targetValue : configuration.getTargets()) {
blockStates.add(targetValue.getValue(worldInfo, random, position, limitedRegion));
}
}

int size = 0;
if (configuration.getSize() != null) {
size = configuration.getSize().getValue(worldInfo, random, position, limitedRegion);
}

float discardChanceOnAirExposure = 0f;
if (configuration.getDiscardChanceOnAirExposure() != null) {
discardChanceOnAirExposure = configuration.getDiscardChanceOnAirExposure().getValue(worldInfo, random, position, limitedRegion);
}

return new OreConfiguration(blockStates, size, discardChanceOnAirExposure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import de.derfrzocker.feature.api.FeatureGenerator;
import de.derfrzocker.feature.api.PlacementModifierConfiguration;
import de.derfrzocker.feature.common.value.number.FixedFloatType;
import de.derfrzocker.feature.common.value.number.FixedFloatValue;
Expand All @@ -39,6 +40,7 @@
import de.derfrzocker.feature.common.value.number.FloatType;
import de.derfrzocker.feature.common.value.number.IntegerType;
import de.derfrzocker.feature.impl.v1_18_R1.feature.generator.OreFeatureGenerator;
import de.derfrzocker.feature.impl.v1_18_R1.feature.generator.ScatteredOreGenerator;
import de.derfrzocker.feature.impl.v1_18_R1.feature.generator.configuration.OreFeatureConfiguration;
import de.derfrzocker.feature.impl.v1_18_R1.placement.CountModifier;
import de.derfrzocker.feature.impl.v1_18_R1.placement.HeightRangeModifier;
Expand All @@ -59,6 +61,7 @@
import de.derfrzocker.ore.control.api.OreControlRegistries;
import de.derfrzocker.ore.control.api.dao.ConfigDao;
import de.derfrzocker.ore.control.impl.v1_18_R1.feature.generator.OreFeatureGeneratorHook;
import de.derfrzocker.ore.control.impl.v1_18_R1.feature.generator.ScatteredOreFeatureGeneratorHook;
import de.derfrzocker.ore.control.impl.v1_18_R1.placement.CountModifierHook;
import de.derfrzocker.ore.control.impl.v1_18_R1.placement.HeightRangeModifierHook;
import de.derfrzocker.ore.control.impl.v1_18_R1.placement.RarityModifierHook;
Expand All @@ -74,6 +77,7 @@
import net.minecraft.world.level.biome.BiomeGenerationSettings;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.feature.OreFeature;
import net.minecraft.world.level.levelgen.feature.ScatteredOreFeature;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import net.minecraft.world.level.levelgen.placement.CountPlacement;
import net.minecraft.world.level.levelgen.placement.HeightRangePlacement;
Expand Down Expand Up @@ -134,6 +138,7 @@ public void registerValueTypes() {

public void registerFeatureGenerators() {
registries.getFeatureGeneratorRegistry().register(new OreFeatureGenerator(registries));
registries.getFeatureGeneratorRegistry().register(new ScatteredOreGenerator(registries));
}

public void registerPlacementModifier() {
Expand Down Expand Up @@ -198,7 +203,7 @@ private void saveFeature(File file, PlacedFeature feature) {
return;
}

if (!(configuredFeature.feature() instanceof OreFeature)) {
if (!(configuredFeature.feature() instanceof OreFeature) && !(configuredFeature.feature() instanceof ScatteredOreFeature)) {
return;
}

Expand All @@ -221,7 +226,17 @@ private void saveFeature(File file, PlacedFeature feature) {
targetValues.add(new FixedTargetValue(state));
}

OreFeatureConfiguration featureConfiguration = new OreFeatureConfiguration(registries.getFeatureGeneratorRegistry().get(NamespacedKey.minecraft("ore")).get(), targetValues, new FixedIntegerValue(configuration.size), new FixedFloatValue(configuration.discardChanceOnAirExposure));
FeatureGenerator<?> featureGenerator;

if (configuredFeature.feature() instanceof OreFeature) {
featureGenerator = registries.getFeatureGeneratorRegistry().get(NamespacedKey.minecraft("ore")).get();
} else if (configuredFeature.feature() instanceof ScatteredOreFeature) {
featureGenerator = registries.getFeatureGeneratorRegistry().get(NamespacedKey.minecraft("scattered_ore")).get();
} else {
throw new RuntimeException("HOW?");
}

OreFeatureConfiguration featureConfiguration = new OreFeatureConfiguration(featureGenerator, targetValues, new FixedIntegerValue(configuration.size), new FixedFloatValue(configuration.discardChanceOnAirExposure));

List<PlacementModifierConfiguration> placementConfiguration = new ArrayList<>();
for (PlacementModifier placement : feature.getPlacement()) {
Expand Down Expand Up @@ -320,7 +335,7 @@ private PlacedFeature check(de.derfrzocker.ore.control.api.Biome biome, Resource
return null;
}

if (!(configuredFeature.feature() instanceof OreFeature)) {
if (!(configuredFeature.feature() instanceof OreFeature) && !(configuredFeature.feature instanceof ScatteredOreFeature)) {
return null;
}

Expand All @@ -341,7 +356,13 @@ private PlacedFeature check(de.derfrzocker.ore.control.api.Biome biome, Resource
}
}

return new PlacedFeature(() -> new ConfiguredFeature(new OreFeatureGeneratorHook(registries, configDao, key, biome), configuredFeature.config), placementModifiers);
if (configuredFeature.feature() instanceof OreFeature) {
return new PlacedFeature(() -> new ConfiguredFeature(new OreFeatureGeneratorHook(registries, configDao, key, biome), configuredFeature.config), placementModifiers);
} else if (configuredFeature.feature() instanceof ScatteredOreFeature) {
return new PlacedFeature(() -> new ConfiguredFeature(new ScatteredOreFeatureGeneratorHook(registries, configDao, key, biome), configuredFeature.config), placementModifiers);
}

throw new RuntimeException("HOW?");
}

@SuppressWarnings("rawtypes")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* MIT License
*
* Copyright (c) 2019 - 2021 Marvin (DerFrZocker)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package de.derfrzocker.ore.control.impl.v1_18_R1.feature.generator;

import de.derfrzocker.feature.api.Registries;
import de.derfrzocker.feature.impl.v1_18_R1.feature.generator.configuration.OreFeatureConfiguration;
import de.derfrzocker.feature.impl.v1_18_R1.value.target.TargetValue;
import de.derfrzocker.ore.control.api.Biome;
import de.derfrzocker.ore.control.api.dao.ConfigDao;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.world.level.levelgen.feature.configurations.OreConfiguration;
import org.bukkit.NamespacedKey;
import org.bukkit.generator.LimitedRegion;
import org.bukkit.generator.WorldInfo;
import org.bukkit.util.BlockVector;
import org.jetbrains.annotations.NotNull;

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

public class ScatteredOreFeatureGeneratorHook extends MinecraftFeatureGeneratorHook<OreConfiguration, OreFeatureConfiguration> {

public ScatteredOreFeatureGeneratorHook(@NotNull Registries registries, ConfigDao configDao, @NotNull NamespacedKey namespacedKey, @NotNull Biome biome) {
super(OreConfiguration.CODEC, Feature.SCATTERED_ORE, registries, configDao, "scattered_ore", biome, namespacedKey);
}

@Override
public OreConfiguration createConfig(@NotNull OreConfiguration defaultConfiguration, @NotNull WorldInfo worldInfo, @NotNull Random random, @NotNull BlockVector position, @NotNull LimitedRegion limitedRegion, @NotNull OreFeatureConfiguration configuration) {
List<OreConfiguration.TargetBlockState> blockStates;
if (configuration.getTargets() == null) {
blockStates = defaultConfiguration.targetStates;
} else {
blockStates = new ArrayList<>();

for (TargetValue targetValue : configuration.getTargets()) {
blockStates.add(targetValue.getValue(worldInfo, random, position, limitedRegion));
}
}

int size;
if (configuration.getSize() == null) {
size = defaultConfiguration.size;
} else {
size = configuration.getSize().getValue(worldInfo, random, position, limitedRegion);
}

float discardChanceOnAirExposure;
if (configuration.getDiscardChanceOnAirExposure() == null) {
discardChanceOnAirExposure = defaultConfiguration.discardChanceOnAirExposure;
} else {
discardChanceOnAirExposure = configuration.getDiscardChanceOnAirExposure().getValue(worldInfo, random, position, limitedRegion);
}

return new OreConfiguration(blockStates, size, discardChanceOnAirExposure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public void onEnable() {
NMSReplacer_v1_18_R1 nmsReplacer = new NMSReplacer_v1_18_R1(registries, configDao);
nmsReplacer.register();
File defaults = new File(getDataFolder(), "data/default");
if (!defaults.exists()) {
nmsReplacer.saveDefaultValues(defaults);
}
nmsReplacer.saveDefaultValues(defaults);

nmsReplacer.hookIntoBiomes();

Expand Down

0 comments on commit f7dd2ab

Please sign in to comment.