All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.creoii.creoapi.impl.block.SpreadableImpl Maven / Gradle / Ivy

package dev.creoii.creoapi.impl.block;

import dev.creoii.creoapi.api.block.Spreadable;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;
import net.minecraft.class_2248;
import net.minecraft.class_2338;
import net.minecraft.class_2680;
import net.minecraft.class_3218;
import net.minecraft.class_5819;

@ApiStatus.Internal
public final class SpreadableImpl {
    public static void applySpreadables(class_2680 state, class_3218 world, class_2338 pos, class_5819 random, CallbackInfo ci) {
        if (state.method_26204() instanceof Spreadable spreadable) {
            if (!spreadable.canSurvive(state, world, pos, random)) {
                world.method_8501(pos, spreadable.getDead().method_9564());
                return;
            } else if (spreadable.canSpread(state, world, pos, random)) {
                for (int i = 0; i < 4; ++i) {
                    class_2338 testPos = pos.method_10069(random.method_43048(3) - 1, random.method_43048(5) - 3, random.method_43048(3) - 1);
                    class_2248 test = getConversion(spreadable.getSpreads(), world.method_8320(testPos).method_26204(), state.method_26204());
                    if (test != null && spreadable.canSpread(state, world, pos, random) && spreadable.canSurvive(state, world, testPos, random)) {
                        if (!world.method_8501(testPos, test.method_9564()))
                            continue;
                        spreadable.onSpread(state, world, pos, random);
                    }
                }
            }
            ci.cancel();
        }
    }

    @Nullable
    private static class_2248 getConversion(List spreads, class_2248 block, class_2248 defaultBlock) {
        for (Spreadable.Spread spread : spreads) {
            if (spread.target() == block) {
                if (spread.conversion() == null) {
                    return defaultBlock;
                } else return spread.conversion();
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy