Search in sources :

Example 1 with IntRange

use of com.almuradev.toolbox.util.math.IntRange in project Almura by AlmuraDev.

the class Grass method getOrLoadAmountRequiredRangeForBiome.

@Nullable
public IntRange getOrLoadAmountRequiredRangeForBiome(final Biome biome) {
    @Nullable IntRange found = this.biomeAmountRanges.get(biome);
    if (found == null) {
        for (final Map.Entry<FunctionPredicate<Biome, ResourceLocation>, IntRange> entry : this.biomeAmountPredicates.entrySet()) {
            final FunctionPredicate<Biome, ResourceLocation> predicate = entry.getKey();
            if (predicate.test(biome)) {
                final IntRange range = entry.getValue();
                this.biomeAmountRanges.put(biome, range);
                found = range;
                break;
            }
        }
    }
    if (found == null && this.globalAmountRange != null) {
        this.biomeAmountRanges.put(biome, this.globalAmountRange);
        found = this.globalAmountRange;
    }
    return found;
}
Also used : Biome(net.minecraft.world.biome.Biome) ResourceLocation(net.minecraft.util.ResourceLocation) IntRange(com.almuradev.toolbox.util.math.IntRange) FunctionPredicate(com.almuradev.content.component.predicate.FunctionPredicate) HashMap(java.util.HashMap) Map(java.util.Map) Nullable(javax.annotation.Nullable) Nullable(javax.annotation.Nullable)

Example 2 with IntRange

use of com.almuradev.toolbox.util.math.IntRange in project Almura by AlmuraDev.

the class MixinBlockTallGrass method getDrops.

/**
 * @author Zidane - Chris Sanders
 * @reason Add in content seeds to drop list for Tall Grass
 */
@Overwrite(remap = false)
public void getDrops(final NonNullList<ItemStack> drops, final IBlockAccess access, final BlockPos pos, final IBlockState state, final int fortune) {
    World world;
    if (access instanceof ChunkCache) {
        world = ((ChunkCache) access).world;
    } else if (access instanceof World) {
        world = (World) access;
    } else {
        return;
    }
    final Random random = world.rand;
    // Roll 1 is Vanilla's 1/8 chance to drop a seed
    final int roll1 = random.nextInt(8);
    if (roll1 == 0) {
        // Forge Start - Lookup seed each time and then do random check. Almura handles its own chance code
        final ItemStack modSeed = net.minecraftforge.common.ForgeHooks.getGrassSeed(random, fortune);
        if (!modSeed.isEmpty()) {
            drops.add(modSeed);
            // Don't double up with Vanilla/mod drops
            return;
        }
        final Biome biome = world.getBiome(pos);
        // Roll 2 is shuffling Almura seeds and picking the first one after shuffling
        registry.getAllOf(ItemType.class).stream().filter(itemType -> itemType instanceof SeedItem && ((SeedItem) itemType).getGrass() != null).collect(Collectors.collectingAndThen(Collectors.toList(), collected -> {
            Collections.shuffle(collected);
            return collected.stream();
        })).findFirst().ifPresent((itemType) -> {
            final SeedItem seed = (SeedItem) itemType;
            final IntRange amountRange = seed.getGrass().getOrLoadAmountRequiredRangeForBiome(biome);
            if (amountRange != null) {
                final int stackSize = amountRange.random(random);
                final DoubleRange chanceRange = seed.getGrass().getOrLoadChanceRangeForBiome(biome);
                if (chanceRange != null) {
                    final double chance = chanceRange.random(random);
                    // Roll 3 is allowing the seed configuration to determine the chance for the drop
                    if (random.nextDouble() <= (chance / 100)) {
                        drops.add((ItemStack) (Object) org.spongepowered.api.item.inventory.ItemStack.of(itemType, stackSize));
                    }
                } else {
                    drops.add((ItemStack) (Object) org.spongepowered.api.item.inventory.ItemStack.of(itemType, stackSize));
                }
            }
        });
    }
// Almura End
}
Also used : ChunkCache(net.minecraft.world.ChunkCache) World(net.minecraft.world.World) BlockPos(net.minecraft.util.math.BlockPos) Random(java.util.Random) Overwrite(org.spongepowered.asm.mixin.Overwrite) SeedItem(com.almuradev.content.type.item.type.seed.SeedItem) BlockTallGrass(net.minecraft.block.BlockTallGrass) Collectors(java.util.stream.Collectors) GameRegistry(org.spongepowered.api.GameRegistry) Inject(javax.inject.Inject) IBlockState(net.minecraft.block.state.IBlockState) ItemStack(net.minecraft.item.ItemStack) Mixin(org.spongepowered.asm.mixin.Mixin) MixinBlock(com.almuradev.content.type.block.mixin.impl.MixinBlock) DoubleRange(com.almuradev.toolbox.util.math.DoubleRange) NonNullList(net.minecraft.util.NonNullList) ItemType(org.spongepowered.api.item.ItemType) Collections(java.util.Collections) IntRange(com.almuradev.toolbox.util.math.IntRange) IBlockAccess(net.minecraft.world.IBlockAccess) Biome(net.minecraft.world.biome.Biome) ChunkCache(net.minecraft.world.ChunkCache) ItemType(org.spongepowered.api.item.ItemType) IntRange(com.almuradev.toolbox.util.math.IntRange) World(net.minecraft.world.World) SeedItem(com.almuradev.content.type.item.type.seed.SeedItem) DoubleRange(com.almuradev.toolbox.util.math.DoubleRange) Biome(net.minecraft.world.biome.Biome) Random(java.util.Random) ItemStack(net.minecraft.item.ItemStack) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

IntRange (com.almuradev.toolbox.util.math.IntRange)2 Biome (net.minecraft.world.biome.Biome)2 FunctionPredicate (com.almuradev.content.component.predicate.FunctionPredicate)1 MixinBlock (com.almuradev.content.type.block.mixin.impl.MixinBlock)1 SeedItem (com.almuradev.content.type.item.type.seed.SeedItem)1 DoubleRange (com.almuradev.toolbox.util.math.DoubleRange)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 Inject (javax.inject.Inject)1 BlockTallGrass (net.minecraft.block.BlockTallGrass)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemStack (net.minecraft.item.ItemStack)1 NonNullList (net.minecraft.util.NonNullList)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 BlockPos (net.minecraft.util.math.BlockPos)1 ChunkCache (net.minecraft.world.ChunkCache)1