Search in sources :

Example 1 with CRecipes

use of com.simibubi.create.foundation.config.CRecipes in project Create by Creators-of-Create.

the class ChromaticCompoundItem method onEntityItemUpdate.

@Override
public boolean onEntityItemUpdate(ItemStack stack, ItemEntity entity) {
    double y = entity.getY();
    double yMotion = entity.getDeltaMovement().y;
    Level world = entity.level;
    CompoundTag data = entity.getPersistentData();
    CompoundTag itemData = entity.getItem().getOrCreateTag();
    Vec3 positionVec = entity.position();
    CRecipes config = AllConfigs.SERVER.recipes;
    if (world.isClientSide) {
        int light = itemData.getInt("CollectingLight");
        if (world.random.nextInt(config.lightSourceCountForRefinedRadiance.get() + 20) < light) {
            Vec3 start = VecHelper.offsetRandomly(positionVec, world.random, 3);
            Vec3 motion = positionVec.subtract(start).normalize().scale(.2f);
            world.addParticle(ParticleTypes.END_ROD, start.x, start.y, start.z, motion.x, motion.y, motion.z);
        }
        return false;
    }
    // Convert to Shadow steel if in void
    if (y < 0 && y - yMotion < -10 && config.enableShadowSteelRecipe.get()) {
        ItemStack newStack = AllItems.SHADOW_STEEL.asStack();
        newStack.setCount(stack.getCount());
        data.putBoolean("JustCreated", true);
        entity.setItem(newStack);
    }
    if (!config.enableRefinedRadianceRecipe.get())
        return false;
    // Convert to Refined Radiance if eaten enough light sources
    if (itemData.getInt("CollectingLight") >= config.lightSourceCountForRefinedRadiance.get()) {
        ItemStack newStack = AllItems.REFINED_RADIANCE.asStack();
        ItemEntity newEntity = new ItemEntity(world, entity.getX(), entity.getY(), entity.getZ(), newStack);
        newEntity.setDeltaMovement(entity.getDeltaMovement());
        newEntity.getPersistentData().putBoolean("JustCreated", true);
        itemData.remove("CollectingLight");
        world.addFreshEntity(newEntity);
        stack.split(1);
        entity.setItem(stack);
        if (stack.isEmpty())
            entity.discard();
        return false;
    }
    // Is inside beacon beam?
    boolean isOverBeacon = false;
    int entityX = Mth.floor(entity.getX());
    int entityZ = Mth.floor(entity.getZ());
    int localWorldHeight = world.getHeight(Heightmap.Types.WORLD_SURFACE, entityX, entityZ);
    BlockPos.MutableBlockPos testPos = new BlockPos.MutableBlockPos(entityX, Math.min(Mth.floor(entity.getY()), localWorldHeight), entityZ);
    while (testPos.getY() > 0) {
        testPos.move(Direction.DOWN);
        BlockState state = world.getBlockState(testPos);
        if (state.getLightBlock(world, testPos) >= 15 && state.getBlock() != Blocks.BEDROCK)
            break;
        if (state.getBlock() == Blocks.BEACON) {
            BlockEntity te = world.getBlockEntity(testPos);
            if (!(te instanceof BeaconBlockEntity))
                break;
            BeaconBlockEntity bte = (BeaconBlockEntity) te;
            if (!bte.beamSections.isEmpty())
                isOverBeacon = true;
            break;
        }
    }
    if (isOverBeacon) {
        ItemStack newStack = AllItems.REFINED_RADIANCE.asStack();
        newStack.setCount(stack.getCount());
        data.putBoolean("JustCreated", true);
        entity.setItem(newStack);
        return false;
    }
    // Find a light source and eat it.
    Random r = world.random;
    int range = 3;
    float rate = 1 / 2f;
    if (r.nextFloat() > rate)
        return false;
    BlockPos randomOffset = new BlockPos(VecHelper.offsetRandomly(positionVec, r, range));
    BlockState state = world.getBlockState(randomOffset);
    TransportedItemStackHandlerBehaviour behaviour = TileEntityBehaviour.get(world, randomOffset, TransportedItemStackHandlerBehaviour.TYPE);
    // Find a placed light source
    if (behaviour == null) {
        if (checkLight(stack, entity, world, itemData, positionVec, randomOffset, state))
            world.destroyBlock(randomOffset, false);
        return false;
    }
    // Find a light source from a depot/belt (chunk rebuild safe)
    MutableBoolean success = new MutableBoolean(false);
    behaviour.handleProcessingOnAllItems(ts -> {
        ItemStack heldStack = ts.stack;
        if (!(heldStack.getItem() instanceof BlockItem))
            return TransportedResult.doNothing();
        BlockItem blockItem = (BlockItem) heldStack.getItem();
        if (blockItem.getBlock() == null)
            return TransportedResult.doNothing();
        BlockState stateToCheck = blockItem.getBlock().defaultBlockState();
        if (!success.getValue() && checkLight(stack, entity, world, itemData, positionVec, randomOffset, stateToCheck)) {
            success.setTrue();
            if (ts.stack.getCount() == 1)
                return TransportedResult.removeItem();
            TransportedItemStack left = ts.copy();
            left.stack.shrink(1);
            return TransportedResult.convertTo(left);
        }
        return TransportedResult.doNothing();
    });
    return false;
}
Also used : ItemEntity(net.minecraft.world.entity.item.ItemEntity) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) CRecipes(com.simibubi.create.foundation.config.CRecipes) BlockItem(net.minecraft.world.item.BlockItem) TransportedItemStackHandlerBehaviour(com.simibubi.create.foundation.tileEntity.behaviour.belt.TransportedItemStackHandlerBehaviour) BlockState(net.minecraft.world.level.block.state.BlockState) Random(java.util.Random) TransportedItemStack(com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack) Vec3(net.minecraft.world.phys.Vec3) BeaconBlockEntity(net.minecraft.world.level.block.entity.BeaconBlockEntity) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) TransportedItemStack(com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack) ItemStack(net.minecraft.world.item.ItemStack) CompoundTag(net.minecraft.nbt.CompoundTag) BeaconBlockEntity(net.minecraft.world.level.block.entity.BeaconBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Aggregations

TransportedItemStack (com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack)1 CRecipes (com.simibubi.create.foundation.config.CRecipes)1 TransportedItemStackHandlerBehaviour (com.simibubi.create.foundation.tileEntity.behaviour.belt.TransportedItemStackHandlerBehaviour)1 Random (java.util.Random)1 BlockPos (net.minecraft.core.BlockPos)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ItemEntity (net.minecraft.world.entity.item.ItemEntity)1 BlockItem (net.minecraft.world.item.BlockItem)1 ItemStack (net.minecraft.world.item.ItemStack)1 Level (net.minecraft.world.level.Level)1 BeaconBlockEntity (net.minecraft.world.level.block.entity.BeaconBlockEntity)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 Vec3 (net.minecraft.world.phys.Vec3)1 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)1