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;
}
Aggregations