Skip to content

Commit

Permalink
finished out limlib 1.18.2
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed May 6, 2022
1 parent 81aaee6 commit 2704635
Show file tree
Hide file tree
Showing 10 changed files with 677 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/main/java/net/ludocrypt/limlib/api/LiminalUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.ludocrypt.limlib.api;

import java.util.List;

import net.fabricmc.fabric.api.dimension.v1.FabricDimensions;
import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.AdvancementProgress;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
import net.minecraft.world.TeleportTarget;
import net.minecraft.world.biome.source.util.MultiNoiseUtil.MultiNoiseSampler;
import net.minecraft.world.gen.densityfunction.DensityFunctionTypes;

public class LiminalUtil {

public static SoundEvent travelingSound = null;
public static float travelingVolume = 1.0F;
public static float travelingPitch = 1.0F;

public static <E extends Entity> E travelTo(E teleported, ServerWorld destination, TeleportTarget target, SoundEvent sound, float volume, float pitch) {
try {
travelingSound = sound;
travelingVolume = volume;
travelingPitch = pitch;
return FabricDimensions.teleport(teleported, destination, target);
} finally {
travelingSound = null;
travelingVolume = 0.0F;
travelingPitch = 0.0F;
}
}

public static void grantAdvancement(PlayerEntity player, Identifier id) {
if (player instanceof ServerPlayerEntity serverPlayerEntity) {
Advancement advancement = serverPlayerEntity.server.getAdvancementLoader().get(id);
AdvancementProgress progress = serverPlayerEntity.getAdvancementTracker().getProgress(advancement);
if (!progress.isDone()) {
progress.getUnobtainedCriteria().forEach((criteria) -> serverPlayerEntity.getAdvancementTracker().grantCriterion(advancement, criteria));
}
}
}

public static MultiNoiseSampler createMultiNoiseSampler(double d1, double d2, double d3, double d4, double d5, double d6) {
return new MultiNoiseSampler(DensityFunctionTypes.constant(d1), DensityFunctionTypes.constant(d2), DensityFunctionTypes.constant(d3), DensityFunctionTypes.constant(d4), DensityFunctionTypes.constant(d5), DensityFunctionTypes.constant(d6), List.of());
}

public static MultiNoiseSampler createMultiNoiseSampler(double d) {
return createMultiNoiseSampler(d, d, d, d, d, d);
}

public static MultiNoiseSampler createMultiNoiseSampler() {
return createMultiNoiseSampler(0.0D);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.ludocrypt.limlib.api.sound;

import java.util.List;

import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public interface MultipleSoundEmitter {

public List<SingleSoundEmitter> getEmitters(MinecraftClient client, World world, BlockPos pos, BlockState state);

public default void playSounds(MinecraftClient client, World world, BlockPos pos, BlockState state) {
this.getEmitters(client, world, pos, state).forEach((emitter) -> emitter.playSound(client, world, pos, state));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.ludocrypt.limlib.api.sound;

import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3i;
import net.minecraft.world.World;

public interface SingleSoundEmitter {

public default Vec3d getOffset(MinecraftClient client, World world, BlockPos pos, BlockState state) {
return Vec3d.ofCenter(Vec3i.ZERO);
}

public SoundEvent getSound(MinecraftClient client, World world, BlockPos pos, BlockState state);

public default float getPitch(MinecraftClient client, World world, BlockPos pos, BlockState state) {
return 1.0F;
}

public default float getVolume(MinecraftClient client, World world, BlockPos pos, BlockState state) {
return 1.0F;
}

public default float getChance(MinecraftClient client, World world, BlockPos pos, BlockState state) {
return 1.0F;
}

public default SoundCategory getCategory(MinecraftClient client, World world, BlockPos pos, BlockState state) {
return SoundCategory.AMBIENT;
}

public default void playSound(MinecraftClient client, World world, BlockPos pos, BlockState state) {
if (world.getRandom().nextFloat() < this.getChance(client, world, pos, state)) {
Vec3d offset = getOffset(client, world, pos, state);
client.getSoundManager().play(new PositionedSoundInstance(getSound(client, world, pos, state), getCategory(client, world, pos, state), getVolume(client, world, pos, state), getPitch(client, world, pos, state), 0, pos.getX() + offset.getX(), pos.getY() + offset.getY()));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package net.ludocrypt.limlib.api.world;

import java.util.HashMap;
import java.util.Optional;

import net.ludocrypt.limlib.mixin.BlockEntityAccessor;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.loot.LootTables;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.structure.StructureSet;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryEntryList;
import net.minecraft.world.ChunkRegion;
import net.minecraft.world.biome.source.BiomeSource;
import net.minecraft.world.biome.source.util.MultiNoiseUtil.MultiNoiseSampler;

public abstract class AbstractNbtChunkGenerator extends LiminalChunkGenerator {

public final HashMap<String, NbtPlacerUtil> loadedStructures = new HashMap<String, NbtPlacerUtil>(30);
public final Identifier nbtId;

public AbstractNbtChunkGenerator(Registry<StructureSet> registry, Optional<RegistryEntryList<StructureSet>> optional, BiomeSource biomeSource, BiomeSource biomeSource2, long l, Identifier nbtId, MultiNoiseSampler multiNoiseSampler) {
super(registry, optional, biomeSource, biomeSource2, l, multiNoiseSampler);
this.nbtId = nbtId;
}

@Override
public int getSeaLevel() {
return 0;
}

@Override
public int getMinimumY() {
return 0;
}

public abstract void storeStructures(ServerWorld world);

protected void store(String id, ServerWorld world) {
loadedStructures.put(id, NbtPlacerUtil.load(world.getServer().getResourceManager(), new Identifier(this.nbtId.getNamespace(), "nbt/" + this.nbtId.getPath() + "/" + id + ".nbt")).get());
}

protected void store(String id, ServerWorld world, int from, int to) {
for (int i = from; i <= to; i++) {
store(id + "_" + i, world);
}
}

protected void generateNbt(ChunkRegion region, BlockPos at, String id) {
generateNbt(region, at, id, BlockRotation.NONE);
}

protected void generateNbt(ChunkRegion region, BlockPos at, String id, BlockRotation rotation) {
loadedStructures.get(id).rotate(rotation).generateNbt(region, at, (pos, state, nbt) -> this.modifyStructure(region, pos, state, nbt)).spawnEntities(region, at, rotation);
}

protected void modifyStructure(ChunkRegion region, BlockPos pos, BlockState state, NbtCompound nbt) {
if (!state.isAir()) {
if (state.isOf(Blocks.BARREL)) {
region.setBlockState(pos, state, Block.NOTIFY_ALL, 1);
if (region.getBlockEntity(pos)instanceof LootableContainerBlockEntity lootTable) {
lootTable.setLootTable(this.getBarrelLootTable(), region.getSeed() + MathHelper.hashCode(pos));
}
} else if (state.isOf(Blocks.BARRIER)) {
region.setBlockState(pos, Blocks.AIR.getDefaultState(), Block.NOTIFY_ALL, 1);
} else {
region.setBlockState(pos, state, Block.NOTIFY_ALL, 1);
}
BlockEntity blockEntity = region.getBlockEntity(pos);
if (blockEntity != null) {
if (state.isOf(blockEntity.getCachedState().getBlock())) {
((BlockEntityAccessor) blockEntity).callWriteNbt(nbt);
}
}
}
}

protected Identifier getBarrelLootTable() {
return LootTables.SIMPLE_DUNGEON_CHEST;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.ludocrypt.limlib.api.world;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;

import com.mojang.datafixers.util.Either;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.server.world.ChunkHolder;
import net.minecraft.server.world.ServerLightingProvider;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.structure.StructureManager;
import net.minecraft.structure.StructureSet;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryEntryList;
import net.minecraft.world.ChunkRegion;
import net.minecraft.world.HeightLimitView;
import net.minecraft.world.Heightmap.Type;
import net.minecraft.world.biome.source.BiomeAccess;
import net.minecraft.world.biome.source.BiomeSource;
import net.minecraft.world.biome.source.util.MultiNoiseUtil.MultiNoiseSampler;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.gen.GenerationStep.Carver;
import net.minecraft.world.gen.StructureAccessor;
import net.minecraft.world.gen.chunk.Blender;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.chunk.VerticalBlockSample;

public abstract class LiminalChunkGenerator extends ChunkGenerator {

private final MultiNoiseSampler multiNoiseSampler;

public LiminalChunkGenerator(Registry<StructureSet> registry, Optional<RegistryEntryList<StructureSet>> optional, BiomeSource biomeSource, BiomeSource biomeSource2, long l, MultiNoiseSampler multiNoiseSampler) {
super(registry, optional, biomeSource, biomeSource2, l);
this.multiNoiseSampler = multiNoiseSampler;
}

public abstract CompletableFuture<Chunk> populateNoise(ChunkRegion chunkRegion, ChunkStatus targetStatus, Executor executor, ServerWorld world, ChunkGenerator generator, StructureManager structureManager, ServerLightingProvider lightingProvider, Function<Chunk, CompletableFuture<Either<Chunk, ChunkHolder.Unloaded>>> function, List<Chunk> chunks, Chunk chunk2, boolean bl);

@Override
public CompletableFuture<Chunk> populateNoise(Executor var1, Blender var2, StructureAccessor var3, Chunk var4) {
throw new UnsupportedOperationException("populateNoise should never be called in LiminalChunkGenerator");
}

public int chunkRadius() {
return 1;
}

@Override
public MultiNoiseSampler getMultiNoiseSampler() {
return this.multiNoiseSampler;
}

@Override
public void carve(ChunkRegion var1, long var2, BiomeAccess var4, StructureAccessor var5, Chunk var6, Carver var7) {
}

@Override
public void buildSurface(ChunkRegion var1, StructureAccessor var2, Chunk var3) {
}

@Override
public void populateEntities(ChunkRegion var1) {
}

@Override
public int getWorldHeight() {
return 0;
}

@Override
public int getHeight(int var1, int var2, Type var3, HeightLimitView var4) {
return 0;
}

@Override
public VerticalBlockSample getColumnSample(int x, int z, HeightLimitView world) {
BlockState[] states = new BlockState[world.getHeight()];
for (int i = 0; i < states.length; i++) {
states[i] = Blocks.AIR.getDefaultState();
}
return new VerticalBlockSample(0, states);
}

@Override
public void getDebugHudText(List<String> var1, BlockPos var2) {
}

}
Loading

0 comments on commit 2704635

Please sign in to comment.