Search in sources :

Example 6 with DirectionProperty

use of net.minecraft.world.level.block.state.properties.DirectionProperty in project assemblylinemachines by HaydenBelanger.

the class UpgradeKitCrafting method tryUpgrade.

public static boolean tryUpgrade(BlockPos upgradeBlock, Level level, ItemStack upgradeKit) {
    if (level.isClientSide)
        throw new IllegalArgumentException("Upgrade attempted from the client side.");
    List<UpgradeKitCrafting> list = level.getRecipeManager().getAllRecipesFor(UPGRADING_RECIPE);
    for (UpgradeKitCrafting recipe : list) {
        if (recipe.inputBlock.get().test(level.getBlockState(upgradeBlock).getBlock().asItem().getDefaultInstance()) && upgradeKit.is(recipe.inputUpgradeKit.item)) {
            HashMap<Integer, ItemStack> copyItems = new HashMap<>();
            if (!recipe.slotCopying.isEmpty() && level.getBlockEntity(upgradeBlock) instanceof Container) {
                Container container = (Container) level.getBlockEntity(upgradeBlock);
                for (Entry<Integer, Integer> sourceSlot : recipe.slotCopying.entrySet()) {
                    copyItems.put(sourceSlot.getValue(), container.getItem(sourceSlot.getKey()));
                    container.removeItemNoUpdate(sourceSlot.getKey());
                }
                container.setChanged();
            }
            BlockState df = recipe.outputBlock.defaultBlockState();
            DirectionProperty facing = HorizontalDirectionalBlock.FACING;
            if (df.hasProperty(facing) && level.getBlockState(upgradeBlock).hasProperty(facing))
                df = df.setValue(facing, level.getBlockState(upgradeBlock).getValue(facing));
            level.setBlockAndUpdate(upgradeBlock, df);
            if (!copyItems.isEmpty()) {
                if (level.getBlockEntity(upgradeBlock) instanceof Container) {
                    Container newContainer = (Container) level.getBlockEntity(upgradeBlock);
                    for (Entry<Integer, ItemStack> destinationSlot : copyItems.entrySet()) {
                        newContainer.setItem(destinationSlot.getKey(), destinationSlot.getValue());
                    }
                    newContainer.setChanged();
                } else {
                    NonNullList<ItemStack> nnl = NonNullList.create();
                    nnl.addAll(copyItems.values());
                    Containers.dropContents(level, upgradeBlock, nnl);
                }
            }
            return true;
        }
    }
    return false;
}
Also used : Container(net.minecraft.world.Container) BlockState(net.minecraft.world.level.block.state.BlockState) ItemStack(net.minecraft.world.item.ItemStack) DirectionProperty(net.minecraft.world.level.block.state.properties.DirectionProperty)

Aggregations

DirectionProperty (net.minecraft.world.level.block.state.properties.DirectionProperty)6 Block (net.minecraft.world.level.block.Block)3 BooleanProperty (com.sk89q.worldedit.registry.state.BooleanProperty)2 DirectionalProperty (com.sk89q.worldedit.registry.state.DirectionalProperty)2 EnumProperty (com.sk89q.worldedit.registry.state.EnumProperty)2 IntegerProperty (com.sk89q.worldedit.registry.state.IntegerProperty)2 Property (com.sk89q.worldedit.registry.state.Property)2 Direction (com.sk89q.worldedit.util.Direction)2 BlockPos (net.minecraft.core.BlockPos)2 ItemStack (net.minecraft.world.item.ItemStack)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 ImmutableList (com.google.common.collect.ImmutableList)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 Futures (com.google.common.util.concurrent.Futures)1